No turnOff on startup; reportOfflineNodeStatus function fix

This commit is contained in:
rasta5man 2025-01-15 22:17:14 +01:00
parent 75bb2794d2
commit fad53c9c01
3 changed files with 49 additions and 68 deletions

View file

@ -767,8 +767,6 @@ exports.install = function(instance) {
const date = Date.now(); 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) => { Object.keys(nodesData).forEach((node, index) => {
setTimeout(function() { setTimeout(function() {
@ -776,10 +774,20 @@ exports.install = function(instance) {
//potrebujem nody k danej linii //potrebujem nody k danej linii
if (line == nodesData[node].line || line == undefined) { if (line == nodesData[node].line || line == undefined) {
let tbname = nodesData[node].tbname; 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);
}) })
} }

View file

@ -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" 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. 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 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 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: rotary_switch_state - sem by sa mal reportovať stav vstupov manual a auto podľa nasledovnej logiky: Manual = 1 a Auto = 0 -> Manual,
Manual = 1 a Auto = 0 -> vyreportuje Manual Manual = 0 a Auto = 0 -> Off, Manual = 0 a Auto = 1 -> Automatic
Manual = 0 a Auto = 0 -> vyreportuje Off
Manual = 0 a Auto = 1 -> vyreportuje Automatic
door_condition - pin 6, dverový kontakt -> 1 -> vyreportuje Closed, 0 -> vyreportuje Open 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 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: 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_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_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'); const { errLogger, logger, monitor } = require('./helper/logger');
@ -77,6 +71,7 @@ let rvoTbName;
let GLOBALS; //FLOW global GLOBALS let GLOBALS; //FLOW global GLOBALS
let SETTINGS; // GLOBALS.settings let SETTINGS; // GLOBALS.settings
let controller_type; let controller_type;
let contactorSwitchCounter = 0; //how many times line contactor switched ?
let alarmStatus = "OFF"; let alarmStatus = "OFF";
@ -104,7 +99,7 @@ exports.install = function(instance) {
let previousValues = {}; let previousValues = {};
let rsPortReceivedData = []; 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_interval = 5;//minutes
let twilight_sensor = []; let twilight_sensor = [];
const twilight_sensor_array = []; const twilight_sensor_array = [];
@ -198,10 +193,6 @@ exports.install = function(instance) {
if (pinsData[key].type == "state_of_contactor") { if (pinsData[key].type == "state_of_contactor") {
let pin = key - 1; let pin = key - 1;
if (controller_type === "unipi") pin = key; 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"); turnAlarm("off");
initialSetting(); initialSetting();
ws.send(JSON.stringify({ "cmd": "all" }));
// we request dev info about neuron device from evok to keep websocket connection alive // 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 // 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); monitor.info(`turnLine ${onOrOff} - (line, pin, force)`, line, pin, force, info);
let cmd = { "cmd": "set", "dev": "relay", "circuit": pin.slice(5), "value": value }; let cmd = { "cmd": "set", "dev": "relay", "circuit": pin.slice(5), "value": value };
ws.send(JSON.stringify(cmd)); 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. //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(); sendRvoStatus();
}) })
// we expect array as flowdata.data // we expect array as flowdata.data
instance.on("1", flowdata => { instance.on("1", flowdata => {
@ -597,22 +588,15 @@ exports.install = function(instance) {
let line = obj.line; let line = obj.line;
let force = obj.force; let force = obj.force;
let info = obj.info; 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); 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 == "off") turnLine("off", line, undefined, force, info);
else if (obj.command == "turnOnAlarm") turnAlarm("on"); else if (obj.command == "turnOnAlarm") turnAlarm("on");
else if (obj.command == "turnOffAlarm") turnAlarm("off"); 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 });
});
}
}) })

View file

