checkDevicesUpdates: use v2 API

This commit is contained in:
Martín M
2020-10-22 02:44:07 -03:00
parent 4038c8c7bd
commit 646dfed03c
4 changed files with 27 additions and 31 deletions

5
package-lock.json generated
View File

@@ -2036,6 +2036,11 @@
"delayed-stream": "~1.0.0"
}
},
"compare-versions": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/compare-versions/-/compare-versions-3.6.0.tgz",
"integrity": "sha512-W6Af2Iw1z4CB7q4uU4hv646dW9GQuBM+YpC0UvUCWSD8w90SJjp+ujJuXaEMtAXBtSqGfMPuFOVn4/+FlaqfBA=="
},
"component-emitter": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz",

View File

@@ -41,6 +41,7 @@
},
"dependencies": {
"arpping": "github:skydiver/arpping",
"compare-versions": "^3.6.0",
"crypto-js": "^4.0.0",
"delay": "^4.4.0",
"node-fetch": "^2.6.1",

View File

@@ -5,7 +5,7 @@ const errors = {
403: 'Forbidden',
404: 'Device does not exist',
406: 'Authentication failed',
503: 'Service Temporarily Unavailable or Device is offline'
503: 'Service Temporarily Unavailable or Device is offline',
};
const customErrors = {
@@ -16,6 +16,7 @@ const customErrors = {
noPower: 'No power usage data found',
noSensor: "Can't read sensor data from device",
noFirmware: "Can't get model or firmware version",
noFirmwares: "Can't find firmware update information",
invalidAuth: 'Library needs to be initialized using email and password',
invalidCredentials: 'Invalid credentials provided',
invalidPowerState: 'Invalid power state. Expecting: "on", "off" or "toggle"',

View File

@@ -1,53 +1,42 @@
const { _get } = require('../helpers/utilities');
const compareVersions = require('compare-versions');
const parseFirmwareUpdates = require('../parsers/parseFirmwareUpdates');
const errors = require('../data/errors');
module.exports = {
async checkDevicesUpdates() {
const devices = await this.getDevices();
const error = _get(devices, 'error', false);
if (error) {
return devices;
}
const deviceInfoList = parseFirmwareUpdates(devices);
const deviceInfoListError = _get(deviceInfoList, 'error', false);
if (deviceInfoListError) {
return deviceInfoList;
}
const updates = await this.makeRequest({
method: 'post',
url: this.getOtaUrl(),
uri: '/app',
uri: '/v2/device/ota/query',
body: { deviceInfoList },
});
const upgradeInfoList = _get(updates, 'upgradeInfoList', false);
const { otaInfoList } = updates;
if (!upgradeInfoList) {
return { error: "Can't find firmware update information" };
if (!otaInfoList) {
throw new Error(`${errors.noFirmwares}`);
}
return upgradeInfoList.map(device => {
const upd = _get(device, 'version', false);
/** Get current versions */
const currentVersions = {};
deviceInfoList.forEach((device) => {
currentVersions[device.deviceid] = device.version;
});
if (!upd) {
return {
status: 'ok',
deviceId: device.deviceid,
msg: 'No update available',
};
}
return otaInfoList.map((device) => {
const current = currentVersions[device.deviceid];
const { version } = device;
const outdated = compareVersions(version, current);
return {
status: 'ok',
update: !!outdated,
deviceId: device.deviceid,
msg: 'Update available',
version: upd,
msg: outdated ? 'Update available' : 'No update available',
current,
version,
};
});
},