From 08b3f470dfe94e097279f43cc9dd87a6fc15f55c Mon Sep 17 00:00:00 2001 From: NWLAB Date: Wed, 21 Feb 2024 17:55:06 +0900 Subject: [PATCH 1/4] mcu_code 1st --- node_types.json | 4 +- nodered.js | 91 +++++++ nodes/code/README.md | 9 + nodes/code/mcu_code.html | 536 +++++++++++++++++++++++++++++++++++++++ nodes/code/mcu_code.js | 7 + nodes/code/package.json | 36 +++ 6 files changed, 682 insertions(+), 1 deletion(-) create mode 100644 nodes/code/README.md create mode 100644 nodes/code/mcu_code.html create mode 100644 nodes/code/mcu_code.js create mode 100644 nodes/code/package.json diff --git a/node_types.json b/node_types.json index d2dece0..a03a0d4 100644 --- a/node_types.json +++ b/node_types.json @@ -86,5 +86,7 @@ "file in": "$(NODEREDMCU)/nodes/storage/file/manifest.json", "openweathermap": "$(NODEREDMCU)/nodes/weather/openweathermap/manifest.json", - "openweathermap in": "$(NODEREDMCU)/nodes/weather/openweathermap/manifest.json" + "openweathermap in": "$(NODEREDMCU)/nodes/weather/openweathermap/manifest.json", + + "mcu_code": "" } diff --git a/nodered.js b/nodered.js index a1b2470..b7409bd 100644 --- a/nodered.js +++ b/nodered.js @@ -743,6 +743,97 @@ class FunctionNode extends Node { } } + +class McuCodeNode extends Node { + #func; + #libs; + #doDone; + //@@ might cache env here + + constructor(id, flow, name) { + super(id, flow, name); + Object.defineProperties(this, { + context: {value: new Context(id)} + }); + Object.defineProperties(this.context, { + global: {value: globalContext}, + flow: {value: flow.context} + }); + } + onStart(config) { + super.onStart(config); + + if (config.libs?.length) { + this.#libs = []; + for (let i = 0; i < config.libs.length; i++) + this.#libs[i] = Modules.importNow(config.libs[i]); + Object.freeze(this.#libs); + } + + this.#func = config.func ?? nop; + this.#doDone = config.doDone; + + try { + const context = this.context; + const initialize = config.initialize; + initialize?.(this, context, context.flow, context.global, this.#libs, {get: name => this.getSetting(name)}); + } + catch (e) { + this.error(e); //@@ what's the right way to handle this? + } + } + onMessage(msg, done) { + try { + const context = this.context; + const func = this.#func; + const _msgid = msg._msgid; + const node = Object.create(this, { + done: {value: done}, + error: {value: (error, msg) => { + this.debug(error.toString()); + if (msg) + done(msg); + }}, + send: {value: msg => { + this.send(msg, _msgid); + }}, + status: {value: status => this.status(status)} + }); + msg = func(msg, node, context, context.flow, context.global, this.#libs, {get: name => this.getSetting(name)}); + if (this.#doDone) + done(); + if (msg) + this.send(msg, _msgid); + } + catch (e) { + done(e); + } + } + send(msg, _msgid) { + _msgid ??= generateId(); + if (Array.isArray(msg)) { + for (let i = 0, length = msg.length; i < length; i++) { + const output = msg[i]; + if (Array.isArray(output)) { + for (let j = 0, length = output.length; j < length; j++) + output[j]._msgid = _msgid; + } + else if (output) + output._msgid = _msgid; + } + } + else + msg._msgid = _msgid; + + return super.send(msg); + } + + static type = "mcu_code"; + static { + RED.nodes.registerType(this.type, this); + } +} + class ChangeNode extends Node { onStart(config) { super.onStart(config); diff --git a/nodes/code/README.md b/nodes/code/README.md new file mode 100644 index 0000000..d7f443b --- /dev/null +++ b/nodes/code/README.md @@ -0,0 +1,9 @@ +# node-red-mcu_code +mcu_code can be used when an error occurs in a Node-RED function. +- No error checking in sandbox. +- You can write a manifest. + +(in Japanese) +mcu_codeは、Node-REDのFunctionではエラーが発生するときに利用できるようにしました。 +- サンドボックスでのエラーチェックをしません。 +- manifestを記せます。 \ No newline at end of file diff --git a/nodes/code/mcu_code.html b/nodes/code/mcu_code.html new file mode 100644 index 0000000..f03cbdd --- /dev/null +++ b/nodes/code/mcu_code.html @@ -0,0 +1,536 @@ + + + diff --git a/nodes/code/mcu_code.js b/nodes/code/mcu_code.js new file mode 100644 index 0000000..5aa5a05 --- /dev/null +++ b/nodes/code/mcu_code.js @@ -0,0 +1,7 @@ +module.exports = function(RED) { + function McuCodeNode(config) { + RED.nodes.createNode(this,config); + console.log(config); + } + RED.nodes.registerType("mcu_code",McuCodeNode); +} \ No newline at end of file diff --git a/nodes/code/package.json b/nodes/code/package.json new file mode 100644 index 0000000..a56e2cf --- /dev/null +++ b/nodes/code/package.json @@ -0,0 +1,36 @@ +{ + "name": "@moddable-node-red/mcu_code", + "version": "0.0.1", + "description": "A suite of code nodes for Node-RED MCU Edition", + "scripts": { + "test": "echo \"Error: no tests\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/phoddie/node-red-mcu.git", + "directory": "nodes/code" + }, + "keywords": [ + "mcu", + "node-red", + "node-red-mcu", + "Moddable", + "code", + "function" + ], + "author": "Peter Hoddie, Patrick Soquet", + "license": "LGPL-3.0-or-later", + "bugs": { + "url": "https://github.com/phoddie/node-red-mcu/issues" + }, + "homepage": "https://github.com/phoddie/node-red-mcu/nodes/code/readme.md", + "engines": { + "node": ">=14.0.0" + }, + "node-red": { + "version": ">=3.0.0", + "nodes": { + "mcu_code": "mcu_code.js" + } + } +} From 1af5f269e682920821c0fa0b62d2fc19a71afde7 Mon Sep 17 00:00:00 2001 From: NWLAB Date: Fri, 23 Feb 2024 18:37:13 +0900 Subject: [PATCH 2/4] fix default moddable_manifest --- nodes/code/mcu_code.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodes/code/mcu_code.html b/nodes/code/mcu_code.html index f03cbdd..78869a2 100644 --- a/nodes/code/mcu_code.html +++ b/nodes/code/mcu_code.html @@ -377,7 +377,7 @@ return true; }}, // manifests: {value: ""}, - moddable_manifest: {value: "{include: \"./manifest.json\"}"} + moddable_manifest: {value: ""} }, inputs:1, outputs:1, From 0ab0ee0f99d37247fd5f59993e62a9859a0e53f4 Mon Sep 17 00:00:00 2001 From: NWLAB Date: Fri, 23 Feb 2024 18:40:12 +0900 Subject: [PATCH 3/4] delete comment --- nodes/code/mcu_code.html | 1 - 1 file changed, 1 deletion(-) diff --git a/nodes/code/mcu_code.html b/nodes/code/mcu_code.html index 78869a2..352d575 100644 --- a/nodes/code/mcu_code.html +++ b/nodes/code/mcu_code.html @@ -376,7 +376,6 @@ } return true; }}, -// manifests: {value: ""}, moddable_manifest: {value: ""} }, inputs:1, From 978deaedaab85e8463062ac446d9b307aeb9874b Mon Sep 17 00:00:00 2001 From: NWLAB Date: Sat, 24 Feb 2024 16:01:27 +0900 Subject: [PATCH 4/4] fix nodered.js duplicate function & mcu_code --- nodered.js | 91 ------------------------------------------------------ 1 file changed, 91 deletions(-) diff --git a/nodered.js b/nodered.js index b7409bd..a1b2470 100644 --- a/nodered.js +++ b/nodered.js @@ -743,97 +743,6 @@ class FunctionNode extends Node { } } - -class McuCodeNode extends Node { - #func; - #libs; - #doDone; - //@@ might cache env here - - constructor(id, flow, name) { - super(id, flow, name); - Object.defineProperties(this, { - context: {value: new Context(id)} - }); - Object.defineProperties(this.context, { - global: {value: globalContext}, - flow: {value: flow.context} - }); - } - onStart(config) { - super.onStart(config); - - if (config.libs?.length) { - this.#libs = []; - for (let i = 0; i < config.libs.length; i++) - this.#libs[i] = Modules.importNow(config.libs[i]); - Object.freeze(this.#libs); - } - - this.#func = config.func ?? nop; - this.#doDone = config.doDone; - - try { - const context = this.context; - const initialize = config.initialize; - initialize?.(this, context, context.flow, context.global, this.#libs, {get: name => this.getSetting(name)}); - } - catch (e) { - this.error(e); //@@ what's the right way to handle this? - } - } - onMessage(msg, done) { - try { - const context = this.context; - const func = this.#func; - const _msgid = msg._msgid; - const node = Object.create(this, { - done: {value: done}, - error: {value: (error, msg) => { - this.debug(error.toString()); - if (msg) - done(msg); - }}, - send: {value: msg => { - this.send(msg, _msgid); - }}, - status: {value: status => this.status(status)} - }); - msg = func(msg, node, context, context.flow, context.global, this.#libs, {get: name => this.getSetting(name)}); - if (this.#doDone) - done(); - if (msg) - this.send(msg, _msgid); - } - catch (e) { - done(e); - } - } - send(msg, _msgid) { - _msgid ??= generateId(); - if (Array.isArray(msg)) { - for (let i = 0, length = msg.length; i < length; i++) { - const output = msg[i]; - if (Array.isArray(output)) { - for (let j = 0, length = output.length; j < length; j++) - output[j]._msgid = _msgid; - } - else if (output) - output._msgid = _msgid; - } - } - else - msg._msgid = _msgid; - - return super.send(msg); - } - - static type = "mcu_code"; - static { - RED.nodes.registerType(this.type, this); - } -} - class ChangeNode extends Node { onStart(config) { super.onStart(config);