setDevicePowerState: use v2 API

This commit is contained in:
Martín M
2020-10-21 21:00:23 -03:00
parent 2a64768822
commit ac80e06b0f

View File

@@ -1,10 +1,6 @@
const { _get, timestamp, nonce } = require('../helpers/utilities');
const { _get } = require('../helpers/utilities');
const errors = require('../data/errors');
const { getDeviceChannelCount } = require('../helpers/ewelink');
const ChangeStateZeroconf = require('../classes/ChangeStateZeroconf');
module.exports = {
/**
* Change power state for a specific device
@@ -17,20 +13,20 @@ module.exports = {
*/
async setDevicePowerState(deviceId, state, channel = 1) {
const device = await this.getDevice(deviceId);
/** Check for errors */
const error = _get(device, 'error', false);
const uiid = _get(device, 'extra.extra.uiid', false);
if (error) {
throw new Error(`[${error}] ${errors[error]}`);
}
let status = _get(device, 'params.switch', false);
const switches = _get(device, 'params.switches', false);
const switchesAmount = getDeviceChannelCount(uiid);
const switchesAmount = switches.length;
if (switchesAmount > 0 && switchesAmount < channel) {
return { error: 404, msg: errors.ch404 };
}
if (error || (!status && !switches)) {
return { error, msg: errors[error] };
throw new Error(`${errors.ch404}`);
}
let stateToSwitch = state;
@@ -51,35 +47,31 @@ module.exports = {
params.switch = stateToSwitch;
}
if (this.devicesCache) {
return ChangeStateZeroconf.set({
url: this.getZeroconfUrl(device),
device,
params,
switches,
state: stateToSwitch,
});
}
const { APP_ID } = this;
// DISABLED DURING v4.0.0 DEVELOPMENT
// if (this.devicesCache) {
// return ChangeStateZeroconf.set({
// url: this.getZeroconfUrl(device),
// device,
// params,
// switches,
// state: stateToSwitch,
// });
// }
const response = await this.makeRequest({
method: 'post',
uri: '/user/device/status',
uri: '/v2/device/thing/status',
body: {
deviceid: deviceId,
type: 1,
id: deviceId,
params,
appid: APP_ID,
nonce,
ts: timestamp,
version: 8,
},
});
const responseError = _get(response, 'error', false);
if (responseError) {
return { error: responseError, msg: errors[responseError] };
throw new Error(`[${error}] ${errors[error]}`);
}
return { status: 'ok', state, channel };