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
|
|
@ -65,6 +65,10 @@ class DeviceRegistry {
|
||||||
return (this.plejdDevices[deviceId] || {}).name;
|
return (this.plejdDevices[deviceId] || {}).name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getScene(sceneId) {
|
||||||
|
return this.sceneDevices[sceneId];
|
||||||
|
}
|
||||||
|
|
||||||
getSceneName(sceneId) {
|
getSceneName(sceneId) {
|
||||||
return (this.sceneDevices[sceneId] || {}).name;
|
return (this.sceneDevices[sceneId] || {}).name;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,10 @@ class MqttClient extends EventEmitter {
|
||||||
password: this.config.mqttPassword,
|
password: this.config.mqttPassword,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.client.on('error', (err) => {
|
||||||
|
logger.warn('Error emitted from mqtt client', err);
|
||||||
|
});
|
||||||
|
|
||||||
this.client.on('connect', () => {
|
this.client.on('connect', () => {
|
||||||
logger.info('Connected to MQTT.');
|
logger.info('Connected to MQTT.');
|
||||||
|
|
||||||
|
|
@ -115,17 +119,26 @@ class MqttClient extends EventEmitter {
|
||||||
} else {
|
} else {
|
||||||
const decodedTopic = decodeTopic(topic);
|
const decodedTopic = decodeTopic(topic);
|
||||||
if (decodedTopic) {
|
if (decodedTopic) {
|
||||||
const device = this.deviceRegistry.getDevice(decodedTopic.id);
|
let device = this.deviceRegistry.getDevice(decodedTopic.id);
|
||||||
const deviceName = device ? device.name : '';
|
|
||||||
|
|
||||||
const command = message.toString().substring(0, 1) === '{'
|
const messageString = message.toString();
|
||||||
? JSON.parse(message.toString())
|
const isJsonMessage = messageString.startsWith('{');
|
||||||
: message.toString();
|
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) {
|
switch (decodedTopic.command) {
|
||||||
case 'set':
|
case 'set':
|
||||||
logger.verbose(
|
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) {
|
if (device) {
|
||||||
|
|
@ -143,7 +156,7 @@ class MqttClient extends EventEmitter {
|
||||||
`Sent mqtt ${decodedTopic.command} command for ${
|
`Sent mqtt ${decodedTopic.command} command for ${
|
||||||
decodedTopic.type
|
decodedTopic.type
|
||||||
}, ${deviceName} (${decodedTopic.id}). ${
|
}, ${deviceName} (${decodedTopic.id}). ${
|
||||||
decodedTopic.command === 'availability' ? message : ''
|
decodedTopic.command === 'availability' ? messageString : ''
|
||||||
}`,
|
}`,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
@ -151,7 +164,7 @@ class MqttClient extends EventEmitter {
|
||||||
logger.verbose(`Warning: Unknown command ${decodedTopic.command} in decoded topic`);
|
logger.verbose(`Warning: Unknown command ${decodedTopic.command} in decoded topic`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.verbose(`Warning: Got unrecognized mqtt command on '${topic}': ${message}`);
|
logger.verbose(`Warning: Got unrecognized mqtt command on '${topic}': ${message.toString()}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ class PlejBLEHandler extends EventEmitter {
|
||||||
plejdService = null;
|
plejdService = null;
|
||||||
plejdDevices = {};
|
plejdDevices = {};
|
||||||
pingRef = null;
|
pingRef = null;
|
||||||
writeQueue = {};
|
writeQueue = [];
|
||||||
writeQueueRef = null;
|
writeQueueRef = null;
|
||||||
reconnectInProgress = false;
|
reconnectInProgress = false;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ class SceneManager extends EventEmitter {
|
||||||
this.scenes = {};
|
this.scenes = {};
|
||||||
scenes.forEach((scene) => {
|
scenes.forEach((scene) => {
|
||||||
const idx = this.deviceRegistry.apiSite.sceneIndex[scene.sceneId];
|
const idx = this.deviceRegistry.apiSite.sceneIndex[scene.sceneId];
|
||||||
this.scenes[scene.id] = new Scene(idx, scene, this.deviceRegistry.apiSite.sceneSteps);
|
this.scenes[idx] = new Scene(idx, scene, this.deviceRegistry.apiSite.sceneSteps);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue