Skip to content

Commit

Permalink
Fix JSONata Expression handling for Node-Red 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Zehir committed Dec 14, 2023
1 parent 7a7cdb1 commit a5f4305
Show file tree
Hide file tree
Showing 12 changed files with 3,887 additions and 73 deletions.
77 changes: 42 additions & 35 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.233.0/containers/typescript-node
{
"name": "Node.js & TypeScript",
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick a Node version: 18, 16, 14.
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local on arm64/Apple Silicon.
"args": {
"VARIANT": "16-bullseye"
}
},
// Set *default* container specific settings.json values on container create.
"settings": {},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"antfu.iconify",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"spmeesseman.vscode-taskexplorer",
"ms-azuretools.vscode-docker",
"eamodio.gitlens",
"mhutchie.git-graph",
"hbenl.vscode-test-explorer",
"hbenl.vscode-mocha-test-adapter"
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "bash ./.devcontainer/postCreateCommand.sh",
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "node",
"portsAttributes": {
"1880": {
"label": "Node-Red"
}
}
"name": "Node.js & TypeScript",
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick a Node version: 18, 16, 14.
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local on arm64/Apple Silicon.
"args": {
"VARIANT": "18-bullseye"
}
},


"customizations": {
"vscode": {
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"antfu.iconify",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"spmeesseman.vscode-taskexplorer",
"ms-azuretools.vscode-docker",
"eamodio.gitlens",
"mhutchie.git-graph",
"hbenl.vscode-test-explorer",
"hbenl.vscode-mocha-test-adapter"
]
}
},


// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pnpm install && echo alias ll=\\'ls -al\\' >> /home/node/.bashrc",

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "node",
"portsAttributes": {
"1880": {
"label": "Node-Red"
}
}
}
3 changes: 0 additions & 3 deletions .devcontainer/postCreateCommand.sh

This file was deleted.

22 changes: 21 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
{
"prettier.enable": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"editor.formatOnSave": true,
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#c00000",
"activityBar.background": "#c00000",
"activityBar.foreground": "#e7e7e7",
"activityBar.inactiveForeground": "#e7e7e799",
"activityBarBadge.background": "#00bf00",
"activityBarBadge.foreground": "#e7e7e7",
"commandCenter.border": "#e7e7e799",
"sash.hoverBorder": "#c00000",
"statusBar.background": "#8d0000",
"statusBar.foreground": "#e7e7e7",
"statusBarItem.hoverBackground": "#c00000",
"statusBarItem.remoteBackground": "#8d0000",
"statusBarItem.remoteForeground": "#e7e7e7",
"titleBar.activeBackground": "#8d0000",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveBackground": "#8d000099",
"titleBar.inactiveForeground": "#e7e7e799"
},
"peacock.remoteColor": "#8D0000",
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] :construction:

### Fixed

- Fix JSONata Expression handling for Node-Red 4.0 @Zehir

## [2.3.9] - 2023-01-04 ![Relative date](https://img.shields.io/date/1672868800?label=)

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion nodes/battery.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = function (RED) {
(async () => {
await Utils.sleep(1500);
initNode();
node.server.propagateStartNews([node.id]);
await node.server.propagateStartNews([node.id]);
})()
.then()
.catch((error) => {
Expand Down
15 changes: 10 additions & 5 deletions nodes/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,16 @@ module.exports = function (RED) {
break;
case "json":
case "jsonata":
let querySrc = RED.util.evaluateJSONataExpression(
RED.util.prepareJSONataExpression(node.config.query, node),
message_in,
undefined
);
const querySrc = await new Promise((resolve, reject) => {
RED.util.evaluateJSONataExpression(
RED.util.prepareJSONataExpression(node.config.query, node),
message_in,
(err, value) => {
if (err) reject(err);
else resolve(value);
}
);
});
try {
for (let r of node.server.device_list.getDevicesByQuery(
querySrc
Expand Down
2 changes: 1 addition & 1 deletion nodes/in.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module.exports = function (RED) {
(async () => {
await Utils.sleep(1500);
initNode();
node.server.propagateStartNews([node.id]);
await node.server.propagateStartNews([node.id]);
})()
.then()
.catch((error) => {
Expand Down
15 changes: 10 additions & 5 deletions nodes/out.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,16 @@ module.exports = function (RED) {
break;
case "json":
case "jsonata":
let querySrc = RED.util.evaluateJSONataExpression(
RED.util.prepareJSONataExpression(node.config.query, node),
message_in,
undefined
);
const querySrc = await new Promise((resolve, reject) => {
RED.util.evaluateJSONataExpression(
RED.util.prepareJSONataExpression(node.config.query, node),
message_in,
(err, value) => {
if (err) reject(err);
else resolve(value);
}
);
});
try {
for (let r of node.server.device_list.getDevicesByQuery(
querySrc
Expand Down
60 changes: 39 additions & 21 deletions nodes/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ module.exports = function (RED) {
"Please check server configuration.";
if (node.state.pooling.errorTriggered === false) {
node.state.pooling.errorTriggered = true;
node.propagateErrorNews(code, reason, true);
await node.propagateErrorNews(code, reason, true);
}
if (node.state.pooling.failCount % 4 === 2) {
node.error(reason);
Expand All @@ -123,7 +123,7 @@ module.exports = function (RED) {

if (node.state.pooling.errorTriggered === false) {
node.state.pooling.errorTriggered = true;
node.propagateErrorNews(code, reason, true);
await node.propagateErrorNews(code, reason, true);
}
if (node.state.pooling.failCount % 4 === 2) {
node.error(reason);
Expand Down Expand Up @@ -248,7 +248,7 @@ module.exports = function (RED) {
"discoverDevices: Can't use to deconz API, invalid api key. " +
"Please check server configuration.";
node.error(reason);
node.propagateErrorNews(code, reason, true);
await node.propagateErrorNews(code, reason, true);
node.onClose();
}
//node.error(`discoverDevices: Can't connect to deconz API.`);
Expand All @@ -257,7 +257,7 @@ module.exports = function (RED) {
}
}

propagateStartNews(whitelistNodes) {
async propagateStartNews(whitelistNodes) {
let node = this;
// Node with device selected

Expand Down Expand Up @@ -293,11 +293,17 @@ module.exports = function (RED) {
}

// TODO Cache JSONata expresssions ?
let querySrc = RED.util.evaluateJSONataExpression(
RED.util.prepareJSONataExpression(target.config.query, target),
{},
undefined
);
const querySrc = await new Promise((resolve, reject) => {
RED.util.evaluateJSONataExpression(
RED.util.prepareJSONataExpression(target.config.query, target),
{},
(err, value) => {
if (err) reject(err);
else resolve(value);
}
);
});

try {
let devices = node.device_list.getDevicesByQuery(querySrc);
if (devices.matched.length === 0) continue;
Expand Down Expand Up @@ -325,7 +331,7 @@ module.exports = function (RED) {
}
}

propagateErrorNews(code, reason, isGlobalError = false) {
async propagateErrorNews(code, reason, isGlobalError = false) {
let node = this;
if (!reason) return;

Expand Down Expand Up @@ -381,11 +387,17 @@ module.exports = function (RED) {
}

// TODO Cache JSONata expresssions ?
let querySrc = RED.util.evaluateJSONataExpression(
RED.util.prepareJSONataExpression(target.config.query, target),
{},
undefined
);
const querySrc = await new Promise((resolve, reject) => {
RED.util.evaluateJSONataExpression(
RED.util.prepareJSONataExpression(target.config.query, target),
{},
(err, value) => {
if (err) reject(err);
else resolve(value);
}
);
});

try {
let devices = node.device_list.getDevicesByQuery(querySrc);
if (devices.matched.length === 0) continue;
Expand Down Expand Up @@ -681,7 +693,7 @@ module.exports = function (RED) {
return changed;
}

onSocketMessage(dataParsed) {
async onSocketMessage(dataParsed) {
let node = this;
node.state.websocket.lastEvent = Date.now();
node.state.websocket.isValid = true;
Expand Down Expand Up @@ -749,11 +761,17 @@ module.exports = function (RED) {
}

// TODO Cache JSONata expresssions ?
let querySrc = RED.util.evaluateJSONataExpression(
RED.util.prepareJSONataExpression(target.config.query, target),
{},
undefined
);
const querySrc = await new Promise((resolve, reject) => {
RED.util.evaluateJSONataExpression(
RED.util.prepareJSONataExpression(target.config.query, target),
{},
(err, value) => {
if (err) reject(err);
else resolve(value);
}
);
});

try {
let query = new Query(querySrc);
if (query.match(device)) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"load-grunt-tasks": "^5.1.0",
"mocha": "^8.3.2",
"nyc": "^15.1.0",
"should": "^13.2.3"
"should": "^13.2.3",
"taze": "^0.13.0"
},
"node-red": {
"version": ">=1.2.9",
Expand Down
Loading

0 comments on commit a5f4305

Please sign in to comment.