Files
dispatcher_ng/src/TimerAdaptor.js
Wolfgang Hottgenroth 2fdd0c9352 refactoring
2018-01-04 23:07:21 +01:00

53 lines
1.8 KiB
JavaScript

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(() => {
logger.info(`timer ${this.itemId} elapsed`);
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;