var timer = flow.get('timer') || {
"running": false,
"startTime": 0,
"endTime": 0,
"ms": 0,
"sec": 0,
"min": 0,
"shift": 0
}
var time = flow.get('time') || 0;
if (msg.topic == "timer_status") {
node.warn({
payload: timer
});
}
if (msg.topic == "start") {
node.warn({
payload: "start pressed"
});
start();
}
if (msg.topic == "stop") {
node.warn({
payload: "stop pressed"
});
stop();
}
if (msg.topic == "value") {
flow.set('time', msg.payload);
node.warn({
payload: flow.get('time')
})
}
function start() {
var timer = flow.get('timer');
timer.running = true;
flow.set('timer', timer);
configTimer();
node.warn({
payload: timer
})
}
function stop() {
var timer = flow.get('timer');
timer.running = false;
flow.set('timer', timer);
node.warn({
payload: timer
});
}
function configTimer() {
var timer = flow.get('timer');
timer.startTime = Date.now();
flow.set('timer', timer)
if (timer.running) {
checkTime();
}
}
function checkTime() {
var timer = flow.get('timer');
if (timer.running) {
var seconds = 0,
minutes = time,
hours = 0,
remaining = 0,
shift = timer.shift;
timer.endTime = (seconds * 1000) + (minutes * 60 * 1000) + (hours * 60 * 60 * 1000) + shift;
if (timer.startTime + timer.endTime >= Date.now()) {
timer.ms = timer.startTime + timer.endTime - Date.now();
timer.min = (timer.ms / 1000 / 60) << 0,
timer.sec = (timer.ms / 1000) % 60 << 0;
remaining = timer.min + ':' + timer.sec;
node.send({
time: remaining
});
setTimeout(checkTime, 1000);
} else {
stop();
}
flow.set('timer', timer);
}
}
--
http://nodered.org
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/70d2b0e5-1cf1-49a0-9f67-2617429acec7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/7edc7cee-396b-41a0-83ce-6c1cf76c9c9e%40googlegroups.com.