Files
ewelink-api/main.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

112 lines
2.3 KiB
JavaScript

const {
APP_ID: DEFAULT_APP_ID,
APP_SECRET: DEFAULT_APP_SECRET,
} = require('./src/data/constants');
const mixins = require('./src/mixins');
const errors = require('./src/data/errors');
class eWeLink {
constructor(parameters = {}) {
const {
region = 'us',
email = null,
phoneNumber = null,
password = null,
at = null,
apiKey = null,
devicesCache = null,
arpTable = null,
APP_ID = DEFAULT_APP_ID,
APP_SECRET = DEFAULT_APP_SECRET,
} = parameters;
const check = this.checkLoginParameters({
region,
email,
phoneNumber,
password,
at,
apiKey,
devicesCache,
arpTable,
});
if (check === false) {
throw new Error(errors.invalidCredentials);
}
this.region = region;
this.phoneNumber = phoneNumber;
this.email = email;
this.password = password;
this.at = at;
this.apiKey = apiKey;
this.devicesCache = devicesCache;
this.arpTable = arpTable;
this.APP_ID = APP_ID;
this.APP_SECRET = APP_SECRET;
}
// eslint-disable-next-line class-methods-use-this
checkLoginParameters(params) {
const { email, phoneNumber, password, devicesCache, arpTable, at } = params;
if (email !== null && phoneNumber !== null) {
return false;
}
if (
(email !== null && password !== null) ||
(phoneNumber !== null && password !== null) ||
(devicesCache !== null && arpTable !== null) ||
at !== null
) {
return true;
}
return false;
}
/**
* 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.getDeviceIP(device);
return `http://${ip}:8081/zeroconf`;
}
}
Object.assign(eWeLink.prototype, mixins);
module.exports = eWeLink;