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) { module.exports = function (RED) {
"use strict"; "use strict";
var SunCalc = require('suncalc'); var SunCalc = require('suncalc');
@ -10,12 +9,10 @@ module.exports = function(RED) {
RED.nodes.createNode(this, n); RED.nodes.createNode(this, n);
this.lat = n.lat; this.lat = n.lat;
this.lon = n.lon; this.lon = n.lon;
this.start = n.start || "sunrise";
this.end = n.end || "sunset";
this.startt = n.starttime; this.startt = n.starttime;
this.endt = n.endtime; this.endt = n.endtime;
this.duskoff = n.duskoff; this.sunriseOffset = n.dawnoff;
this.dawnoff = n.dawnoff; this.sunsetOffset = n.duskoff;
this.mytopic = n.mytopic; this.mytopic = n.mytopic;
this.timezone = n.timezone || "UTC"; this.timezone = n.timezone || "UTC";
@ -26,6 +23,7 @@ module.exports = function(RED) {
this.thu = n.thu; this.thu = n.thu;
this.fri = n.fri; this.fri = n.fri;
this.sat = n.sat; this.sat = n.sat;
this.jan = n.jan; this.jan = n.jan;
this.feb = n.feb; this.feb = n.feb;
this.mar = n.mar; this.mar = n.mar;
@ -40,12 +38,8 @@ module.exports = function(RED) {
this.dec = n.dec; this.dec = n.dec;
var node = this; 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 // current global time
const now = spacetime.now(); const now = spacetime.now();
const nowNative = now.toNativeDate(); const nowNative = now.toNativeDate();
@ -55,17 +49,25 @@ module.exports = function(RED) {
let sunriseDateTime = spacetime(sunEvents[SUNRISE_KEY]).nearest("minute"); let sunriseDateTime = spacetime(sunEvents[SUNRISE_KEY]).nearest("minute");
let sunsetDateTime = spacetime(sunEvents[SUNSET_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 // check if sun event has already occurred today
if (now.isAfter(sunriseDateTime)) { if (now.isAfter(sunriseDateTime)) {
// get tomorrow's sunrise, since it'll be different // 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"); 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)) { if (now.isAfter(sunsetDateTime)) {
// get tomorrow's sunset, since it'll be different // 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"); 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) { if (RED.settings.verbose) {
node.log(`Sunrise ${sunriseDateTime.format("time")} - Sunset ${sunsetDateTime.format("time")} `); node.log(`Sunrise ${sunriseDateTime.format("time")} - Sunset ${sunsetDateTime.format("time")} `);
} }
@ -116,8 +118,7 @@ module.exports = function(RED) {
shape: "dot", shape: "dot",
text: `on until ${nextTime.format("time")}` text: `on until ${nextTime.format("time")}`
}); });
} } else {
else {
node.status({ node.status({
fill: "blue", fill: "blue",
shape: "dot", shape: "dot",
@ -125,7 +126,9 @@ module.exports = function(RED) {
}); });
} }
var msg = {}; var msg = {};
if (node.mytopic) { msg.topic = node.mytopic; } if (node.mytopic) {
msg.topic = node.mytopic;
}
msg.payload = payload; msg.payload = payload;
node.send(msg); 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 the chronological order is NOW, OFF, ON, then now should be ON
if (proceed && selectedOffTime.isBefore(selectedOnTime)) { if (proceed && selectedOffTime.isBefore(selectedOnTime)) {
sendPayload(1, selectedOnTime); sendPayload(1, selectedOffTime);
return; return;
} }
}); });
@ -190,8 +193,12 @@ module.exports = function(RED) {
}, 60000); // trigger every 60 secs }, 60000); // trigger every 60 secs
this.on("close", function () { this.on("close", function () {
if (tock) { clearTimeout(tock); } if (tock) {
if (tick) { clearInterval(tick); } clearTimeout(tock);
}
if (tick) {
clearInterval(tick);
}
}); });
} }
@ -199,15 +206,15 @@ module.exports = function(RED) {
var node = RED.nodes.getNode(req.params.id); var node = RED.nodes.getNode(req.params.id);
if (node != null) { if (node != null) {
try { try {
node.emit("input", {payload:"reset"}); node.emit("input", {
payload: "reset"
});
res.sendStatus(200); res.sendStatus(200);
} } catch (err) {
catch (err) {
res.sendStatus(500); res.sendStatus(500);
node.error("Inject failed:" + err); node.error("Inject failed:" + err);
} }
} } else {
else {
res.sendStatus(404); res.sendStatus(404);
} }
}); });