refactoring

This commit is contained in:
Wolfgang Hottgenroth
2018-01-02 21:51:05 +01:00
parent 1c06b0f3a3
commit 0bcc75f53f
7 changed files with 84 additions and 50 deletions

View File

@ -1,43 +1,64 @@
let logger = require('./log');
let mqtt = require('./mqttHandler');
let AItem = require('./AItem')
let AHomematicItem = require('./AHomematicItem')
class HomematicDimmerItem extends AItem {
constructor(floor, room, item, actionTopic) {
super(floor, room, item);
class HomematicDimmerItem extends AHomematicItem {
constructor(floor, room, item, hmId) {
super(floor, room, item, hmId);
this.stateTopic = `${this.topicFirstPart}/state`;
this.brightTopic = `${this.topicFirstPart}/bright`;
this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback`;
this.brightFeedbackTopic = `${this.topicFirstPart}/bright/feedback`;
this.actionTopic = actionTopic;
this.actionTopic = `${this.actionTopicPre}/1/LEVEL`;
this.errorOverheatTopic = `${this.deviceTopicPre}/1/ERROR_OVERHEAT`;
this.errorOverloadTopic = `${this.deviceTopicPre}/1/ERROR_OVERLOAD`;
this.errorReducedTopic = `${this.deviceTopicPre}/1/ERROR_REDUCED`;
this.subscribeTopics = [
this.stateTopic,
this.brightTopic,
this.errorOverheatTopic,
this.errorOverloadTopic,
this.errorReducedTopic
];
this.state = 'OFF';
this.oldState = undefined;
this.bright = 0;
this.oldBright = undefined;
}
start() {
mqtt.register([this.stateTopic, this.brightTopic], (topic, payload) => {
payload = payload.toString('UTF-8');
logger.info(`item ${this.itemId}: ${topic}, ${payload}`)
if (topic == this.stateTopic) {
dimmerAction() {
if ((this.state != this.oldState) || (this.bright != this.oldBright)) {
if (this.state == 'ON') {
mqtt.send(this.actionTopic, `${this.bright}`);
} else {
mqtt.send(this.actionTopic, '0');
}
this.oldState = this.state;
this.oldBright = this.bright;
}
}
processMessage(topic, payload) {
switch (topic) {
case this.stateTopic:
this.state = payload;
mqtt.send(this.stateFeedbackTopic, this.state);
} else if (topic == this.brightTopic) {
this.dimmerAction();
break;
case this.brightTopic:
this.bright = payload;
mqtt.send(this.brightFeedbackTopic, this.bright);
}
if ((this.state != this.oldState) || (this.bright != this.oldBright)) {
if (this.state == 'ON') {
mqtt.send(this.actionTopic, `${this.bright}`);
} else {
mqtt.send(this.actionTopic, '0');
this.dimmerAction();
break;
case this.errorOverheatTopic:
case this.errorOverloadTopic:
case this.errorReducedTopic:
if (payload != 'false') {
logger.warn(`Homematic dimmer ${this.hmId}, ${this.itemId} reports: ${topic}: ${payload}`);
}
this.oldState = this.state;
this.oldBright = this.bright;
}
});
break;
}
}
}