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
|
|
@ -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'
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue