Skip to content

Commit

Permalink
getDevice: use v2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
skydiver committed Oct 21, 2020
1 parent 5d517ba commit c302c62
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/data/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 12 additions & 9 deletions src/mixins/getDevice.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { nonce, timestamp, _get } = require('../helpers/utilities');
const { _get } = require('../helpers/utilities');
const errors = require('../data/errors');

module.exports = {
Expand All @@ -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 }
]
},
});

Expand All @@ -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;
},
};

0 comments on commit c302c62

Please sign in to comment.