Put sun event offset back in; reformat

This commit is contained in:
J.D. Mallen 2022-08-22 20:30:54 -04:00
parent 5cfd36e1dc
commit 5faab8507a
No known key found for this signature in database
GPG Key ID: 91A7CC9239E54C92

View File

@ -1,4 +1,3 @@
module.exports = function (RED) {
"use strict";
var SunCalc = require('suncalc');
@ -10,12 +9,10 @@ module.exports = function(RED) {
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";
@ -26,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;
@ -40,12 +38,8 @@ 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();
@ -55,17 +49,25 @@ module.exports = function(RED) {
let sunriseDateTime = spacetime(sunEvents[SUNRISE_KEY]).nearest("minute");
let sunsetDateTime = spacetime(sunEvents[SUNSET_KEY]).nearest("minute");
// 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");
}
// log
// log sun events
if (RED.settings.verbose) {
node.log(`Sunrise ${sunriseDateTime.format("time")} - Sunset ${sunsetDateTime.format("time")} `);
}
@ -116,8 +118,7 @@ module.exports = function(RED) {
shape: "dot",
text: `on until ${nextTime.format("time")}`
});
}
else {
} else {
node.status({
fill: "blue",
shape: "dot",
@ -125,7 +126,9 @@ module.exports = function(RED) {
});
}
var msg = {};
if (node.mytopic) { msg.topic = node.mytopic; }
if (node.mytopic) {
msg.topic = node.mytopic;
}
msg.payload = payload;
node.send(msg);
};
@ -176,7 +179,7 @@ module.exports = function(RED) {
// if the chronological order is NOW, OFF, ON, then now should be ON
if (proceed && selectedOffTime.isBefore(selectedOnTime)) {
sendPayload(1, selectedOnTime);
sendPayload(1, selectedOffTime);
return;
}
});
@ -190,8 +193,12 @@ module.exports = function(RED) {
}, 60000); // trigger every 60 secs
this.on("close", function () {
if (tock) { clearTimeout(tock); }
if (tick) { clearInterval(tick); }
if (tock) {
clearTimeout(tock);
}
if (tick) {
clearInterval(tick);
}
});
}
@ -199,15 +206,15 @@ module.exports = function(RED) {
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);
}
});