getDevice: use v2 API

This commit is contained in:
Martín M
2020-10-21 19:49:59 -03:00
parent 16772a5c54
commit 9c00d8280a
2 changed files with 13 additions and 9 deletions

View File

@@ -11,6 +11,7 @@ const errors = {
const customErrors = {
ch404: 'Device channel does not exist',
unknown: 'An unknown error occurred',
noDevice: 'No device found',
noDevices: 'No devices found',
noPower: 'No power usage data found',
noSensor: "Can't read sensor data from device",

View File

@@ -1,4 +1,4 @@
const { nonce, timestamp, _get } = require('../helpers/utilities');
const { _get } = require('../helpers/utilities');
const errors = require('../data/errors');
module.exports = {
@@ -16,13 +16,12 @@ module.exports = {
const { APP_ID } = this;
const device = await this.makeRequest({
uri: `/user/device/${deviceId}`,
qs: {
deviceid: deviceId,
appid: APP_ID,
nonce,
ts: timestamp,
version: 8,
method: 'post',
uri: `/v2/device/thing/`,
body: {
thingList: [
{ id: deviceId, itemType: 1 }
]
},
});
@@ -32,6 +31,10 @@ module.exports = {
return { error, msg: errors[error] };
}
return device;
if (device.thingList.length === 0) {
throw new Error(`${errors.noDevice}`);
}
return device.thingList.shift().itemData;
},
};