mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
Address incorrect timestamps bug in node-red-node-timeswitch; increase readability (#945)
* Initial fixes before testing and reformatting * Put sun event offset back in; reformat * Tabs to spaces * Fix "Start+X" OFF time; add more comments * Undo some formatting changes * Add contributors to package.json
This commit is contained in:
parent
ecb2849675
commit
29e0bed000
@ -25,7 +25,19 @@
|
||||
},
|
||||
"contributors": [
|
||||
{
|
||||
"name": "@pmacostapdi"
|
||||
"name": "@dceejay"
|
||||
},
|
||||
{
|
||||
"name": "@pmacostapdi"
|
||||
},
|
||||
{
|
||||
"name": "@heikokue"
|
||||
},
|
||||
{
|
||||
"name": "@sammachin"
|
||||
},
|
||||
{
|
||||
"name": "@jdmallen"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -1,19 +1,18 @@
|
||||
|
||||
module.exports = function(RED) {
|
||||
module.exports = function (RED) {
|
||||
"use strict";
|
||||
var SunCalc = require('suncalc');
|
||||
const spacetime = require("spacetime")
|
||||
const SUNRISE_KEY = "sunrise";
|
||||
const SUNSET_KEY = "sunset";
|
||||
|
||||
function TimeswitchNode(n) {
|
||||
RED.nodes.createNode(this, n);
|
||||
this.lat = n.lat;
|
||||
this.lon = n.lon;
|
||||
this.start = n.start || "sunrise";
|
||||
this.end = n.end || "sunset";
|
||||
this.startt = n.starttime;
|
||||
this.endt = n.endtime;
|
||||
this.duskoff = n.duskoff;
|
||||
this.dawnoff = n.dawnoff;
|
||||
this.sunriseOffset = n.dawnoff;
|
||||
this.sunsetOffset = n.duskoff;
|
||||
this.mytopic = n.mytopic;
|
||||
this.timezone = n.timezone || "UTC";
|
||||
|
||||
@ -24,6 +23,7 @@ module.exports = function(RED) {
|
||||
this.thu = n.thu;
|
||||
this.fri = n.fri;
|
||||
this.sat = n.sat;
|
||||
|
||||
this.jan = n.jan;
|
||||
this.feb = n.feb;
|
||||
this.mar = n.mar;
|
||||
@ -38,117 +38,192 @@ module.exports = function(RED) {
|
||||
this.dec = n.dec;
|
||||
|
||||
var node = this;
|
||||
var ison = 0;
|
||||
var newendtime = 0;
|
||||
|
||||
this.on("input", function(msg2) {
|
||||
if (msg2.payload === "reset") { ison = 0; }
|
||||
this.on("input", function () {
|
||||
// current global time
|
||||
const now = spacetime.now();
|
||||
const nowNative = now.toNativeDate();
|
||||
|
||||
var timeOffset = spacetime(Date.now()).goto(this.timezone.toLowerCase()).timezone().current.offset * 60 * 60 * 1000;
|
||||
var now = new Date(Date.now() + timeOffset);
|
||||
var nowMillis = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), 0);
|
||||
var midnightMillis = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 0, 0);
|
||||
var today = Math.round((nowMillis - midnightMillis) / 60000) % 1440;
|
||||
var starttime = Number(node.startt);
|
||||
var endtime = Number(node.endt);
|
||||
var tzOff = (new Date()).getTimezoneOffset();
|
||||
// all sun events for the given lat/long
|
||||
const sunEvents = SunCalc.getTimes(nowNative, node.lat, node.lon);
|
||||
let sunriseDateTime = spacetime(sunEvents[SUNRISE_KEY]).nearest("minute");
|
||||
let sunsetDateTime = spacetime(sunEvents[SUNSET_KEY]).nearest("minute");
|
||||
|
||||
if ((starttime >= 5000) || (endtime == 5000) || (endtime == 6000)) {
|
||||
var times = SunCalc.getTimes(now, node.lat, node.lon);
|
||||
var startMillis = Date.UTC(times[node.start].getUTCFullYear(), times[node.start].getUTCMonth(), times[node.start].getUTCDate(), times[node.start].getUTCHours(), times[node.start].getUTCMinutes());
|
||||
var endMillis = Date.UTC(times[node.end].getUTCFullYear(), times[node.end].getUTCMonth(), times[node.end].getUTCDate(), times[node.end].getUTCHours(), times[node.end].getUTCMinutes());
|
||||
var dawn = ((startMillis - midnightMillis) / 60000) + Number(node.dawnoff);
|
||||
var dusk = ((endMillis - midnightMillis) / 60000) + Number(node.duskoff);
|
||||
if (starttime == 5000) { starttime = dawn; }
|
||||
if (starttime == 6000) { starttime = dusk; }
|
||||
if (endtime == 5000) { endtime = dawn; }
|
||||
if (endtime == 6000) { endtime = dusk; }
|
||||
if (RED.settings.verbose) { node.log("Dawn " + parseInt(dawn / 60) + ":" + dawn % 60 + " - Dusk " + parseInt(dusk / 60) + ":" + dusk % 60); }
|
||||
// add optional sun event offset, if specified
|
||||
sunriseDateTime = sunriseDateTime.add(Number(node.sunriseOffset), "minutes");
|
||||
sunsetDateTime = sunsetDateTime.add(Number(node.sunsetOffset), "minutes");
|
||||
|
||||
// check if sun event has already occurred today
|
||||
if (now.isAfter(sunriseDateTime)) {
|
||||
// get tomorrow's sunrise, since it'll be different
|
||||
sunriseDateTime = spacetime(SunCalc.getTimes(now.add(1, "day").toNativeDate(), node.lat, node.lon)[SUNRISE_KEY]).nearest("minute");
|
||||
// add optional sun event offset, if specified (again)
|
||||
sunriseDateTime = sunriseDateTime.add(Number(node.sunriseOffset), "minutes");
|
||||
}
|
||||
if (now.isAfter(sunsetDateTime)) {
|
||||
// get tomorrow's sunset, since it'll be different
|
||||
sunsetDateTime = spacetime(SunCalc.getTimes(now.add(1, "day").toNativeDate(), node.lat, node.lon)[SUNSET_KEY]).nearest("minute");
|
||||
// add optional sun event offset, if specified (again)
|
||||
sunsetDateTime = sunsetDateTime.add(Number(node.sunsetOffset), "minutes");
|
||||
}
|
||||
|
||||
var proceed = 0;
|
||||
switch (now.getDay()) {
|
||||
case 0 : { if (node.sun) { proceed++; } break; }
|
||||
case 1 : { if (node.mon) { proceed++; } break; }
|
||||
case 2 : { if (node.tue) { proceed++; } break; }
|
||||
case 3 : { if (node.wed) { proceed++; } break; }
|
||||
case 4 : { if (node.thu) { proceed++; } break; }
|
||||
case 5 : { if (node.fri) { proceed++; } break; }
|
||||
case 6 : { if (node.sat) { proceed++; } break; }
|
||||
// log sun events
|
||||
if (RED.settings.verbose) {
|
||||
node.log(`Sunrise ${sunriseDateTime.format("time")} - Sunset ${sunsetDateTime.format("time")} `);
|
||||
}
|
||||
|
||||
if (proceed) {
|
||||
switch (now.getMonth()) {
|
||||
case 0 : { if (node.jan) { proceed++; } break; }
|
||||
case 1 : { if (node.feb) { proceed++; } break; }
|
||||
case 2 : { if (node.mar) { proceed++; } break; }
|
||||
case 3 : { if (node.apr) { proceed++; } break; }
|
||||
case 4 : { if (node.may) { proceed++; } break; }
|
||||
case 5 : { if (node.jun) { proceed++; } break; }
|
||||
case 6 : { if (node.jul) { proceed++; } break; }
|
||||
case 7 : { if (node.aug) { proceed++; } break; }
|
||||
case 8 : { if (node.sep) { proceed++; } break; }
|
||||
case 9 : { if (node.oct) { proceed++; } break; }
|
||||
case 10: { if (node.nov) { proceed++; } break; }
|
||||
case 11: { if (node.dec) { proceed++; } break; }
|
||||
// apply selected timezone to selected times (not to sunrise/sunset-- those are based on lat/long)
|
||||
const currentTimeZone = now.timezone();
|
||||
const selectedTimeZone = spacetime(now.epoch, this.timezone.toLowerCase()).timezone();
|
||||
|
||||
// handler function to convert minute strings (from <option> tags) to spacetime objects, called below
|
||||
let getSelectedTimeFromMinuteString = minuteString => {
|
||||
const selectedTimeInMinutesAfterMidnight = Number(minuteString);
|
||||
let selectedTime = spacetime.now();
|
||||
// if less than 1440, what are the time values for the next start and stop time?
|
||||
if (selectedTimeInMinutesAfterMidnight < 1440) {
|
||||
// determine offset to get from selected time zone to current timezone
|
||||
// e.g. current (EDT) is -4, selected (PDT) is -7
|
||||
// in order to get from PDT to EDT, you must add 3
|
||||
// (-4) - (-7) = +3
|
||||
const offset = currentTimeZone.current.offset - selectedTimeZone.current.offset;
|
||||
const selectedHourValue = Math.floor(selectedTimeInMinutesAfterMidnight / 60);
|
||||
const selectedMinuteValue = Math.floor(selectedTimeInMinutesAfterMidnight % 60);
|
||||
selectedTime = selectedTime.hour(selectedHourValue).minute(selectedMinuteValue).second(0).millisecond(0);
|
||||
selectedTime = selectedTime.add(offset, "hours");
|
||||
// select the next time if it's in the past
|
||||
if (now.isAfter(selectedTime)) {
|
||||
selectedTime = selectedTime.add(1, "day");
|
||||
}
|
||||
} else if (selectedTimeInMinutesAfterMidnight == 5000) { // sunrise
|
||||
selectedTime = sunriseDateTime;
|
||||
} else if (selectedTimeInMinutesAfterMidnight == 6000) { // sunset
|
||||
selectedTime = sunsetDateTime;
|
||||
}
|
||||
return selectedTime;
|
||||
};
|
||||
|
||||
// our definitive next ON time
|
||||
let selectedOnTime = getSelectedTimeFromMinuteString(node.startt);
|
||||
// our definitive next OFF time
|
||||
let selectedOffTime = getSelectedTimeFromMinuteString(node.endt);
|
||||
|
||||
// handle the "Start + X Minutes" cases
|
||||
if (node.endt >= 10000) {
|
||||
// even though the next start time might be tomorrow,
|
||||
// the start time + X minutes might still be coming today,
|
||||
// so we need to go back a day first
|
||||
const selectedOnTimeMinus1Day = selectedOnTime.subtract(1, "day");
|
||||
selectedOffTime = selectedOnTimeMinus1Day.add(node.endt - 10000, "minutes");
|
||||
// _now_ we can check if the off time is in the past
|
||||
if (now.isAfter(selectedOffTime)) {
|
||||
selectedOffTime = selectedOffTime.add(1, "day");
|
||||
}
|
||||
}
|
||||
|
||||
if (proceed >= 2) { proceed = 1; }
|
||||
else { proceed = 0; }
|
||||
|
||||
newendtime = endtime;
|
||||
if (endtime > 10000) { newendtime = starttime + (endtime - 10000); }
|
||||
|
||||
if (proceed) { // have to handle midnight wrap
|
||||
if (starttime <= newendtime) {
|
||||
if ((today >= starttime) && (today <= newendtime)) { proceed++; }
|
||||
// handler function for the node payload, called below
|
||||
let sendPayload = (payload, nextTime) => {
|
||||
if (payload == 1) {
|
||||
node.status({
|
||||
fill: "yellow",
|
||||
shape: "dot",
|
||||
text: `on until ${nextTime.format("time")}`
|
||||
});
|
||||
} else {
|
||||
node.status({
|
||||
fill: "blue",
|
||||
shape: "dot",
|
||||
text: `off until ${nextTime.format("time")}`
|
||||
});
|
||||
}
|
||||
else {
|
||||
if ((today >= starttime) || (today <= newendtime)) { proceed++; }
|
||||
var msg = {};
|
||||
if (node.mytopic) {
|
||||
msg.topic = node.mytopic;
|
||||
}
|
||||
msg.payload = payload;
|
||||
node.send(msg);
|
||||
};
|
||||
|
||||
var proceed = true;
|
||||
|
||||
// if today is not among the selected days of the week, stop here
|
||||
switch (nowNative.getDay()) {
|
||||
case 0 : { if (!node.sun) { proceed &= false; } break; }
|
||||
case 1 : { if (!node.mon) { proceed &= false; } break; }
|
||||
case 2 : { if (!node.tue) { proceed &= false; } break; }
|
||||
case 3 : { if (!node.wed) { proceed &= false; } break; }
|
||||
case 4 : { if (!node.thu) { proceed &= false; } break; }
|
||||
case 5 : { if (!node.fri) { proceed &= false; } break; }
|
||||
case 6 : { if (!node.sat) { proceed &= false; } break; }
|
||||
}
|
||||
|
||||
if (proceed >= 2) {
|
||||
node.status({fill:"yellow", shape:"dot", text:"on until " + parseInt((newendtime -tzOff) / 60) + ":" + ("0" + (newendtime - tzOff) % 60).substr(-2)});
|
||||
}
|
||||
else {
|
||||
node.status({fill:"blue", shape:"dot", text:"off until " + parseInt((starttime - tzOff) / 60) + ":" + ("0" + (starttime - tzOff) % 60).substr(-2)});
|
||||
if (!proceed) {
|
||||
sendPayload(0, selectedOnTime);
|
||||
return;
|
||||
}
|
||||
|
||||
var msg = {};
|
||||
if (node.mytopic) { msg.topic = node.mytopic; }
|
||||
msg.payload = (proceed >= 2) ? 1 : 0;
|
||||
node.send(msg);
|
||||
// if this month is not among the selected months, stop here
|
||||
switch (nowNative.getMonth()) {
|
||||
case 0 : { if (!node.jan) { proceed &= false; } break; }
|
||||
case 1 : { if (!node.feb) { proceed &= false; } break; }
|
||||
case 2 : { if (!node.mar) { proceed &= false; } break; }
|
||||
case 3 : { if (!node.apr) { proceed &= false; } break; }
|
||||
case 4 : { if (!node.may) { proceed &= false; } break; }
|
||||
case 5 : { if (!node.jun) { proceed &= false; } break; }
|
||||
case 6 : { if (!node.jul) { proceed &= false; } break; }
|
||||
case 7 : { if (!node.aug) { proceed &= false; } break; }
|
||||
case 8 : { if (!node.sep) { proceed &= false; } break; }
|
||||
case 9 : { if (!node.oct) { proceed &= false; } break; }
|
||||
case 10: { if (!node.nov) { proceed &= false; } break; }
|
||||
case 11: { if (!node.dec) { proceed &= false; } break; }
|
||||
}
|
||||
|
||||
if (!proceed) {
|
||||
sendPayload(0, selectedOnTime);
|
||||
return;
|
||||
}
|
||||
|
||||
// if the chronological order is NOW --> ON --> OFF, then now should be OFF
|
||||
if (proceed && selectedOffTime.isAfter(selectedOnTime)) {
|
||||
sendPayload(0, selectedOnTime);
|
||||
return;
|
||||
}
|
||||
|
||||
// if the chronological order is NOW --> OFF --> ON, then now should be ON
|
||||
if (proceed && selectedOffTime.isBefore(selectedOnTime)) {
|
||||
sendPayload(1, selectedOffTime);
|
||||
return;
|
||||
}
|
||||
|
||||
// Note: we already ensured that all ON or OFF times would be in the future,
|
||||
// so there is no midnight wrapping issue.
|
||||
});
|
||||
|
||||
var tock = setTimeout(function() {
|
||||
var tock = setTimeout(function () {
|
||||
node.emit("input", {});
|
||||
}, 2000); // wait 2 secs before starting to let things settle down – e.g. UI connect
|
||||
|
||||
var tick = setInterval(function() {
|
||||
var tick = setInterval(function () {
|
||||
node.emit("input", {});
|
||||
}, 60000); // trigger every 60 secs
|
||||
|
||||
this.on("close", function() {
|
||||
this.on("close", function () {
|
||||
if (tock) { clearTimeout(tock); }
|
||||
if (tick) { clearInterval(tick); }
|
||||
});
|
||||
}
|
||||
|
||||
RED.httpAdmin.post("/timeswitch/:id", RED.auth.needsPermission("timeswitch.write"), function(req, res) {
|
||||
RED.httpAdmin.post("/timeswitch/:id", RED.auth.needsPermission("timeswitch.write"), function (req, res) {
|
||||
var node = RED.nodes.getNode(req.params.id);
|
||||
if (node != null) {
|
||||
try {
|
||||
node.emit("input", {payload:"reset"});
|
||||
node.emit("input", { payload: "reset" });
|
||||
res.sendStatus(200);
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
res.sendStatus(500);
|
||||
node.error("Inject failed:" + err);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user