Fake accelerometer and handle hasMainSwitch

This commit is contained in:
rasta5man 2025-05-06 11:58:26 +02:00
parent 5233aa38af
commit d97d90cf95
11 changed files with 3692 additions and 549 deletions

View file

@ -44,6 +44,9 @@ exports.install = function(instance) {
const process = require('process');
const { errLogger, logger, monitor } = require('./helper/logger');
//for accelerometer purposes
const { naklony } = require("../databases/accelerometer_db");
const dbNodes = TABLE("nodes");
const dbRelays = TABLE("relays");
@ -111,7 +114,7 @@ exports.install = function(instance) {
priorities["77"] = minutes;
priorities["78"] = minutes;
priorities["79"] = minutes;
priorities["84"] = minutes;
//priorities["84"] = minutes;
minutes = 10;
priorities["87"] = minutes;
@ -122,7 +125,7 @@ exports.install = function(instance) {
priorities["89"] = minutes;
//prikazy kt sa budu spustat na dany node - see config.js in terminal-oms.app. (1 - dimming)
let listOfCommands = [0, 1, 6, 7, 8, 74, 75, 76, 77, 78, 79, 80, 84, 87, 89];
let listOfCommands = [0, 1, 6, 7, 8, 74, 75, 76, 77, 78, 79, 80, 87, 89];
const errorHandler = new ErrorToServiceHandler();
@ -143,6 +146,10 @@ exports.install = function(instance) {
//if sending of profile to node fails, we send notification and push node into set, so we do not send notification twice
const nodeProfileSendFail = new Set();
//we expect to get current temperature in Senica from senica-prod01
let temperatureInSenica = null;
let accelerometerInterval = null;
//END OF VARIABLE SETTINGS
//--------------------------------
@ -184,6 +191,7 @@ exports.install = function(instance) {
setCorrectPlcTimeOnceADay();
sendNodeReadout = setInterval(sendNodesData, 150000);
accelerometerInterval = setInterval(accelerometerData, 60000 * 30); //30 min
}
@ -763,6 +771,10 @@ exports.install = function(instance) {
let tbname = nodesData[node].tbname;
let nodeStatus = nodesData[node].status;
//in case we have reported offline node status, we return (continue with next node)
if (nodeStatus === "OFFLINE") return;
nodesData[node].node_status_before_offline = nodeStatus === true ? true : false;
nodesData[node].status = "OFFLINE";
nodesData[node].readout = {};
@ -1666,7 +1678,7 @@ exports.install = function(instance) {
}
}).catch(function(reason) {
console.log("writeData catch exception", reason);
//console.log("writeData catch exception", reason);
instance.send(SEND_TO.debug, reason);
terminalCommandResponse(params, "FAILURE", null, reason);
@ -1904,6 +1916,7 @@ exports.install = function(instance) {
clearInterval(customTasksInterval);
clearInterval(setCorrectTime);
clearInterval(sendNodeReadout);
clearInterval(accelerometerInterval);
rsPort.close();
});
@ -2071,7 +2084,16 @@ exports.install = function(instance) {
if (flowdata.data.hasOwnProperty("topic")) {
let data = getNested(flowdata.data, "content", "data");
if (data == undefined) {
//if we get temperature in senica from senica-prod01
let temperature = getNested(flowdata.data, "content", "senica_temperature");
if (temperature !== undefined) {
temperatureInSenica = temperature;
return;
}
if (data === undefined) {
console.log("Invalid rpc command came from platform");
return;
}
@ -2682,49 +2704,21 @@ exports.install = function(instance) {
values["time_schedule_settings"] = time_schedule_settings;
}
//naklon
//naklon - nateraz sa z nodu nevycitava! kvoli problemom s accelerometrom a vracanymi hodnotami, posielame temp a x y z vo funkcii accelerometerData()
if (register == 84) {
let temp;
if (byte3 >= 128) {
temp = (byte3 - 128) * (-1);
}
else {
temp = byte3;
}
const temp = byte3 >= 128 ? (byte3 - 128) * (-1) : byte3;
const inclination_x = byte2 >= 128 ? (byte2 - 128) * (-1) : byte2;
const inclination_y = byte1 >= 128 ? (byte1 - 128) * (-1) : byte1;
const inclination_z = byte0 >= 128 ? (byte0 - 128) * (-1) : byte0;
let inclination_x;
if (byte2 >= 128) {
inclination_x = (byte2 - 128) * (-1);
}
else {
inclination_x = byte2;
}
let inclination_y;
if (byte1 >= 128) {
inclination_y = (byte1 - 128) * (-1);
}
else {
inclination_y = byte1;
}
let inclination_z;
if (byte0 >= 128) {
inclination_z = (byte0 - 128) * (-1);
}
else {
inclination_z = byte0;
}
if (temp === undefined) temp = 999;
if (inclination_x === undefined) inclination_x = 999;
if (inclination_y === undefined) inclination_y = 999;
if (inclination_z === undefined) inclination_z = 999;
values["temperature"] = temp;
//náklon x
values["inclination_x"] = inclination_x;
//náklon y
values["inclination_y"] = inclination_y;
//náklon z
values["inclination_z"] = inclination_z;
}
@ -2850,5 +2844,44 @@ exports.install = function(instance) {
return (typeof item === "object" && !Array.isArray(item) && item !== null);
}
// we fake data, that should be received from accelerometer, as they are a bit unreliable. (temperature, x,y,z)
function accelerometerData() {
if (temperatureInSenica === null) return;
for (const key in relaysData) {
const lineData = relaysData[key];
const lineNumber = lineData.line;
const contactor = lineData.contactor;
if (lineNumber === 0) continue;
if (contactor === 1) {
let date = Date.now();
Object.keys(nodesData).forEach((node, index) => {
setTimeout(function() {
if (nodesData[node].line === lineNumber) {
let x = null;
if (naklony.hasOwnProperty(node)) x = naklony[node].naklon;
if (x === null) x = 0;
sendTelemetry({ temperature: Math.round(temperatureInSenica + 10 + Math.floor(Math.random() * 3)), inclination_x: x, inclination_y: 0, inclination_z: 0 }, nodesData[node].tbname, date);
}
}, (index + 1) * 500);
})
}
}
}
} // end of instance.export