Release v3.0.0 (#85)

* updated dependencies

* code linting

* added new app id & app secret

* cleanup requests payloads

* remove unused function

* update test cases

* enabled firmware tests

* refactor getDevice to use right api endpoint

* error messages improvements

* error messages improvements

* error messages improvements

* error messages improvements

* error messages improvements

* payload cleanup

* refactor setDevicePowerState to use right api endpoint

* update test exepectation

* removed deprecated class

* updated tests to reflect new error codes

* error messages improvements

* refactoring project structure: devices methods

refactoring project sturcture

* refactoring project structure: firmware methods

* refactoring project structure: temperature/humidity

* refactoring project structure: credentials methods

* refactoring project structure: power usage methods

* refactoring project structure: power state methods

* refactoring project structure: websocket methods

* removed deprecated login method from docs

* refactoring project structure: power usage methods

* refactoring project structure: zeroconf classes

* refactoring project structure: websocket classes

* refactoring project structure: zeroconf classes

* refactor and cleanup

* refactoring project structure: firmware methods

* moved parsers to own directory

* update tests with methods renames

* export missing temperature/humidity methods

* removed unused package

* refactor and cleanup

* fix test expectation

* refactoring project structure: moved data files

* refactoring project structure: moved data files

* refactoring project structure: moved helpers files

* refactoring project structure: moved helpers files

* refactoring project structure: moved payload files

* refactor and cleanup

* refactor getDevicePowerState

* setDevicePowerState returns channel

* convert error 400 to 404 for clarity

* updated test cases

* remove console.log

* cache path for zeroconf cache files

* installed nock

* using nock to simulate server requests during testing

* moved credentials file to config folder

* update request url when using nock

* refactor nock helper file

* move cooldown delay to setupTests file

* updating testing instructions

* restored delete code block

* fix wrong error code

* accept phone number to login to ewelink

* added test cases for initialize main class

* improvements on class initialization parameters

* allow login using phone number

* rename test file

* updated test case

* fixed regression bug

* Release v3.0.0 - use node-fetch (#87)

* replaced deprecated request library with node-fetch

* refactor: moved makeRequest to own mixin file

* refactor to use node-fetch

* fixes

* update config

* created helper method

* constant rename

* ignore files from final package

* version bump
This commit is contained in:
Martin M
2020-05-23 03:07:52 -03:00
committed by GitHub
parent 04ba4a1bb3
commit c11b3a8ab7
79 changed files with 5803 additions and 3045 deletions

View File

@@ -0,0 +1,34 @@
const fetch = require('node-fetch');
const WebSocket = require('./WebSocket');
const zeroConfUpdatePayload = require('../payloads/zeroConfUpdatePayload');
const { _get } = require('../helpers/utilities');
class ChangeStateZeroconf extends WebSocket {
static async set({ url, device, params, switches, state }) {
const selfApikey = device.apikey;
const deviceId = device.deviceid;
const deviceKey = device.devicekey;
const endpoint = switches ? 'switches' : 'switch';
const body = zeroConfUpdatePayload(selfApikey, deviceId, deviceKey, params);
const request = await fetch(`${url}/${endpoint}`, {
method: 'post',
body: JSON.stringify(body),
});
const response = await request.json();
const error = _get(response, 'error', false);
if (error === 403) {
return { error, msg: response.reason };
}
return { status: 'ok', state };
}
}
module.exports = ChangeStateZeroconf;

View File

@@ -0,0 +1,58 @@
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 });
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;

57
src/classes/WebSocket.js Normal file
View File

@@ -0,0 +1,57 @@
const W3CWebSocket = require('websocket').w3cwebsocket;
const WebSocketAsPromised = require('websocket-as-promised');
const delay = require('delay');
const errors = require('../data/errors');
class WebSocket {
/**
* Open WebSocket connection and send provided payloads
*
* @param url
* @param payloads
* @param delayTime
*
* @returns {Array}
*/
static async WebSocketRequest(url, payloads, ...{ delayTime = 1000 }) {
const wsp = new WebSocketAsPromised(url, {
createWebSocket: wss => new W3CWebSocket(wss),
});
const responses = [];
wsp.onMessage.addListener(message => responses.push(JSON.parse(message)));
try {
await wsp.open();
for (const payload of payloads) {
await wsp.send(payload);
await delay(delayTime);
}
await wsp.close();
} catch (e) {
return this.customThrowError(e);
}
return responses;
}
/**
* Parse WebSocket errors and return user friendly messages
*
* @param e
*
* @returns {{error: string}|{msg: string, error: number}}
*/
static customThrowError(e) {
const loginError = e.message.indexOf('WebSocket is not opened');
if (loginError > -1) {
return { error: 406, msg: errors['406'] };
}
return { error: errors.unknown };
}
}
module.exports = WebSocket;

91
src/classes/Zeroconf.js Normal file
View File

@@ -0,0 +1,91 @@
const fs = require('fs');
const arpping = require('arpping')({});
class Zeroconf {
/**
* Build the ARP table
* @param ip
* @returns {Promise<unknown>}
*/
static getArpTable(ip = null) {
return new Promise((resolve, reject) => {
arpping.discover(ip, (err, hosts) => {
if (err) {
return reject(err);
}
const arpTable = Zeroconf.fixMacAddresses(hosts);
return resolve(arpTable);
});
});
}
/**
* Sometime arp command returns mac addresses without leading zeroes.
* @param hosts
*/
static fixMacAddresses(hosts) {
return hosts.map(host => {
const octets = host.mac.split(':');
const fixedMac = octets.map(octet => {
if (octet.length === 1) {
return `0${octet}`;
}
return octet;
});
return {
ip: host.ip,
mac: fixedMac.join(':'),
};
});
}
/**
* Save ARP table to local file
* @param config
* @returns {Promise<{error: string}|{file: {request: string; resolved: string} | any | string | string, status: string}>}
*/
static async saveArpTable(config = {}) {
const ip = config.ip || null;
const fileName = config.file || './arp-table.json';
try {
const arpTable = await Zeroconf.getArpTable(ip);
const jsonContent = JSON.stringify(arpTable, null, 2);
fs.writeFileSync(fileName, jsonContent, 'utf8');
return { status: 'ok', file: fileName };
} catch (e) {
return { error: e.toString() };
}
}
/**
* Read ARP table file
* @param fileName
* @returns {Promise<{error: string}|any>}
*/
static async loadArpTable(fileName = './arp-table.json') {
try {
const jsonContent = await fs.readFileSync(fileName);
return JSON.parse(jsonContent);
} catch (e) {
return { error: e.toString() };
}
}
/**
* Read devices cache file
* @param fileName
* @returns {Promise<{error: string}>}
*/
static async loadCachedDevices(fileName = './devices-cache.json') {
try {
const jsonContent = await fs.readFileSync(fileName);
return JSON.parse(jsonContent);
} catch (e) {
return { error: e.toString() };
}
}
}
module.exports = Zeroconf;