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

@ -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;