@ -4,15 +4,15 @@ class DataToTbHandler {
this.index = index; this.index = index;
// time, after new value for the given key will be resend to tb (e.g. {status: "OK"}) // 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.previousValues = {};
this.debug = false; this.debug = false;
this.messageCounter = 0; this.messageCounter = 0;
this.sender = ""; 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 = { this.attributeChangeLimit = {
temperature: 0.5, temperature: 0.5,
Phase_1_voltage: 2, Phase_1_voltage: 2,
@ -52,7 +52,7 @@ class DataToTbHandler {
} }
isEmptyObject(obj) { isEmptyObject(obj) {
for (var name in obj) { for (var _ in obj) {
return false; return false;
} }
return true; return true;
@ -62,9 +62,8 @@ class DataToTbHandler {
let keys = Object.keys(dataToTb); let keys = Object.keys(dataToTb);
if(keys.length == 0) if (keys.length == 0) {
{ if (this.debug) console.log("sendToTb received empty object", dataToTb);
if(this.debug) console.log("sendToTb received empty object", dataToTb);
return; return;
} }
@ -74,19 +73,16 @@ class DataToTbHandler {
let arrayOfValues = dataToTb[tbname]; let arrayOfValues = dataToTb[tbname];
let arrayOfValuesToSend = []; let arrayOfValuesToSend = [];
for(let i = 0; i < arrayOfValues.length; i++) for (let i = 0; i < arrayOfValues.length; i++) {
{
ts = arrayOfValues[i].ts; ts = arrayOfValues[i].ts;
let values = this.prepareValuesForTb(tbname, ts, arrayOfValues[i].values); let values = this.prepareValuesForTb(tbname, ts, arrayOfValues[i].values);
if(!this.isEmptyObject(values)) if (!this.isEmptyObject(values)) {
{ arrayOfValuesToSend.push({ ts: ts, values: 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"); //if(this.debug) console.log("data not sent - empty array");
return; return;
} }
@ -94,7 +90,7 @@ class DataToTbHandler {
this.messageCounter++; this.messageCounter++;
let dataToTbModified = { 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); //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) { prepareValuesForTb(tbname, timestamp, values) {
let keys = Object.keys(values); let keys = Object.keys(values);
if(!this.previousValues.hasOwnProperty(tbname)) if (!this.previousValues.hasOwnProperty(tbname)) {
{
this.previousValues[tbname] = {}; this.previousValues[tbname] = {};
} }
//if(this.debug) console.log("prepareValuesForTb", tbname, timestamp, values); //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 key = keys[i];
let value = values[key]; let value = values[key];
if(!this.previousValues[tbname].hasOwnProperty(key)) if (!this.previousValues[tbname].hasOwnProperty(key)) {
{ this.previousValues[tbname][key] = { ts: timestamp, value: value };
this.previousValues[tbname][key] = {ts: timestamp, value: value};
continue; continue;
} }
// attributeData ==> voltage: {ts:333333, value:5} // attributeData ==> voltage: {ts:333333, value:5}
let attributeData = this.previousValues[tbname][key]; let attributeData = this.previousValues[tbname][key];
let attributeToChange = false; let attributeToChange = false;
if(key in this.attributeChangeLimit) attributeToChange = true; if (key in this.attributeChangeLimit) attributeToChange = true;
let limit = this.attributeChangeLimit[key]; 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 diff = timestamp - attributeData.ts;
let timestampDiffToRemoveKey = this.getDiffTimestamp(key); let timestampDiffToRemoveKey = this.getDiffTimestamp(key);
if(diff > timestampDiffToRemoveKey) if (diff > timestampDiffToRemoveKey) {
{
attributeData.ts = Date.now(); attributeData.ts = Date.now();
//if(this.debug) console.log(this.sender + ": update ts for key", key, "diff is", diff, "messageCounter", this.messageCounter); //if(this.debug) console.log(this.sender + ": update ts for key", key, "diff is", diff, "messageCounter", this.messageCounter);
} }
else else {
{
delete values[key]; delete values[key];
//if(this.debug) console.log(this.sender + ": delete key", key, "diff is", diff, "messageCounter", this.messageCounter, timestampDiffToRemoveKey); //if(this.debug) console.log(this.sender + ": delete key", key, "diff is", diff, "messageCounter", this.messageCounter, timestampDiffToRemoveKey);
} }
} }
else else {
{
attributeData.value = value; attributeData.value = value;
attributeData.ts = timestamp; attributeData.ts = timestamp;
} }
@ -162,5 +151,5 @@ class DataToTbHandler {
} }
} }
module.exports = DataToTbHandler; module.exports = DataToTbHandler;