-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathmain.js
111 lines (95 loc) · 2.25 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;