Log ota message to MQTT (#2936)

* Log ota message to MQTT

* Fix lint

* Amend MQTT log messages

* Fix typo

* Fix lint

* some changes.

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
This commit is contained in:
timstanley1985
2020-02-14 23:41:34 +01:00
committed by GitHub
co-authored by Koen Kanters
parent 97af26b340
commit b379830e36
+25 -10
View File
@@ -39,9 +39,10 @@ class OTAUpdate extends BaseExtension {
const device = this.zigbee.resolveEntity(message);
assert(device != null && device.type === 'device', 'Device not found or not a device');
if (!device.mapped || !device.mapped.ota) {
logger.error(`Device '${device.name}' does not support OTA updates`);
const message = `Device '${device.name}' does not support OTA updates`;
logger.error(message);
this.mqtt.log('ota_update', message, {status: `not_supported`, device: device.name});
return;
}
@@ -53,17 +54,25 @@ class OTAUpdate extends BaseExtension {
const type = topic.split('/')[3];
if (type === 'check') {
logger.info(`Checking if update available for '${device.name}'`);
const message = `Checking if update available for '${device.name}'`;
logger.info(message);
this.mqtt.log('ota_update', message, {status: `checking_if_available`, device: device.name});
try {
const available = await device.mapped.ota.isUpdateAvailable(device.device, logger);
logger.info(available ?
`Update available for '${device.name}'` : `No update available for '${device.name}'`,
);
const message=(available ?
`Update available for '${device.name}'` : `No update available for '${device.name}'`);
logger.info(message);
const meta = {status: available ? 'available' : 'not_available', device: device.name};
this.mqtt.log('ota_update', message, meta);
} catch (error) {
logger.error(`Failed to check if update available for '${device.name}' (${error.message})`);
const message = `Failed to check if update available for '${device.name}' (${error.message})`;
logger.error(message);
this.mqtt.log('ota_update', message, {status: `check_failed`, device: device.name});
}
} else { // type === 'update'
logger.info(`Updating '${device.name}' to latest firmware`);
const message = `Updating '${device.name}' to latest firmware`;
logger.info(message);
this.mqtt.log('ota_update', message, {status: `update_in_progress`, device: device.name});
try {
const onProgress = (progress, remaining) => {
let message = `Update of '${device.name}' at ${progress}%`;
@@ -72,15 +81,21 @@ class OTAUpdate extends BaseExtension {
}
logger.info(message);
this.mqtt.log('ota_update', message, {status: `update_progress`, device: device.name, progress});
};
const from_ = await this.readSoftwareBuildIDAndDateCode(device.device, false);
await device.mapped.ota.updateToLatest(device.device, logger, onProgress);
const to = await this.readSoftwareBuildIDAndDateCode(device.device, true);
const [fromS, toS] = [JSON.stringify(from_), JSON.stringify(to)];
logger.info(`Finished update of '${device.name}'` + (to ? `, from '${fromS}' to '${toS}'` : ``));
const message = `Finished update of '${device.name}'` + (to ? `, from '${fromS}' to '${toS}'` : ``);
logger.info(message);
const meta = {status: `update_succeeded`, device: device.name, from: from_, to};
this.mqtt.log('ota_update', message, meta);
} catch (error) {
logger.error(`Update of '${device.name}' failed (${error.message})`);
const message = `Update of '${device.name}' failed (${error.message})`;
logger.error(message);
this.mqtt.log('ota_update', message, {status: `update_failed`, device: device.name});
}
}