Skip to content

Commit

Permalink
change to API v2
Browse files Browse the repository at this point in the history
- login
- getDevice
- getDevices
- getDevicePowerState
- setDevicePowerState
- getDevicePowerUsage
  • Loading branch information
baugp committed Sep 12, 2023
1 parent 3c00519 commit 2f4d1b0
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 47 deletions.
4 changes: 3 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class eWeLink {
email = null,
phoneNumber = null,
password = null,
countryCode = '+1',
at = null,
apiKey = null,
devicesCache = null,
Expand Down Expand Up @@ -40,6 +41,7 @@ class eWeLink {
this.phoneNumber = phoneNumber;
this.email = email;
this.password = password;
this.countryCode = countryCode;
this.at = at;
this.apiKey = apiKey;
this.devicesCache = devicesCache;
Expand Down Expand Up @@ -75,7 +77,7 @@ class eWeLink {
* @returns {string}
*/
getApiUrl() {
return `https://${this.region}-api.coolkit.cc:8080/api`;
return `https://${this.region}-apia.coolkit.cc/v2`;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/data/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const APP_ID = 'YzfeftUVcZ6twZw1OoVKPRFYTrGEg01Q';
const APP_SECRET = '4G91qSoboqYO4Y0XJ0LPPKIsq8reHdfa';
const APP_ID = 'Uw83EKZFxdif7XFXEsrpduz5YyjP7nTl';
const APP_SECRET = 'mXLOjea0woSMvK9gw7Fjsy7YlFO4iSu6';

module.exports = {
APP_ID,
Expand Down
4 changes: 1 addition & 3 deletions src/helpers/utilities.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const nonce = Math.random()
.toString(36)
.slice(5);
const nonce = Math.random().toString(36).slice(-8);

const timestamp = Math.floor(new Date() / 1000);

Expand Down
18 changes: 10 additions & 8 deletions src/mixins/getCredentials.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fetch = require('node-fetch');

const { _get } = require('../helpers/utilities');
const { _get, nonce } = require('../helpers/utilities');
const credentialsPayload = require('../payloads/credentialsPayload');
const { makeAuthorizationSign } = require('../helpers/ewelink');
const errors = require('../data/errors');
Expand All @@ -14,25 +14,27 @@ module.exports = {
async getCredentials() {
const { APP_ID, APP_SECRET } = this;

const body = credentialsPayload({
appid: APP_ID,
const body = {
email: this.email,
phoneNumber: this.phoneNumber,
password: this.password,
});
countryCode: this.countryCode,
};

const request = await fetch(`${this.getApiUrl()}/user/login`, {
method: 'post',
headers: {
Authorization: `Sign ${makeAuthorizationSign(APP_SECRET, body)}`,
'X-CK-Appid': APP_ID,
'X-CK-Nonce': nonce,
'Content-Type': 'application/json'
},
body: JSON.stringify(body),
});

let response = await request.json();

const error = _get(response, 'error', false);
const region = _get(response, 'region', false);
const region = _get(response, 'data.region', false);

if (error && [400, 401, 404].indexOf(parseInt(error)) !== -1) {
return { error: 406, msg: errors['406'] };
Expand All @@ -47,8 +49,8 @@ module.exports = {
return { error, msg: 'Region does not exist' };
}

this.apiKey = _get(response, 'user.apikey', '');
this.at = _get(response, 'at', '');
this.apiKey = _get(response, 'data.user.apikey', '');
this.at = _get(response, 'data.at', '');
return response;
},
};
16 changes: 8 additions & 8 deletions src/mixins/getDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ module.exports = {
const { APP_ID } = this;

const device = await this.makeRequest({
uri: `/user/device/${deviceId}`,
qs: {
deviceid: deviceId,
appid: APP_ID,
nonce,
ts: timestamp,
version: 8,
method: 'post',
uri: `/device/thing`,
body: {
thingList: [{
itemType: 1,
id: deviceId
}]
},
});

Expand All @@ -32,6 +32,6 @@ module.exports = {
return { error, msg: errors[error] };
}

return device;
return device.data.thingList[0].itemData;
},
};
14 changes: 7 additions & 7 deletions src/mixins/getDevicePowerState.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ module.exports = {
*/
async getDevicePowerState(deviceId, channel = 1) {
const status = await this.makeRequest({
uri: '/user/device/status',
qs: deviceStatusPayload({
appid: this.APP_ID,
deviceId,
uri: '/device/thing/status',
qs: {
type: 1,
id: deviceId,
params: 'switch|switches',
}),
},
});

const error = _get(status, 'error', false);
Expand All @@ -29,8 +29,8 @@ module.exports = {
return { error: err, msg: errors[err] };
}

let state = _get(status, 'params.switch', false);
const switches = _get(status, 'params.switches', false);
let state = _get(status, 'data.params.switch', false);
const switches = _get(status, 'data.params.switches', false);

const switchesAmount = switches ? switches.length : 1;

Expand Down
11 changes: 2 additions & 9 deletions src/mixins/getDevices.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@ module.exports = {
const { APP_ID } = this;

const response = await this.makeRequest({
uri: '/user/device',
qs: {
lang: 'en',
appid: APP_ID,
ts: timestamp,
version: 8,
getTags: 1,
},
uri: '/device/thing',
});

const error = _get(response, 'error', false);
const devicelist = _get(response, 'devicelist', false);
const devicelist = _get(response, 'data.thingList', false);

if (error) {
return { error, msg: errors[error] };
Expand Down
3 changes: 2 additions & 1 deletion src/mixins/makeRequest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fetch = require('node-fetch');
const { _get, _empty, toQueryString } = require('../helpers/utilities');
const { _get, _empty, toQueryString, nonce } = require('../helpers/utilities');
const errors = require('../data/errors');

module.exports = {
Expand Down Expand Up @@ -31,6 +31,7 @@ module.exports = {
headers: {
Authorization: `Bearer ${this.at}`,
'Content-Type': 'application/json',
'X-CK-Nonce': nonce
},
};

Expand Down
13 changes: 5 additions & 8 deletions src/mixins/setDevicePowerState.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
async setDevicePowerState(deviceId, state, channel = 1, extraParams = {}) {
const device = await this.getDevice(deviceId);
const error = _get(device, 'error', false);
const uiid = _get(device, 'extra.extra.uiid', false);
const uiid = _get(device, 'extra.uiid', false);

let status = _get(device, 'params.switch', false);
const switches = _get(device, 'params.switches', false);
Expand Down Expand Up @@ -71,14 +71,11 @@ module.exports = {

const response = await this.makeRequest({
method: 'post',
uri: '/user/device/status',
uri: '/device/thing/status',
body: {
deviceid: deviceId,
params,
appid: APP_ID,
nonce,
ts: timestamp,
version: 8,
type: 1,
id: deviceId,
params: params,
},
});

Expand Down

0 comments on commit 2f4d1b0

Please sign in to comment.