This commit is contained in:
Koen Kanters
2020-06-13 23:46:04 +02:00
parent 3658588618
commit 7f8fbe8b65
+14 -6
View File
@@ -37,7 +37,12 @@ class Bridge extends Extension {
async onMQTTMessage(topic, message) { async onMQTTMessage(topic, message) {
const match = topic.match(requestRegex); const match = topic.match(requestRegex);
if (match && this.requestLookup[match[1].toLowerCase()]) { if (match && this.requestLookup[match[1].toLowerCase()]) {
try {message = JSON.parse(message);} catch {} try {
message = JSON.parse(message);
} catch (e) {
e;
}
try { try {
const response = await this.requestLookup[match[1].toLowerCase()](message); const response = await this.requestLookup[match[1].toLowerCase()](message);
await this.mqtt.publish(`bridge/response/${match[1]}`, JSON.stringify(response)); await this.mqtt.publish(`bridge/response/${match[1]}`, JSON.stringify(response));
@@ -142,7 +147,7 @@ class Bridge extends Extension {
const cleanup = (o) => { const cleanup = (o) => {
delete o.friendlyName; delete o.friendly_name; delete o.ID; delete o.type; delete o.devices; delete o.friendlyName; delete o.friendly_name; delete o.ID; delete o.type; delete o.devices;
return o; return o;
} };
const oldOptions = cleanup(entity.settings); const oldOptions = cleanup(entity.settings);
const newOptions = cleanup(settings.getEntity(ID)); const newOptions = cleanup(settings.getEntity(ID));
return utils.getResponse(message, {from: oldOptions, to: newOptions, ID}, null); return utils.getResponse(message, {from: oldOptions, to: newOptions, ID}, null);
@@ -150,12 +155,13 @@ class Bridge extends Extension {
renameEntity(entityType, message) { renameEntity(entityType, message) {
const deviceAndHasLast = entityType === 'device' && typeof message === 'object' && message.last === true; const deviceAndHasLast = entityType === 'device' && typeof message === 'object' && message.last === true;
if (typeof message !== 'object' || (!message.hasOwnProperty('from') && !deviceAndHasLast) || !message.hasOwnProperty('to')) { if (typeof message !== 'object' || (!message.hasOwnProperty('from') && !deviceAndHasLast) ||
!message.hasOwnProperty('to')) {
throw new Error(`Invalid payload`); throw new Error(`Invalid payload`);
} }
if (deviceAndHasLast && !this.lastJoinedDeviceIeeeAddr) { if (deviceAndHasLast && !this.lastJoinedDeviceIeeeAddr) {
throw new Error('No device has joined since start') throw new Error('No device has joined since start');
} }
const from = deviceAndHasLast ? this.lastJoinedDeviceIeeeAddr : message.from; const from = deviceAndHasLast ? this.lastJoinedDeviceIeeeAddr : message.from;
@@ -197,7 +203,7 @@ class Bridge extends Extension {
} }
if (force) { if (force) {
await entity.device.removeFromDatabase() await entity.device.removeFromDatabase();
} else { } else {
await entity.device.removeFromNetwork(); await entity.device.removeFromNetwork();
} }
@@ -230,7 +236,9 @@ class Bridge extends Extension {
return utils.getResponse(message, {ID}, null); return utils.getResponse(message, {ID}, null);
} }
} catch (error) { } catch (error) {
throw new Error(`Failed to remove ${entity.type} '${entity.settings.friendlyName}'${banForceLog} (${error})`); throw new Error(
`Failed to remove ${entity.type} '${entity.settings.friendlyName}'${banForceLog} (${error})`,
);
} }
} }