timer handling
This commit is contained in:
51
src/TimerAdaptor.js
Normal file
51
src/TimerAdaptor.js
Normal file
@ -0,0 +1,51 @@
|
||||
let AItem = require('./AItem')
|
||||
let logger = require('./log')
|
||||
let mqtt = require('./mqttHandler');
|
||||
|
||||
|
||||
class TimerAdaptor extends AItem {
|
||||
constructor(floor, room, item, delay) {
|
||||
super(floor, room, item);
|
||||
this.actionStateTopic = `${this.topicFirstPart}/state`;
|
||||
this.inTopic = `${this.topicFirstPart}/timerIn`;
|
||||
this.subscribeTopics = [ this.inTopic ];
|
||||
this.state = 'OFF';
|
||||
this.delay = delay;
|
||||
this.timer = null;
|
||||
}
|
||||
|
||||
processMessage(topic, payload) {
|
||||
switch (topic) {
|
||||
case this.inTopic:
|
||||
switch (payload) {
|
||||
case 'SHORT':
|
||||
if ((this.state == 'ON') && (this.timer != null)) {
|
||||
clearTimeout(this.timer);
|
||||
}
|
||||
this.state = 'ON';
|
||||
mqtt.send(this.actionStateTopic, this.state, true);
|
||||
this.timer = setTimeout(() => {
|
||||
this.state = 'OFF';
|
||||
mqtt.send(this.actionStateTopic, this.state, true);
|
||||
}, (this.delay * 1000));
|
||||
break;
|
||||
case 'LONG_END':
|
||||
clearTimeout(this.timer);
|
||||
if (this.state == 'OFF') {
|
||||
this.state = 'ON';
|
||||
this.overwrite = true;
|
||||
} else {
|
||||
this.state = 'OFF';
|
||||
this.overwrite = false;
|
||||
}
|
||||
mqtt.send(this.actionStateTopic, this.state, true);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = TimerAdaptor;
|
||||
|
Reference in New Issue
Block a user