From fad53c9c01c840293275f315e4b0eec391eee1a3 Mon Sep 17 00:00:00 2001 From: rasta5man Date: Wed, 15 Jan 2025 22:17:14 +0100 Subject: [PATCH] No turnOff on startup; reportOfflineNodeStatus function fix --- flow/cmd_manager.js | 16 +++++++--- flow/dido_controller.js | 46 ++++++++++------------------ flow/helper/DataToTbHandler.js | 55 ++++++++++++++-------------------- 3 files changed, 49 insertions(+), 68 deletions(-) diff --git a/flow/cmd_manager.js b/flow/cmd_manager.js index 280a5a6..8739037 100644 --- a/flow/cmd_manager.js +++ b/flow/cmd_manager.js @@ -767,8 +767,6 @@ exports.install = function(instance) { const date = Date.now(); - // it happens, that some data did not get to tb after sending - // we setTimeout to make more time for db to process telemetry (eg 150 messages at once) Object.keys(nodesData).forEach((node, index) => { setTimeout(function() { @@ -776,10 +774,20 @@ exports.install = function(instance) { //potrebujem nody k danej linii if (line == nodesData[node].line || line == undefined) { let tbname = nodesData[node].tbname; - sendTelemetry(values, tbname, date) + const dataToTb = { + [tbname]: [ + { + "ts": date, + "values": values + } + ] + } + //NOTE: we can not use sendTelemetry function, as it modifies input data (values object) + instance.send(SEND_TO.tb, dataToTb); + } - }, (index + 1) * 1000); + }, (index + 1) * 300); }) } diff --git a/flow/dido_controller.js b/flow/dido_controller.js index a6861b4..745c0f6 100644 --- a/flow/dido_controller.js +++ b/flow/dido_controller.js @@ -34,23 +34,17 @@ Currently we are interested in pins no. 1,2,3,6,8,9,10,16 pins number 11, 12, 13 (we receive 10,11,12 in rsPortReceivedData) are "stykace" When port receives data, it must be exactly 4 bytes long. Second byte is pin, that changed its value, fourth byte is value itself. After that, we set this value to "previousValues[allPins[whichpin]]" variable -*/ - -/* -RVO objekt: state_of_main_switch - reportovať stav hlavného ističa : 0-> off 1-> on -rotary_switch_state - sem by sa mal reportovať stav vstupov manual a auto podľa nasledovnej logiky: - Manual = 1 a Auto = 0 -> vyreportuje Manual - Manual = 0 a Auto = 0 -> vyreportuje Off - Manual = 0 a Auto = 1 -> vyreportuje Automatic +rotary_switch_state - sem by sa mal reportovať stav vstupov manual a auto podľa nasledovnej logiky: Manual = 1 a Auto = 0 -> Manual, +Manual = 0 a Auto = 0 -> Off, Manual = 0 a Auto = 1 -> Automatic -door_condition - pin 6, dverový kontakt -> 1 -> vyreportuje Closed, 0 -> vyreportuje Open -twilight_sensor - hodnotu, ktorú vracia ten analógový vstup (17) treba poslať sem ako float number. Zrejme tu potom pridáme nejaký koeficient prevodu na luxy +door_condition - pin 6, dverový kontakt -> 1 -> vyreportuje Closed, 0 -> vyreportuje Open +twilight_sensor - hodnotu, ktorú vracia ten analógový vstup (17) treba poslať sem ako float number. Zrejme tu potom pridáme nejaký koeficient prevodu na luxy Na každú líniu: -state_of_breaker - podľa indexu ističa sa reportuje jeho stav, teda istič 1 na líniu 1: 0-> off 1-> on -state_of_contactor - podľa indexu stykača sa reportuje jeho stav, teda stykač 1 na líniu 1: 0-> off 1-> on +state_of_breaker - podľa indexu ističa sa reportuje jeho stav, teda istič 1 na líniu 1: 0-> off, 1-> on +state_of_contactor - podľa indexu stykača sa reportuje jeho stav, teda stykač 1 na líniu 1: 0-> off, 1-> on */ const { errLogger, logger, monitor } = require('./helper/logger'); @@ -77,6 +71,7 @@ let rvoTbName; let GLOBALS; //FLOW global GLOBALS let SETTINGS; // GLOBALS.settings let controller_type; +let contactorSwitchCounter = 0; //how many times line contactor switched ? let alarmStatus = "OFF"; @@ -104,7 +99,7 @@ exports.install = function(instance) { let previousValues = {}; let rsPortReceivedData = []; - //to be able to get proper twilight values, when + //to be able to get proper twilight values let twilight_sensor_interval = 5;//minutes let twilight_sensor = []; const twilight_sensor_array = []; @@ -198,10 +193,6 @@ exports.install = function(instance) { if (pinsData[key].type == "state_of_contactor") { let pin = key - 1; if (controller_type === "unipi") pin = key; - - //this will modify database - let forceTurnOff = true; - turnLine("off", line, pin, forceTurnOff, "turn off on startup"); } } @@ -317,7 +308,6 @@ exports.install = function(instance) { turnAlarm("off"); initialSetting(); - ws.send(JSON.stringify({ "cmd": "all" })); // we request dev info about neuron device from evok to keep websocket connection alive // for some reason this request returns no data, but connection keeps alive @@ -528,7 +518,6 @@ exports.install = function(instance) { monitor.info(`turnLine ${onOrOff} - (line, pin, force)`, line, pin, force, info); let cmd = { "cmd": "set", "dev": "relay", "circuit": pin.slice(5), "value": value }; ws.send(JSON.stringify(cmd)); - switchLogic(pin, value) } //if rvo is 24/7, it has just one switching profile point at 13:00. we do not want to send notification as it repeats every day. @@ -585,6 +574,8 @@ exports.install = function(instance) { sendRvoStatus(); }) + + // we expect array as flowdata.data instance.on("1", flowdata => { @@ -597,22 +588,15 @@ exports.install = function(instance) { let line = obj.line; let force = obj.force; let info = obj.info; - + + //how many times did the lines switched ? if all lines (except line 0) has switched, we request data from evok: + contactorSwitchCounter++; + if(contactorSwitchCounter == Object.keys(relaysData).length-1 && ws) setTimeout(function(){ws.send(JSON.stringify({cmd:"all"}))},5000); + if (obj.command == "on") turnLine("on", line, undefined, force, info); else if (obj.command == "off") turnLine("off", line, undefined, force, info); else if (obj.command == "turnOnAlarm") turnAlarm("on"); else if (obj.command == "turnOffAlarm") turnAlarm("off"); - - //! ake data prichadzaju z cmd_manager.js ??? - //TODO transform to websocket - if (Array.isArray(obj)) { - - rsPort.write(Buffer.from(obj), function(err) { - switchLogic(obj); - - instance.send(SEND_TO.debug, { "WRITE": obj }); - }); - } }) diff --git a/flow/helper/DataToTbHandler.js b/flow/helper/DataToTbHandler.js index 65e4ec3..f8734cc 100644 --- a/flow/helper/DataToTbHandler.js +++ b/flow/helper/DataToTbHandler.js @@ -4,15 +4,15 @@ class DataToTbHandler { this.index = index; // time, after new value for the given key will be resend to tb (e.g. {status: "OK"}) - this.timeToHoldTbValue = 30*60; //30 minutes + this.timeToHoldTbValue = 30 * 60; //30 minutes this.previousValues = {}; this.debug = false; this.messageCounter = 0; this.sender = ""; - - // if attribute difference is less than limit value, we do not send to tb. + + // if attribute change difference is less than limit value, we do not send to tb. this.attributeChangeLimit = { temperature: 0.5, Phase_1_voltage: 2, @@ -52,7 +52,7 @@ class DataToTbHandler { } isEmptyObject(obj) { - for (var name in obj) { + for (var _ in obj) { return false; } return true; @@ -62,9 +62,8 @@ class DataToTbHandler { let keys = Object.keys(dataToTb); - if(keys.length == 0) - { - if(this.debug) console.log("sendToTb received empty object", dataToTb); + if (keys.length == 0) { + if (this.debug) console.log("sendToTb received empty object", dataToTb); return; } @@ -74,19 +73,16 @@ class DataToTbHandler { let arrayOfValues = dataToTb[tbname]; let arrayOfValuesToSend = []; - for(let i = 0; i < arrayOfValues.length; i++) - { + for (let i = 0; i < arrayOfValues.length; i++) { ts = arrayOfValues[i].ts; let values = this.prepareValuesForTb(tbname, ts, arrayOfValues[i].values); - if(!this.isEmptyObject(values)) - { - arrayOfValuesToSend.push({ts: ts, values: values}); + if (!this.isEmptyObject(values)) { + arrayOfValuesToSend.push({ ts: ts, values: values }); } } - if(arrayOfValuesToSend.length == 0) - { + if (arrayOfValuesToSend.length == 0) { //if(this.debug) console.log("data not sent - empty array"); return; } @@ -94,7 +90,7 @@ class DataToTbHandler { this.messageCounter++; let dataToTbModified = { - [tbname]: arrayOfValuesToSend + [tbname]: arrayOfValuesToSend } //console.log(this.sender + " DATA SEND TO TB ", tbname, this.messageCounter, new Date(ts), dataToTbModified[tbname][0].values, this.instance); @@ -111,47 +107,40 @@ class DataToTbHandler { prepareValuesForTb(tbname, timestamp, values) { let keys = Object.keys(values); - if(!this.previousValues.hasOwnProperty(tbname)) - { + if (!this.previousValues.hasOwnProperty(tbname)) { this.previousValues[tbname] = {}; } //if(this.debug) console.log("prepareValuesForTb", tbname, timestamp, values); - for(let i = 0; i < keys.length; i++) - { + for (let i = 0; i < keys.length; i++) { let key = keys[i]; let value = values[key]; - if(!this.previousValues[tbname].hasOwnProperty(key)) - { - this.previousValues[tbname][key] = {ts: timestamp, value: value}; + if (!this.previousValues[tbname].hasOwnProperty(key)) { + this.previousValues[tbname][key] = { ts: timestamp, value: value }; continue; } // attributeData ==> voltage: {ts:333333, value:5} let attributeData = this.previousValues[tbname][key]; let attributeToChange = false; - if(key in this.attributeChangeLimit) attributeToChange = true; + if (key in this.attributeChangeLimit) attributeToChange = true; let limit = this.attributeChangeLimit[key]; - - if(attributeData.value === value || attributeToChange && Math.abs(attributeData.value - value) < limit) - { + + if (attributeData.value === value || attributeToChange && Math.abs(attributeData.value - value) < limit) { let diff = timestamp - attributeData.ts; let timestampDiffToRemoveKey = this.getDiffTimestamp(key); - if(diff > timestampDiffToRemoveKey) - { + if (diff > timestampDiffToRemoveKey) { attributeData.ts = Date.now(); //if(this.debug) console.log(this.sender + ": update ts for key", key, "diff is", diff, "messageCounter", this.messageCounter); } - else - { + else { delete values[key]; //if(this.debug) console.log(this.sender + ": delete key", key, "diff is", diff, "messageCounter", this.messageCounter, timestampDiffToRemoveKey); } } - else - { + else { attributeData.value = value; attributeData.ts = timestamp; } @@ -162,5 +151,5 @@ class DataToTbHandler { } } -module.exports = DataToTbHandler; +module.exports = DataToTbHandler;