From ca39e1266c637d1e9a8152e798a84536136def9e Mon Sep 17 00:00:00 2001 From: rasta5man Date: Sun, 22 Sep 2024 22:18:46 +0200 Subject: [PATCH] Rvo status handled just in dido_controller --- flow/cmd_manager.js | 68 +----- flow/dido_controller.js | 372 ++++++++++++--------------------- flow/helper/DataToTbHandler.js | 31 ++- flow/modbus_reader.js | 6 +- flow/thermometer.js | 5 +- flow/wsmqttpublish.js | 2 +- 6 files changed, 166 insertions(+), 318 deletions(-) diff --git a/flow/cmd_manager.js b/flow/cmd_manager.js index 0fdfea1..613d1f6 100644 --- a/flow/cmd_manager.js +++ b/flow/cmd_manager.js @@ -142,7 +142,6 @@ let nodesData = {};//key is node, value data from db let cmdCounter = {};//key is node, value is counter let cmdNOKNodeCounter = {};//key is node, value is counter -let testTbName = "deleteAfterTesting" //for status testing purposes; //END OF VARIABLE SETTINGS //-------------------------------- @@ -319,8 +318,6 @@ function processNodeProfile(node) cmdCounter[node] = 1; tasks.push(params); - - //sendNotification("CMD Manager: process cmd", relaysData[0].tbname, ERRWEIGHT.NOTICE, "Master node is working again", "", SEND_TO.tb, instance ); } else { @@ -1794,9 +1791,7 @@ exports.install = function(instance) { message = "off"; } - //sendNotification("CMD Manager: process cmd", relaysData[0].tbname, ERRWEIGHT.INFO, "aplikovaný bod profilu línie " + params.line + " - stav: " + message, "", SEND_TO.tb, instance, null ); sendNotification("CMD Manager: process cmd", FLOW.OMS_edgeName, "switching_profile_point_applied_to_line", {line: params.line, value: message}, "", SEND_TO.tb, instance ); - interval = setInterval(runTasks, SHORT_INTERVAL); return; } @@ -1936,7 +1931,6 @@ exports.install = function(instance) { //TODO astrohodiny let dusk = "Time of dusk: " + sunCalcResult["dusk"]; - //sendNotification("CMD Manager: calculated Time of dusk", relaysData[0].tbname, ERRWEIGHT.INFO, dusk, "", SEND_TO.tb, instance, null ); } } @@ -1956,7 +1950,6 @@ exports.install = function(instance) { //TODO astrohodiny let dawn = "Time of dawn: " + sunCalcResult["dawn"]; - //sendNotification("CMD Manager: calculated Time of dusk", relaysData[0].tbname, ERRWEIGHT.INFO, dawn, "", SEND_TO.tb, instance, null ); } } @@ -1968,7 +1961,7 @@ exports.install = function(instance) { startTime = new Date(); let saveToTb = true; - if(!tbname) saveToTb = false; + if(!tbname) saveToTb = false; let itIsNodeCommand = listOfCommands.includes(register); //reading data from node (voltage, current, dimming, status) let resp = com_generic(nodeAddress, params.recipient, params.rw, register, params.name, params.byte1, params.byte2, params.byte3, params.byte4); @@ -1993,13 +1986,6 @@ exports.install = function(instance) { let message_type = result.type; let error = result.error; - //testing purpose node 638, tbname rDbQ84xzwgdqEoPm3kbJQWk9anOZY1RXyBv2LVM6 - if(tbname == testTbName) { - message = "NOK"; - message_type = "ERROR"; - error = "type is: ERROR"; - } - if(params.debug != "generated cmd") { //debug("writeData: done " + message_type + " duration: " + timeDiff + " message_type: " + params.debug, params); @@ -2057,24 +2043,16 @@ exports.install = function(instance) { } //master node - if(nodeAddress == 0 && register == 4) - { - values.status = "OK"; - values["edge_fw_version"] = FLOW.OMS_edge_fw_version; - } - if(nodeAddress == 0) { - //sendNotification("CMD Manager: process cmd", relaysData[0].tbname, ERRWEIGHT.NOTICE, "Master node is working again", "", SEND_TO.tb, instance, "rvo_status" ); - //sendNotification("CMD Manager: process cmd", relaysData[0].tbname, "master_node_is_responding_again", {}, "", SEND_TO.tb, instance, "rvo_status" ); sendNotification("CMD Manager: process cmd", FLOW.OMS_edgeName, "master_node_is_responding_again", {}, "", SEND_TO.tb, instance, "rvo_status" ); FLOW.OMS_masterNodeIsResponding = true; + if(register == 4) values["edge_fw_version"] = FLOW.OMS_edge_fw_version; } //odoslanie príkazu z terminálu - dáta if(type == "cmd-terminal") { - //sendNotification("CMD Manager: process cmd", relaysData[0].tbname, ERRWEIGHT.DEBUG, "odoslanie príkazu z terminálu", params, SEND_TO.tb, instance, null ); sendNotification("CMD Manager: process cmd", FLOW.OMS_edgeName, "command_was_sent_from_terminal_interface", {}, params, SEND_TO.tb, instance ); } @@ -2169,23 +2147,23 @@ exports.install = function(instance) { let values = {}; - console.log(message); + // console.log(message); let updateStatus = updateNodeStatus(node, false); + //master node if(node == 0) { - //sendNotification("CMD Manager: process cmd", relaysData[0].tbname, ERRWEIGHT.ALERT, "Master node not responding", "", SEND_TO.tb, instance, "rvo_status"); sendNotification("CMD Manager: process cmd", FLOW.OMS_edgeName, "master_node_is_not_responding", {}, "", SEND_TO.tb, instance, "rvo_status"); logger.debug("master_node_is_not_responding", params); FLOW.OMS_masterNodeIsResponding = false; + + if(register == 4) values["master_node_version"] = "NOK"; } if(type == "set_node_profile") { delete cmdCounter[node]; - logger.debug( "profil nebol úspešne odoslaný na node č. ", params, resp); - - //sendNotification("CMD Manager: process cmd", tbname, ERRWEIGHT.ALERT, "odosielanie profilu na node č. " + node + " zlyhalo", "", SEND_TO.tb, instance, null ); + logger.debug( "profil nebol úspešne odoslaný na node č. ", params); sendNotification("CMD Manager: process cmd", tbName, "configuration_of_dimming_profile_to_node_failed", {node: node}, "", SEND_TO.tb, instance ); } @@ -2199,14 +2177,7 @@ exports.install = function(instance) { values.status = "NOK"; } - //master node - if(node == 0 && register == 4) - { - values.status = "NOK"; - values["master_node_version"] = "NOK"; - } - - console.log("------",node, register, type, itIsNodeCommand, updateStatus, saveToTb, values); + // console.log("------",node, register, type, itIsNodeCommand, updateStatus, saveToTb, values); if(saveToTb && Object.keys(values).length > 0 && tbName) { @@ -2391,12 +2362,6 @@ exports.install = function(instance) { rsPort.close(); }); - instance.on("1", function(flowdata){ - if(flowdata.data.hasOwnProperty("testTbName")) - { - testTbName = flowdata.data.testTbName; - } - }) instance.on("data", async function(flowdata) { @@ -2535,7 +2500,6 @@ exports.install = function(instance) { tasks.push(params); - //process profiles turnOnOffLinesAccordingToLuxSensor(lux_sensor); } @@ -2552,14 +2516,7 @@ exports.install = function(instance) { state_of_breaker[line] = value; let status = "OK"; - let weight = ERRWEIGHT.NOTICE; - let message = `zapnutý istič línie č. ${line}`; - if(value == "Off") - { - weight = ERRWEIGHT.ERROR; - message = `vypnutý istič línie č. ${line}`; - status = "NOK"; - } + if(value == "Off") status = "NOK"; if(dataChanged) { @@ -2588,11 +2545,7 @@ exports.install = function(instance) { tbHandler.sendToTb(dataToTb, instance); //current value - if(value == "Off") - { - //vyreportovat vsetky svietdla na linii - reportOfflineNodeStatus(line); - } + if(value == "Off") reportOfflineNodeStatus(line); //vyreportovat vsetky svietidla na linii else reportOnlineNodeStatus(line); } @@ -2788,7 +2741,6 @@ exports.install = function(instance) { if(profile === "") logger.debug("worksys - update node profile done - profile is empty"); //profil úspešne prijatý pre node č. xx - //sendNotification("CMD manager", tbname, ERRWEIGHT.INFO, `profil úspešne poslaný z platformy na RVO pre node č. ${node}`, profile, SEND_TO.tb, instance, null ); sendNotification("CMD manager", tbname, "dimming_profile_was_processed_for_node", {node: node}, profile, SEND_TO.tb, instance ); nodesData[node].processed = false; diff --git a/flow/dido_controller.js b/flow/dido_controller.js index 05cf3b4..9e61344 100644 --- a/flow/dido_controller.js +++ b/flow/dido_controller.js @@ -3,7 +3,7 @@ exports.title = 'DIDO_Controller'; exports.version = '2.0.0'; exports.group = 'Worksys'; exports.color = '#2134B0'; -exports.input = 3; +exports.input = 2; exports.output = ["red", "white", "yellow"]; exports.click = false; exports.icon = 'bolt'; @@ -59,7 +59,8 @@ state_of_contactor - podľa indexu stykača sa reportuje jeho stav, teda //globals //FIRMWARE version -FLOW.OMS_edge_fw_version = "2024-07-08";//rok-mesiac-den +//TODO remove FLOW.OMS_edgeName variable, as we have FLOW.OMS_rvo_tbname +FLOW.OMS_edge_fw_version = "2024-09-23";//rok-mesiac-den FLOW.OMS_edgeName = ""; FLOW.OMS_maintenance_mode = false; @@ -83,6 +84,9 @@ const SEND_TO = { cmd_manager: 2 } +const TIME_AFTER_TEMPERATURE_NOK_STATUS = 3600; //seconds +const DIFFERENCE_TO_SEND_TEMPERATURE = 0.31; + var log4js = require("log4js"); var path = require('path'); @@ -178,21 +182,20 @@ exports.install = function(instance) { let statusData = null; //status for calculating Statecodes - let deviceStatus = {};//key is device name: temperature,.... - deviceStatus["state_of_main_switch"] = "Off";//Hlavný istič - deviceStatus["rotary_switch_state"] = "Off";//Prevádzkový mód - deviceStatus["door_condition"] = "closed";//Dverový kontakt - deviceStatus["em"] = "OK";//elektromer rvo - deviceStatus["temperature"] = "OK";//templomer - deviceStatus["battery"] = "OK";//Batéria - deviceStatus["power_supply"] = "OK";//Zdroj - deviceStatus["master_node"] = "OK";//MN - FLOW.OMS_masterNodeIsResponding - deviceStatus["no_voltage"] = "OK";//FLOW.OMS_no_voltage - výpadok napätia na fáze - - deviceStatus["state_of_breaker"] = {};//"Off";//Istič - deviceStatus["state_of_contactor"] = {};//"Off";//Stykač - deviceStatus["twilight_sensor"] = "OK"; //lux sensor - + let deviceStatus = { //key is device name: temperature,.... + "state_of_main_switch": "Off", //Hlavný istič + "rotary_switch_state": "Off", //Prevádzkový mód + "door_condition": "closed", //Dverový kontakt + "em": "OK", //elektromer rvo + "temperature": "OK", //templomer + "battery": "OK", //Batéria + "power_supply": "OK", //Zdroj + "master_node": "OK", //MN - FLOW.OMS_masterNodeIsResponding + "no_voltage": "OK", //FLOW.OMS_no_voltage - výpadok napätia na fáze + "state_of_breaker": {}, //"Off",//Istič + "state_of_contactor": {}, //"Off",//Stykač + "twilight_sensor": "OK" //lux sensor + }; const SerialPort = require('serialport'); const WebSocket = require('ws'); @@ -230,9 +233,9 @@ exports.install = function(instance) { relaysData = makeMapFromDbResult(responseRelays, "line"); let responseStatus = await promisifyBuilder(dbStatus.find()); - statusData = responseStatus[0]; // {thermometer: '{"status":"OK","temperature":0}', em: 'OK', twilight_sensor: 'OK'} + statusData = responseStatus[0]; // {thermometer: 'OK', em: 'OK', twilight_sensor: 'OK'} deviceStatus["temperature"] = statusData.thermometer; - + FLOW.OMS_rvo_tbname = relaysData[0].tbname; if(controller_type === "lm") @@ -290,7 +293,6 @@ exports.install = function(instance) { let values = {}; values["edge_fw_version"] = FLOW.OMS_edge_fw_version; values["maintenance_mode"] = FLOW.OMS_maintenance_mode; - values["status"] = "OK"; edgeName = relaysData[0].tbname; FLOW.OMS_edgeName = edgeName; @@ -455,7 +457,7 @@ exports.install = function(instance) { // dev: 'input', // mode: 'Simple' // }, - ws.onmessage = function(data) { + ws.onmessage = async function(data) { data = JSON.parse(data.data); @@ -463,18 +465,22 @@ exports.install = function(instance) { 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") { - writeThermometerStatusToDb("OK"); + 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) > 0.21 ) + if(Math.abs(previousValues["temperature"]["value"] - value) > DIFFERENCE_TO_SEND_TEMPERATURE) { previousValues["temperature"]["value"] = value; - sendTelemetry({temperature: value}, FLOW.OMS_rvo_tbname); + values['temperature'] = value; + sendTelemetry(values, FLOW.OMS_rvo_tbname); } return; } @@ -802,7 +808,6 @@ exports.install = function(instance) { ws.send(JSON.stringify(cmd)); switchLogic(pin, 0) } - } @@ -851,18 +856,13 @@ exports.install = function(instance) { else if(values.hasOwnProperty("total_power") || values.hasOwnProperty("total_energy") || values.hasOwnProperty("power_factor") || values.hasOwnProperty("Phase_1_voltage") || values.hasOwnProperty("Phase_1_current")) { deviceStatus["em"] = "OK"; - } - else - { - return; + FLOW.OMS_no_voltage.size > 0 ? deviceStatus["no_voltage"] = "NOK": deviceStatus["no_voltage"] = "OK"; } - const updateStatus = checkFinalRVOStatus(); - if(updateStatus) values.status = "OK"; - sendTelemetry(values, FLOW.OMS_rvo_tbname); } + sendRvoStatus(); }) @@ -903,11 +903,12 @@ exports.install = function(instance) { let bits = []; //Hlavný istič - state_of_main_switch - if(deviceStatus["state_of_main_switch"] == "On") + //TODO state_of main_switch is door contact in senica rvo - values should be "open" and "closed" + if(deviceStatus["state_of_main_switch"] == "closed") { bits.push(0); } - else if(deviceStatus["state_of_main_switch"] == "Off") + else { bits.push(1); } @@ -1037,7 +1038,7 @@ exports.install = function(instance) { } - function sendDeviceStatus() { + async function sendRvoStatus() { const table = { "OK": 1, @@ -1054,124 +1055,57 @@ exports.install = function(instance) { "master_node_status": table[deviceStatus["master_node"]] }; - for (const phase of FLOW.OMS_no_voltage) { - if(phase == 1) dataToTb["phase_1_status"] = 0; - if(phase == 2) dataToTb["phase_2_status"] = 0; - if(phase == 3) dataToTb["phase_3_status"] = 0; - } + for (const phase of FLOW.OMS_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"] + 3600 < Date.now() / 1000) + if(previousValues["temperature"]["lastTimeTemperatureReceived"] + TIME_AFTER_TEMPERATURE_NOK_STATUS < Date.now() / 1000) { - writeThermometerStatusToDb("NOK"); + // 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(); + sendTelemetry(dataToTb, FLOW.OMS_rvo_tbname); } - setInterval(sendDeviceStatus, 150000); - - function checkFinalRVOStatus() { + function checkRvoStatus() { // we check if any of these pins values are 0 --> it means status RVO is "NOK" // pinIndex 6 is door_condition - if it is opened in maintenance mode - status = OK - //set RVO state let status = "OK"; - if(deviceStatus["em"] == "NOK") - { - let writeToFile = errorHandler.processMessage("checkFinalRVOStatus: EM status is NOK"); - if(writeToFile) errLogger.error("checkFinalRVOStatus: EM status is NOK"); - - status = "NOK"; + for (const [key, value] of Object.entries(deviceStatus)) { + if(["em", "twilight_sensor", "temperature"].includes(key) && value == "NOK") status = "NOK"; } - if(deviceStatus["twilight_sensor"] == "NOK") - { - let writeToFile = errorHandler.processMessage("checkFinalRVOStatus: twilight_sensor is NOK"); - if(writeToFile) errLogger.error("checkFinalRVOStatus: twilight sensor is NOK"); - - status = "NOK"; - } - - //ak teplomer NOK, rvo nok - if(deviceStatus["temperature"] == "NOK") - { - - let writeToFile = errorHandler.processMessage("checkFinalRVOStatus: temperature status is NOK"); - if(writeToFile) errLogger.error("checkFinalRVOStatus: temperature status is NOK"); - - status = "NOK"; - } - if(status == "OK") { let pinIndexes = [1, 4, 6]; if(controller_type == 'unipi') pinIndexes = ['input1_01', 'input1_04', 'input1_05']; - //console.log('-------- previousValues', previousValues); - for (const pinIndex of pinIndexes) { if (previousValues[pinIndex] === 0) { - if ((pinIndex === 6 || pinIndex === 'input1_01' || pinIndex === 'input1_05') && FLOW.OMS_maintenance_mode) continue; - - let writeToFile = errorHandler.processMessage("checkFinalRVOStatus: value is 0"); - if(writeToFile) errLogger.error("checkFinalRVOStatus: value is 0", pinsData[pinIndex].type); - status = "NOK"; - break; } } } // battery status. If value is 1 - battery is NOK - if (previousValues[5] === 1) - { - let writeToFile = errorHandler.processMessage("checkFinalRVOStatus: NOK status generated by battery"); - if(writeToFile) errLogger.error("checkFinalRVOStatus: NOK status generated by battery"); + if (previousValues[5] === 1) status = "NOK"; + if(!FLOW.OMS_masterNodeIsResponding) status = "NOK"; + if(FLOW.OMS_no_voltage.size > 0) status = "NOK"; - status = "NOK"; - } - - //console.log("FLOW.OMS_masterNodeIsResponding", FLOW.OMS_masterNodeIsResponding); - - if(!FLOW.OMS_masterNodeIsResponding) - { - //errLogger.error("Master node is not responding"); - errorHandler.sendMessageToService("Master node is not responding"); - status = "NOK"; - - deviceStatus["master_node"] = "NOK"; - } - else deviceStatus["master_node"] = "OK"; - - //console.log("checkFinalRVOStatus", status); - if(FLOW.OMS_no_voltage.size > 0) - { - let writeToFile = errorHandler.processMessage("no voltage detected"); - if(writeToFile) errLogger.error("no voltage detected", FLOW.OMS_no_voltage); - - status = "NOK"; - - deviceStatus["no_voltage"] = "NOK"; - } - else deviceStatus["no_voltage"] = "OK"; - - if(status == "NOK") - { - sendTelemetry({status: "NOK"}, FLOW.OMS_rvo_tbname); - return false; - } - - return true; + return status; } @@ -1179,8 +1113,8 @@ exports.install = function(instance) { // we pass two values in case of websocket ==> switchLogic("relay1_03",1) ==> ["relay1_03",1] const switchLogic = (...args) => { - let values = {status: "OK"}; - let dataToTb, pinIndex, newPinValue, twilight; + let values = {}; + let pinIndex, newPinValue, twilight; //data from rsPort if(args.length == 1) @@ -1201,6 +1135,7 @@ exports.install = function(instance) { if(obj == undefined) { previousValues[pinIndex] = newPinValue; + logger.debug("dido-switchLogic ==> no pinIndex", pinIndex); return; } @@ -1216,22 +1151,22 @@ exports.install = function(instance) { //Hlavný istič //! po novom uz 'state of main switch' nemame. Namiesto neho je 'door_condition', kedze mame dvoje dveri //! takze ked pride z evoku signal pre 'input1_05', handlujeme ho ako 'door_condition' - if(type === "!!!state_of_main_switch") - { - if (newPinValue === 0 && newPinValue !== previousValues[pinIndex]) - { - sendNotification("switchLogic", edgeName, "main_switch_has_been_turned_off", {}, "", SEND_TO.tb, instance , "state_of_main_switch"); - values["status"] = "NOK"; + // if(type === "!!!state_of_main_switch") + // { + // if (newPinValue === 0 && newPinValue !== previousValues[pinIndex]) + // { + // sendNotification("switchLogic", edgeName, "main_switch_has_been_turned_off", {}, "", SEND_TO.tb, instance , "state_of_main_switch"); + // values["status"] = "NOK"; - deviceStatus["state_of_main_switch"] = "Off"; - } - else if (newPinValue === 1 && newPinValue !== previousValues[pinIndex]) - { - sendNotification("switchLogic", edgeName, "main_switch_has_been_turned_on", {}, "", SEND_TO.tb, instance , "state_of_main_switch"); + // deviceStatus["state_of_main_switch"] = "Off"; + // } + // else if (newPinValue === 1 && newPinValue !== previousValues[pinIndex]) + // { + // sendNotification("switchLogic", edgeName, "main_switch_has_been_turned_on", {}, "", SEND_TO.tb, instance , "state_of_main_switch"); - deviceStatus["state_of_main_switch"] = "On"; - } - } + // deviceStatus["state_of_main_switch"] = "On"; + // } + // } //Prevádzkový mód if(type == "rotary_switch_state") @@ -1271,8 +1206,8 @@ exports.install = function(instance) { //automatic - profilu pre nody sa vykonavaju //ak je spracovany, a automatic - tak ho zapnem //ak nie je spracovany, iba profil zapisem - - instance.send(SEND_TO.cmd_manager, {sender: "dido_controller", cmd: "rotary_switch_state", value: value}); + + if(pin2 != undefined && pin3 != undefined) instance.send(SEND_TO.cmd_manager, {sender: "dido_controller", cmd: "rotary_switch_state", value: value}); //console.log("rotary_switch_state pin", pin2, pin3, value); } @@ -1283,7 +1218,6 @@ exports.install = function(instance) { { //sendNotification("switchLogic", edgeName, ERRWEIGHT.ALERT, "Power supply is not OK", "", SEND_TO.tb, instance); sendNotification("switchLogic", edgeName, "power_supply_has_disconnected_input", {}, "", SEND_TO.tb, instance, "power_supply"); - values["status"] = "NOK"; deviceStatus["power_supply"] = "NOK"; } @@ -1302,7 +1236,6 @@ exports.install = function(instance) { { //sendNotification("switchLogic", edgeName, ERRWEIGHT.ERROR, "Battery is not OK", "", SEND_TO.tb, instance); sendNotification("switchLogic", edgeName, "battery_level_is_low", {}, "", SEND_TO.tb, instance, "battery_level"); - values["status"] = "NOK"; deviceStatus["battery"] = "NOK"; } @@ -1320,13 +1253,6 @@ exports.install = function(instance) { else if(type == "door_condition" || type === "state_of_main_switch") { newPinValue === 0 ? value = "open" : value = "closed"; - - if (newPinValue != previousValues[pinIndex]) - { - //sendNotification("switchLogic", edgeName, ERRWEIGHT.NOTICE, `RVO door ${value}`, "", SEND_TO.tb, instance, "rvo_door"); - //TODO ? sendNotification("switchLogic", edgeName, "door_value", {value: value}, "", SEND_TO.tb, instance, "rvo_door"); - } - if (value === "open" && FLOW.OMS_maintenance_mode) { sendNotification("switchLogic", edgeName, "door_has_been_open", {}, "", SEND_TO.tb, instance, "rvo_door"); @@ -1336,9 +1262,6 @@ exports.install = function(instance) { { //sendNotification("switchLogic", edgeName, ERRWEIGHT.WARNING, "RVO open door out of maintenance mode", "", SEND_TO.tb, instance); sendNotification("switchLogic", edgeName, "door_has_been_open_without_permision_alarm_is_on", {}, "", SEND_TO.tb, instance, "rvo_door"); - values["status"] = "NOK"; - - //console.log(door_has_been_open_without_permision_alarm_is_on); // zapneme sirenu // ak sa otvoria dvere len na elektromeri (type === "state_of_main_switch") alarm sa nema spustit. alarm sa spusti len ked sa otvoria hlavne dvere (type === "door_condition") @@ -1353,8 +1276,7 @@ exports.install = function(instance) { sendNotification("switchLogic", edgeName, "door_has_been_closed", {}, "", SEND_TO.tb, instance, "rvo_door"); } - deviceStatus["door_condition"] = value; - + deviceStatus[type] = value; } //lux sensor else if(type == "twilight_sensor") @@ -1362,7 +1284,6 @@ exports.install = function(instance) { //! TODO - to show nok status, if lux value is not changing more then 10 times. //Daylight is far more than 1000. So most of the day, when it is sunshine comes just value 1000. But lux sensor is not NOK. //This is not the case in LM. If value from LM is the same 10x, there is 99% possibility, that sensor is NOK. - values["status"] = "OK"; value = newPinValue; if(controller_type === 'lm') @@ -1387,7 +1308,6 @@ exports.install = function(instance) { if(set.size === 1 && !twilightError) { twilightError = true; - values["status"] = "NOK"; let value = twilight_sensor_array.shift(); //sendNotification("switchLogic", edgeName, ERRWEIGHT.ERROR, "Lux sensor error", {"Repeating value": value}, SEND_TO.tb, instance ); newPinValue = 0; @@ -1401,13 +1321,11 @@ exports.install = function(instance) { } else if (set.size === 1 && twilightError) { - values["status"] = "NOK"; twilight_sensor_array.shift(); newPinValue = 0; } } - let diff = twilight_sensor[ twilight_sensor.length - 1 ].timestamp - twilight_sensor[0].timestamp; if(diff >= twilight_sensor_interval * 60 * 1000) { @@ -1424,7 +1342,6 @@ exports.install = function(instance) { else if(type == "state_of_contactor") { //sendNotification("switchLogic", edgeName, ERRWEIGHT.INFO, `State of contactor ${line} is now ${value}`, "", SEND_TO.tb, instance ); - if(!(deviceStatus["state_of_contactor"][line] == value)) { sendNotification("switchLogic", edgeName, "state_of_contactor_for_line", {line: line, value: value}, "", SEND_TO.tb, instance ); @@ -1465,8 +1382,7 @@ exports.install = function(instance) { }); }); } - - if(type === "state_of_breaker") + else if(type === "state_of_breaker") { let valueChanged = false; @@ -1528,66 +1444,20 @@ exports.install = function(instance) { reportLineStatus(line); } + else return; values[type] = value; - - //if(FLOW.OMS_rvo_tbname == tbname) values["statecode"] = calculateStateCode(); - if(pinsData.hasOwnProperty(pinIndex)) + if(type == "rotary_switch_state") { - let valueChanged = false; - if(newPinValue != previousValues[pinIndex]) - { - valueChanged = true; - //pin was changed - previousValues[pinIndex] = newPinValue; - } - - let result = checkFinalRVOStatus(); - if(!result && line == 0) - { - values["status"] = "NOK"; - } - - if(type == "state_of_contactor") valueChanged = true; - if(type == "rotary_switch_state") valueChanged = true; - if(type === "state_of_breaker") - { - //console.log(type, values, valueChanged); - } - - if(FLOW.OMS_rvo_tbname == "") - { - console.log("FLOW.OMS_rvo_tbname is EMPTY"); - } - - if(FLOW.OMS_rvo_tbname == tbname) - { - values["statecode"] = calculateStateCode(); - //console.log('**********************', type, values, valueChanged, FLOW.OMS_rvo_tbname, tbname); - } - - if(valueChanged) - { - sendTelemetry(values, tbname); - } - - if(type == "rotary_switch_state") - { - if(FLOW.OMS_maintenance_mode) value = "maintenance"; - value = value.toLowerCase(); - - let values = {}; - values["power_mode"] = value; - - sendTelemetry(values, tbname); - } - } - else - { - logger.debug("no pinIndex", pinsData[pinIndex], pinsData); + if(FLOW.OMS_maintenance_mode) value = "maintenance"; + value = value.toLowerCase(); + values["power_mode"] = value; } + if(newPinValue != previousValues[pinIndex]) previousValues[pinIndex] = newPinValue; + if(FLOW.OMS_rvo_tbname == tbname) sendRvoStatus(); + if(Object.keys(values).length > 0 && tbname) sendTelemetry(values, tbname); } @@ -1607,15 +1477,20 @@ exports.install = function(instance) { function writeThermometerStatusToDb(status) { - 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}`); - } - }); - }); + 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") + }); + }); + }) } @@ -1902,25 +1777,52 @@ exports.install = function(instance) { //! pins_data --> from UNIPI // { -// '16': { pin: '16', type: 'twilight_sensor', line: 0 }, -// al_mswitch: { pin: 'al_mswitch', type: 'state_of_main_switch', line: 0 }, -// al_rswitch1: { pin: 'al_rswitch1', type: 'rotary_switch_state', line: 0 }, -// al_rswitch2: { pin: 'al_rswitch2', type: 'rotary_switch_state', line: 0 }, -// al_power: { pin: 'al_power', type: 'power_supply', line: 0 }, -// al_battery: { pin: 'al_battery', type: 'battery', line: 0 }, -// al_door: { pin: 'al_door', type: 'door_condition', line: 0 }, -// al_breaker1: { pin: 'al_breaker1', type: 'state_of_breaker', line: 1 }, -// al_breaker2: { pin: 'al_breaker2', type: 'state_of_breaker', line: 2 }, -// al_breaker3: { pin: 'al_breaker3', type: 'state_of_breaker', line: 3 }, -// al_breaker4: { pin: 'al_breaker4', type: 'state_of_breaker', line: 4 }, -// al_relay_1: { pin: 'al_relay_1', type: 'state_of_contactor', line: 1 }, -// al_relay_2: { pin: 'al_relay_2', type: 'state_of_contactor', line: 2 }, -// al_relay_3: { pin: 'al_relay_3', type: 'state_of_contactor', line: 3 }, -// al_relay_4: { pin: 'al_relay_4', type: 'state_of_contactor', line: 4 }, -// '28744F7791180257': { pin: '28744F7791180257', type: 'temperature', line: 0 } +// input1_01: { +// pin: 'input1_01', +// type: 'door_condition', +// line: 0, +// tbname: 'PLBJzmK1r3Gynd6OW0gGYz0e5wV4vx9bDEqNgYR8' +// }, +// input1_02: { +// pin: 'input1_02', +// type: 'rotary_switch_state', +// line: 0, +// tbname: 'PLBJzmK1r3Gynd6OW0gGYz0e5wV4vx9bDEqNgYR8' +// }, +// input1_03: { +// pin: 'input1_03', +// type: 'rotary_switch_state', +// line: 0, +// tbname: 'PLBJzmK1r3Gynd6OW0gGYz0e5wV4vx9bDEqNgYR8' +// }, +// input1_04: { +// pin: 'input1_04', +// type: 'power_supply', +// line: 0, +// tbname: 'PLBJzmK1r3Gynd6OW0gGYz0e5wV4vx9bDEqNgYR8' +// }, +// input1_05: { +// pin: 'input1_05', +// type: 'state_of_main_switch', +// line: 0, +// tbname: 'PLBJzmK1r3Gynd6OW0gGYz0e5wV4vx9bDEqNgYR8' +// }, +// input1_06: { +// pin: 'input1_06', +// type: 'state_of_breaker', +// line: 1, +// tbname: '52dD6ZlV1QaOpRBmbAqK8bkKnGzWMLj4eJq38Pgo' +// }, +// relay1_02: { +// pin: 'relay1_02', +// type: 'state_of_contactor', +// line: 1, +// tbname: '52dD6ZlV1QaOpRBmbAqK8bkKnGzWMLj4eJq38Pgo' +// }, +// '28F46E9D0E00008B': { pin: '28F46E9D0E00008B', type: 'temperature', line: 0 }, +// twilight_sensor: { pin: 'twilight_sensor', type: 'twilight_sensor', line: 0 } // } - //! relays_data // { // '0': { diff --git a/flow/helper/DataToTbHandler.js b/flow/helper/DataToTbHandler.js index cf29b82..06dd438 100644 --- a/flow/helper/DataToTbHandler.js +++ b/flow/helper/DataToTbHandler.js @@ -1,5 +1,5 @@ -class DataToTbHandler -{ +class DataToTbHandler { + constructor(index) { this.index = index; @@ -13,27 +13,24 @@ class DataToTbHandler this.sender = ""; } - dump() - { + dump() { console.log("----------------------------"); console.log("previousValues", this.previousValues); console.log("----------------------------"); } - setSender(sender) - { + setSender(sender) { this.sender = sender; } - isEmptyObject( obj ) { - for ( var name in obj ) { - return false; + isEmptyObject(obj) { + for (var name in obj) { + return false; } return true; } - sendToTb(dataToTb, instance) - { + sendToTb(dataToTb, instance) { if(!FLOW.OMS_brokerready) { @@ -44,11 +41,10 @@ class DataToTbHandler if(keys.length == 0) { - if(this.debug) console.log("sendToTb received epty object", dataToTb); + if(this.debug) console.log("sendToTb received empty object", dataToTb); return; } - let tbname = keys[0]; let ts; @@ -67,7 +63,7 @@ class DataToTbHandler if(!this.isEmptyObject(values)) { - arrayOfValuesToSend.push({ts: ts, values: values}); + arrayOfValuesToSend.push({ts: ts, values: values}); } } @@ -100,15 +96,14 @@ class DataToTbHandler instance.send(this.index, dataToTbModified); } - getDiffTimestamp(key) - { + getDiffTimestamp(key) { //TODO set different value for given key!!! //if(key == "status") this.timeToHoldTbValue = 2*60*60;//2h return this.timeToHoldTbValue * 1000; } - prepareValuesForTb(tbname, timestamp, values) - { + prepareValuesForTb(tbname, timestamp, values) { + let keys = Object.keys(values); if(!this.previousValues.hasOwnProperty(tbname)) { diff --git a/flow/modbus_reader.js b/flow/modbus_reader.js index 3bee223..e253bb2 100644 --- a/flow/modbus_reader.js +++ b/flow/modbus_reader.js @@ -173,9 +173,9 @@ exports.install = function(instance) { numberOfNotResponding[obj.device] += 1; } - console.error(require('util').inspect(arguments, { - depth: null - })) + // console.error(require('util').inspect(arguments, { + // depth: null + // })) // if reading out of device's last register returns error, we send accumulated allValues to dido_controller (if allValues are not an empty object) if(obj.index + 1 >= obj.lengthOfActualDeviceStream) diff --git a/flow/thermometer.js b/flow/thermometer.js index 9b51fac..b5ee420 100644 --- a/flow/thermometer.js +++ b/flow/thermometer.js @@ -213,12 +213,11 @@ exports.install = function(instance) { setTimeout(function(){ start(); - }, 15000); + }, 10000); startRead = setInterval(start, timeoutMin * 1000 * 60); //testing //setInterval(() => {instance.send(instanceSendTo.dido_controller, {status: "NOK-thermometer"})}, 180000); -}; - +}; \ No newline at end of file diff --git a/flow/wsmqttpublish.js b/flow/wsmqttpublish.js index 3064de1..20f64fa 100644 --- a/flow/wsmqttpublish.js +++ b/flow/wsmqttpublish.js @@ -327,7 +327,7 @@ exports.install = function(instance) { else { - if(logger) logger.debug("Broker unavailable. Data not sent !", data.data); + if(logger) logger.debug("Broker unavailable. Data not sent !", JSON.stringify(data.data)); instance.send(instanceSendTo.debug, {"message":"Broker unavailable. Data not sent !", "data": data.data }); if(saveTelemetryOnError)