mirror of
https://github.com/skydiver/ewelink-api.git
synced 2025-12-24 06:28:30 +01:00
* Added arpTableSolver (#18) * Added arpTableSolver * fix package import * linting class * changed arp library * refactor arp class * using arpping fork * refactor arpTableSolver class * Added Zero Conf functionality (LAN mode) (#46) * added crypto-js * zeroconf helper functions * zeroconf update payload * new method to save devices cache file * class renamed * refactor Zeroconf class * return cached device if exists * moved method to get local ip address * fix mac addresses without leading zeroes * refactor Zeroconf class * using new zeroconf functionality * zeroconf working with single and multichannel devices * save device mixin enhancement * working on zeroconf test cases * catch errors on filesystem methods * zeroconf: added extra test cases * better error handling * zeroconf: 100% code coverage * removed deprecated login method * updates on credentials file * version bump * Docs for v2.0 (#52) * added v1 docs * added zeroconf docs * updated readme * docs updated * removed zeroconf article warning * updated vscode config Co-authored-by: Luis Llamas <luisllamas@hotmail.com>
173 lines
4.2 KiB
JavaScript
173 lines
4.2 KiB
JavaScript
const rp = require('request-promise');
|
|
|
|
const { _get } = require('./lib/helpers');
|
|
|
|
class eWeLink {
|
|
constructor({
|
|
region = 'us',
|
|
email,
|
|
password,
|
|
at,
|
|
apiKey,
|
|
devicesCache,
|
|
arpTable,
|
|
}) {
|
|
if (!devicesCache && !arpTable && !at && (!email && !password)) {
|
|
return { error: 'No credentials provided' };
|
|
}
|
|
|
|
this.region = region;
|
|
this.email = email;
|
|
this.password = password;
|
|
this.at = at;
|
|
this.apiKey = apiKey;
|
|
this.devicesCache = devicesCache;
|
|
this.arpTable = arpTable;
|
|
}
|
|
|
|
/**
|
|
* Generate eWeLink API URL
|
|
*
|
|
* @returns {string}
|
|
*/
|
|
getApiUrl() {
|
|
return `https://${this.region}-api.coolkit.cc:8080/api`;
|
|
}
|
|
|
|
/**
|
|
* Generate eWeLink OTA API URL
|
|
* @returns {string}
|
|
*/
|
|
getOtaUrl() {
|
|
return `https://${this.region}-ota.coolkit.cc:8080/otaother`;
|
|
}
|
|
|
|
/**
|
|
* Generate eWeLink WebSocket URL
|
|
*
|
|
* @returns {string}
|
|
*/
|
|
getApiWebSocket() {
|
|
return `wss://${this.region}-pconnect3.coolkit.cc:8080/api/ws`;
|
|
}
|
|
|
|
/**
|
|
* Generate Zeroconf URL
|
|
* @param device
|
|
* @returns {string}
|
|
*/
|
|
getZeroconfUrl(device) {
|
|
const ip = this.getLocalIp(device);
|
|
return `http://${ip}:8081/zeroconf`;
|
|
}
|
|
|
|
/**
|
|
* Generate http requests helpers
|
|
*
|
|
* @param method
|
|
* @param url
|
|
* @param uri
|
|
* @param body
|
|
* @param qs
|
|
*
|
|
* @returns {Promise<{msg: string, error: *}>}
|
|
*/
|
|
async makeRequest({ method = 'GET', url, uri, body = {}, qs = {} }) {
|
|
const { at } = this;
|
|
|
|
if (!at) {
|
|
await this.getCredentials();
|
|
}
|
|
|
|
let apiUrl = this.getApiUrl();
|
|
|
|
if (url) {
|
|
apiUrl = url;
|
|
}
|
|
|
|
const response = await rp({
|
|
method,
|
|
uri: `${apiUrl}${uri}`,
|
|
headers: { Authorization: `Bearer ${this.at}` },
|
|
body,
|
|
qs,
|
|
json: true,
|
|
});
|
|
|
|
const error = _get(response, 'error', false);
|
|
if (error && [401, 402].indexOf(parseInt(error)) !== -1) {
|
|
return { error, msg: 'Authentication error' };
|
|
}
|
|
|
|
return response;
|
|
}
|
|
}
|
|
|
|
/* LOAD MIXINS: user */
|
|
const getCredentialsMixin = require('./mixins/user/getCredentialsMixin');
|
|
const getRegionMixin = require('./mixins/user/getRegionMixin');
|
|
|
|
/* LOAD MIXINS: power state */
|
|
const getDevicePowerStateMixin = require('./mixins/powerState/getDevicePowerStateMixin');
|
|
const setDevicePowerState = require('./mixins/powerState/setDevicePowerStateMixin');
|
|
const toggleDeviceMixin = require('./mixins/powerState/toggleDeviceMixin');
|
|
|
|
/* LOAD MIXINS: power usage */
|
|
const getDevicePowerUsageMixin = require('./mixins/powerUsage/getDevicePowerUsageMixin');
|
|
const getDeviceRawPowerUsageMixin = require('./mixins/powerUsage/getDeviceRawPowerUsageMixin');
|
|
|
|
/* LOAD MIXINS: temperature & humidity */
|
|
const getTHMixin = require('./mixins/temphumd/getTHMixin');
|
|
|
|
/* LOAD MIXINS: devices */
|
|
const getDevicesMixin = require('./mixins/devices/getDevicesMixin');
|
|
const getDeviceMixin = require('./mixins/devices/getDeviceMixin');
|
|
const getDeviceChannelCountMixin = require('./mixins/devices/getDeviceChannelCountMixin');
|
|
const getLocalIpMixin = require('./mixins/devices/getLocalIpMixin');
|
|
const saveDevicesCacheMixin = require('./mixins/devices/saveDevicesCacheMixin');
|
|
|
|
/* LOAD MIXINS: firmware */
|
|
const getFirmwareVersionMixin = require('./mixins/firmware/getFirmwareVersionMixin');
|
|
const checkDeviceUpdateMixin = require('./mixins/firmware/checkDeviceUpdateMixin');
|
|
const checkDevicesUpdatesMixin = require('./mixins/firmware/checkDevicesUpdatesMixin');
|
|
|
|
/* LOAD MIXINS: websocket */
|
|
const openWebSocketMixin = require('./mixins/websocket/openWebSocketMixin');
|
|
|
|
Object.assign(eWeLink.prototype, getCredentialsMixin, getRegionMixin);
|
|
|
|
Object.assign(
|
|
eWeLink.prototype,
|
|
getDevicePowerStateMixin,
|
|
setDevicePowerState,
|
|
toggleDeviceMixin
|
|
);
|
|
|
|
Object.assign(
|
|
eWeLink.prototype,
|
|
getDevicePowerUsageMixin,
|
|
getDeviceRawPowerUsageMixin
|
|
);
|
|
|
|
Object.assign(eWeLink.prototype, getTHMixin);
|
|
|
|
Object.assign(
|
|
eWeLink.prototype,
|
|
getDevicesMixin,
|
|
getDeviceMixin,
|
|
getDeviceChannelCountMixin,
|
|
getLocalIpMixin,
|
|
saveDevicesCacheMixin
|
|
);
|
|
|
|
Object.assign(
|
|
eWeLink.prototype,
|
|
getFirmwareVersionMixin,
|
|
checkDeviceUpdateMixin,
|
|
checkDevicesUpdatesMixin
|
|
);
|
|
|
|
Object.assign(eWeLink.prototype, openWebSocketMixin);
|
|
|
|
module.exports = eWeLink;
|