From 4038c8c7bda828913f331dd6da27ae1f4fc9a6ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marti=CC=81n=20M?= Date: Thu, 22 Oct 2020 02:18:53 -0300 Subject: [PATCH] checkDeviceUpdate: use v2 API --- src/mixins/checkDeviceUpdate.js | 37 ++++++++++------------------- src/parsers/parseFirmwareUpdates.js | 8 +++---- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/mixins/checkDeviceUpdate.js b/src/mixins/checkDeviceUpdate.js index e88ecff..107fced 100644 --- a/src/mixins/checkDeviceUpdate.js +++ b/src/mixins/checkDeviceUpdate.js @@ -11,38 +11,25 @@ module.exports = { */ async checkDeviceUpdate(deviceId) { const device = await this.getDevice(deviceId); - - const error = _get(device, 'error', false); - - if (error) { - return device; - } - const deviceInfoList = parseFirmwareUpdates([device]); - const deviceInfoListError = _get(deviceInfoList, 'error', false); - - if (deviceInfoListError) { - return deviceInfoList; - } - const update = await this.makeRequest({ method: 'post', - url: this.getOtaUrl(), - uri: '/app', + uri: '/v2/device/ota/query', body: { deviceInfoList }, }); - const isUpdate = _get(update, 'upgradeInfoList.0.version', false); + const isUpdate = _get(update, 'otaInfoList.0.version', false); - if (!isUpdate) { - return { status: 'ok', msg: 'No update available' }; - } - - return { - status: 'ok', - msg: 'Update available', - version: isUpdate, - }; + return isUpdate + ? { + status: 'ok', + msg: 'Update available', + version: isUpdate, + } + : { + status: 'ok', + msg: 'No update available', + }; }, }; diff --git a/src/parsers/parseFirmwareUpdates.js b/src/parsers/parseFirmwareUpdates.js index 47e760c..79ff2b7 100644 --- a/src/parsers/parseFirmwareUpdates.js +++ b/src/parsers/parseFirmwareUpdates.js @@ -1,13 +1,13 @@ const { _get } = require('../helpers/utilities'); const errors = require('../data/errors'); -const parseFirmwareUpdates = devicesList => - devicesList.map(device => { - const model = _get(device, 'extra.extra.model', false); +const parseFirmwareUpdates = (devicesList) => + devicesList.map((device) => { + const model = _get(device, 'extra.model', false); const fwVersion = _get(device, 'params.fwVersion', false); if (!model || !fwVersion) { - return { error: 500, msg: errors.noFirmware }; + throw new Error(`${errors.noFirmware}`); } return { model, version: fwVersion, deviceid: device.deviceid };