-
Notifications
You must be signed in to change notification settings - Fork 1
/
alarm-get.js
25 lines (22 loc) · 1023 Bytes
/
alarm-get.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module.exports = (RED) => {
RED.nodes.registerType('alarm-get', function(config) {
const node = this;
RED.nodes.createNode(node, config);
node.clock = RED.nodes.getNode(config.clock);
node.on('input', (msg) => {
const sec = parseInt(node.clock.time);
const now = new Date();
const midnight = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0,0,0);
var nextRing = new Date(midnight.getTime());
nextRing.setSeconds(sec);
if (nextRing < now) nextRing.setSeconds(nextRing.getSeconds() + 60*60*24);
msg.payload = {
secondsPastMidnight: sec,
date: nextRing,
str: (String(nextRing.getHours()).padStart(2, '0') + ":" + String(nextRing.getMinutes()).padStart(2, '0') + ":" + String(nextRing.getSeconds()).padStart(2, '0')),
countdown: Math.round((nextRing - now)/1000)
};
node.send(msg);
});
});
}