From 8d2734e7482134af110a537fd54e300d2387ca3e Mon Sep 17 00:00:00 2001 From: JcDenis Date: Sat, 27 Apr 2024 11:54:34 +0200 Subject: [PATCH] try using flow2src node to separate flows --- flows.json | 27 ++++++++++- package.json | 3 +- src/K_Routine/minute_to_HH_mm.js | 7 +++ src/Kernel/check_device.js | 42 +++++++++++++++++ src/Kernel/check_device_finalizejs | 1 + src/Kernel/check_device_initializejs | 1 + src/Kernel/check_metric.js | 50 ++++++++++++++++++++ src/Kernel/set_gate_message.js | 11 +++++ src/Kernel/set_gate_message2.js | 11 +++++ src/Kernel/set_gate_message2_finalizejs | 1 + src/Kernel/set_gate_message2_initializejs | 1 + src/Kernel/set_gate_message_finalizejs | 1 + src/Kernel/set_gate_message_initializejs | 1 + src/manifest.json | 57 +++++++++++++++++++++++ 14 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 src/K_Routine/minute_to_HH_mm.js create mode 100644 src/Kernel/check_device.js create mode 100644 src/Kernel/check_device_finalizejs create mode 100644 src/Kernel/check_device_initializejs create mode 100644 src/Kernel/check_metric.js create mode 100644 src/Kernel/set_gate_message.js create mode 100644 src/Kernel/set_gate_message2.js create mode 100644 src/Kernel/set_gate_message2_finalizejs create mode 100644 src/Kernel/set_gate_message2_initializejs create mode 100644 src/Kernel/set_gate_message_finalizejs create mode 100644 src/Kernel/set_gate_message_initializejs create mode 100644 src/manifest.json diff --git a/flows.json b/flows.json index 2ea7f97..190dee4 100644 --- a/flows.json +++ b/flows.json @@ -730,7 +730,8 @@ "af148bf44b019b35", "b961d989c357e6d6", "2cc2cdcf0b1f710e", - "56d93c268a656fc4" + "56d93c268a656fc4", + "d02f1b2f2c3a2a30" ], "x": 94, "y": 3019, @@ -6083,6 +6084,30 @@ "y": 2280, "wires": [] }, + { + "id": "d02f1b2f2c3a2a30", + "type": "comment", + "z": "ab51b1e8cad7b700", + "g": "d1b208e623179a25", + "name": "MQTT nodered base topic must be same as automate name (default is iotredloop)", + "info": "", + "x": 820, + "y": 3120, + "wires": [] + }, + { + "id": "ff47fafeee1a758e", + "type": "flow2src", + "z": "ab51b1e8cad7b700", + "name": "", + "incFlows": "Kernel,K.Routine", + "incSubflows": "", + "srcFolder": "src", + "chkAutoFlow2Src": true, + "x": 1220, + "y": 60, + "wires": [] + }, { "id": "2623529da72f4fab", "type": "catch", diff --git a/package.json b/package.json index c767fad..dd16e06 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "node-red-contrib-msg-speed": "2.1.0", "node-red-contrib-queue-gate": "1.5.5", "node-red-contrib-simple-gate": "0.5.2", - "node-red-contrib-stackhero-influxdb-v2": "1.0.4" + "node-red-contrib-stackhero-influxdb-v2": "1.0.4", + "@steveorevo/node-red-flow2src": "1.1.0" }, "node-red": { "settings": { diff --git a/src/K_Routine/minute_to_HH_mm.js b/src/K_Routine/minute_to_HH_mm.js new file mode 100644 index 0000000..c7707fb --- /dev/null +++ b/src/K_Routine/minute_to_HH_mm.js @@ -0,0 +1,7 @@ +var d = Number(msg.payload); +var h = Math.floor(d / 60); +var m = Math.floor(d % 60); +if (h < 10) { h = h < 5 ? '00' : '0' + h; } +if (m < 10) { m = m < 5 ? '00' : '0' + m; } +msg.payload = h + ':' + m; +return msg; \ No newline at end of file diff --git a/src/Kernel/check_device.js b/src/Kernel/check_device.js new file mode 100644 index 0000000..01dda35 --- /dev/null +++ b/src/Kernel/check_device.js @@ -0,0 +1,42 @@ +// Check and recreate clean message of device description +// output 1 : error message +// output 2 : device registration + +// script var +var msg = msg; +var newmsg = {}; + +// no message +if (typeof msg === 'undefined'){ + return [null,null]; +} + +// check possible var existence +if (typeof msg.bucket === 'undefined' || + typeof msg.measurement === 'undefined' || + typeof msg.field === 'undefined' || + typeof msg.format === 'undefined' || + typeof msg.default === 'undefined' +) { + node.status({ + fill: "red", shape: "ring", text: 'undefined device'}); + return [msg,null]; +} + +// recreate msg +newmsg = { + 'bucket': msg.bucket, + 'measurement': msg.measurement, + 'field': msg.field, + 'format': msg.format, + 'path': msg.bucket + '/' + msg.measurement + '/' + msg.field, + 'payload': msg.default, + + 'protocol': typeof msg.protocol === 'undefined' ? '' : msg.protocol, + 'model': typeof msg.model === 'undefined' ? '' : msg.model, + 'id': typeof msg.id === 'undefined' ? '' : msg.id, + 'option': typeof msg.option === 'undefined' ? 0 : msg.option +}; + +node.status({ fill: 'green', shape: "ring", text: newmsg.path + ' = ' + newmsg.payload }); +return [null,newmsg]; \ No newline at end of file diff --git a/src/Kernel/check_device_finalizejs b/src/Kernel/check_device_finalizejs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/Kernel/check_device_finalizejs @@ -0,0 +1 @@ + diff --git a/src/Kernel/check_device_initializejs b/src/Kernel/check_device_initializejs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/Kernel/check_device_initializejs @@ -0,0 +1 @@ + diff --git a/src/Kernel/check_metric.js b/src/Kernel/check_metric.js new file mode 100644 index 0000000..2a8c3a4 --- /dev/null +++ b/src/Kernel/check_metric.js @@ -0,0 +1,50 @@ +// output 1 : error message +// output 2 : device registration + +// script var +var msg = msg; +var newmsg = {}; + +// try if msg.topic is a path +if (typeof msg.topic === 'string' && + typeof msg.bucket === 'undefined' && + typeof msg.measurement === 'undefined' && + typeof msg.field === 'undefined' +) { + const device = msg.topic.split('/'); + if (typeof device[1] === 'string' && + typeof device[2] === 'string' && + typeof device[3] === 'string' + ) { + msg.source = device[0]; + msg.bucket = device[1]; + msg.measurement = device[2]; + msg.field = device[3]; + + } +} + +// check var existence +if (typeof msg.bucket === 'undefined' || + typeof msg.measurement === 'undefined' || + typeof msg.field === 'undefined' +) { + node.status({ + fill: "red", shape: "ring", text: 'undefined device'}); + return [msg,null]; +} + +// recreate msg +newmsg = { + 'topic': 'metric', + 'source': typeof msg.source === 'string' ? msg.source : 'unknow', + 'bucket': msg.bucket, + 'measurement': msg.measurement, + 'field': msg.field, + 'path': msg.bucket + '/' + msg.measurement + '/' + msg.field, + 'payload': msg.payload, + 'repeat': typeof msg.repeat === 'boolean' ? msg.repeat : false +}; + +node.status({ fill: 'green', shape: "ring", text: newmsg.source + ' : ' + newmsg.path + ' = ' + newmsg.payload }); +return [null,newmsg]; \ No newline at end of file diff --git a/src/Kernel/set_gate_message.js b/src/Kernel/set_gate_message.js new file mode 100644 index 0000000..7b5b47f --- /dev/null +++ b/src/Kernel/set_gate_message.js @@ -0,0 +1,11 @@ +// script var +var msg = msg; +var newmsg = {}; + +// recreate msg +newmsg = { + 'topic':'gate', + 'payload':'open' +}; + +return newmsg; \ No newline at end of file diff --git a/src/Kernel/set_gate_message2.js b/src/Kernel/set_gate_message2.js new file mode 100644 index 0000000..60960ba --- /dev/null +++ b/src/Kernel/set_gate_message2.js @@ -0,0 +1,11 @@ +// script var +var msg = msg; +var newmsg = {}; + +// recreate msg +newmsg = { + 'topic':'gate', + 'payload': msg.payload +}; + +return newmsg; \ No newline at end of file diff --git a/src/Kernel/set_gate_message2_finalizejs b/src/Kernel/set_gate_message2_finalizejs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/Kernel/set_gate_message2_finalizejs @@ -0,0 +1 @@ + diff --git a/src/Kernel/set_gate_message2_initializejs b/src/Kernel/set_gate_message2_initializejs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/Kernel/set_gate_message2_initializejs @@ -0,0 +1 @@ + diff --git a/src/Kernel/set_gate_message_finalizejs b/src/Kernel/set_gate_message_finalizejs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/Kernel/set_gate_message_finalizejs @@ -0,0 +1 @@ + diff --git a/src/Kernel/set_gate_message_initializejs b/src/Kernel/set_gate_message_initializejs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/Kernel/set_gate_message_initializejs @@ -0,0 +1 @@ + diff --git a/src/manifest.json b/src/manifest.json new file mode 100644 index 0000000..fb5fabd --- /dev/null +++ b/src/manifest.json @@ -0,0 +1,57 @@ +[ + { + "id": "fabb645e0d88c91c", + "property": "func", + "file": "Kernel/check_device.js" + }, + { + "id": "fabb645e0d88c91c", + "property": "initialize", + "file": "Kernel/check_device_initializejs" + }, + { + "id": "fabb645e0d88c91c", + "property": "finalize", + "file": "Kernel/check_device_finalizejs" + }, + { + "id": "2ba23548e87b0292", + "property": "func", + "file": "Kernel/set_gate_message.js" + }, + { + "id": "2ba23548e87b0292", + "property": "initialize", + "file": "Kernel/set_gate_message_initializejs" + }, + { + "id": "2ba23548e87b0292", + "property": "finalize", + "file": "Kernel/set_gate_message_finalizejs" + }, + { + "id": "82229e46e51f94f5", + "property": "func", + "file": "Kernel/check_metric.js" + }, + { + "id": "e6a4e861a99b196c", + "property": "func", + "file": "Kernel/set_gate_message2.js" + }, + { + "id": "e6a4e861a99b196c", + "property": "initialize", + "file": "Kernel/set_gate_message2_initializejs" + }, + { + "id": "e6a4e861a99b196c", + "property": "finalize", + "file": "Kernel/set_gate_message2_finalizejs" + }, + { + "id": "0385dd3208b83d41", + "property": "func", + "file": "K_Routine/minute_to_HH_mm.js" + } +] \ No newline at end of file