From b379830e36b96f24b5f702491d415fe109d7d90e Mon Sep 17 00:00:00 2001 From: timstanley1985 <6829542+timstanley1985@users.noreply.github.com> Date: Fri, 14 Feb 2020 22:41:34 +0000 Subject: [PATCH] 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 --- lib/extension/otaUpdate.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/extension/otaUpdate.js b/lib/extension/otaUpdate.js index c633b8248..975391d33 100644 --- a/lib/extension/otaUpdate.js +++ b/lib/extension/otaUpdate.js @@ -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}); } }