major updated line switching version 2024-05-10
This commit is contained in:
parent
d289a99d07
commit
54038a06f8
4 changed files with 112 additions and 353 deletions
|
|
@ -1,4 +1,4 @@
|
|||
const timeoutInterval = 300000;
|
||||
const timeoutInterval = 150000;
|
||||
const deviceConfig = [
|
||||
{
|
||||
device: "em340",
|
||||
|
|
|
|||
|
|
@ -236,6 +236,17 @@ async function loadSettings()
|
|||
initNotifications();
|
||||
}
|
||||
|
||||
loadSettings();
|
||||
|
||||
|
||||
async function loadNodes()
|
||||
{
|
||||
const responseNodes = await promisifyBuilder(dbNodes.find());
|
||||
nodesData = makeMapFromDbResult(responseNodes, "node");
|
||||
}
|
||||
|
||||
loadNodes();
|
||||
|
||||
|
||||
//nastav profil nodu
|
||||
function processNodeProfile(node)
|
||||
|
|
@ -680,7 +691,6 @@ function removeTask(obj)
|
|||
}
|
||||
|
||||
|
||||
loadSettings();
|
||||
|
||||
|
||||
exports.install = function(instance) {
|
||||
|
|
@ -1002,6 +1012,8 @@ exports.install = function(instance) {
|
|||
//report FLOW.OMS_edge_fw_version as fw_version
|
||||
//report date as startdate
|
||||
|
||||
//return;
|
||||
|
||||
monitor.info("buildTasks - params", params);
|
||||
|
||||
let processLine; //defined line
|
||||
|
|
@ -1224,6 +1236,10 @@ exports.install = function(instance) {
|
|||
|
||||
try {
|
||||
|
||||
|
||||
/**
|
||||
* we process line profiles: timepoints, astro clock, lux_sensor, offsets ...
|
||||
*/
|
||||
if(profilestr === "") throw ("Profile is not defined");
|
||||
let profile = JSON.parse(profilestr);
|
||||
if(Object.keys(profile).length === 0) throw ("Profile is empty");
|
||||
|
|
@ -1245,6 +1261,7 @@ exports.install = function(instance) {
|
|||
let currentValue = 0;
|
||||
if(time_points.length > 0) currentValue = time_points[time_points.length - 1].value;
|
||||
|
||||
|
||||
/**
|
||||
* if astro_clock is true, we create timepoints, that switch on/off relays accordingly.
|
||||
* we need to manage, astro clock timepoints has the greatest priority - normal timepoints will not switch off/on lines before dusk or dawn
|
||||
|
|
@ -1698,121 +1715,88 @@ exports.install = function(instance) {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* We process line profile, where "astro_clock": true
|
||||
* example profile:
|
||||
*
|
||||
"dawn_lux_sensor": true,
|
||||
"dusk_lux_sensor": true,
|
||||
"dawn_lux_sensor_value": 5,
|
||||
"dusk_lux_sensor_value": 5,
|
||||
"dawn_astro_clock_offset": 0,
|
||||
"dusk_astro_clock_offset": 10,
|
||||
"dawn_lux_sensor_time_window": 30,
|
||||
"dusk_lux_sensor_time_window": 30,
|
||||
"dawn_astro_clock_time_window": 60,
|
||||
"dusk_astro_clock_time_window": 60
|
||||
|
||||
* if dawn: if currentTimestamp is in timewindow "dawnTime + and - dawn_lux_sensor_time_window" and lux value >= lux_sensor_value, we switch off the line.
|
||||
* if dusk: we do oposite
|
||||
*
|
||||
* dawn: usvit - lux je nad hranicou - vypnem
|
||||
* dusk: sumrak - lux je pod hranicou - zapnem
|
||||
*/
|
||||
function turnOnOffLinesAccordingToLuxSensor(lux_sensor_value)
|
||||
{
|
||||
//let dusk_hours = sunCalcResult["dusk_hours"];
|
||||
//let dusk_minutes = sunCalcResult["dusk_minutes"];
|
||||
|
||||
let duskTimeStamp;
|
||||
let downTimeStamp;
|
||||
|
||||
//prejedme si line s profilom, kde mame "astro_clock": true
|
||||
|
||||
/*
|
||||
"dawn_lux_sensor": true,
|
||||
"dusk_lux_sensor": true,
|
||||
"dawn_lux_sensor_value": 5,
|
||||
"dusk_lux_sensor_value": 5,
|
||||
"dawn_astro_clock_offset": 0,
|
||||
"dusk_astro_clock_offset": 10,
|
||||
"dawn_lux_sensor_time_window": 30,
|
||||
"dusk_lux_sensor_time_window": 30,
|
||||
"dawn_astro_clock_time_window": 60,
|
||||
"dusk_astro_clock_time_window": 60
|
||||
*/
|
||||
|
||||
//ak sme pred/po vychode a lux value <= lux_sensor_value, liniu zapneme
|
||||
|
||||
//ak sme pred/po zapade a lux_value <= lux_sensor_value, liniu zapneme
|
||||
|
||||
let now = new Date();
|
||||
let currentTimestamp = now.getTime();
|
||||
|
||||
let keys = Object.keys(relaysData);
|
||||
|
||||
for(let i = 0; i < keys.length; i++)
|
||||
{
|
||||
let line = keys[i];//line is turned off by default
|
||||
let profilestr = relaysData[line].profile;
|
||||
let contactor = relaysData[line].contactor; // 0 or 1 - vypnuta/zapnuta
|
||||
|
||||
try{
|
||||
let line = keys[i]; //line is turned off by default
|
||||
let profilestr = relaysData[line].profile;
|
||||
const contactor = relaysData[line].contactor;
|
||||
|
||||
try {
|
||||
|
||||
let profile = JSON.parse(profilestr);
|
||||
if(Object.keys(profile).length === 0) throw ("profile is not defined");
|
||||
if(Object.keys(profile).length === 0) throw ("turnOnOffLinesAccordingToLuxSensor - profile is not defined");
|
||||
|
||||
if(profile.astro_clock == true)
|
||||
{
|
||||
let sunCalcResult = calculateDuskDawn(now, line);
|
||||
let sunCalcResult = calculateDuskDawn(now, line);
|
||||
|
||||
//dawn: usvit/vychod - lux je nad hranicou - vypnem
|
||||
//dusk: zapad pod hranicou - zapnem
|
||||
//usvit
|
||||
if(profile.dawn_lux_sensor == true)
|
||||
{
|
||||
let lux_sensor_time_window1 = sunCalcResult.dawn_time - (parseInt( profile.dawn_lux_sensor_time_window ) * 1000 * 60); // LUX_SENSOR_TIME_WINDOW x 1000 x 60 --> dostaneme odpocet/pripocitanie minut
|
||||
let lux_sensor_time_window2 = sunCalcResult.dawn_time + (parseInt( profile.dawn_lux_sensor_time_window ) * 1000 * 60);
|
||||
|
||||
//"dawn_lux_sensor_time_window": 30,
|
||||
//"dusk_lux_sensor_time_window": 30,
|
||||
|
||||
//vychod
|
||||
// LUX_SENSOR_TIME_WINDOW x 1000 x 60 --> dostaneme odpocet/pripocitanie minut
|
||||
if(profile.dawn_lux_sensor == true)
|
||||
if(currentTimestamp >= lux_sensor_time_window1 && currentTimestamp <= lux_sensor_time_window2)
|
||||
{
|
||||
let lux_sensor_time_window1 = sunCalcResult.dawn_time - (parseInt( profile.dawn_lux_sensor_time_window ) * 1000 * 60);
|
||||
let lux_sensor_time_window2 = sunCalcResult.dawn_time + (parseInt( profile.dawn_lux_sensor_time_window ) * 1000 * 60);
|
||||
//console.log('------>>>', new Date(lux_sensor_time_window1), new Date(lux_sensor_time_window2), lux_sensor_time_window1, lux_sensor_time_window2)
|
||||
//console.log('++++-->>>', new Date(sunCalcResult.dusk_time), new Date(sunCalcResult.dawn_time))
|
||||
|
||||
if(currentTimestamp >= lux_sensor_time_window1 && currentTimestamp <= lux_sensor_time_window2)
|
||||
{
|
||||
//dawn: usvit/vychod - lux je nad hranicou - vypnem
|
||||
if(lux_sensor_value > profile.dawn_lux_sensor_value)
|
||||
{
|
||||
//vypnem
|
||||
if(contactor) turnOffLine(line, "profile: dawn - turnOff line according to lux sensor");
|
||||
}
|
||||
// else
|
||||
// {
|
||||
// //zapnem
|
||||
// if(!contactor) turnOnLine(line, "profile: dawn - turnOn line according to lux sensor");
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
//ak sme po vychode
|
||||
if(currentTimestamp > lux_sensor_time_window2)
|
||||
{
|
||||
//vypneme
|
||||
//urobime jednorazovy prikaz
|
||||
}
|
||||
if(lux_sensor_value > profile.dawn_lux_sensor_value)
|
||||
{
|
||||
if(contactor) turnOffLine(line, "Profile: dawn - turnOff line according to lux sensor");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//zapad
|
||||
if(profile.dusk_lux_sensor == true)
|
||||
//sumrak
|
||||
if(profile.dusk_lux_sensor == true)
|
||||
{
|
||||
let lux_sensor_time_window1 = sunCalcResult.dusk_time - (parseInt( profile.dusk_lux_sensor_time_window ) * 1000 * 60);
|
||||
let lux_sensor_time_window2 = sunCalcResult.dusk_time + (parseInt( profile.dusk_lux_sensor_time_window ) * 1000 * 60);
|
||||
|
||||
if(currentTimestamp >= lux_sensor_time_window1 && currentTimestamp <= lux_sensor_time_window2)
|
||||
{
|
||||
let lux_sensor_time_window1 = sunCalcResult.dusk_time - (parseInt( profile.dusk_lux_sensor_time_window ) * 1000 * 60);
|
||||
let lux_sensor_time_window2 = sunCalcResult.dusk_time + (parseInt( profile.dusk_lux_sensor_time_window ) * 1000 * 60);
|
||||
|
||||
if(currentTimestamp >= lux_sensor_time_window1 && currentTimestamp <= lux_sensor_time_window2)
|
||||
{
|
||||
//dusk: zapad pod hranicou - zapnem
|
||||
if(lux_sensor_value < profile.dusk_lux_sensor_value)
|
||||
{
|
||||
//zapnem
|
||||
if(!contactor) turnOnLine(line, "profile: dusk - turnOn line according to lux sensor");
|
||||
}
|
||||
// else
|
||||
// {
|
||||
// //vypnem
|
||||
// if(contactor) turnOffLine(line, "profile: dusk - turnOff line according to lux sensor");
|
||||
// }
|
||||
|
||||
}
|
||||
if(lux_sensor_value < profile.dusk_lux_sensor_value)
|
||||
{
|
||||
if(!contactor) turnOnLine(line, "Profile: dusk - turnOn line according to lux sensor");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
//if(profilestr !=="" ) logger.debug(profilestr, error);
|
||||
if(profilestr !== "" ) monitor.info('Error parsing profile in turnOnOffLinesAccordingToLuxSensor', error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1842,7 +1826,7 @@ exports.install = function(instance) {
|
|||
|
||||
|
||||
async function runTasks() {
|
||||
|
||||
|
||||
clearInterval(interval);
|
||||
|
||||
let currentTimestamp = Date.now();
|
||||
|
|
@ -2049,19 +2033,14 @@ exports.install = function(instance) {
|
|||
|
||||
if(type == "edge_date_time")
|
||||
{
|
||||
const ts = Date.now();
|
||||
|
||||
//var d = new Date();
|
||||
//let hours = addZeroBefore(d.getHours());
|
||||
//let minutes = addZeroBefore(d.getMinutes());
|
||||
//let seconds = addZeroBefore(d.getSeconds());
|
||||
//let values = {"edge_date_time": `${hours}:${minutes}:${seconds}`};
|
||||
|
||||
let values = {"edge_date_time": Date.now()};
|
||||
let values = {"edge_date_time": ts};
|
||||
|
||||
let dataToTb = {
|
||||
[tbname]: [
|
||||
{
|
||||
"ts": Date.now(),
|
||||
"ts": ts,
|
||||
"values": values
|
||||
}
|
||||
]
|
||||
|
|
@ -2069,11 +2048,8 @@ exports.install = function(instance) {
|
|||
|
||||
tasks[0].timestamp = currentTimestamp + tasks[0].addMinutesToTimestamp * 60000;
|
||||
|
||||
//instance.send(SEND_TO.tb, dataToTb);
|
||||
tbHandler.sendToTb(dataToTb, instance);
|
||||
|
||||
instance.send(SEND_TO.tb, dataToTb);
|
||||
interval = setInterval(runTasks, SHORT_INTERVAL);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -3832,33 +3808,8 @@ function com_generic(adresa, rec, rw, register, name, byte1, byte2, byte3, byte4
|
|||
|
||||
|
||||
|
||||
|
||||
const lineTimepointsExample1 =
|
||||
[
|
||||
{ start_time: '05:17', value: 0, name: 'dawn' },
|
||||
{ value: 0, end_time: '13:00', start_time: '05:30' },
|
||||
{ value: 0, end_time: '20:00', start_time: '13:00' },
|
||||
{ value: 1, end_time: '05:30', start_time: '20:00' },
|
||||
{ start_time: '20:19', value: 1, name: 'dusk' }
|
||||
]
|
||||
|
||||
|
||||
|
||||
const lineTimepointsExample2 =
|
||||
[
|
||||
{ value: 0, end_time: '13:00', start_time: '05:30' },
|
||||
{ value: 0, start_time: '5:47', name: 'luxOff' },
|
||||
{ value: 0, end_time: '20:00', start_time: '13:00' },
|
||||
{ value: 1, end_time: '05:30', start_time: '20:00' },
|
||||
{ value: 1, start_time: '20:49', name: 'luxOn' }
|
||||
]
|
||||
|
||||
|
||||
|
||||
|
||||
// SAMPLE DATA
|
||||
|
||||
|
||||
const relaysDataExample =
|
||||
{
|
||||
'0': {
|
||||
|
|
@ -3888,36 +3839,6 @@ const relaysDataExample =
|
|||
}
|
||||
|
||||
|
||||
// [2024-05-08T11:43:33.055] [DEBUG] default - --->reportOfflineNodeStatus for line 2
|
||||
// [2024-05-08T11:43:33.055] [DEBUG] default - --->reportOfflineNodeStatus for line 3
|
||||
// [2024-05-08T11:43:35.737] [DEBUG] default - -->CMD MANAGER - BUILD TASKS
|
||||
// [2024-05-08T11:43:35.742] [INFO] monitorLogs - buildTasks - params undefined
|
||||
// [2024-05-08T11:43:35.743] [DEBUG] default - -->buildTasks clear tasks
|
||||
// [2024-05-08T11:43:35.744] [INFO] monitorLogs - buildTasks: profile for line 1
|
||||
// [2024-05-08T11:43:35.745] [INFO] monitorLogs - profile: {
|
||||
// intervals: [ { value: 1, end_time: '13:00', start_time: '13:00' } ],
|
||||
// astro_clock: false,
|
||||
// dawn_lux_sensor: false,
|
||||
// dusk_lux_sensor: false,
|
||||
// dawn_lux_sensor_value: 5,
|
||||
// dusk_lux_sensor_value: 5,
|
||||
// dawn_astro_clock_offset: 0,
|
||||
// dusk_astro_clock_offset: 0,
|
||||
// dawn_lux_sensor_time_window: 30,
|
||||
// dusk_lux_sensor_time_window: 30,
|
||||
// dawn_astro_clock_time_window: 60,
|
||||
// dusk_astro_clock_time_window: 60
|
||||
// }
|
||||
// [2024-05-08T11:43:35.747] [INFO] monitorLogs - -->comming events turn on/off lines:
|
||||
// [2024-05-08T11:43:35.748] [INFO] monitorLogs - turn on line: 1 2024-05-08T11:00:00.747Z
|
||||
// [2024-05-08T11:43:35.749] [INFO] monitorLogs - -->time_points final 1 [ { value: 1, end_time: '13:00', start_time: '13:00' } ]
|
||||
// [2024-05-08T11:43:35.751] [INFO] monitorLogs - -->currentValue for relay 1 1
|
||||
//
|
||||
//
|
||||
// typ paramu ...... / cmd
|
||||
|
||||
|
||||
|
||||
const rpcSwitchOffLine =
|
||||
{
|
||||
"topic": "v1/gateway/rpc",
|
||||
|
|
@ -4088,8 +4009,6 @@ const rpcNodeProfile =
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
const sunCalcExample = {
|
||||
dusk_no_offset: '20:18',
|
||||
dawn_no_offset: '05:19',
|
||||
|
|
@ -4104,167 +4023,3 @@ const rpcNodeProfile =
|
|||
dusk_astro_clock_offset: 0,
|
||||
dawn_astro_clock_offset: 0
|
||||
}
|
||||
|
||||
|
||||
// TODO - in runTasks function, when processing tasks, wrong task gets updated
|
||||
/*
|
||||
[2024-05-09T20:48:00.200] [DEBUG] default - currentTask is not processed - task is in the future {
|
||||
address: 0,
|
||||
byte1: 0,
|
||||
byte2: 0,
|
||||
byte3: 0,
|
||||
byte4: 0,
|
||||
recipient: 0,
|
||||
register: -1,
|
||||
rw: 0,
|
||||
priority: 3,
|
||||
timestamp: 1715280480204,
|
||||
addMinutesToTimestamp: 0,
|
||||
type: 'relay',
|
||||
line: 1,
|
||||
value: 1,
|
||||
tbname: 'MgnK93rkoAazbqdQ4yB2Q0yZ1YXGx6pmwBeVEP2O',
|
||||
timePointName: 'luxOn',
|
||||
dawn_lux_sensor_time_window: 30,
|
||||
dusk_lux_sensor_time_window: 30,
|
||||
info: 'luxOn: turn on line: 1',
|
||||
debug: true
|
||||
}
|
||||
[2024-05-09T20:48:00.302] [INFO] monitorLogs - pparrams ************* {
|
||||
address: 0,
|
||||
byte1: 0,
|
||||
byte2: 0,
|
||||
byte3: 0,
|
||||
byte4: 0,
|
||||
recipient: 0,
|
||||
register: -1,
|
||||
rw: 0,
|
||||
priority: 3,
|
||||
timestamp: 1715280480204,
|
||||
addMinutesToTimestamp: 0,
|
||||
type: 'relay',
|
||||
line: 1,
|
||||
value: 1,
|
||||
tbname: 'MgnK93rkoAazbqdQ4yB2Q0yZ1YXGx6pmwBeVEP2O',
|
||||
timePointName: 'luxOn',
|
||||
dawn_lux_sensor_time_window: 30,
|
||||
dusk_lux_sensor_time_window: 30,
|
||||
info: 'luxOn: turn on line: 1',
|
||||
debug: true
|
||||
}
|
||||
new relay task ............ {
|
||||
address: 4292,
|
||||
byte1: 0,
|
||||
byte2: 0,
|
||||
byte3: 0,
|
||||
byte4: 0,
|
||||
recipient: 1,
|
||||
register: 1,
|
||||
rw: 0,
|
||||
priority: 6,
|
||||
timestamp: 1715280540301,
|
||||
addMinutesToTimestamp: 1,
|
||||
type: 'cmd',
|
||||
tbname: 'jbN4q7JPZmexgdnz2yKbWdDYAWwO0Q3BMX6ERLoV',
|
||||
info: 'generated cmd - buildTasks (node)'
|
||||
}
|
||||
[2024-05-09T20:48:00.304] [INFO] monitorLogs - new relay task ............. {
|
||||
address: 4292,
|
||||
byte1: 0,
|
||||
byte2: 0,
|
||||
byte3: 0,
|
||||
byte4: 0,
|
||||
recipient: 1,
|
||||
register: 1,
|
||||
rw: 0,
|
||||
priority: 6,
|
||||
timestamp: 1715280540301,
|
||||
addMinutesToTimestamp: 1,
|
||||
type: 'cmd',
|
||||
tbname: 'jbN4q7JPZmexgdnz2yKbWdDYAWwO0Q3BMX6ERLoV',
|
||||
info: 'generated cmd - buildTasks (node)'
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
~~~~~~~~~~~~~~~~~~~~~pp {
|
||||
address: 0,
|
||||
byte1: 0,
|
||||
byte2: 0,
|
||||
byte3: 0,
|
||||
byte4: 0,
|
||||
recipient: 0,
|
||||
register: -1,
|
||||
rw: 0,
|
||||
priority: 0,
|
||||
timestamp: 0,
|
||||
addMinutesToTimestamp: 0,
|
||||
type: 'relay',
|
||||
line: 1,
|
||||
tbname: 'MgnK93rkoAazbqdQ4yB2Q0yZ1YXGx6pmwBeVEP2O',
|
||||
value: 0,
|
||||
debug: true,
|
||||
info: 'turn off line on startup: 1'
|
||||
}
|
||||
deleting task from tasks +++++++++++++++++++++ {
|
||||
address: 0,
|
||||
byte1: 0,
|
||||
byte2: 0,
|
||||
byte3: 0,
|
||||
byte4: 0,
|
||||
recipient: 0,
|
||||
register: -1,
|
||||
rw: 0,
|
||||
priority: 0,
|
||||
timestamp: 0,
|
||||
addMinutesToTimestamp: 0,
|
||||
type: 'relay',
|
||||
line: 1,
|
||||
tbname: 'MgnK93rkoAazbqdQ4yB2Q0yZ1YXGx6pmwBeVEP2O',
|
||||
value: 0,
|
||||
debug: true,
|
||||
info: 'turn off line on startup: 1'
|
||||
}
|
||||
[2024-05-10T09:31:36.629] [INFO] monitorLogs - pparrams ************* {
|
||||
address: 0,
|
||||
byte1: 0,
|
||||
byte2: 0,
|
||||
byte3: 0,
|
||||
byte4: 0,
|
||||
recipient: 0,
|
||||
register: -1,
|
||||
rw: 0,
|
||||
priority: 0,
|
||||
timestamp: 0,
|
||||
addMinutesToTimestamp: 0,
|
||||
type: 'relay',
|
||||
line: 1,
|
||||
tbname: 'MgnK93rkoAazbqdQ4yB2Q0yZ1YXGx6pmwBeVEP2O',
|
||||
value: 0,
|
||||
debug: true,
|
||||
info: 'turn off line on startup: 1'
|
||||
}
|
||||
else task in relay ????? should be ???
|
||||
[2024-05-10T09:31:36.631] [INFO] monitorLogs - new relay task ............. {
|
||||
address: 0,
|
||||
byte1: 0,
|
||||
byte2: 0,
|
||||
byte3: 0,
|
||||
byte4: 0,
|
||||
recipient: 0,
|
||||
register: -1,
|
||||
rw: 0,
|
||||
priority: 0,
|
||||
timestamp: 0,
|
||||
addMinutesToTimestamp: 0,
|
||||
type: 'relay',
|
||||
line: 2,
|
||||
tbname: 'jBL12pg63eX4N9P7zy0lJLyEJKmlbkGwZMx0avQV',
|
||||
value: 1,
|
||||
debug: true,
|
||||
info: 'turn on line on startup: 2'
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -864,6 +864,10 @@ exports.install = function(instance) {
|
|||
{
|
||||
deviceStatuses["em"] = "OK";
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const updateStatus = checkFinalRVOStatus();
|
||||
if(updateStatus) values.status = "OK";
|
||||
|
|
|
|||
|
|
@ -16,17 +16,12 @@ exports.readme = `
|
|||
|
||||
const modbus = require('jsmodbus')
|
||||
const SerialPort = require('serialport')
|
||||
|
||||
const { timeoutInterval, deviceConfig } = require("../databases/modbus_config");
|
||||
|
||||
const ErrorToServiceHandler = require('./helper/ErrorToServiceHandler');
|
||||
const errorHandler = new ErrorToServiceHandler();
|
||||
|
||||
const { sendNotification } = require('./helper/notification_reporter');
|
||||
|
||||
const DELAY_BETWEEN_DEVICES = 10000;
|
||||
|
||||
const instanceSendTo = {
|
||||
const SEND_TO = {
|
||||
debug: 0,
|
||||
dido_controller: 1,
|
||||
tb: 2
|
||||
|
|
@ -49,9 +44,9 @@ exports.install = function(instance) {
|
|||
this.index = 0;
|
||||
this.timeoutInterval = 5000;
|
||||
|
||||
// kedze potrebujeme ist stale dookola pre jednotlive zariadenia, potrebujeme ci uz index ako aj adresu zariadenia, a aj pocet registrov na vycitanie
|
||||
this.deviceAddress = null; // adresa zariadenia (1 ma EM340 a 2 ma twilight_sensor)
|
||||
this.indexInDeviceConfig = 0; // prvy item v deviceConfig
|
||||
// we need to go always around for all devices. So we need index value, device address, as well as number of registers for single device
|
||||
this.deviceAddress = null; // device address (1 - EM340 and 2 for twilight_sensor)
|
||||
this.indexInDeviceConfig = 0; // first item in deviceConfig
|
||||
this.lengthOfActualDeviceStream = null;
|
||||
this.device = null;
|
||||
|
||||
|
|
@ -69,7 +64,7 @@ exports.install = function(instance) {
|
|||
// we create a client for every deviceAddress ( = address) in list and push them into dictionary
|
||||
for( let i = 0; i < deviceConfig.length; i++)
|
||||
{
|
||||
this.clients[deviceConfig[i].deviceAddress] = new modbus.client.RTU(this.socket, deviceConfig[i].deviceAddress);
|
||||
this.clients[deviceConfig[i].deviceAddress] = new modbus.client.RTU(this.socket, deviceConfig[i].deviceAddress, 2000); // 2000 is timeout in register request, default is 5000, which is too long
|
||||
}
|
||||
|
||||
this.socket.on('error', function(e) {
|
||||
|
|
@ -133,13 +128,13 @@ exports.install = function(instance) {
|
|||
{
|
||||
message = "twilight_sensor_ok";
|
||||
}
|
||||
message && sendNotification("modbus_reader: readRegisters", tbName, message, {}, "", instanceSendTo.tb, instance);
|
||||
message && sendNotification("modbus_reader: readRegisters", tbName, message, {}, "", SEND_TO.tb, instance);
|
||||
delete numberOfNotResponding[obj.device];
|
||||
}
|
||||
|
||||
obj.transformResponse(resp, register, obj.deviceAddress);
|
||||
obj.transformResponse(resp, register);
|
||||
|
||||
obj.errors = 0;
|
||||
//obj.errors = 0;
|
||||
obj.index++;
|
||||
obj.readAnotherRegister();
|
||||
|
||||
|
|
@ -150,7 +145,7 @@ exports.install = function(instance) {
|
|||
obj.errors++;
|
||||
if(obj.errors == obj.lengthOfActualDeviceStream)
|
||||
{
|
||||
instance.send(instanceSendTo.dido_controller, {status: "NOK-" + obj.device}); // NOK-em340, NOK-em111, NOK-twilight_sensor, NOK-thermometer
|
||||
instance.send(SEND_TO.dido_controller, {status: "NOK-" + obj.device}); // NOK-em340, NOK-em111, NOK-twilight_sensor, NOK-thermometer
|
||||
|
||||
//todo - neposlalo notification, ked sme vypojili twilight a neposle to do tb, ale do dido ??
|
||||
if(!numberOfNotResponding.hasOwnProperty(obj.device))
|
||||
|
|
@ -164,7 +159,7 @@ exports.install = function(instance) {
|
|||
{
|
||||
message = "electrometer_nok";
|
||||
}
|
||||
message && sendNotification("modbus_reader: readingTimeouted", tbName, message, {}, "", instanceSendTo.tb, instance);
|
||||
message && sendNotification("modbus_reader: readingTimeouted", tbName, message, {}, "", SEND_TO.tb, instance);
|
||||
numberOfNotResponding[obj.device] = 1;
|
||||
}
|
||||
|
||||
|
|
@ -176,6 +171,12 @@ exports.install = function(instance) {
|
|||
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)
|
||||
{
|
||||
if(!isObjectEmpty(obj.allValues)) instance.send(SEND_TO.dido_controller, {values: obj.allValues});
|
||||
obj.allValues = {};
|
||||
}
|
||||
obj.index++;
|
||||
obj.readAnotherRegister();
|
||||
})
|
||||
|
|
@ -187,7 +188,7 @@ exports.install = function(instance) {
|
|||
else this.setNewStream();
|
||||
}
|
||||
|
||||
transformResponse = (response, register, deviceAddress) => {
|
||||
transformResponse = (response, register) => {
|
||||
|
||||
for (let i = 0; i < this.lengthOfActualDeviceStream; i++) {
|
||||
|
||||
|
|
@ -198,11 +199,11 @@ exports.install = function(instance) {
|
|||
let multiplier = a.multiplier;
|
||||
|
||||
let value = this.calculateValue(response, multiplier);
|
||||
// console.log(deviceAddress, register, tbName, tbAttribute, response, a.multiplier, value);
|
||||
// console.log(register, tbName, tbAttribute, response, a.multiplier, value);
|
||||
|
||||
// if(tbName == undefined) return;
|
||||
|
||||
if(this.index + 1 + this.errors < this.lengthOfActualDeviceStream)
|
||||
if(this.index + 1 < this.lengthOfActualDeviceStream)
|
||||
{
|
||||
this.allValues[tbAttribute] = value;
|
||||
return;
|
||||
|
|
@ -215,7 +216,7 @@ exports.install = function(instance) {
|
|||
|
||||
this.checkNullVoltage(values);
|
||||
|
||||
instance.send(instanceSendTo.dido_controller, {values: values});
|
||||
instance.send(SEND_TO.dido_controller, {values: values});
|
||||
|
||||
this.allValues = {};
|
||||
break;
|
||||
|
|
@ -227,8 +228,6 @@ exports.install = function(instance) {
|
|||
|
||||
setNewStream = () =>
|
||||
{
|
||||
// console.log('------------',this.lengthOfActualDeviceStream, this.index);
|
||||
// console.log('------------',this.indexInDeviceConfig, deviceConfig.length);
|
||||
if(this.lengthOfActualDeviceStream == this.index)
|
||||
{
|
||||
if(this.indexInDeviceConfig + 1 == deviceConfig.length)
|
||||
|
|
@ -260,13 +259,13 @@ exports.install = function(instance) {
|
|||
// ]
|
||||
// };
|
||||
|
||||
// instance.send(instanceSendTo.tb, dataToTB);
|
||||
// instance.send(SEND_TO.tb, dataToTB);
|
||||
|
||||
// const dataToDiDo = {
|
||||
// values: values
|
||||
// }
|
||||
|
||||
// instance.send(instanceSendTo.dido_controller, dataToDiDo);
|
||||
// instance.send(SEND_TO.dido_controller, dataToDiDo);
|
||||
// }
|
||||
|
||||
|
||||
|
|
@ -314,27 +313,28 @@ exports.install = function(instance) {
|
|||
if(values[singleValue] == 0)
|
||||
{
|
||||
FLOW.OMS_no_voltage.add(phase);
|
||||
sendNotification("modbus_citys: checkNullVoltage", tbName, "no_voltage_on_phase", {phase: phase}, "", instanceSendTo.tb, instance, "voltage" + phase );
|
||||
sendNotification("modbus_citys: checkNullVoltage", tbName, "no_voltage_on_phase", {phase: phase}, "", SEND_TO.tb, instance, "voltage" + phase );
|
||||
// console.log('no voltage')
|
||||
}
|
||||
else
|
||||
{
|
||||
FLOW.OMS_no_voltage.delete(phase);
|
||||
// console.log('voltage detected')
|
||||
sendNotification("modbus_citys: checkNullVoltage", tbName, "voltage_on_phase_restored", {phase: phase}, "", instanceSendTo.tb, instance, "voltage" + phase);
|
||||
sendNotification("modbus_citys: checkNullVoltage", tbName, "voltage_on_phase_restored", {phase: phase}, "", SEND_TO.tb, instance, "voltage" + phase);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// we use dataToTbHandler. Therefore we need to check, if objects we send to dido_controller are not empty
|
||||
isObjectEmpty = (objectName) => {
|
||||
return Object.keys(objectName).length === 0 && objectName.constructor === Object;
|
||||
}
|
||||
}
|
||||
|
||||
const isObjectEmpty = (objectName) => {
|
||||
return Object.keys(objectName).length === 0 && objectName.constructor === Object;
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
const newSocket = new SocketWithClients();
|
||||
tbName = FLOW.OMS_rvo_tbname;
|
||||
}, 25000);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue