// respond to inputs....
this.on('input', function (msg) {
myVar = setInterval(myTimer() ,1000);
});
function myTimer() {
node.send(msg);
clearInterval(myVar);
}
29 May 14:19:05 - [red] Uncaught Exception:
29 May 14:19:05 - TypeError: timer._repeat is not a function
at wrapper [as _onTimeout] (timers.js:275:11)
at Timer.listOnTimeout (timers.js:92:15)
setInterval(upload_random_image, 3600000);
msg.payload = msg.payload.command
// Calculate the elapsed time
function elapsed() {
var el = {};
// Milliseconds
if (context.openTime) {
el.ms = new Date() - context.openTime;
} else {
el.ms = 0;
}
// seconds
el.sec = Math.round( el.ms / 1000 ); // or .toFixed(numdp);
// minutes
el.min = Math.round( el.ms / 60000 ); // or .toFixed(numdp);
// return object
return el;
} // --- end of elapsed() --- //
// What to do after the timout interval
function action() {
// This will complete a timer so there is now one less
context.timers--;
// When countdown expires, check if door is still open
if ( context.state == "Open" ) {
// Check whether door has really been open all the time or whether
// it has been closed then reopened. In the later case, the elapsed
// time will be less than the timeout. Wouldn't need this if we could use clearTimeout()!
if ( elapsed()['ms'] >= timeout ) {
//node.warn( "Freezer door is still open! - triggered from timeout - Elapsed time: " + elapsed()['min'] );
// Send the msg downstream
node.send( {'payload': 'Freezer door has been open for ' + elapsed()['min'] + ' minutes!', 'timers': context.timers} );
// Set up another reminder in timeout's time (keep going till someone shuts the door!) & keep track of how many
context.timers++;
setTimeout(action, timeout);
} else {
//node.warn("Freezer door was closed then opened again - triggered from timeout - Elapsed time: " + elapsed()['min'] + ', Timers: ' + context.timers );
}
} else {
//node.warn("Freezer door already closed - triggered from timeout - Elapsed time: " + elapsed()['min'] + ', Timers: ' + context.timers );
}
} // --- End of action() --- //
// How many minutes do we want to wait until we trigger the alarm?
// 5 minute countdown = 5*60*1000= 300000
var timeout = 300000;
// Check how many timeouts we have started
if ( !('timers' in context) ) context.timers = 0;
if ( msg.payload == "On" ) {
node.warn( 'Freezer door opened, Timers: ' + context.timers );
// Keep track of how long the door is open
if (!context.openTime) context.openTime = new Date();
// Track state
context.state = "Open";
// Create a new timer & keep track of how many
context.timers++;
//context.timeout1 = setTimeout(action, timeout);
setTimeout(action, timeout);
} else {
node.warn( "Freezer door has been closed - Elapsed time: " + elapsed()['min'] + ', Timers: ' + context.timers );
// track state
context.state = "Closed";
// If the door was open a long time, send a msg that it is now closed
if ( elapsed()['ms'] >= timeout ) {
node.send( {'payload': 'Freezer door is now closed but was open for ' + elapsed()['min'] + ' minutes!', 'timers': context.timers} );
}
// Reset the open time
if (context.openTime) context.openTime = false;
// Cancel the timeout
//clearTimeout(context.timeout1);
}