Handle when scene and device have the same Id
- Catch emitted errors in Mqtt
This commit is contained in:
parent
dca491bf00
commit
ef7a5086a1
4 changed files with 27 additions and 10 deletions
|
|
@ -85,6 +85,10 @@ class MqttClient extends EventEmitter {
|
|||
password: this.config.mqttPassword,
|
||||
});
|
||||
|
||||
this.client.on('error', (err) => {
|
||||
logger.warn('Error emitted from mqtt client', err);
|
||||
});
|
||||
|
||||
this.client.on('connect', () => {
|
||||
logger.info('Connected to MQTT.');
|
||||
|
||||
|
|
@ -115,17 +119,26 @@ class MqttClient extends EventEmitter {
|
|||
} else {
|
||||
const decodedTopic = decodeTopic(topic);
|
||||
if (decodedTopic) {
|
||||
const device = this.deviceRegistry.getDevice(decodedTopic.id);
|
||||
const deviceName = device ? device.name : '';
|
||||
let device = this.deviceRegistry.getDevice(decodedTopic.id);
|
||||
|
||||
const command = message.toString().substring(0, 1) === '{'
|
||||
? JSON.parse(message.toString())
|
||||
: message.toString();
|
||||
const messageString = message.toString();
|
||||
const isJsonMessage = messageString.startsWith('{');
|
||||
const command = isJsonMessage
|
||||
? JSON.parse(messageString)
|
||||
: messageString;
|
||||
|
||||
if (!isJsonMessage && messageString === 'ON' && this.deviceRegistry.getScene(decodedTopic.id)) {
|
||||
// Guess that id that got state command without dim value belongs to Scene, not Device
|
||||
// This guess could very well be wrong depending on the installation...
|
||||
logger.warn(`Device id ${decodedTopic.id} belongs to both scene and device, guessing Scene is what should be set to ON. OFF commands still sent to device.`);
|
||||
device = this.deviceRegistry.getScene(decodedTopic.id);
|
||||
}
|
||||
const deviceName = device ? device.name : '';
|
||||
|
||||
switch (decodedTopic.command) {
|
||||
case 'set':
|
||||
logger.verbose(
|
||||
`Got mqtt SET command for ${decodedTopic.type}, ${deviceName} (${decodedTopic.id}): ${message}`,
|
||||
`Got mqtt SET command for ${decodedTopic.type}, ${deviceName} (${decodedTopic.id}): ${messageString}`,
|
||||
);
|
||||
|
||||
if (device) {
|
||||
|
|
@ -143,7 +156,7 @@ class MqttClient extends EventEmitter {
|
|||
`Sent mqtt ${decodedTopic.command} command for ${
|
||||
decodedTopic.type
|
||||
}, ${deviceName} (${decodedTopic.id}). ${
|
||||
decodedTopic.command === 'availability' ? message : ''
|
||||
decodedTopic.command === 'availability' ? messageString : ''
|
||||
}`,
|
||||
);
|
||||
break;
|
||||
|
|
@ -151,7 +164,7 @@ class MqttClient extends EventEmitter {
|
|||
logger.verbose(`Warning: Unknown command ${decodedTopic.command} in decoded topic`);
|
||||
}
|
||||
} else {
|
||||
logger.verbose(`Warning: Got unrecognized mqtt command on '${topic}': ${message}`);
|
||||
logger.verbose(`Warning: Got unrecognized mqtt command on '${topic}': ${message.toString()}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue