Files
ewelink-api/src/classes/DevicePowerUsageRaw.js
Martin M b87d092a71 Release v3.1.0 (#93)
* 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
2020-10-12 19:01:57 -03:00

59 lines
1.4 KiB
JavaScript

const WebSocket = require('./WebSocket');
const wssLoginPayload = require('../payloads/wssLoginPayload');
const wssUpdatePayload = require('../payloads/wssUpdatePayload');
const { _get } = require('../helpers/utilities');
const errors = require('../data/errors');
class DevicePowerUsageRaw extends WebSocket {
/**
* Get specific device power usage (raw data)
*
* @param apiUrl
* @param at
* @param apiKey
* @param deviceId
* @returns {Promise<{error: string}|{data: {hundredDaysKwhData: *}, status: string}|{msg: any, error: *}|{msg: string, error: number}>}
*/
static async get({ apiUrl, at, apiKey, deviceId }) {
const payloadLogin = wssLoginPayload({ at, apiKey, appid: this.APP_ID });
const payloadUpdate = wssUpdatePayload({
apiKey,
deviceId,
params: { hundredDaysKwh: 'get' },
});
const response = await this.WebSocketRequest(apiUrl, [
payloadLogin,
payloadUpdate,
]);
if (response.length === 1) {
return { error: errors.noPower };
}
const error = _get(response[1], 'error', false);
if (error === 403) {
return { error, msg: response[1].reason };
}
const hundredDaysKwhData = _get(
response[1],
'config.hundredDaysKwhData',
false
);
if (!hundredDaysKwhData) {
return { error: errors.noPower };
}
return {
status: 'ok',
data: { hundredDaysKwhData },
};
}
}
module.exports = DevicePowerUsageRaw;