53 lines
1.8 KiB
JavaScript
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;
|
|
|