refactoring
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user