-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
593 lines (538 loc) · 18.9 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
'use strict';
const url = require('url');
const axios = require('axios').default;
const crypto = require('crypto');
const fs = require('fs');
const constants = require('./library/constants');
const uniqid = require('uniqid');
const tools = require('./library/tools');
/** @type {Object} */
const countries = require('./countries.json').countries;
/** @type {Object} */
const packageInfo = require('./package.json');
/**
* @class EcovacsAPI
* An instance of this class provides access to the Ecovacs account and to the API
* @property @private {string} resource - the resource of the device
* @property @private {string} country - the country code of the country where the Ecovacs account is registered
* @property @private {string} continent - the continent where the Ecovacs account is registered
* @property @private {string} deviceId - the device ID of the bot
* @property @private {string} authDomain - the domain for the authentication API
*/
class EcovacsAPI {
/**
* @param {string} deviceId - the device ID of the bot
* @param {string} country - the country code of the country where the Ecovacs account is registered
* @param {string} [continent=''] - the continent code
* @param {string} [authDomain='ecovacs.com'] - the domain for the authentication API
*/
constructor(deviceId, country, continent = '', authDomain = '') {
tools.envLogInfo('Setting up EcovacsAPI instance');
this.deviceId = deviceId;
this.country = country.toUpperCase();
this.continent = continent ? continent : this.getContinent();
this.authDomain = authDomain ? authDomain : constants.AUTH_DOMAIN;
this.resource = deviceId.substring(0, 8);
}
/**
* @param {string} accountId - The account ID (Email or Ecovacs ID)
* @param {string} passwordHash - The password hash
* @returns {Promise<string>}
*/
async connect(accountId, passwordHash) {
tools.envLogHeader(`connect(accountId,passwordHash)`);
let error;
if (!accountId) {
error = new Error('No account ID provided');
}
if (!this.country) {
error = new Error('No country code provided');
}
if (!countries[this.country]) {
error = new Error('Wrong or unknown country code provided');
}
if (error) {
throw error;
}
let result = await this.callUserAuthApi(this.getLoginPath(), {
'account': accountId,
'password': passwordHash
});
this.uid = result.uid;
const loginAccessToken = result.accessToken;
result = await this.callUserAuthApi(constants.USER_GETAUTHCODE_PATH, {
'uid': this.uid,
'accessToken': loginAccessToken
});
this.authCode = result['authCode'];
result = await this.callUserApiLoginByItToken();
this.user_access_token = result['token'];
this.uid = result['userId'];
tools.envLogSuccess('user authentication complete');
return 'ready';
}
/**
* Get the parameters for the user login
* @param {Object} params - an object with the data to retrieve the parameters
* @returns {string} the parameters
*/
getUserLoginParams(params) {
params['authTimeZone'] = 'GMT-8';
let authSignParams = JSON.parse(JSON.stringify(this.getMetaObject()));
for (let key in params) {
if (params.hasOwnProperty(key)) {
authSignParams[key] = params[key];
}
}
let authAppkey = constants.AUTH_USERLOGIN_AUTH_APPKEY;
if (this.authDomain === constants.AUTH_DOMAIN_YD) {
authAppkey = constants.AUTH_USERLOGIN_AUTH_APPKEY_YD;
}
let authSecret = constants.AUTH_USERLOGIN_SECRET;
if (this.authDomain === constants.AUTH_DOMAIN_YD) {
authSecret = constants.AUTH_USERLOGIN_SECRET_YD;
}
return this.buildQueryList(params, authSignParams, authAppkey, authSecret);
}
/**
* Get the parameters for authentication
* @param {Object} params - an object with the data to retrieve the parameters
* @returns {string} the parameters
*/
getAuthParams(params) {
let authSignParams = params;
authSignParams['openId'] = 'global';
let authAppkey = constants.AUTH_GETAUTH_AUTH_APPKEY;
if (this.authDomain === constants.AUTH_DOMAIN_YD) {
authAppkey = constants.AUTH_GETAUTH_AUTH_APPKEY_YD;
}
let authSecret = constants.AUTH_GETAUTH_SECRET;
if (this.authDomain === constants.AUTH_DOMAIN_YD) {
authSecret = constants.AUTH_GETAUTH_SECRET_YD;
}
return this.buildQueryList(params, authSignParams, authAppkey, authSecret);
}
/**
* Used to generate the URL search parameters for the request
* @param params - the basic set of parameters for the request
* @param authSignParams - additional set of parameters for the request
* @param authAppkey - The appkey for the request
* @param authSecret - The secret key for the request
* @returns An array of query strings
*/
buildQueryList(params, authSignParams, authAppkey, authSecret) {
let authSignText = this.buildAuthSignText(authAppkey, authSignParams, authSecret);
params['authAppkey'] = authAppkey;
params['authSign'] = EcovacsAPI.md5(authSignText);
return tools.paramsToQueryList(params);
}
buildAuthSignText(authAppkey, authSignParams, authSecret) {
let authSignText = authAppkey;
let keys = Object.keys(authSignParams);
keys.sort();
for (let i = 0; i < keys.length; i++) {
let k = keys[i];
authSignText += k + "=" + authSignParams[k];
}
authSignText += authSecret;
return authSignText;
}
/**
* Get the meta-object that will be used to make a request to the server
* @returns {Object}
*/
getMetaObject() {
let appCode = 'global_e';
let appVersion = '2.2.3';
if (this.authDomain === constants.AUTH_DOMAIN_YD) {
appCode = 'yd_global_e';
appVersion = '1.3.0';
}
// deviceType 1 = Android
return {
'country': this.country,
'lang': 'EN',
'deviceId': this.deviceId,
'appCode': appCode,
'appVersion': appVersion,
'channel': 'google_play',
'deviceType': '1'
};
}
/**
* @param {string} loginPath - the login path
* @param {Object} params - an object with the data to retrieve the parameters
* @returns {Promise<Object>} an object including access token and user ID
*/
async callUserAuthApi(loginPath, params) {
if (loginPath === 'user/login') {
tools.envLogHeader(`callUserAuthApi('${loginPath}',{account:accountId,password:passwordHash})`);
} else {
tools.envLogHeader(`callUserAuthApi('${loginPath}',${JSON.stringify(params)})`);
}
let portalPath = this.getPortalPath(loginPath);
let portalUrl;
let searchParams;
params['authTimespan'] = Date.now();
if (loginPath === constants.USER_GETAUTHCODE_PATH) {
params['bizType'] = '';
params['deviceId'] = this.deviceId;
portalUrl = new url.URL(tools.formatString(portalPath, this.getMetaObject()));
searchParams = new url.URLSearchParams(this.getAuthParams(params));
} else {
params['requestId'] = EcovacsAPI.md5(uniqid());
portalUrl = new url.URL(tools.formatString(portalPath + "/" + loginPath, this.getMetaObject()));
searchParams = new url.URLSearchParams(this.getUserLoginParams(params));
}
const axiosConfig = {
params: searchParams
};
tools.envLogInfo(`portalUrl.href: '${portalUrl.href}'`);
tools.envLogInfo(`searchParams: '${searchParams.toString()}'`);
try {
const res = await axios.get(portalUrl.href, axiosConfig);
const result = res.data;
tools.envLogPayload(result);
if (result.code === '0000') {
return result.data;
} else {
let error;
if (result.code === '1005') {
error = new Error('Incorrect account id or password');
} else {
error = new Error(`Failure code ${result.code}: ${result.msg}`);
}
throw error;
}
} catch (err) {
tools.envLogError(`error: '${err}'`);
throw err;
}
}
/**
* Returns the portal path for the given login path
* @param {string} loginPath - the path for the login
* @returns {string} the portal path
*/
getPortalPath(loginPath) {
let portalPath = constants.AUTH_GL_API;
if (loginPath === constants.USER_GETAUTHCODE_PATH) {
portalPath = constants.AUTH_GL_OPENAPI;
}
portalPath = tools.formatString(portalPath, {domain: this.authDomain});
if (this.country === 'CN') {
portalPath = portalPath.replace('.com','.cn');
}
return portalPath;
}
/**
* @param {string} loginPath - the API path
* @param {string} func - the API function to be called
* @param {Object} args - an object with the params for the POST request
* @returns {Promise<Object>}
*/
async callPortalApi(loginPath, func, args) {
tools.envLogHeader(`callPortalApi('${loginPath}','${func}','${JSON.stringify(args)}')`);
let params = {
'todo': func
};
for (let key in args) {
if (args.hasOwnProperty(key)) {
params[key] = args[key];
}
}
tools.envLogInfo(`params: ${JSON.stringify(params)}`);
let portalUrlFormat = constants.PORTAL_ECOUSER_API;
if (this.country === 'CN') {
portalUrlFormat = constants.PORTAL_ECOUSER_API_CN;
} else if ((this.country === 'WW') || (this.continent.toUpperCase() === 'WW')) {
portalUrlFormat = constants.PORTAL_ECOUSER_API_LEGACY;
}
let portalUrl = tools.formatString(portalUrlFormat + "/" + loginPath, {continent: this.continent});
let headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(JSON.stringify(params))
};
tools.envLogInfo(`portalUrl: '${portalUrl}'`);
const res = await axios.post(portalUrl, params, {
headers: headers
});
const response = res.data;
if ((response['result'] !== 'ok') && (response['ret'] !== 'ok') && (response['msg'] !== 'success')) {
tools.envLogError(`failure code '${response['errno']}' (${response['error']}) for call '${func}'`);
throw new Error(`Failure code ${response['errno']} (${response['error']}) for call ${func}`);
} else {
tools.envLogPayload(response);
}
return response;
}
/**
* It calls the API to login by access token
* @returns {Promise<Object>} an object including user token and user ID
*/
callUserApiLoginByItToken() {
let org = 'ECOWW';
if (this.authDomain === constants.AUTH_DOMAIN_YD) {
org = 'ECOYDWW';
}
let country = this.country;
if (this.country === 'CN') {
org = 'ECOCN';
if (this.authDomain === constants.AUTH_DOMAIN_YD) {
org = 'ECOYDCN';
}
country = 'Chinese';
}
return this.callPortalApi(constants.USER_API_PATH, 'loginByItToken', {
'edition': 'ECOGLOBLE',
'userId': this.uid,
'token': this.authCode,
'realm': constants.REALM,
'resource': this.resource,
'org': org,
'last': '',
'country': country
});
}
/**
* Get the login path for the current country
* @returns {string} the login path is being returned.
*/
getLoginPath() {
let loginPath = constants.USER_LOGIN_PATH;
if (this.country === 'CN') {
loginPath = `${loginPath}CheckMobile`;
}
return loginPath;
}
/**
* @returns {Promise<Object>} a dictionary of Ecovacs products
*/
getConfigProducts() {
return new Promise((resolve, reject) => {
this.callPortalApi('pim/product/getConfigProducts', 'GetConfigProducts', {
'userid': this.uid,
'auth': {
'with': 'users',
'userid': this.uid,
'realm': constants.REALM,
'token': this.user_access_token,
'resource': this.resource
}
}).then((data) => {
resolve(data['data']);
}).catch((e) => {
reject(e);
});
});
}
/**
* @param {string} api - the API path
* @param {string} func - the API function to be called
* @returns {Promise<Object>} a dictionary of all devices of the users Ecovacs account
*/
async getDevices(api = constants.USER_API_PATH, func = 'GetDeviceList') {
return new Promise((resolve, reject) => {
this.callPortalApi(api, func, {
'userid': this.uid,
'auth': {
'with': 'users',
'userid': this.uid,
'realm': constants.REALM,
'token': this.user_access_token,
'resource': this.resource
}
}).then((data) => {
resolve(data['devices']);
}).catch((e) => {
reject(e);
});
});
}
/**
* @returns {Promise<Object>} a dictionary of all devices of the users Ecovacs account
*/
async devices() {
const deviceList = await this.getDevices(constants.USER_API_PATH, 'GetDeviceList');
const globalDeviceList = await this.getDevices('appsvr/app.do', 'GetGlobalDeviceList');
return this.mergeDeviceLists(deviceList, globalDeviceList);
}
/**
* Merge the data from the global device list (GetGlobalDeviceList)
* with the data from the device list (GetDeviceList) of the users Ecovacs account
* @param {Object} deviceList - the list of devices of the Ecovacs account
* @param {Object} globalDeviceList - the global device list returned by the API
* @returns {Object} a dictionary of all known devices
*/
mergeDeviceLists(deviceList, globalDeviceList) {
// This is a workaround to keep compatibility
// The device lists are not returned in the same order
for (let deviceNumber=0; deviceNumber<deviceList.length; deviceNumber++) {
for (let index=0; index<globalDeviceList.length; index++) {
if (globalDeviceList[index].did === deviceList[deviceNumber].did) {
deviceList[deviceNumber] = Object.assign(globalDeviceList[index]);
deviceList[deviceNumber].deviceNumber = deviceNumber;
}
}
}
return deviceList;
}
/**
* Get all known devices
* @returns {Object} a dictionary of all known devices
*/
getAllKnownDevices() {
return tools.getAllKnownDevices();
}
/**
* Get the name of the country from the countries object
* @returns {string} the name of the country
*/
getCountryName() {
if (countries[this.country]) {
return countries[this.country].name;
}
return 'unknown';
}
/**
* Get the continent code from the countries object
* @returns {string} the continent (lower case)
*/
getContinent() {
if (countries[this.country]) {
return countries[this.country].continent.toLowerCase();
}
return 'ww';
}
/**
* Wrapper method for the `getVacBot` method (but with only 1 parameter)
* @param {Object} vacuum - The object for the vacuum, retrieved by the `devices` dictionary
* @returns {Object} a corresponding instance of the 'vacBot' class
*/
getVacBotObj(vacuum) {
return this.getVacBot(this.uid, EcovacsAPI.REALM, this.resource, this.user_access_token, vacuum);
}
/**
* Get a corresponding instance of the `vacBot` class
* @param {string} user - the user ID (retrieved from Ecovacs API)
* @param {string} hostname - the host name (for the Ecovacs API)
* @param {string} resource - the resource of the vacuum
* @param {string} userToken - the user token
* @param {Object} vacuum - the object for the specific device retrieved by the devices dictionary
* @param {string} [continent] - the continent
* @returns {Object} a corresponding instance of the `VacBot` class
*/
getVacBot(user, hostname, resource, userToken, vacuum, continent = '') {
tools.envLogHeader(`getVacBot('${user}','${hostname}','${resource}','${userToken}','${vacuum}','${continent}')`);
if (continent !== '') {
tools.envLogWarn(`got value '${continent}' for continent (deprecated)`);
}
let vacBotClass;
const defaultValue = EcovacsAPI.isMQTTProtocolUsed(vacuum['company']);
const is950Type = EcovacsAPI.isDeviceClass950type(vacuum['class'], defaultValue);
if (is950Type) {
tools.envLogSuccess(`'950type' model identified`);
vacBotClass = require('./library/950type/vacBot');
} else {
tools.envLogWarn(`'non950type' model identified (deprecated)`);
vacBotClass = require('./library/non950type/vacBot');
}
return new vacBotClass(user, hostname, resource, userToken, vacuum, this.getContinent(), this.country, '', this.authDomain);
}
/**
* Get the version of the package
* @returns {string} the version of the package
*/
getVersion() {
return packageInfo.version;
}
/**
* Get the version of the package
* @returns {string} the version of the package
*/
static version() {
return packageInfo.version;
}
/**
* Is the canvas module available?
* @returns {boolean} a boolean value
*/
getCanvasModuleIsAvailable() {
return EcovacsAPI.isCanvasModuleAvailable();
}
/**
* Is the canvas module available?
* @returns {boolean} a boolean value
*/
static isCanvasModuleAvailable() {
return tools.isCanvasModuleAvailable();
}
/**
* @param {string} company
* @returns {boolean}
*/
static isMQTTProtocolUsed(company) {
return (company === 'eco-ng');
}
/**
* Returns true if the device class is 950 type
* @param {string} deviceClass - The device class to check
* @param [isMQTTProtocolUsed=true] - This value is used as default value if the deviceClass is not registered
* @returns {boolean} the value of the '950type' property
*/
static isDeviceClass950type(deviceClass, isMQTTProtocolUsed = true) {
return tools.getDeviceProperty(deviceClass, '950type', isMQTTProtocolUsed);
}
/**
* Returns true if the device class is not 950 type
* @param {string} deviceClass - The device class of the device
* @returns {boolean} a boolean value.
*/
static isDeviceClassNot950type(deviceClass) {
return (!EcovacsAPI.isDeviceClass950type(deviceClass));
}
/**
* Given a machine id and a device number, return the device ID
* @param {string} machineId - the id of the device
* @param {number} [deviceNumber=0] - the device number is a number that is assigned to each device
* @returns {string} the device ID
*/
static getDeviceId(machineId, deviceNumber = 0) {
return EcovacsAPI.md5(machineId + deviceNumber.toString());
}
/**
* Create a hash of the given text using the MD5 algorithm
* @param {string} text - the text to be hashed
* @returns {string} the MD5 hash of the text
*/
static md5(text) {
return crypto.createHash('md5').update(text).digest("hex");
}
/**
* It takes a string and encrypts it using the public key
* @param {string} text - the text to encrypt
* @returns {string} the encrypted string
*/
static encrypt(text) {
return crypto.publicEncrypt({
key: EcovacsAPI.PUBLIC_KEY,
padding: crypto.constants.RSA_PKCS1_PADDING
}, Buffer.from(text)).toString('base64');
}
logInfo(message) {
tools.logInfo(message);
}
logWarn(message) {
tools.logWarn(message);
}
logError(message) {
tools.logError(message);
}
logEvent(event, value) {
tools.logEvent(event, value);
}
}
EcovacsAPI.PUBLIC_KEY = fs.readFileSync(__dirname + "/key.pem", "utf8");
EcovacsAPI.REALM = constants.REALM;
module.exports.EcoVacsAPI = EcovacsAPI;
module.exports.countries = countries;