mirror of
https://github.com/skydiver/ewelink-api.git
synced 2025-12-26 07:13:36 +01:00
checkDevicesUpdates: use v2 API
This commit is contained in:
5
package-lock.json
generated
5
package-lock.json
generated
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"',
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user