Compare nodesDb; thermometer instead evok; no status.table

This commit is contained in:
rasta5man 2024-12-02 17:06:49 +01:00
parent f63ac50497
commit 5270a898a3
13 changed files with 1255 additions and 4969 deletions

View file

@ -56,7 +56,6 @@ state_of_contactor - podľa indexu stykača sa reportuje jeho stav, teda
momentálne sa stav zmení len keď vo flow klikneš aby sa zmenil, ale zmena by sa mala ukázať aj na platforme
*/
const dbStatus = TABLE("status");
const { errLogger, logger, monitor } = require('./helper/logger');
const SerialPort = require('serialport');
const WebSocket = require('ws');
@ -90,9 +89,6 @@ const SEND_TO = {
cmd_manager: 2
}
const TIME_AFTER_TEMPERATURE_NOK_STATUS = 3600; //seconds
const DIFFERENCE_TO_SEND_TEMPERATURE = 0.31;
exports.install = function(instance) {
@ -108,8 +104,7 @@ exports.install = function(instance) {
//process.exit(1);
})
// temperature value is initialized to -1000. It can be literally anything, we just needs to be able to enter if block in ws.onmessage function, when first temperatere data comes
let previousValues = {temperature: {value: -1000, lastTimeTemperatureReceived: Date.now() / 1000}};
let previousValues = {};
let rsPortReceivedData = [];
//to be able to get proper twilight values, when
@ -163,15 +158,12 @@ exports.install = function(instance) {
rvoTbName = SETTINGS.rvoTbName;
pinsData = GLOBALS.pinsData;
relaysData = GLOBALS.relaysData;
statusData = GLOBALS.statusData;
tbHandler = new DataToTbHandler(SEND_TO.tb)
tbHandler.setSender(exports.title);
controller_type = SETTINGS.controller_type //"lm" or "unipi" //logicMachine
if(controller_type == "") controller_type = "lm";
deviceStatus["temperature"] = statusData.thermometer;
console.log(exports.title, "controller type: ", controller_type);
@ -379,39 +371,19 @@ exports.install = function(instance) {
data = JSON.parse(data.data);
// data comes in array except of "temperature" ==> it comes as an object
if(isObject(data))
{
let value = data['value'];
const values = {};
previousValues["temperature"]["lastTimeTemperatureReceived"] = data['time'];
// we received data from thermometer, but thermometer status is NOK:
if(deviceStatus["temperature"] === "NOK")
{
await writeThermometerStatusToDb("OK");
sendRvoStatus();
}
// temperature value comes very often. To handle it, we check if it change for more than 0.3 degrees, if yes, we send to TB
if(Math.abs(previousValues["temperature"]["value"] - value) > DIFFERENCE_TO_SEND_TEMPERATURE)
{
previousValues["temperature"]["value"] = value;
values['temperature'] = value;
sendTelemetry(values, rvoTbName);
}
return;
}
// we do not handle temperature from evok any more => we return, if temperature comes:
if(isObject(data)) return;
data.map(item => {
let value = item['value'];
let pin = item["dev"] + item["circuit"]; // for example "relay1_03" or "input1_01"
if(pin == undefined) return;
if(pin == undefined) return;
switchLogic(pin, value);
})
}
ws.on('error', (err) => {
monitor.info('websocket error, reconnect')
@ -464,19 +436,19 @@ exports.install = function(instance) {
{
let value = 0;
if(onOrOff == "on") value = 1;
if(value == 1 && SETTINGS.maintenance_mode) return;
alarmStatus = "OFF";
if(value == 1) alarmStatus = "ON";
if(rsPort)
{
let arr = [0x55];
arr.push(13);
arr.push(0);
arr.push(value);
rsPort.write(Buffer.from(arr), function(err) {
logger.debug(`sirena - ${onOrOff}`);
});
@ -605,7 +577,7 @@ exports.install = function(instance) {
//data from modbus_reader or temperature sensor or twilight sensor or other modbus device
instance.on("0", flowdata => {
if(!flowdata.data instanceof Object) return;
if(!isObject(flowdata.data)) return;
// console.log('***********************', flowdata.data)
instance.send(SEND_TO.debug, flowdata.data);
@ -622,10 +594,8 @@ exports.install = function(instance) {
{
deviceStatus["em"] = "NOK";
}
//"NOK-thermometer" comes just from LM. Unipi handles thermometer from ws evok.
else if(status == "NOK-thermometer")
{
previousValues["temperature"]["lastTimeTemperatureReceived"] = null;
deviceStatus["temperature"] = "NOK";
}
}
@ -637,10 +607,8 @@ exports.install = function(instance) {
instance.send(SEND_TO.cmd_manager, {sender: "dido_controller", cmd: "lux_sensor", value: values["twilight_sensor"]});
deviceStatus["twilight_sensor"] = "OK"
}
//"temperature" comes just from LM. Unipi handles thermometer from ws evok.
else if(values.hasOwnProperty("temperature"))
{
previousValues["temperature"]["lastTimeTemperatureReceived"] = Date.now() / 1000; //time in seconds
deviceStatus["temperature"] = "OK";
}
// EM
@ -669,8 +637,8 @@ exports.install = function(instance) {
let force = obj.force;
let info = obj.info;
if(obj.command == "turnOn") turnLine("on", line, undefined, force, info);
else if(obj.command == "turnOff") turnLine("off", 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 == "turnOnAlarm") turnAlarm("on");
else if(obj.command == "turnOffAlarm") turnAlarm("off");
@ -822,15 +790,13 @@ exports.install = function(instance) {
let byte = bytesToInt([byte1, byte0]);
//console.log("calculateStateCode -------------------", byte);
return byte;
}
async function sendRvoStatus() {
// test if dbLoaded is ok to check
//if(!FLOW.dbLoaded) return;
if(SETTINGS === undefined) return;
const table = {
@ -849,21 +815,11 @@ exports.install = function(instance) {
};
for (const phase of SETTINGS.no_voltage) dataToTb[`phase_${phase}_status`] = 0;
//thermometer did not send data for more than a hour. Time in seconds
if(deviceStatus["temperature"] === "OK")
{
if(previousValues["temperature"]["lastTimeTemperatureReceived"] + TIME_AFTER_TEMPERATURE_NOK_STATUS < Date.now() / 1000)
{
// to be able to calculate proper RVO status, we need to await writeThermometerStatusToDb function
await writeThermometerStatusToDb("NOK");
dataToTb["thermometer_status"] = 0;
}
}
dataToTb["status"] = checkRvoStatus();
dataToTb["statecode"] = calculateStateCode();
//console.log(dataToTb);
sendTelemetry(dataToTb, rvoTbName);
}
@ -876,8 +832,7 @@ exports.install = function(instance) {
let status = "OK";
for (const [key, value] of Object.entries(deviceStatus)) {
//if(["em", "twilight_sensor", "temperature"].includes(key) && value == "NOK") status = "NOK";
if(["em", "twilight_sensor"].includes(key) && value == "NOK") status = "NOK";
if(["em", "twilight_sensor", "temperature"].includes(key) && value == "NOK") status = "NOK";
}
if(status == "OK")
@ -899,6 +854,7 @@ exports.install = function(instance) {
if(!SETTINGS.masterNodeIsResponding) status = "NOK";
if(SETTINGS.no_voltage.size > 0) status = "NOK";
// console.log("rvo status",status)
return status;
}
@ -1258,7 +1214,6 @@ exports.install = function(instance) {
}
if(newPinValue != previousValues[pinIndex]) previousValues[pinIndex] = newPinValue;
if(rvoTbName == tbname) sendRvoStatus();
if(Object.keys(values).length > 0 && tbname) sendTelemetry(values, tbname);
}
@ -1277,24 +1232,6 @@ exports.install = function(instance) {
}
function writeThermometerStatusToDb(status) {
return new Promise(function(resolve, reject) {
dbStatus.modify({ thermometer: status }).make(function(builder) {
builder.callback(function(err, response) {
if(!err)
{
deviceStatus["temperature"] = status;
console.log(`Wrote to db status: thermometer ${status}`);
resolve("ok")
}
reject("nok")
});
});
})
}
function isObject (item) {
return (typeof item === "object" && !Array.isArray(item) && item !== null);
}