mirror of
https://github.com/skydiver/ewelink-api.git
synced 2025-12-21 21:33:11 +01:00
* set APP_ID and APP_SECRET from main class * add APP_ID and APP_SECRET as class constructor parameters * updated test case * updated test case * added new test case * docs updated * Release v3.1.0 - "setWSDevicePowerState" (#96) * new mixing to control devices using websocket * switch status on single channel devices * working on deviceControl mixin * better error handling * working on fix for shared devices * refactor/cleanup * added helper function * added docs for new method * return device new status * added test cases * properly close websocket connection and clean used properties * added test cases * error detection enhancements * added test cases * error detection enhancements * added new test file to jest setup * method renamed * fix for closing websocket connection * new getWSDevicePowerState method * added test cases * re-arrange tests * added new test cases * extract helpers methods * added test case * close WebSocket connection on auth error * updated docs * updated dependencies * fix for "forbidden" error * updated dependencies
48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
const { _get } = require('../helpers/utilities');
|
|
const errors = require('../data/errors');
|
|
|
|
const deviceStatusPayload = require('../payloads/deviceStatus');
|
|
|
|
module.exports = {
|
|
/**
|
|
* Get current power state for a specific device
|
|
*
|
|
* @param deviceId
|
|
* @param channel
|
|
*
|
|
* @returns {Promise<{state: *, status: string}|{msg: string, error: *}>}
|
|
*/
|
|
async getDevicePowerState(deviceId, channel = 1) {
|
|
const status = await this.makeRequest({
|
|
uri: '/user/device/status',
|
|
qs: deviceStatusPayload({
|
|
appid: this.APP_ID,
|
|
deviceId,
|
|
params: 'switch|switches',
|
|
}),
|
|
});
|
|
|
|
const error = _get(status, 'error', false);
|
|
|
|
if (error) {
|
|
const err = error === 400 ? 404 : error;
|
|
return { error: err, msg: errors[err] };
|
|
}
|
|
|
|
let state = _get(status, 'params.switch', false);
|
|
const switches = _get(status, 'params.switches', false);
|
|
|
|
const switchesAmount = switches ? switches.length : 1;
|
|
|
|
if (switchesAmount > 0 && switchesAmount < channel) {
|
|
return { error: 404, msg: errors.ch404 };
|
|
}
|
|
|
|
if (switches) {
|
|
state = switches[channel - 1].switch;
|
|
}
|
|
|
|
return { status: 'ok', state, channel };
|
|
},
|
|
};
|