Improved error handling in event handling code
- Earlier errors would restart the addon
This commit is contained in:
parent
35cbf9a12a
commit
5562c2d838
3 changed files with 75 additions and 42 deletions
|
|
@ -106,7 +106,15 @@ class MqttClient extends EventEmitter {
|
||||||
} else if (topic.includes('set')) {
|
} else if (topic.includes('set')) {
|
||||||
logger.verbose(`Got mqtt command on ${topic} - ${message}`);
|
logger.verbose(`Got mqtt command on ${topic} - ${message}`);
|
||||||
const device = self.devices.find((x) => getCommandTopic(x) === topic);
|
const device = self.devices.find((x) => getCommandTopic(x) === topic);
|
||||||
self.emit('stateChanged', device, command);
|
if (device) {
|
||||||
|
self.emit('stateChanged', device, command);
|
||||||
|
} else {
|
||||||
|
logger.warn(
|
||||||
|
`Device for topic ${topic} not found! Can happen if HA calls previously existing devices.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else if (topic.includes('state')) {
|
||||||
|
logger.verbose(`State update sent over mqtt to HA ${topic} - ${message}`);
|
||||||
} else {
|
} else {
|
||||||
logger.verbose(`Warning: Got unrecognized mqtt command on ${topic} - ${message}`);
|
logger.verbose(`Warning: Got unrecognized mqtt command on ${topic} - ${message}`);
|
||||||
}
|
}
|
||||||
|
|
@ -145,7 +153,11 @@ class MqttClient extends EventEmitter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.verbose(`Updating state for ${device.name}: ${data.state}`);
|
logger.verbose(
|
||||||
|
`Updating state for ${device.name}: ${data.state}${
|
||||||
|
data.brightness ? `, dim: ${data.brightness}` : ''
|
||||||
|
}`,
|
||||||
|
);
|
||||||
let payload = null;
|
let payload = null;
|
||||||
|
|
||||||
if (device.type === 'switch') {
|
if (device.type === 'switch') {
|
||||||
|
|
|
||||||
|
|
@ -253,9 +253,9 @@ class PlejdService extends EventEmitter {
|
||||||
turnOff(deviceId, command) {
|
turnOff(deviceId, command) {
|
||||||
const deviceName = this._getDeviceName(deviceId);
|
const deviceName = this._getDeviceName(deviceId);
|
||||||
logger.info(
|
logger.info(
|
||||||
`Plejd got turn off command for ${deviceName} (${deviceId}), brightness ${
|
`Plejd got turn off command for ${deviceName} (${deviceId})${
|
||||||
command.brightness
|
command.transition ? `, transition: ${command.transition}` : ''
|
||||||
}${command.transition ? `, transition: ${command.transition}` : ''}`,
|
}`,
|
||||||
);
|
);
|
||||||
this._transitionTo(deviceId, 0, command.transition, deviceName);
|
this._transitionTo(deviceId, 0, command.transition, deviceName);
|
||||||
}
|
}
|
||||||
|
|
@ -447,7 +447,7 @@ class PlejdService extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.verbose(`Sending ${data.length} byte(s) of data to Plejd`, data);
|
logger.verbose(`Sending ${data.length} byte(s) of data to Plejd. ${data.toString('hex')}`);
|
||||||
const encryptedData = this._encryptDecrypt(this.cryptoKey, this.plejdService.addr, data);
|
const encryptedData = this._encryptDecrypt(this.cryptoKey, this.plejdService.addr, data);
|
||||||
await this.characteristics.data.WriteValue([...encryptedData], {});
|
await this.characteristics.data.WriteValue([...encryptedData], {});
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -576,7 +576,10 @@ class PlejdService extends EventEmitter {
|
||||||
const regex = /dev_([0-9A-F_]+)$/;
|
const regex = /dev_([0-9A-F_]+)$/;
|
||||||
const dirtyAddr = regex.exec(dev);
|
const dirtyAddr = regex.exec(dev);
|
||||||
const addr = this._reverseBuffer(
|
const addr = this._reverseBuffer(
|
||||||
Buffer.from(String(dirtyAddr[1]).replace(/-/g, '').replace(/_/g, '').replace(/:/g, ''), 'hex'),
|
Buffer.from(
|
||||||
|
String(dirtyAddr[1]).replace(/-/g, '').replace(/_/g, '').replace(/:/g, ''),
|
||||||
|
'hex',
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
// eslint-disable-next-line no-restricted-syntax
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
|
|
|
||||||
|
|
@ -36,8 +36,12 @@ async function main() {
|
||||||
const devices = plejdApi.getDevices();
|
const devices = plejdApi.getDevices();
|
||||||
|
|
||||||
client.on('connected', () => {
|
client.on('connected', () => {
|
||||||
logger.verbose('connected to mqtt.');
|
try {
|
||||||
client.discover(devices);
|
logger.verbose('connected to mqtt.');
|
||||||
|
client.discover(devices);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('Error in MqttClient.connected callback in main.js', err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.init();
|
client.init();
|
||||||
|
|
@ -54,7 +58,9 @@ async function main() {
|
||||||
plejd.on('connectFailed', () => {
|
plejd.on('connectFailed', () => {
|
||||||
logger.verbose('Were unable to connect, will retry connection in 10 seconds.');
|
logger.verbose('Were unable to connect, will retry connection in 10 seconds.');
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
plejd.init();
|
plejd
|
||||||
|
.init()
|
||||||
|
.catch((e) => logger.error('Error in init() from connectFailed in main.js', e));
|
||||||
}, 10000);
|
}, 10000);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -66,50 +72,62 @@ async function main() {
|
||||||
|
|
||||||
// subscribe to changes from Plejd
|
// subscribe to changes from Plejd
|
||||||
plejd.on('stateChanged', (deviceId, command) => {
|
plejd.on('stateChanged', (deviceId, command) => {
|
||||||
client.updateState(deviceId, command);
|
try {
|
||||||
|
client.updateState(deviceId, command);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('Error in PlejdService.stateChanged callback in main.js', err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
plejd.on('sceneTriggered', (deviceId, scene) => {
|
plejd.on('sceneTriggered', (deviceId, scene) => {
|
||||||
client.sceneTriggered(scene);
|
try {
|
||||||
|
client.sceneTriggered(scene);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('Error in PlejdService.sceneTriggered callback in main.js', err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// subscribe to changes from HA
|
// subscribe to changes from HA
|
||||||
client.on('stateChanged', (device, command) => {
|
client.on('stateChanged', (device, command) => {
|
||||||
const deviceId = device.id;
|
try {
|
||||||
|
const deviceId = device.id;
|
||||||
|
|
||||||
if (device.typeName === 'Scene') {
|
if (device.typeName === 'Scene') {
|
||||||
// we're triggering a scene, lets do that and jump out.
|
// we're triggering a scene, lets do that and jump out.
|
||||||
// since scenes aren't "real" devices.
|
// since scenes aren't "real" devices.
|
||||||
plejd.triggerScene(device.id);
|
plejd.triggerScene(device.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let state = 'OFF';
|
let state = 'OFF';
|
||||||
let commandObj = {};
|
let commandObj = {};
|
||||||
|
|
||||||
if (typeof command === 'string') {
|
if (typeof command === 'string') {
|
||||||
// switch command
|
// switch command
|
||||||
state = command;
|
state = command;
|
||||||
commandObj = {
|
commandObj = {
|
||||||
state,
|
state,
|
||||||
};
|
};
|
||||||
|
|
||||||
// since the switch doesn't get any updates on whether it's on or not,
|
// since the switch doesn't get any updates on whether it's on or not,
|
||||||
// we fake this by directly send the updateState back to HA in order for
|
// we fake this by directly send the updateState back to HA in order for
|
||||||
// it to change state.
|
// it to change state.
|
||||||
client.updateState(deviceId, {
|
client.updateState(deviceId, {
|
||||||
state: state === 'ON' ? 1 : 0,
|
state: state === 'ON' ? 1 : 0,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// eslint-disable-next-line prefer-destructuring
|
// eslint-disable-next-line prefer-destructuring
|
||||||
state = command.state;
|
state = command.state;
|
||||||
commandObj = command;
|
commandObj = command;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state === 'ON') {
|
if (state === 'ON') {
|
||||||
plejd.turnOn(deviceId, commandObj);
|
plejd.turnOn(deviceId, commandObj);
|
||||||
} else {
|
} else {
|
||||||
plejd.turnOff(deviceId, commandObj);
|
plejd.turnOff(deviceId, commandObj);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('Error in MqttClient.stateChanged callback in main.js', err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue