mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
timeswitch node: time zone support; do not mark as misconfigured when sunrise/sunset not used and lat/lon not given (#757)
* timeswitch node: a) do not mark node as misconfigured if sunrise and sunset are not used and latitude/longitude are not given. b) support for specifying time zone when on and/or off times are not specified as sunrise and/or sunset. * Replaced moment dependency with spacetime * Timezone defaults to UTC for compatibility with previous node version
This commit is contained in:
committed by
GitHub
parent
c368e3bcd4
commit
d69da74843
@@ -2,6 +2,7 @@
|
||||
module.exports = function(RED) {
|
||||
"use strict";
|
||||
var SunCalc = require('suncalc');
|
||||
const spacetime = require("spacetime")
|
||||
|
||||
function TimeswitchNode(n) {
|
||||
RED.nodes.createNode(this, n);
|
||||
@@ -14,6 +15,7 @@ module.exports = function(RED) {
|
||||
this.duskoff = n.duskoff;
|
||||
this.dawnoff = n.dawnoff;
|
||||
this.mytopic = n.mytopic;
|
||||
this.timezone = n.timezone || "UTC";
|
||||
|
||||
this.sun = n.sun;
|
||||
this.mon = n.mon;
|
||||
@@ -42,10 +44,9 @@ module.exports = function(RED) {
|
||||
this.on("input", function(msg2) {
|
||||
if (msg2.payload === "reset") { ison = 0; }
|
||||
|
||||
var now = new Date();
|
||||
var nowoff = -now.getTimezoneOffset() * 60000;
|
||||
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);
|
||||
nowMillis += nowoff;
|
||||
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);
|
||||
@@ -55,8 +56,6 @@ module.exports = function(RED) {
|
||||
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());
|
||||
startMillis += nowoff;
|
||||
endMillis += nowoff;
|
||||
var dawn = ((startMillis - midnightMillis) / 60000) + Number(node.dawnoff);
|
||||
var dusk = ((endMillis - midnightMillis) / 60000) + Number(node.duskoff);
|
||||
if (starttime == 5000) { starttime = dawn; }
|
||||
|
Reference in New Issue
Block a user