eufy-security-client 3.6.0 → 3.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore/342/200/216 +8 -0
- package/.prettierrc +11 -0
- package/README.md +18 -0
- package/a.ts +61 -0
- package/build/error.js.map +1 -1
- package/build/eufysecurity.d.ts +1 -0
- package/build/eufysecurity.js +721 -224
- package/build/eufysecurity.js.map +1 -1
- package/build/http/api.d.ts +29 -0
- package/build/http/api.js +991 -701
- package/build/http/api.js.map +1 -1
- package/build/http/cache.js.map +1 -1
- package/build/http/const.d.ts +6 -1
- package/build/http/const.js +2044 -7536
- package/build/http/const.js.map +1 -1
- package/build/http/device.d.ts +4 -0
- package/build/http/device.js +1325 -440
- package/build/http/device.js.map +1 -1
- package/build/http/error.js.map +1 -1
- package/build/http/index.js.map +1 -1
- package/build/http/interfaces.d.ts +22 -15
- package/build/http/models.d.ts +2 -1
- package/build/http/parameter.js +74 -63
- package/build/http/parameter.js.map +1 -1
- package/build/http/station.d.ts +20 -2
- package/build/http/station.js +8639 -3566
- package/build/http/station.js.map +1 -1
- package/build/http/types.d.ts +12 -0
- package/build/http/types.js +297 -155
- package/build/http/types.js.map +1 -1
- package/build/http/utils.d.ts +16 -6
- package/build/http/utils.js +335 -208
- package/build/http/utils.js.map +1 -1
- package/build/index.js.map +1 -1
- package/build/interfaces.d.ts +4 -3
- package/build/logging.js +8 -13
- package/build/logging.js.map +1 -1
- package/build/mqtt/interface.d.ts +2 -2
- package/build/mqtt/service.js +12 -3
- package/build/mqtt/service.js.map +1 -1
- package/build/p2p/ble.js +7 -6
- package/build/p2p/ble.js.map +1 -1
- package/build/p2p/error.js.map +1 -1
- package/build/p2p/interfaces.d.ts +41 -6
- package/build/p2p/session.js +1484 -383
- package/build/p2p/session.js.map +1 -1
- package/build/p2p/talkback.js.map +1 -1
- package/build/p2p/types.js +36 -36
- package/build/p2p/types.js.map +1 -1
- package/build/p2p/utils.d.ts +10 -0
- package/build/p2p/utils.js +183 -90
- package/build/p2p/utils.js.map +1 -1
- package/build/push/client.js +15 -4
- package/build/push/client.js.map +1 -1
- package/build/push/error.js.map +1 -1
- package/build/push/interfaces.d.ts +8 -8
- package/build/push/models.js.map +1 -1
- package/build/push/parser.js +6 -2
- package/build/push/parser.js.map +1 -1
- package/build/push/service.js +214 -85
- package/build/push/service.js.map +1 -1
- package/build/push/types.js.map +1 -1
- package/build/push/utils.js.map +1 -1
- package/build/utils.js +7 -15
- package/build/utils.js.map +1 -1
- package/coverage/clover.xml +11133 -13648
- package/coverage/coverage-final.json +20 -30
- package/coverage/lcov-report/error.ts.html +3 -3
- package/coverage/lcov-report/index.html +50 -65
- package/coverage/lcov-report/logging.ts.html +598 -0
- package/coverage/lcov.info +21072 -25751
- package/dont-care.js +101 -0
- package/package.json +9 -5
- package/build/package.json +0 -81
package/build/http/api.js
CHANGED
|
@@ -69,7 +69,7 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
69
69
|
nick_name: "",
|
|
70
70
|
device_public_keys: {},
|
|
71
71
|
clientPrivateKey: "",
|
|
72
|
-
serverPublicKey: this.SERVER_PUBLIC_KEY
|
|
72
|
+
serverPublicKey: this.SERVER_PUBLIC_KEY,
|
|
73
73
|
};
|
|
74
74
|
headers = {
|
|
75
75
|
"User-Agent": undefined,
|
|
@@ -89,32 +89,61 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
89
89
|
Timezone: "GMT+01:00",
|
|
90
90
|
"Cache-Control": "no-cache",
|
|
91
91
|
};
|
|
92
|
+
apiVerifyCode = "v1/sms/send/verify_code";
|
|
93
|
+
apiTrustDevice = "v1/app/trust_device/add";
|
|
94
|
+
apiGetStations = "v2/house/station_list";
|
|
95
|
+
apiGetDevices = "v2/house/device_list";
|
|
96
|
+
apiPushCheck = "v1/app/review/app_push_check";
|
|
97
|
+
apiRegisterPushToken = "v1/apppush/register_push_token";
|
|
98
|
+
apiSetParameters = "v1/app/upload_devs_params";
|
|
99
|
+
apiGetCiphers = "v2/app/cipher/get_ciphers";
|
|
100
|
+
apiEventGetAllVideoRecord = "v2/event/app/get_all_video_record";
|
|
101
|
+
apiEventGetAllAlarmRecord = "v2/event/app/get_all_alarm_record";
|
|
102
|
+
apiEventGetAllHistoryRecord = "v2/event/app/get_all_history_record";
|
|
103
|
+
apiGetInvites = "v2/family/get_invites";
|
|
104
|
+
apiConfirmInvite = "v1/family/confirm_invite";
|
|
105
|
+
apiGetSensorHistory = "v1/app/get_sensor_history";
|
|
106
|
+
apiGetHouseDetail = "v2/house/detail";
|
|
107
|
+
apiGetHouseList = "v1/house/list";
|
|
108
|
+
apiGetHouseInvites = "v1/house/invite_list";
|
|
109
|
+
apiConfirmHouseInvite = "v1/house/confirm_invite";
|
|
110
|
+
apiAddLocalUser = "v1/app/device/local_user/add";
|
|
111
|
+
apiDeleteLocalUser = "v1/app/device/user/delete";
|
|
112
|
+
apiUpdateLocalUser = "v1/app/device/local_user/update";
|
|
113
|
+
apiUpdateUserPassword = "v1/app/device/password/save_or_update";
|
|
114
|
+
FIFTEEN_YEARS_IN_MS = 15 * 365 * 24 * 60 * 60 * 1000;
|
|
92
115
|
constructor(apiBase, country, username, password, persistentData) {
|
|
93
116
|
super();
|
|
94
117
|
this.username = username;
|
|
95
118
|
this.password = password;
|
|
96
119
|
this.apiBase = apiBase;
|
|
97
|
-
logging_1.rootHTTPLogger.debug(`Loaded API`, {
|
|
120
|
+
logging_1.rootHTTPLogger.debug(`Loaded API`, {
|
|
121
|
+
apieBase: apiBase,
|
|
122
|
+
country: country,
|
|
123
|
+
username: username,
|
|
124
|
+
persistentData: persistentData,
|
|
125
|
+
});
|
|
98
126
|
this.headers.timezone = (0, utils_1.getTimezoneGMTString)();
|
|
99
127
|
this.headers.country = country.toUpperCase();
|
|
100
128
|
if (persistentData) {
|
|
101
129
|
this.persistentData = persistentData;
|
|
102
130
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
this.persistentData.clientPrivateKey = this.ecdh.getPrivateKey().toString("hex");
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
131
|
+
// Generated key based on the provided one
|
|
132
|
+
if (this.persistentData.clientPrivateKey !== undefined && this.persistentData.clientPrivateKey !== "") {
|
|
108
133
|
try {
|
|
109
134
|
this.ecdh.setPrivateKey(Buffer.from(this.persistentData.clientPrivateKey, "hex"));
|
|
110
135
|
}
|
|
111
136
|
catch (err) {
|
|
112
137
|
const error = (0, error_1.ensureError)(err);
|
|
113
|
-
logging_1.rootHTTPLogger.debug(`Invalid client private key, generate new client private key...`, {
|
|
114
|
-
|
|
115
|
-
|
|
138
|
+
logging_1.rootHTTPLogger.debug(`Invalid client private key, generate new client private key...`, {
|
|
139
|
+
error: (0, utils_2.getError)(error),
|
|
140
|
+
});
|
|
141
|
+
this.generateNewKey();
|
|
116
142
|
}
|
|
117
143
|
}
|
|
144
|
+
else {
|
|
145
|
+
this.generateNewKey();
|
|
146
|
+
}
|
|
118
147
|
if (this.persistentData.serverPublicKey === undefined || this.persistentData.serverPublicKey === "") {
|
|
119
148
|
this.persistentData.serverPublicKey = this.SERVER_PUBLIC_KEY;
|
|
120
149
|
}
|
|
@@ -124,12 +153,26 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
124
153
|
}
|
|
125
154
|
catch (err) {
|
|
126
155
|
const error = (0, error_1.ensureError)(err);
|
|
127
|
-
logging_1.rootHTTPLogger.debug(`Invalid server public key, fallback to default server public key...`, {
|
|
156
|
+
logging_1.rootHTTPLogger.debug(`Invalid server public key, fallback to default server public key...`, {
|
|
157
|
+
error: (0, utils_2.getError)(error),
|
|
158
|
+
});
|
|
128
159
|
this.persistentData.serverPublicKey = this.SERVER_PUBLIC_KEY;
|
|
129
160
|
}
|
|
130
161
|
}
|
|
131
162
|
}
|
|
163
|
+
generateNewKey() {
|
|
164
|
+
/*
|
|
165
|
+
Generate a new client private key
|
|
166
|
+
*/
|
|
167
|
+
this.ecdh.generateKeys();
|
|
168
|
+
this.persistentData.clientPrivateKey = this.ecdh.getPrivateKey().toString("hex");
|
|
169
|
+
}
|
|
132
170
|
static async getApiBaseFromCloud(country) {
|
|
171
|
+
/**
|
|
172
|
+
Query the main api with the country code ton ensure it is a valid and returns the correct api
|
|
173
|
+
|
|
174
|
+
@param country
|
|
175
|
+
**/
|
|
133
176
|
const { default: got } = await import("got");
|
|
134
177
|
const response = await got(`domain/${country}`, {
|
|
135
178
|
prefixUrl: this.apiDomainBase,
|
|
@@ -137,16 +180,22 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
137
180
|
responseType: "json",
|
|
138
181
|
retry: {
|
|
139
182
|
limit: 1,
|
|
140
|
-
methods: ["GET"]
|
|
141
|
-
}
|
|
183
|
+
methods: ["GET"],
|
|
184
|
+
},
|
|
142
185
|
});
|
|
143
186
|
const result = response.body;
|
|
144
187
|
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
145
188
|
return `https://${result.data.domain}`;
|
|
146
189
|
}
|
|
147
|
-
throw new error_2.ApiBaseLoadError("Error identifying API base from cloud", {
|
|
190
|
+
throw new error_2.ApiBaseLoadError("Error identifying API base from cloud", {
|
|
191
|
+
context: { code: result.code, message: result.msg },
|
|
192
|
+
});
|
|
148
193
|
}
|
|
149
194
|
async loadLibraries() {
|
|
195
|
+
/**
|
|
196
|
+
* Load library
|
|
197
|
+
*
|
|
198
|
+
*/
|
|
150
199
|
const { default: pThrottle } = await import("p-throttle");
|
|
151
200
|
const { default: got } = await import("got");
|
|
152
201
|
this.throttle = pThrottle({
|
|
@@ -161,23 +210,10 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
161
210
|
retry: {
|
|
162
211
|
limit: 3,
|
|
163
212
|
methods: ["GET", "POST"],
|
|
164
|
-
statusCodes: [
|
|
165
|
-
404,
|
|
166
|
-
408,
|
|
167
|
-
413,
|
|
168
|
-
423,
|
|
169
|
-
429,
|
|
170
|
-
500,
|
|
171
|
-
502,
|
|
172
|
-
503,
|
|
173
|
-
504,
|
|
174
|
-
521,
|
|
175
|
-
522,
|
|
176
|
-
524
|
|
177
|
-
],
|
|
213
|
+
statusCodes: [404, 408, 413, 423, 429, 500, 502, 503, 504, 521, 522, 524],
|
|
178
214
|
calculateDelay: ({ computedValue }) => {
|
|
179
215
|
return computedValue * 3;
|
|
180
|
-
}
|
|
216
|
+
},
|
|
181
217
|
},
|
|
182
218
|
hooks: {
|
|
183
219
|
afterResponse: [
|
|
@@ -185,15 +221,19 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
185
221
|
// Unauthorized
|
|
186
222
|
if (response.statusCode === 401) {
|
|
187
223
|
const oldToken = this.token;
|
|
188
|
-
logging_1.rootHTTPLogger.debug("Invalidate token an get a new one...", {
|
|
224
|
+
logging_1.rootHTTPLogger.debug("Invalidate token an get a new one...", {
|
|
225
|
+
requestUrl: response.requestUrl,
|
|
226
|
+
statusCode: response.statusCode,
|
|
227
|
+
statusMessage: response.statusMessage,
|
|
228
|
+
});
|
|
189
229
|
this.invalidateToken();
|
|
190
230
|
await this.login({ force: true });
|
|
191
231
|
if (oldToken !== this.token && this.token) {
|
|
192
232
|
// Refresh the access token
|
|
193
233
|
const updatedOptions = {
|
|
194
234
|
headers: {
|
|
195
|
-
"X-Auth-Token": this.token
|
|
196
|
-
}
|
|
235
|
+
"X-Auth-Token": this.token,
|
|
236
|
+
},
|
|
197
237
|
};
|
|
198
238
|
// Update the defaults
|
|
199
239
|
this.requestEufyCloud.defaults.options.merge(updatedOptions);
|
|
@@ -203,7 +243,7 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
203
243
|
}
|
|
204
244
|
// No changes otherwise
|
|
205
245
|
return response;
|
|
206
|
-
}
|
|
246
|
+
},
|
|
207
247
|
],
|
|
208
248
|
beforeRetry: [
|
|
209
249
|
(error) => {
|
|
@@ -214,10 +254,10 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
214
254
|
const body = error.response?.body ? error.response?.body : error.message;
|
|
215
255
|
logging_1.rootHTTPLogger.debug(`Retrying [${error.request?.retryCount !== undefined ? error.request?.retryCount + 1 : 1}]: ${error.code} (${error.request?.requestUrl})\n${statusCode} ${method} ${shortUrl}\n${body}`);
|
|
216
256
|
// Retrying [1]: ERR_NON_2XX_3XX_RESPONSE
|
|
217
|
-
}
|
|
257
|
+
},
|
|
218
258
|
],
|
|
219
259
|
beforeError: [
|
|
220
|
-
error => {
|
|
260
|
+
(error) => {
|
|
221
261
|
const { response, options } = error;
|
|
222
262
|
const statusCode = response?.statusCode || 0;
|
|
223
263
|
const { method, url, prefixUrl } = options;
|
|
@@ -228,15 +268,17 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
228
268
|
error.message = `${statusCode} ${method} ${shortUrl}\n${body}`;
|
|
229
269
|
}
|
|
230
270
|
return error;
|
|
231
|
-
}
|
|
271
|
+
},
|
|
232
272
|
],
|
|
233
273
|
beforeRequest: [
|
|
234
274
|
async (_options) => {
|
|
235
|
-
await this.throttle(async () => {
|
|
236
|
-
|
|
237
|
-
|
|
275
|
+
await this.throttle(async () => {
|
|
276
|
+
return;
|
|
277
|
+
})();
|
|
278
|
+
},
|
|
279
|
+
],
|
|
238
280
|
},
|
|
239
|
-
mutableDefaults: true
|
|
281
|
+
mutableDefaults: true,
|
|
240
282
|
});
|
|
241
283
|
}
|
|
242
284
|
static async initialize(country, username, password, persistentData) {
|
|
@@ -249,6 +291,10 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
249
291
|
throw new error_1.InvalidCountryCodeError("Invalid ISO 3166-1 Alpha-2 country code", { context: { countryCode: country } });
|
|
250
292
|
}
|
|
251
293
|
clearScheduleRenewAuthToken() {
|
|
294
|
+
/**
|
|
295
|
+
* Clear schedule to renew the auth token if running
|
|
296
|
+
*
|
|
297
|
+
**/
|
|
252
298
|
if (this.renewAuthTokenJob !== undefined) {
|
|
253
299
|
this.renewAuthTokenJob.cancel();
|
|
254
300
|
}
|
|
@@ -256,7 +302,7 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
256
302
|
scheduleRenewAuthToken() {
|
|
257
303
|
this.clearScheduleRenewAuthToken();
|
|
258
304
|
if (this.tokenExpiration !== null) {
|
|
259
|
-
const scheduleDate = new Date(this.tokenExpiration.getTime() -
|
|
305
|
+
const scheduleDate = new Date(this.tokenExpiration.getTime() - 1000 * 60 * 60 * 24);
|
|
260
306
|
if (this.renewAuthTokenJob === undefined) {
|
|
261
307
|
this.renewAuthTokenJob = schedule.scheduleJob("renewAuthToken", scheduleDate, async () => {
|
|
262
308
|
logging_1.rootHTTPLogger.info("Authentication token is soon expiring, fetching a new one...");
|
|
@@ -273,32 +319,49 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
273
319
|
this.token = null;
|
|
274
320
|
this.requestEufyCloud.defaults.options.merge({
|
|
275
321
|
headers: {
|
|
276
|
-
"X-Auth-Token": undefined
|
|
277
|
-
}
|
|
322
|
+
"X-Auth-Token": undefined,
|
|
323
|
+
},
|
|
278
324
|
});
|
|
279
325
|
this.tokenExpiration = null;
|
|
280
326
|
this.persistentData.serverPublicKey = this.SERVER_PUBLIC_KEY;
|
|
281
327
|
this.clearScheduleRenewAuthToken();
|
|
282
328
|
this.emit("auth token invalidated");
|
|
283
329
|
}
|
|
330
|
+
updateApiHeader() {
|
|
331
|
+
this.requestEufyCloud.defaults.options.merge({
|
|
332
|
+
headers: this.headers,
|
|
333
|
+
});
|
|
334
|
+
}
|
|
284
335
|
setPhoneModel(model) {
|
|
336
|
+
/**
|
|
337
|
+
*
|
|
338
|
+
* Set a new phone model for the http request
|
|
339
|
+
*
|
|
340
|
+
*/
|
|
285
341
|
this.headers.phone_model = model.toUpperCase();
|
|
286
342
|
this.requestEufyCloud.defaults.options.merge({
|
|
287
|
-
headers: this.headers
|
|
343
|
+
headers: this.headers,
|
|
288
344
|
});
|
|
289
345
|
}
|
|
290
346
|
getPhoneModel() {
|
|
347
|
+
/**
|
|
348
|
+
*
|
|
349
|
+
* Return the current phone model used
|
|
350
|
+
*
|
|
351
|
+
*/
|
|
291
352
|
return this.headers.phone_model;
|
|
292
353
|
}
|
|
293
354
|
getCountry() {
|
|
355
|
+
/**
|
|
356
|
+
* Return the current country
|
|
357
|
+
*
|
|
358
|
+
*/
|
|
294
359
|
return this.headers.country;
|
|
295
360
|
}
|
|
296
361
|
setLanguage(language) {
|
|
297
362
|
if ((0, i18n_iso_languages_1.isValid)(language) && language.length === 2) {
|
|
298
363
|
this.headers.language = language;
|
|
299
|
-
this.
|
|
300
|
-
headers: this.headers
|
|
301
|
-
});
|
|
364
|
+
this.updateApiHeader();
|
|
302
365
|
}
|
|
303
366
|
else
|
|
304
367
|
throw new error_1.InvalidLanguageCodeError("Invalid ISO 639 language code", { context: { languageCode: language } });
|
|
@@ -306,88 +369,132 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
306
369
|
getLanguage() {
|
|
307
370
|
return this.headers.language;
|
|
308
371
|
}
|
|
372
|
+
loginCompleted(dataResult) {
|
|
373
|
+
if (dataResult.server_secret_info?.public_key)
|
|
374
|
+
this.persistentData.serverPublicKey = dataResult.server_secret_info.public_key;
|
|
375
|
+
this.persistentData.user_id = dataResult.user_id;
|
|
376
|
+
this.persistentData.email = this.decryptAPIData(dataResult.email, false);
|
|
377
|
+
this.persistentData.nick_name = dataResult.nick_name;
|
|
378
|
+
this.setToken(dataResult.auth_token);
|
|
379
|
+
this.tokenExpiration = new Date(dataResult.token_expires_at * 1000);
|
|
380
|
+
this.headers = {
|
|
381
|
+
...this.headers,
|
|
382
|
+
gtoken: (0, utils_2.md5)(dataResult.user_id),
|
|
383
|
+
};
|
|
384
|
+
logging_1.rootHTTPLogger.debug("Login - Token data", {
|
|
385
|
+
token: this.token,
|
|
386
|
+
tokenExpiration: this.tokenExpiration,
|
|
387
|
+
serverPublicKey: this.persistentData.serverPublicKey,
|
|
388
|
+
});
|
|
389
|
+
if (!this.connected) {
|
|
390
|
+
this.connected = true;
|
|
391
|
+
this.emit("connect");
|
|
392
|
+
}
|
|
393
|
+
this.scheduleRenewAuthToken();
|
|
394
|
+
}
|
|
395
|
+
async loginVerifyCode(dataResult) {
|
|
396
|
+
/**
|
|
397
|
+
* Send verification code
|
|
398
|
+
*/
|
|
399
|
+
logging_1.rootHTTPLogger.debug(`Login - Send verification code...`);
|
|
400
|
+
this.setToken(dataResult.auth_token);
|
|
401
|
+
this.tokenExpiration = new Date(dataResult.token_expires_at * 1000);
|
|
402
|
+
logging_1.rootHTTPLogger.debug("Token data", { token: this.token, tokenExpiration: this.tokenExpiration });
|
|
403
|
+
await this.sendVerifyCode(types_1.VerfyCodeTypes.TYPE_EMAIL);
|
|
404
|
+
logging_1.rootHTTPLogger.info("Please send required verification code to proceed with authentication");
|
|
405
|
+
this.emit("tfa request");
|
|
406
|
+
}
|
|
407
|
+
loginRequestCaptcha(dataResult) {
|
|
408
|
+
logging_1.rootHTTPLogger.debug("Login - Captcha verification received", {
|
|
409
|
+
captchaId: dataResult.captcha_id,
|
|
410
|
+
item: dataResult.item,
|
|
411
|
+
});
|
|
412
|
+
logging_1.rootHTTPLogger.info("Please send requested captcha to proceed with authentication");
|
|
413
|
+
this.emit("captcha request", dataResult.captcha_id, dataResult.item);
|
|
414
|
+
}
|
|
309
415
|
async login(options) {
|
|
310
416
|
options = (0, utils_2.mergeDeep)(options, {
|
|
311
|
-
force: false
|
|
417
|
+
force: false,
|
|
418
|
+
});
|
|
419
|
+
logging_1.rootHTTPLogger.debug("Login and get an access token", {
|
|
420
|
+
token: this.token,
|
|
421
|
+
tokenExpiration: this.tokenExpiration,
|
|
422
|
+
options: options,
|
|
312
423
|
});
|
|
313
|
-
|
|
314
|
-
|
|
424
|
+
const isInvalidToken = !this.token;
|
|
425
|
+
const isTokenExpired = this.tokenExpiration && new Date().getTime() >= this.tokenExpiration.getTime();
|
|
426
|
+
if (isInvalidToken || isTokenExpired || options.verifyCode || options.captcha || options.force) {
|
|
315
427
|
try {
|
|
428
|
+
const timezoneOffset = new Date().getTimezoneOffset();
|
|
316
429
|
const data = {
|
|
317
430
|
ab: this.headers.country,
|
|
318
431
|
client_secret_info: {
|
|
319
|
-
public_key: this.ecdh.getPublicKey("hex")
|
|
432
|
+
public_key: this.ecdh.getPublicKey("hex"),
|
|
320
433
|
},
|
|
321
434
|
enc: 0,
|
|
322
435
|
email: this.username,
|
|
323
436
|
password: (0, utils_1.encryptAPIData)(this.password, this.ecdh.computeSecret(Buffer.from(this.SERVER_PUBLIC_KEY, "hex"))),
|
|
324
|
-
time_zone:
|
|
325
|
-
transaction: `${new Date().getTime()}
|
|
437
|
+
time_zone: timezoneOffset !== 0 ? -timezoneOffset * 60 * 1000 : 0,
|
|
438
|
+
transaction: `${new Date().getTime()}`,
|
|
326
439
|
};
|
|
440
|
+
// Add verification code to the data
|
|
327
441
|
if (options.verifyCode) {
|
|
328
442
|
data.verify_code = options.verifyCode;
|
|
329
443
|
}
|
|
330
444
|
else if (options.captcha) {
|
|
445
|
+
// Add captcha response
|
|
331
446
|
data.captcha_id = options.captcha.captchaId;
|
|
332
447
|
data.answer = options.captcha.captchaCode;
|
|
333
448
|
}
|
|
334
449
|
const response = await this.request({
|
|
335
450
|
method: "post",
|
|
336
451
|
endpoint: "v2/passport/login_sec",
|
|
337
|
-
data: data
|
|
452
|
+
data: data,
|
|
338
453
|
});
|
|
339
454
|
if (response.status == 200) {
|
|
340
455
|
const result = response.data;
|
|
341
456
|
if (result.data !== undefined) {
|
|
342
457
|
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
343
|
-
|
|
344
|
-
if (dataresult.server_secret_info?.public_key)
|
|
345
|
-
this.persistentData.serverPublicKey = dataresult.server_secret_info.public_key;
|
|
346
|
-
this.persistentData.user_id = dataresult.user_id;
|
|
347
|
-
this.persistentData.email = this.decryptAPIData(dataresult.email, false);
|
|
348
|
-
this.persistentData.nick_name = dataresult.nick_name;
|
|
349
|
-
this.setToken(dataresult.auth_token);
|
|
350
|
-
this.tokenExpiration = new Date(dataresult.token_expires_at * 1000);
|
|
351
|
-
this.headers = {
|
|
352
|
-
...this.headers,
|
|
353
|
-
gtoken: (0, utils_2.md5)(dataresult.user_id)
|
|
354
|
-
};
|
|
355
|
-
logging_1.rootHTTPLogger.debug("Login - Token data", { token: this.token, tokenExpiration: this.tokenExpiration, serverPublicKey: this.persistentData.serverPublicKey });
|
|
356
|
-
if (!this.connected) {
|
|
357
|
-
this.connected = true;
|
|
358
|
-
this.emit("connect");
|
|
359
|
-
}
|
|
360
|
-
this.scheduleRenewAuthToken();
|
|
458
|
+
this.loginCompleted(result.data);
|
|
361
459
|
}
|
|
362
460
|
else if (result.code == types_1.ResponseErrorCode.CODE_NEED_VERIFY_CODE) {
|
|
363
|
-
|
|
364
|
-
const dataresult = result.data;
|
|
365
|
-
this.setToken(dataresult.auth_token);
|
|
366
|
-
this.tokenExpiration = new Date(dataresult.token_expires_at * 1000);
|
|
367
|
-
logging_1.rootHTTPLogger.debug("Token data", { token: this.token, tokenExpiration: this.tokenExpiration });
|
|
368
|
-
await this.sendVerifyCode(types_1.VerfyCodeTypes.TYPE_EMAIL);
|
|
369
|
-
logging_1.rootHTTPLogger.info("Please send required verification code to proceed with authentication");
|
|
370
|
-
this.emit("tfa request");
|
|
461
|
+
this.loginVerifyCode(result.data);
|
|
371
462
|
}
|
|
372
|
-
else if (result.code == types_1.ResponseErrorCode.LOGIN_NEED_CAPTCHA ||
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
logging_1.rootHTTPLogger.info("Please send requested captcha to proceed with authentication");
|
|
376
|
-
this.emit("captcha request", dataresult.captcha_id, dataresult.item);
|
|
463
|
+
else if (result.code == types_1.ResponseErrorCode.LOGIN_NEED_CAPTCHA ||
|
|
464
|
+
result.code == types_1.ResponseErrorCode.LOGIN_CAPTCHA_ERROR) {
|
|
465
|
+
this.loginRequestCaptcha(result.data);
|
|
377
466
|
}
|
|
378
467
|
else {
|
|
379
|
-
logging_1.rootHTTPLogger.error("Login - Response code not ok", {
|
|
380
|
-
|
|
468
|
+
logging_1.rootHTTPLogger.error("Login - Response code not ok", {
|
|
469
|
+
code: result.code,
|
|
470
|
+
msg: result.msg,
|
|
471
|
+
data: response.data,
|
|
472
|
+
});
|
|
473
|
+
this.emit("connection error", new error_2.ApiResponseCodeError("API response code not ok", {
|
|
474
|
+
context: { code: result.code, message: result.msg },
|
|
475
|
+
}));
|
|
381
476
|
}
|
|
382
477
|
}
|
|
383
478
|
else {
|
|
384
|
-
logging_1.rootHTTPLogger.error("Login - Response data is missing", {
|
|
385
|
-
|
|
479
|
+
logging_1.rootHTTPLogger.error("Login - Response data is missing", {
|
|
480
|
+
code: result.code,
|
|
481
|
+
msg: result.msg,
|
|
482
|
+
data: result.data,
|
|
483
|
+
});
|
|
484
|
+
this.emit("connection error", new error_2.ApiInvalidResponseError("API response data is missing", {
|
|
485
|
+
context: { code: result.code, message: result.msg, data: result.data },
|
|
486
|
+
}));
|
|
386
487
|
}
|
|
387
488
|
}
|
|
388
489
|
else {
|
|
389
|
-
logging_1.rootHTTPLogger.error("Login - Status return code not 200", {
|
|
390
|
-
|
|
490
|
+
logging_1.rootHTTPLogger.error("Login - Status return code not 200", {
|
|
491
|
+
status: response.status,
|
|
492
|
+
statusText: response.statusText,
|
|
493
|
+
data: response.data,
|
|
494
|
+
});
|
|
495
|
+
this.emit("connection error", new error_2.ApiHTTPResponseCodeError("API HTTP response code not ok", {
|
|
496
|
+
context: { status: response.status, statusText: response.statusText },
|
|
497
|
+
}));
|
|
391
498
|
}
|
|
392
499
|
}
|
|
393
500
|
catch (err) {
|
|
@@ -417,18 +524,41 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
417
524
|
}
|
|
418
525
|
}
|
|
419
526
|
}
|
|
420
|
-
async
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
527
|
+
async makePostRequest(endPoint, data) {
|
|
528
|
+
if (this.connected) {
|
|
529
|
+
try {
|
|
530
|
+
const response = await this.request({
|
|
531
|
+
method: "post",
|
|
532
|
+
endpoint: endPoint,
|
|
533
|
+
data: data,
|
|
534
|
+
});
|
|
535
|
+
if (response.status == 200) {
|
|
536
|
+
return response;
|
|
430
537
|
}
|
|
431
|
-
|
|
538
|
+
else {
|
|
539
|
+
logging_1.rootHTTPLogger.error(`Post to ${endPoint} - Status return code not 200`, {
|
|
540
|
+
status: response.status,
|
|
541
|
+
statusText: response.statusText,
|
|
542
|
+
data: response.data,
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
catch (err) {
|
|
547
|
+
const error = (0, error_1.ensureError)(err);
|
|
548
|
+
logging_1.rootHTTPLogger.error("Send verify code - Generic Error", { error: (0, utils_2.getError)(error) });
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return undefined;
|
|
552
|
+
}
|
|
553
|
+
async sendVerifyCode(type) {
|
|
554
|
+
if (!type)
|
|
555
|
+
type = types_1.VerfyCodeTypes.TYPE_EMAIL;
|
|
556
|
+
const data = {
|
|
557
|
+
message_type: type,
|
|
558
|
+
transaction: `${new Date().getTime()}`,
|
|
559
|
+
};
|
|
560
|
+
const response = await this.makePostRequest(this.apiVerifyCode, data);
|
|
561
|
+
if (response != undefined) {
|
|
432
562
|
if (response.status == 200) {
|
|
433
563
|
const result = response.data;
|
|
434
564
|
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
@@ -436,17 +566,21 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
436
566
|
return true;
|
|
437
567
|
}
|
|
438
568
|
else {
|
|
439
|
-
logging_1.rootHTTPLogger.error("Send verify code - Response code not ok", {
|
|
569
|
+
logging_1.rootHTTPLogger.error("Send verify code - Response code not ok", {
|
|
570
|
+
code: result.code,
|
|
571
|
+
msg: result.msg,
|
|
572
|
+
data: response.data,
|
|
573
|
+
});
|
|
440
574
|
}
|
|
441
575
|
}
|
|
442
576
|
else {
|
|
443
|
-
logging_1.rootHTTPLogger.error("Send verify code - Status return code not 200", {
|
|
577
|
+
logging_1.rootHTTPLogger.error("Send verify code - Status return code not 200", {
|
|
578
|
+
status: response.status,
|
|
579
|
+
statusText: response.statusText,
|
|
580
|
+
data: response.data,
|
|
581
|
+
});
|
|
444
582
|
}
|
|
445
583
|
}
|
|
446
|
-
catch (err) {
|
|
447
|
-
const error = (0, error_1.ensureError)(err);
|
|
448
|
-
logging_1.rootHTTPLogger.error("Send verify code - Generic Error", { error: (0, utils_2.getError)(error) });
|
|
449
|
-
}
|
|
450
584
|
return false;
|
|
451
585
|
}
|
|
452
586
|
async listTrustDevice() {
|
|
@@ -454,7 +588,7 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
454
588
|
try {
|
|
455
589
|
const response = await this.request({
|
|
456
590
|
method: "get",
|
|
457
|
-
endpoint: "v1/app/trust_device/list"
|
|
591
|
+
endpoint: "v1/app/trust_device/list",
|
|
458
592
|
});
|
|
459
593
|
if (response.status == 200) {
|
|
460
594
|
const result = response.data;
|
|
@@ -464,11 +598,19 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
464
598
|
}
|
|
465
599
|
}
|
|
466
600
|
else {
|
|
467
|
-
logging_1.rootHTTPLogger.error("List trust device - Response code not ok", {
|
|
601
|
+
logging_1.rootHTTPLogger.error("List trust device - Response code not ok", {
|
|
602
|
+
code: result.code,
|
|
603
|
+
msg: result.msg,
|
|
604
|
+
data: response.data,
|
|
605
|
+
});
|
|
468
606
|
}
|
|
469
607
|
}
|
|
470
608
|
else {
|
|
471
|
-
logging_1.rootHTTPLogger.error("List trust device - Status return code not 200", {
|
|
609
|
+
logging_1.rootHTTPLogger.error("List trust device - Status return code not 200", {
|
|
610
|
+
status: response.status,
|
|
611
|
+
statusText: response.statusText,
|
|
612
|
+
data: response.data,
|
|
613
|
+
});
|
|
472
614
|
}
|
|
473
615
|
}
|
|
474
616
|
catch (err) {
|
|
@@ -479,120 +621,122 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
479
621
|
return [];
|
|
480
622
|
}
|
|
481
623
|
async addTrustDevice(verifyCode) {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
return true;
|
|
504
|
-
}
|
|
505
|
-
else {
|
|
506
|
-
logging_1.rootHTTPLogger.error("Add trust device - Response code not ok", { code: result.code, msg: result.msg, verifyCode: verifyCode, data: response.data });
|
|
507
|
-
}
|
|
624
|
+
const data = {
|
|
625
|
+
verify_code: verifyCode,
|
|
626
|
+
transaction: `${new Date().getTime()}`,
|
|
627
|
+
};
|
|
628
|
+
const response = await this.makePostRequest(this.apiTrustDevice, data);
|
|
629
|
+
if (response != undefined) {
|
|
630
|
+
logging_1.rootHTTPLogger.debug("Add trust device - Response trust device", { verifyCode: verifyCode, data: response.data });
|
|
631
|
+
if (response.status == 200) {
|
|
632
|
+
const result = response.data;
|
|
633
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
634
|
+
logging_1.rootHTTPLogger.info(`2FA authentication successfully done. Device trusted.`);
|
|
635
|
+
const trusted_devices = await this.listTrustDevice();
|
|
636
|
+
trusted_devices.forEach((trusted_device) => {
|
|
637
|
+
if (trusted_device.is_current_device === 1) {
|
|
638
|
+
logging_1.rootHTTPLogger.debug("Add trust device - This device is trusted. Token expiration extended:", {
|
|
639
|
+
trustDevice: { phoneModel: trusted_device.phone_model, openUdid: trusted_device.open_udid },
|
|
640
|
+
tokenExpiration: this.tokenExpiration,
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
return true;
|
|
508
645
|
}
|
|
509
646
|
else {
|
|
510
|
-
logging_1.rootHTTPLogger.error("Add trust device -
|
|
647
|
+
logging_1.rootHTTPLogger.error("Add trust device - Response code not ok", {
|
|
648
|
+
code: result.code,
|
|
649
|
+
msg: result.msg,
|
|
650
|
+
verifyCode: verifyCode,
|
|
651
|
+
data: response.data,
|
|
652
|
+
});
|
|
511
653
|
}
|
|
512
654
|
}
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
655
|
+
else {
|
|
656
|
+
logging_1.rootHTTPLogger.error("Add trust device - Status return code not 200", {
|
|
657
|
+
status: response.status,
|
|
658
|
+
statusText: response.statusText,
|
|
659
|
+
verifyCode: verifyCode,
|
|
660
|
+
data: response.data,
|
|
661
|
+
});
|
|
516
662
|
}
|
|
517
663
|
}
|
|
518
664
|
return false;
|
|
519
665
|
}
|
|
520
666
|
async getStationList() {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
if (result.data) {
|
|
540
|
-
const stationList = this.decryptAPIData(result.data);
|
|
541
|
-
logging_1.rootHTTPLogger.debug("Decrypted station list data", stationList);
|
|
542
|
-
return stationList;
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
else {
|
|
546
|
-
logging_1.rootHTTPLogger.error("Station list - Response code not ok", { code: result.code, msg: result.msg, data: response.data });
|
|
667
|
+
const data = {
|
|
668
|
+
device_sn: "",
|
|
669
|
+
num: 1000,
|
|
670
|
+
orderby: "",
|
|
671
|
+
page: 0,
|
|
672
|
+
station_sn: "",
|
|
673
|
+
time_zone: new Date().getTimezoneOffset() !== 0 ? -new Date().getTimezoneOffset() * 60 * 1000 : 0,
|
|
674
|
+
transaction: `${new Date().getTime()}`,
|
|
675
|
+
};
|
|
676
|
+
const response = await this.makePostRequest(this.apiGetStations, data);
|
|
677
|
+
if (response != undefined) {
|
|
678
|
+
if (response.status == 200) {
|
|
679
|
+
const result = response.data;
|
|
680
|
+
if (result.code == 0) {
|
|
681
|
+
if (result.data) {
|
|
682
|
+
const stationList = this.decryptAPIData(result.data);
|
|
683
|
+
logging_1.rootHTTPLogger.debug("Decrypted station list data", stationList);
|
|
684
|
+
return stationList;
|
|
547
685
|
}
|
|
548
686
|
}
|
|
549
687
|
else {
|
|
550
|
-
logging_1.rootHTTPLogger.error("Station list -
|
|
688
|
+
logging_1.rootHTTPLogger.error("Station list - Response code not ok", {
|
|
689
|
+
code: result.code,
|
|
690
|
+
msg: result.msg,
|
|
691
|
+
data: response.data,
|
|
692
|
+
});
|
|
551
693
|
}
|
|
552
694
|
}
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
695
|
+
else {
|
|
696
|
+
logging_1.rootHTTPLogger.error("Station list - Status return code not 200", {
|
|
697
|
+
status: response.status,
|
|
698
|
+
statusText: response.statusText,
|
|
699
|
+
data: response.data,
|
|
700
|
+
});
|
|
556
701
|
}
|
|
557
702
|
}
|
|
558
703
|
return [];
|
|
559
704
|
}
|
|
560
705
|
async getDeviceList() {
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
if (result.data) {
|
|
580
|
-
const deviceList = this.decryptAPIData(result.data);
|
|
581
|
-
logging_1.rootHTTPLogger.debug("Decrypted device list data", deviceList);
|
|
582
|
-
return deviceList;
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
else {
|
|
586
|
-
logging_1.rootHTTPLogger.error("Device list - Response code not ok", { code: result.code, msg: result.msg, data: response.data });
|
|
706
|
+
const data = {
|
|
707
|
+
device_sn: "",
|
|
708
|
+
num: 1000,
|
|
709
|
+
orderby: "",
|
|
710
|
+
page: 0,
|
|
711
|
+
station_sn: "",
|
|
712
|
+
time_zone: new Date().getTimezoneOffset() !== 0 ? -new Date().getTimezoneOffset() * 60 * 1000 : 0,
|
|
713
|
+
transaction: `${new Date().getTime()}`,
|
|
714
|
+
};
|
|
715
|
+
const response = await this.makePostRequest(this.apiGetDevices, data);
|
|
716
|
+
if (response != undefined) {
|
|
717
|
+
if (response.status == 200) {
|
|
718
|
+
const result = response.data;
|
|
719
|
+
if (result.code == 0) {
|
|
720
|
+
if (result.data) {
|
|
721
|
+
const deviceList = this.decryptAPIData(result.data);
|
|
722
|
+
logging_1.rootHTTPLogger.debug("Decrypted device list data", deviceList);
|
|
723
|
+
return deviceList;
|
|
587
724
|
}
|
|
588
725
|
}
|
|
589
726
|
else {
|
|
590
|
-
logging_1.rootHTTPLogger.error("Device list -
|
|
727
|
+
logging_1.rootHTTPLogger.error("Device list - Response code not ok", {
|
|
728
|
+
code: result.code,
|
|
729
|
+
msg: result.msg,
|
|
730
|
+
data: response.data,
|
|
731
|
+
});
|
|
591
732
|
}
|
|
592
733
|
}
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
734
|
+
else {
|
|
735
|
+
logging_1.rootHTTPLogger.error("Device list - Status return code not 200", {
|
|
736
|
+
status: response.status,
|
|
737
|
+
statusText: response.statusText,
|
|
738
|
+
data: response.data,
|
|
739
|
+
});
|
|
596
740
|
}
|
|
597
741
|
}
|
|
598
742
|
return [];
|
|
@@ -601,7 +745,7 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
601
745
|
//Get Houses
|
|
602
746
|
const houses = await this.getHouseList();
|
|
603
747
|
if (houses && houses.length > 0) {
|
|
604
|
-
houses.forEach(element => {
|
|
748
|
+
houses.forEach((element) => {
|
|
605
749
|
this.houses[element.house_id] = element;
|
|
606
750
|
});
|
|
607
751
|
}
|
|
@@ -614,7 +758,7 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
614
758
|
//Get Stations
|
|
615
759
|
const stations = await this.getStationList();
|
|
616
760
|
if (stations && stations.length > 0) {
|
|
617
|
-
stations.forEach(element => {
|
|
761
|
+
stations.forEach((element) => {
|
|
618
762
|
this.hubs[element.station_sn] = element;
|
|
619
763
|
});
|
|
620
764
|
}
|
|
@@ -627,7 +771,7 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
627
771
|
//Get Devices
|
|
628
772
|
const devices = await this.getDeviceList();
|
|
629
773
|
if (devices && devices.length > 0) {
|
|
630
|
-
devices.forEach(element => {
|
|
774
|
+
devices.forEach((element) => {
|
|
631
775
|
this.devices[element.device_sn] = element;
|
|
632
776
|
});
|
|
633
777
|
}
|
|
@@ -646,7 +790,13 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
646
790
|
await this.refreshDeviceData();
|
|
647
791
|
}
|
|
648
792
|
async request(request, withoutUrlPrefix = false) {
|
|
649
|
-
logging_1.rootHTTPLogger.debug("Api request", {
|
|
793
|
+
logging_1.rootHTTPLogger.debug("Api request", {
|
|
794
|
+
method: request.method,
|
|
795
|
+
endpoint: request.endpoint,
|
|
796
|
+
responseType: request.responseType,
|
|
797
|
+
token: this.token,
|
|
798
|
+
data: request.data,
|
|
799
|
+
});
|
|
650
800
|
try {
|
|
651
801
|
let options;
|
|
652
802
|
switch (request.responseType) {
|
|
@@ -690,159 +840,187 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
690
840
|
if (error instanceof (await import("got")).HTTPError) {
|
|
691
841
|
if (error.response.statusCode === 401) {
|
|
692
842
|
this.invalidateToken();
|
|
693
|
-
logging_1.rootHTTPLogger.error("Status return code 401, invalidate token", {
|
|
843
|
+
logging_1.rootHTTPLogger.error("Status return code 401, invalidate token", {
|
|
844
|
+
status: error.response.statusCode,
|
|
845
|
+
statusText: error.response.statusMessage,
|
|
846
|
+
});
|
|
694
847
|
this.emit("close");
|
|
695
848
|
}
|
|
696
849
|
}
|
|
697
|
-
throw new error_2.ApiRequestError("API request error", {
|
|
850
|
+
throw new error_2.ApiRequestError("API request error", {
|
|
851
|
+
cause: error,
|
|
852
|
+
context: {
|
|
853
|
+
method: request.method,
|
|
854
|
+
endpoint: request.endpoint,
|
|
855
|
+
responseType: request.responseType,
|
|
856
|
+
token: this.token,
|
|
857
|
+
data: request.data,
|
|
858
|
+
},
|
|
859
|
+
});
|
|
698
860
|
}
|
|
699
861
|
}
|
|
700
862
|
async checkPushToken() {
|
|
701
863
|
//Check push notification token
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
const result = response.data;
|
|
714
|
-
if (result.code == 0) {
|
|
715
|
-
logging_1.rootHTTPLogger.debug(`Check push token - Push token OK`);
|
|
716
|
-
return true;
|
|
717
|
-
}
|
|
718
|
-
else {
|
|
719
|
-
logging_1.rootHTTPLogger.error("Check push token - Response code not ok", { code: result.code, msg: result.msg, data: response.data });
|
|
720
|
-
}
|
|
864
|
+
const data = {
|
|
865
|
+
app_type: "eufySecurity",
|
|
866
|
+
transaction: `${new Date().getTime()}`,
|
|
867
|
+
};
|
|
868
|
+
const response = await this.makePostRequest(this.apiPushCheck, data);
|
|
869
|
+
if (response !== undefined) {
|
|
870
|
+
if (response.status == 200) {
|
|
871
|
+
const result = response.data;
|
|
872
|
+
if (result.code == 0) {
|
|
873
|
+
logging_1.rootHTTPLogger.debug(`Check push token - Push token OK`);
|
|
874
|
+
return true;
|
|
721
875
|
}
|
|
722
876
|
else {
|
|
723
|
-
logging_1.rootHTTPLogger.error("Check push token -
|
|
877
|
+
logging_1.rootHTTPLogger.error("Check push token - Response code not ok", {
|
|
878
|
+
code: result.code,
|
|
879
|
+
msg: result.msg,
|
|
880
|
+
data: response.data,
|
|
881
|
+
});
|
|
724
882
|
}
|
|
725
883
|
}
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
884
|
+
else {
|
|
885
|
+
logging_1.rootHTTPLogger.error("Check push token - Status return code not 200", {
|
|
886
|
+
status: response.status,
|
|
887
|
+
statusText: response.statusText,
|
|
888
|
+
data: response.data,
|
|
889
|
+
});
|
|
729
890
|
}
|
|
730
891
|
}
|
|
731
892
|
return false;
|
|
732
893
|
}
|
|
733
894
|
async registerPushToken(token) {
|
|
734
895
|
//Register push notification token
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
const result = response.data;
|
|
748
|
-
if (result.code == 0) {
|
|
749
|
-
logging_1.rootHTTPLogger.debug(`Register push token - Push token registered successfully`);
|
|
750
|
-
return true;
|
|
751
|
-
}
|
|
752
|
-
else {
|
|
753
|
-
logging_1.rootHTTPLogger.error("Register push token - Response code not ok", { code: result.code, msg: result.msg, data: response.data, token: token });
|
|
754
|
-
}
|
|
896
|
+
const data = {
|
|
897
|
+
is_notification_enable: true,
|
|
898
|
+
token: token,
|
|
899
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
900
|
+
};
|
|
901
|
+
const response = await this.makePostRequest(this.apiRegisterPushToken, data);
|
|
902
|
+
if (response !== undefined) {
|
|
903
|
+
if (response.status == 200) {
|
|
904
|
+
const result = response.data;
|
|
905
|
+
if (result.code == 0) {
|
|
906
|
+
logging_1.rootHTTPLogger.debug(`Register push token - Push token registered successfully`);
|
|
907
|
+
return true;
|
|
755
908
|
}
|
|
756
909
|
else {
|
|
757
|
-
logging_1.rootHTTPLogger.error("Register push token -
|
|
910
|
+
logging_1.rootHTTPLogger.error("Register push token - Response code not ok", {
|
|
911
|
+
code: result.code,
|
|
912
|
+
msg: result.msg,
|
|
913
|
+
data: response.data,
|
|
914
|
+
token: token,
|
|
915
|
+
});
|
|
758
916
|
}
|
|
759
917
|
}
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
918
|
+
else {
|
|
919
|
+
logging_1.rootHTTPLogger.error("Register push token - Status return code not 200", {
|
|
920
|
+
status: response.status,
|
|
921
|
+
statusText: response.statusText,
|
|
922
|
+
data: response.data,
|
|
923
|
+
token: token,
|
|
924
|
+
});
|
|
763
925
|
}
|
|
764
926
|
}
|
|
765
927
|
return false;
|
|
766
928
|
}
|
|
767
929
|
async setParameters(stationSN, deviceSN, params) {
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
930
|
+
const tmp_params = [];
|
|
931
|
+
params.forEach((param) => {
|
|
932
|
+
tmp_params.push({
|
|
933
|
+
param_type: param.paramType,
|
|
934
|
+
param_value: parameter_1.ParameterHelper.writeValue(param.paramType, param.paramValue),
|
|
772
935
|
});
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
}
|
|
936
|
+
});
|
|
937
|
+
const data = {
|
|
938
|
+
device_sn: deviceSN,
|
|
939
|
+
station_sn: stationSN,
|
|
940
|
+
params: tmp_params,
|
|
941
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
942
|
+
};
|
|
943
|
+
const response = await this.makePostRequest(this.apiSetParameters, data);
|
|
944
|
+
if (response !== undefined) {
|
|
945
|
+
logging_1.rootHTTPLogger.debug("Set parameter - Response:", {
|
|
946
|
+
stationSN: stationSN,
|
|
947
|
+
deviceSN: deviceSN,
|
|
948
|
+
params: tmp_params,
|
|
949
|
+
response: response.data,
|
|
950
|
+
});
|
|
951
|
+
if (response.status == 200) {
|
|
952
|
+
const result = response.data;
|
|
953
|
+
if (result.code == 0) {
|
|
954
|
+
const dataresult = result.data;
|
|
955
|
+
logging_1.rootHTTPLogger.debug("Set parameter - New parameters set", { params: tmp_params, response: dataresult });
|
|
956
|
+
return true;
|
|
795
957
|
}
|
|
796
958
|
else {
|
|
797
|
-
logging_1.rootHTTPLogger.error("Set parameter -
|
|
959
|
+
logging_1.rootHTTPLogger.error("Set parameter - Response code not ok", {
|
|
960
|
+
code: result.code,
|
|
961
|
+
msg: result.msg,
|
|
962
|
+
data: response.data,
|
|
963
|
+
stationSN: stationSN,
|
|
964
|
+
deviceSN: deviceSN,
|
|
965
|
+
params: params,
|
|
966
|
+
});
|
|
798
967
|
}
|
|
799
968
|
}
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
969
|
+
else {
|
|
970
|
+
logging_1.rootHTTPLogger.error("Set parameter - Status return code not 200", {
|
|
971
|
+
status: response.status,
|
|
972
|
+
statusText: response.statusText,
|
|
973
|
+
data: response.data,
|
|
974
|
+
stationSN: stationSN,
|
|
975
|
+
deviceSN: deviceSN,
|
|
976
|
+
params: params,
|
|
977
|
+
});
|
|
803
978
|
}
|
|
804
979
|
}
|
|
805
980
|
return false;
|
|
806
981
|
}
|
|
807
982
|
async getCiphers(/*stationSN: string, */ cipherIDs, userID) {
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
if (
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
if (Array.isArray(decrypted)) {
|
|
828
|
-
decrypted.forEach((cipher) => {
|
|
829
|
-
ciphers[cipher.cipher_id] = cipher;
|
|
830
|
-
});
|
|
831
|
-
}
|
|
832
|
-
return ciphers;
|
|
983
|
+
const data = {
|
|
984
|
+
cipher_ids: cipherIDs,
|
|
985
|
+
user_id: userID,
|
|
986
|
+
//sn: stationSN
|
|
987
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
988
|
+
};
|
|
989
|
+
const response = await this.makePostRequest(this.apiGetCiphers, data);
|
|
990
|
+
if (response !== undefined) {
|
|
991
|
+
if (response.status == 200) {
|
|
992
|
+
const result = response.data;
|
|
993
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
994
|
+
if (result.data) {
|
|
995
|
+
const ciphers = {};
|
|
996
|
+
const decrypted = this.decryptAPIData(result.data);
|
|
997
|
+
logging_1.rootHTTPLogger.debug("Get ciphers - Decrypted ciphers data", { ciphers: decrypted });
|
|
998
|
+
if (Array.isArray(decrypted)) {
|
|
999
|
+
decrypted.forEach((cipher) => {
|
|
1000
|
+
ciphers[cipher.cipher_id] = cipher;
|
|
1001
|
+
});
|
|
833
1002
|
}
|
|
834
|
-
|
|
835
|
-
else {
|
|
836
|
-
logging_1.rootHTTPLogger.error("Get ciphers - Response code not ok", { code: result.code, msg: result.msg, data: response.data, cipherIDs: cipherIDs, userID: userID });
|
|
1003
|
+
return ciphers;
|
|
837
1004
|
}
|
|
838
1005
|
}
|
|
839
1006
|
else {
|
|
840
|
-
logging_1.rootHTTPLogger.error("Get ciphers -
|
|
1007
|
+
logging_1.rootHTTPLogger.error("Get ciphers - Response code not ok", {
|
|
1008
|
+
code: result.code,
|
|
1009
|
+
msg: result.msg,
|
|
1010
|
+
data: response.data,
|
|
1011
|
+
cipherIDs: cipherIDs,
|
|
1012
|
+
userID: userID,
|
|
1013
|
+
});
|
|
841
1014
|
}
|
|
842
1015
|
}
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
1016
|
+
else {
|
|
1017
|
+
logging_1.rootHTTPLogger.error("Get ciphers - Status return code not 200", {
|
|
1018
|
+
status: response.status,
|
|
1019
|
+
statusText: response.statusText,
|
|
1020
|
+
data: response.data,
|
|
1021
|
+
cipherIDs: cipherIDs,
|
|
1022
|
+
userID: userID,
|
|
1023
|
+
});
|
|
846
1024
|
}
|
|
847
1025
|
}
|
|
848
1026
|
return {};
|
|
@@ -852,7 +1030,7 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
852
1030
|
try {
|
|
853
1031
|
const response = await this.request({
|
|
854
1032
|
method: "get",
|
|
855
|
-
endpoint: `v1/voice/response/lists/${deviceSN}
|
|
1033
|
+
endpoint: `v1/voice/response/lists/${deviceSN}`,
|
|
856
1034
|
});
|
|
857
1035
|
if (response.status == 200) {
|
|
858
1036
|
const result = response.data;
|
|
@@ -866,11 +1044,21 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
866
1044
|
}
|
|
867
1045
|
}
|
|
868
1046
|
else {
|
|
869
|
-
logging_1.rootHTTPLogger.error("Get Voices - Response code not ok", {
|
|
1047
|
+
logging_1.rootHTTPLogger.error("Get Voices - Response code not ok", {
|
|
1048
|
+
code: result.code,
|
|
1049
|
+
msg: result.msg,
|
|
1050
|
+
data: response.data,
|
|
1051
|
+
deviceSN: deviceSN,
|
|
1052
|
+
});
|
|
870
1053
|
}
|
|
871
1054
|
}
|
|
872
1055
|
else {
|
|
873
|
-
logging_1.rootHTTPLogger.error("Get Voices - Status return code not 200", {
|
|
1056
|
+
logging_1.rootHTTPLogger.error("Get Voices - Status return code not 200", {
|
|
1057
|
+
status: response.status,
|
|
1058
|
+
statusText: response.statusText,
|
|
1059
|
+
data: response.data,
|
|
1060
|
+
deviceSN: deviceSN,
|
|
1061
|
+
});
|
|
874
1062
|
}
|
|
875
1063
|
}
|
|
876
1064
|
catch (err) {
|
|
@@ -899,188 +1087,201 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
899
1087
|
this.token = token;
|
|
900
1088
|
this.requestEufyCloud.defaults.options.merge({
|
|
901
1089
|
headers: {
|
|
902
|
-
"X-Auth-Token": token
|
|
903
|
-
}
|
|
1090
|
+
"X-Auth-Token": token,
|
|
1091
|
+
},
|
|
904
1092
|
});
|
|
905
1093
|
}
|
|
906
1094
|
setTokenExpiration(tokenExpiration) {
|
|
907
1095
|
this.tokenExpiration = tokenExpiration;
|
|
908
1096
|
}
|
|
909
1097
|
getAPIBase() {
|
|
910
|
-
return typeof this.requestEufyCloud.defaults.options.prefixUrl === "string"
|
|
1098
|
+
return typeof this.requestEufyCloud.defaults.options.prefixUrl === "string"
|
|
1099
|
+
? this.requestEufyCloud.defaults.options.prefixUrl
|
|
1100
|
+
: this.requestEufyCloud.defaults.options.prefixUrl.toString();
|
|
911
1101
|
}
|
|
912
1102
|
setOpenUDID(openudid) {
|
|
913
1103
|
this.headers.openudid = openudid;
|
|
914
|
-
this.
|
|
915
|
-
headers: this.headers
|
|
916
|
-
});
|
|
1104
|
+
this.updateApiHeader();
|
|
917
1105
|
}
|
|
918
1106
|
setSerialNumber(serialnumber) {
|
|
919
1107
|
this.headers.sn = serialnumber;
|
|
920
|
-
this.
|
|
921
|
-
headers: this.headers
|
|
922
|
-
});
|
|
1108
|
+
this.updateApiHeader();
|
|
923
1109
|
}
|
|
924
1110
|
async _getEvents(functionName, endpoint, startTime, endTime, filter, maxResults) {
|
|
925
1111
|
const records = [];
|
|
926
|
-
if (
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
logging_1.rootHTTPLogger.debug(`${functionName} - Record:`, record);
|
|
962
|
-
records.push(record);
|
|
963
|
-
});
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
else {
|
|
967
|
-
logging_1.rootHTTPLogger.error(`${functionName} - Response data is missing`, { code: result.code, msg: result.msg, data: response.data, endpoint: endpoint, startTime: startTime, endTime: endTime, filter: filter, maxResults: maxResults });
|
|
968
|
-
}
|
|
1112
|
+
if (filter === undefined)
|
|
1113
|
+
filter = { deviceSN: "", stationSN: "", storageType: types_1.StorageType.NONE };
|
|
1114
|
+
if (maxResults === undefined)
|
|
1115
|
+
maxResults = 1000;
|
|
1116
|
+
const data = {
|
|
1117
|
+
device_sn: filter.deviceSN !== undefined ? filter.deviceSN : "",
|
|
1118
|
+
end_time: Math.trunc(endTime.getTime() / 1000),
|
|
1119
|
+
exclude_guest: false,
|
|
1120
|
+
house_id: "HOUSEID_ALL_DEVICE",
|
|
1121
|
+
id: 0,
|
|
1122
|
+
id_type: 1,
|
|
1123
|
+
is_favorite: false,
|
|
1124
|
+
num: maxResults,
|
|
1125
|
+
pullup: true,
|
|
1126
|
+
shared: true,
|
|
1127
|
+
start_time: Math.trunc(startTime.getTime() / 1000),
|
|
1128
|
+
station_sn: filter.stationSN !== undefined ? filter.stationSN : "",
|
|
1129
|
+
storage: filter.storageType !== undefined ? filter.storageType : types_1.StorageType.NONE,
|
|
1130
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1131
|
+
};
|
|
1132
|
+
const response = await this.makePostRequest(endpoint, data);
|
|
1133
|
+
if (response !== undefined) {
|
|
1134
|
+
logging_1.rootHTTPLogger.debug(`${functionName} - Response:`, response.data);
|
|
1135
|
+
if (response.status == 200) {
|
|
1136
|
+
const result = response.data;
|
|
1137
|
+
if (result.code == 0) {
|
|
1138
|
+
if (result.data) {
|
|
1139
|
+
const dataresult = this.decryptAPIData(result.data);
|
|
1140
|
+
logging_1.rootHTTPLogger.debug(`${functionName} - Decrypted data:`, dataresult);
|
|
1141
|
+
if (dataresult) {
|
|
1142
|
+
dataresult.forEach((record) => {
|
|
1143
|
+
logging_1.rootHTTPLogger.debug(`${functionName} - Record:`, record);
|
|
1144
|
+
records.push(record);
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
969
1147
|
}
|
|
970
1148
|
else {
|
|
971
|
-
logging_1.rootHTTPLogger.error(`${functionName} - Response
|
|
1149
|
+
logging_1.rootHTTPLogger.error(`${functionName} - Response data is missing`, {
|
|
1150
|
+
code: result.code,
|
|
1151
|
+
msg: result.msg,
|
|
1152
|
+
data: response.data,
|
|
1153
|
+
endpoint: endpoint,
|
|
1154
|
+
startTime: startTime,
|
|
1155
|
+
endTime: endTime,
|
|
1156
|
+
filter: filter,
|
|
1157
|
+
maxResults: maxResults,
|
|
1158
|
+
});
|
|
972
1159
|
}
|
|
973
1160
|
}
|
|
974
1161
|
else {
|
|
975
|
-
logging_1.rootHTTPLogger.error(`${functionName} -
|
|
1162
|
+
logging_1.rootHTTPLogger.error(`${functionName} - Response code not ok`, {
|
|
1163
|
+
code: result.code,
|
|
1164
|
+
msg: result.msg,
|
|
1165
|
+
data: response.data,
|
|
1166
|
+
endpoint: endpoint,
|
|
1167
|
+
startTime: startTime,
|
|
1168
|
+
endTime: endTime,
|
|
1169
|
+
filter: filter,
|
|
1170
|
+
maxResults: maxResults,
|
|
1171
|
+
});
|
|
976
1172
|
}
|
|
977
1173
|
}
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
1174
|
+
else {
|
|
1175
|
+
logging_1.rootHTTPLogger.error(`${functionName} - Status return code not 200`, {
|
|
1176
|
+
status: response.status,
|
|
1177
|
+
statusText: response.statusText,
|
|
1178
|
+
data: response.data,
|
|
1179
|
+
endpoint: endpoint,
|
|
1180
|
+
startTime: startTime,
|
|
1181
|
+
endTime: endTime,
|
|
1182
|
+
filter: filter,
|
|
1183
|
+
maxResults: maxResults,
|
|
1184
|
+
});
|
|
981
1185
|
}
|
|
982
1186
|
}
|
|
983
1187
|
return records;
|
|
984
1188
|
}
|
|
985
1189
|
async getVideoEvents(startTime, endTime, filter, maxResults) {
|
|
986
|
-
return this._getEvents("getVideoEvents",
|
|
1190
|
+
return this._getEvents("getVideoEvents", this.apiEventGetAllVideoRecord, startTime, endTime, filter, maxResults);
|
|
987
1191
|
}
|
|
988
1192
|
async getAlarmEvents(startTime, endTime, filter, maxResults) {
|
|
989
|
-
return this._getEvents("getAlarmEvents",
|
|
1193
|
+
return this._getEvents("getAlarmEvents", this.apiEventGetAllAlarmRecord, startTime, endTime, filter, maxResults);
|
|
990
1194
|
}
|
|
991
1195
|
async getHistoryEvents(startTime, endTime, filter, maxResults) {
|
|
992
|
-
return this._getEvents("getHistoryEvents",
|
|
1196
|
+
return this._getEvents("getHistoryEvents", this.apiEventGetAllHistoryRecord, startTime, endTime, filter, maxResults);
|
|
993
1197
|
}
|
|
994
1198
|
async getAllVideoEvents(filter, maxResults) {
|
|
995
|
-
|
|
996
|
-
return this.getVideoEvents(new Date(new Date().getTime() - fifteenYearsInMilliseconds), new Date(), filter, maxResults);
|
|
1199
|
+
return this.getVideoEvents(new Date(new Date().getTime() - this.FIFTEEN_YEARS_IN_MS), new Date(), filter, maxResults);
|
|
997
1200
|
}
|
|
998
1201
|
async getAllAlarmEvents(filter, maxResults) {
|
|
999
|
-
|
|
1000
|
-
return this.getAlarmEvents(new Date(new Date().getTime() - fifteenYearsInMilliseconds), new Date(), filter, maxResults);
|
|
1202
|
+
return this.getAlarmEvents(new Date(new Date().getTime() - this.FIFTEEN_YEARS_IN_MS), new Date(), filter, maxResults);
|
|
1001
1203
|
}
|
|
1002
1204
|
async getAllHistoryEvents(filter, maxResults) {
|
|
1003
|
-
|
|
1004
|
-
return this.getHistoryEvents(new Date(new Date().getTime() - fifteenYearsInMilliseconds), new Date(), filter, maxResults);
|
|
1205
|
+
return this.getHistoryEvents(new Date(new Date().getTime() - this.FIFTEEN_YEARS_IN_MS), new Date(), filter, maxResults);
|
|
1005
1206
|
}
|
|
1006
1207
|
isConnected() {
|
|
1007
1208
|
return this.connected;
|
|
1008
1209
|
}
|
|
1009
1210
|
async getInvites() {
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
if (
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
if (data === undefined)
|
|
1035
|
-
data = [];
|
|
1036
|
-
invites[invite.invite_id].devices = data;
|
|
1037
|
-
});
|
|
1038
|
-
}
|
|
1039
|
-
return invites;
|
|
1211
|
+
const data = {
|
|
1212
|
+
num: 100,
|
|
1213
|
+
orderby: "",
|
|
1214
|
+
own: false,
|
|
1215
|
+
page: 0,
|
|
1216
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1217
|
+
};
|
|
1218
|
+
const response = await this.makePostRequest(this.apiGetInvites, data);
|
|
1219
|
+
if (response !== undefined) {
|
|
1220
|
+
if (response.status == 200) {
|
|
1221
|
+
const result = response.data;
|
|
1222
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1223
|
+
if (result.data) {
|
|
1224
|
+
const invites = {};
|
|
1225
|
+
const decrypted = this.decryptAPIData(result.data);
|
|
1226
|
+
logging_1.rootHTTPLogger.debug("Get invites - Decrypted invites data", { invites: decrypted });
|
|
1227
|
+
if (Array.isArray(decrypted)) {
|
|
1228
|
+
decrypted.forEach((invite) => {
|
|
1229
|
+
invites[invite.invite_id] = invite;
|
|
1230
|
+
let data = (0, utils_2.parseJSON)(invites[invite.invite_id].devices, logging_1.rootHTTPLogger);
|
|
1231
|
+
if (data === undefined)
|
|
1232
|
+
data = [];
|
|
1233
|
+
invites[invite.invite_id].devices = data;
|
|
1234
|
+
});
|
|
1040
1235
|
}
|
|
1041
|
-
|
|
1042
|
-
else {
|
|
1043
|
-
logging_1.rootHTTPLogger.error("Get invites - Response code not ok", { code: result.code, msg: result.msg, data: response.data });
|
|
1236
|
+
return invites;
|
|
1044
1237
|
}
|
|
1045
1238
|
}
|
|
1046
1239
|
else {
|
|
1047
|
-
logging_1.rootHTTPLogger.error("Get invites -
|
|
1240
|
+
logging_1.rootHTTPLogger.error("Get invites - Response code not ok", {
|
|
1241
|
+
code: result.code,
|
|
1242
|
+
msg: result.msg,
|
|
1243
|
+
data: response.data,
|
|
1244
|
+
});
|
|
1048
1245
|
}
|
|
1049
1246
|
}
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1247
|
+
else {
|
|
1248
|
+
logging_1.rootHTTPLogger.error("Get invites - Status return code not 200", {
|
|
1249
|
+
status: response.status,
|
|
1250
|
+
statusText: response.statusText,
|
|
1251
|
+
data: response.data,
|
|
1252
|
+
});
|
|
1053
1253
|
}
|
|
1054
1254
|
}
|
|
1055
1255
|
return {};
|
|
1056
1256
|
}
|
|
1057
1257
|
async confirmInvites(confirmInvites) {
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
if (response.status == 200) {
|
|
1069
|
-
const result = response.data;
|
|
1070
|
-
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1071
|
-
return true;
|
|
1072
|
-
}
|
|
1073
|
-
else {
|
|
1074
|
-
logging_1.rootHTTPLogger.error("Confirm invites - Response code not ok", { code: result.code, msg: result.msg, data: response.data, confirmInvites: confirmInvites });
|
|
1075
|
-
}
|
|
1258
|
+
const data = {
|
|
1259
|
+
invites: confirmInvites,
|
|
1260
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1261
|
+
};
|
|
1262
|
+
const response = await this.makePostRequest(this.apiConfirmInvite, data);
|
|
1263
|
+
if (response !== undefined) {
|
|
1264
|
+
if (response.status == 200) {
|
|
1265
|
+
const result = response.data;
|
|
1266
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1267
|
+
return true;
|
|
1076
1268
|
}
|
|
1077
1269
|
else {
|
|
1078
|
-
logging_1.rootHTTPLogger.error("Confirm invites -
|
|
1270
|
+
logging_1.rootHTTPLogger.error("Confirm invites - Response code not ok", {
|
|
1271
|
+
code: result.code,
|
|
1272
|
+
msg: result.msg,
|
|
1273
|
+
data: response.data,
|
|
1274
|
+
confirmInvites: confirmInvites,
|
|
1275
|
+
});
|
|
1079
1276
|
}
|
|
1080
1277
|
}
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1278
|
+
else {
|
|
1279
|
+
logging_1.rootHTTPLogger.error("Confirm invites - Status return code not 200", {
|
|
1280
|
+
status: response.status,
|
|
1281
|
+
statusText: response.statusText,
|
|
1282
|
+
data: response.data,
|
|
1283
|
+
confirmInvites: confirmInvites,
|
|
1284
|
+
});
|
|
1084
1285
|
}
|
|
1085
1286
|
}
|
|
1086
1287
|
return false;
|
|
@@ -1095,7 +1296,7 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1095
1296
|
else {
|
|
1096
1297
|
const response = await this.request({
|
|
1097
1298
|
method: "get",
|
|
1098
|
-
endpoint: `v1/app/public_key/query?device_sn=${deviceSN}&type=${type}
|
|
1299
|
+
endpoint: `v1/app/public_key/query?device_sn=${deviceSN}&type=${type}`,
|
|
1099
1300
|
});
|
|
1100
1301
|
if (response.status == 200) {
|
|
1101
1302
|
const result = response.data;
|
|
@@ -1107,17 +1308,33 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1107
1308
|
}
|
|
1108
1309
|
}
|
|
1109
1310
|
else {
|
|
1110
|
-
logging_1.rootHTTPLogger.error("Get public key - Response code not ok", {
|
|
1311
|
+
logging_1.rootHTTPLogger.error("Get public key - Response code not ok", {
|
|
1312
|
+
code: result.code,
|
|
1313
|
+
msg: result.msg,
|
|
1314
|
+
data: response.data,
|
|
1315
|
+
deviceSN: deviceSN,
|
|
1316
|
+
type: type,
|
|
1317
|
+
});
|
|
1111
1318
|
}
|
|
1112
1319
|
}
|
|
1113
1320
|
else {
|
|
1114
|
-
logging_1.rootHTTPLogger.error("Get public key - Status return code not 200", {
|
|
1321
|
+
logging_1.rootHTTPLogger.error("Get public key - Status return code not 200", {
|
|
1322
|
+
status: response.status,
|
|
1323
|
+
statusText: response.statusText,
|
|
1324
|
+
data: response.data,
|
|
1325
|
+
deviceSN: deviceSN,
|
|
1326
|
+
type: type,
|
|
1327
|
+
});
|
|
1115
1328
|
}
|
|
1116
1329
|
}
|
|
1117
1330
|
}
|
|
1118
1331
|
catch (err) {
|
|
1119
1332
|
const error = (0, error_1.ensureError)(err);
|
|
1120
|
-
logging_1.rootHTTPLogger.error("Get public key - Generic Error", {
|
|
1333
|
+
logging_1.rootHTTPLogger.error("Get public key - Generic Error", {
|
|
1334
|
+
error: (0, utils_2.getError)(error),
|
|
1335
|
+
deviceSN: deviceSN,
|
|
1336
|
+
type: type,
|
|
1337
|
+
});
|
|
1121
1338
|
}
|
|
1122
1339
|
}
|
|
1123
1340
|
return "";
|
|
@@ -1130,7 +1347,10 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1130
1347
|
}
|
|
1131
1348
|
catch (err) {
|
|
1132
1349
|
const error = (0, error_1.ensureError)(err);
|
|
1133
|
-
logging_1.rootHTTPLogger.error("Data decryption error, invalidating session data and reconnecting...", {
|
|
1350
|
+
logging_1.rootHTTPLogger.error("Data decryption error, invalidating session data and reconnecting...", {
|
|
1351
|
+
error: (0, utils_2.getError)(error),
|
|
1352
|
+
serverPublicKey: this.persistentData.serverPublicKey,
|
|
1353
|
+
});
|
|
1134
1354
|
this.persistentData.serverPublicKey = this.SERVER_PUBLIC_KEY;
|
|
1135
1355
|
this.invalidateToken();
|
|
1136
1356
|
this.emit("close");
|
|
@@ -1147,179 +1367,186 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1147
1367
|
return undefined;
|
|
1148
1368
|
}
|
|
1149
1369
|
async getSensorHistory(stationSN, deviceSN) {
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1167
|
-
if (result.data) {
|
|
1168
|
-
const entries = result.data;
|
|
1169
|
-
return entries;
|
|
1170
|
-
}
|
|
1171
|
-
}
|
|
1172
|
-
else {
|
|
1173
|
-
logging_1.rootHTTPLogger.error("Get sensor history - Response code not ok", { code: result.code, msg: result.msg, data: response.data, stationSN: stationSN, deviceSN: deviceSN });
|
|
1370
|
+
const data = {
|
|
1371
|
+
devicse_sn: deviceSN,
|
|
1372
|
+
max_time: 0, //TODO: Finish implementation
|
|
1373
|
+
num: 500, //TODO: Finish implementation
|
|
1374
|
+
page: 0, //TODO: Finish implementation
|
|
1375
|
+
station_sn: stationSN,
|
|
1376
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1377
|
+
};
|
|
1378
|
+
const response = await this.makePostRequest(this.apiGetSensorHistory, data);
|
|
1379
|
+
if (response !== undefined) {
|
|
1380
|
+
if (response.status == 200) {
|
|
1381
|
+
const result = response.data;
|
|
1382
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1383
|
+
if (result.data) {
|
|
1384
|
+
const entries = result.data;
|
|
1385
|
+
return entries;
|
|
1174
1386
|
}
|
|
1175
1387
|
}
|
|
1176
1388
|
else {
|
|
1177
|
-
logging_1.rootHTTPLogger.error("Get sensor history -
|
|
1389
|
+
logging_1.rootHTTPLogger.error("Get sensor history - Response code not ok", {
|
|
1390
|
+
code: result.code,
|
|
1391
|
+
msg: result.msg,
|
|
1392
|
+
data: response.data,
|
|
1393
|
+
stationSN: stationSN,
|
|
1394
|
+
deviceSN: deviceSN,
|
|
1395
|
+
});
|
|
1178
1396
|
}
|
|
1179
1397
|
}
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1398
|
+
else {
|
|
1399
|
+
logging_1.rootHTTPLogger.error("Get sensor history - Status return code not 200", {
|
|
1400
|
+
status: response.status,
|
|
1401
|
+
statusText: response.statusText,
|
|
1402
|
+
data: response.data,
|
|
1403
|
+
stationSN: stationSN,
|
|
1404
|
+
deviceSN: deviceSN,
|
|
1405
|
+
});
|
|
1183
1406
|
}
|
|
1184
1407
|
}
|
|
1185
1408
|
return [];
|
|
1186
1409
|
}
|
|
1187
1410
|
async getHouseDetail(houseID) {
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
if (result.data) {
|
|
1202
|
-
const houseDetail = this.decryptAPIData(result.data);
|
|
1203
|
-
logging_1.rootHTTPLogger.debug("Get house detail - Decrypted house detail data", { details: houseDetail });
|
|
1204
|
-
return houseDetail;
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1207
|
-
else {
|
|
1208
|
-
logging_1.rootHTTPLogger.error("Get house detail - Response code not ok", { code: result.code, msg: result.msg, data: response.data, houseID: houseID });
|
|
1411
|
+
const data = {
|
|
1412
|
+
house_id: houseID,
|
|
1413
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1414
|
+
};
|
|
1415
|
+
const response = await this.makePostRequest(this.apiGetHouseDetail, data);
|
|
1416
|
+
if (response !== undefined) {
|
|
1417
|
+
if (response.status == 200) {
|
|
1418
|
+
const result = response.data;
|
|
1419
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1420
|
+
if (result.data) {
|
|
1421
|
+
const houseDetail = this.decryptAPIData(result.data);
|
|
1422
|
+
logging_1.rootHTTPLogger.debug("Get house detail - Decrypted house detail data", { details: houseDetail });
|
|
1423
|
+
return houseDetail;
|
|
1209
1424
|
}
|
|
1210
1425
|
}
|
|
1211
1426
|
else {
|
|
1212
|
-
logging_1.rootHTTPLogger.error("Get house detail -
|
|
1427
|
+
logging_1.rootHTTPLogger.error("Get house detail - Response code not ok", {
|
|
1428
|
+
code: result.code,
|
|
1429
|
+
msg: result.msg,
|
|
1430
|
+
data: response.data,
|
|
1431
|
+
houseID: houseID,
|
|
1432
|
+
});
|
|
1213
1433
|
}
|
|
1214
1434
|
}
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1435
|
+
else {
|
|
1436
|
+
logging_1.rootHTTPLogger.error("Get house detail - Status return code not 200", {
|
|
1437
|
+
status: response.status,
|
|
1438
|
+
statusText: response.statusText,
|
|
1439
|
+
data: response.data,
|
|
1440
|
+
houseID: houseID,
|
|
1441
|
+
});
|
|
1218
1442
|
}
|
|
1219
1443
|
}
|
|
1220
1444
|
return null;
|
|
1221
1445
|
}
|
|
1222
1446
|
async getHouseList() {
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1235
|
-
if (result.data) {
|
|
1236
|
-
logging_1.rootHTTPLogger.debug("Get house list - houses", { houses: result.data });
|
|
1237
|
-
return result.data;
|
|
1238
|
-
}
|
|
1239
|
-
}
|
|
1240
|
-
else {
|
|
1241
|
-
logging_1.rootHTTPLogger.error("Get house list - Response code not ok", { code: result.code, msg: result.msg, data: response.data });
|
|
1447
|
+
const data = {
|
|
1448
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1449
|
+
};
|
|
1450
|
+
const response = await this.makePostRequest(this.apiGetHouseList, data);
|
|
1451
|
+
if (response !== undefined) {
|
|
1452
|
+
if (response.status == 200) {
|
|
1453
|
+
const result = response.data;
|
|
1454
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1455
|
+
if (result.data) {
|
|
1456
|
+
logging_1.rootHTTPLogger.debug("Get house list - houses", { houses: result.data });
|
|
1457
|
+
return result.data;
|
|
1242
1458
|
}
|
|
1243
1459
|
}
|
|
1244
1460
|
else {
|
|
1245
|
-
logging_1.rootHTTPLogger.error("Get house list -
|
|
1461
|
+
logging_1.rootHTTPLogger.error("Get house list - Response code not ok", {
|
|
1462
|
+
code: result.code,
|
|
1463
|
+
msg: result.msg,
|
|
1464
|
+
data: response.data,
|
|
1465
|
+
});
|
|
1246
1466
|
}
|
|
1247
1467
|
}
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1468
|
+
else {
|
|
1469
|
+
logging_1.rootHTTPLogger.error("Get house list - Status return code not 200", {
|
|
1470
|
+
status: response.status,
|
|
1471
|
+
statusText: response.statusText,
|
|
1472
|
+
data: response.data,
|
|
1473
|
+
});
|
|
1251
1474
|
}
|
|
1252
1475
|
}
|
|
1253
1476
|
return [];
|
|
1254
1477
|
}
|
|
1255
1478
|
async getHouseInviteList(isInviter = 1) {
|
|
1256
1479
|
//TODO: Understand the other values of isInviter and document it
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
//rootHTTPLogger.debug("Get house invite list - Decrypted house invite list data", houseInviteList);
|
|
1273
|
-
const houseInviteList = result.data;
|
|
1274
|
-
logging_1.rootHTTPLogger.debug("Get house invite list - House invite list data", houseInviteList);
|
|
1275
|
-
return houseInviteList;
|
|
1276
|
-
}
|
|
1277
|
-
}
|
|
1278
|
-
else {
|
|
1279
|
-
logging_1.rootHTTPLogger.error("Get house invite list - Response code not ok", { code: result.code, msg: result.msg, data: response.data, isInviter: isInviter });
|
|
1480
|
+
const data = {
|
|
1481
|
+
is_inviter: isInviter,
|
|
1482
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1483
|
+
};
|
|
1484
|
+
const response = await this.makePostRequest(this.apiGetHouseInvites, data);
|
|
1485
|
+
if (response !== undefined) {
|
|
1486
|
+
if (response.status == 200) {
|
|
1487
|
+
const result = response.data;
|
|
1488
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1489
|
+
if (result.data) {
|
|
1490
|
+
//const houseInviteList = this.decryptAPIData(result.data) as Array<HouseInviteListResponse>; // No more encrypted!?
|
|
1491
|
+
//rootHTTPLogger.debug("Get house invite list - Decrypted house invite list data", houseInviteList);
|
|
1492
|
+
const houseInviteList = result.data;
|
|
1493
|
+
logging_1.rootHTTPLogger.debug("Get house invite list - House invite list data", houseInviteList);
|
|
1494
|
+
return houseInviteList;
|
|
1280
1495
|
}
|
|
1281
1496
|
}
|
|
1282
1497
|
else {
|
|
1283
|
-
logging_1.rootHTTPLogger.error("Get house invite list -
|
|
1498
|
+
logging_1.rootHTTPLogger.error("Get house invite list - Response code not ok", {
|
|
1499
|
+
code: result.code,
|
|
1500
|
+
msg: result.msg,
|
|
1501
|
+
data: response.data,
|
|
1502
|
+
isInviter: isInviter,
|
|
1503
|
+
});
|
|
1284
1504
|
}
|
|
1285
1505
|
}
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1506
|
+
else {
|
|
1507
|
+
logging_1.rootHTTPLogger.error("Get house invite list - Status return code not 200", {
|
|
1508
|
+
status: response.status,
|
|
1509
|
+
statusText: response.statusText,
|
|
1510
|
+
data: response.data,
|
|
1511
|
+
isInviter: isInviter,
|
|
1512
|
+
});
|
|
1289
1513
|
}
|
|
1290
1514
|
}
|
|
1291
1515
|
return [];
|
|
1292
1516
|
}
|
|
1293
1517
|
async confirmHouseInvite(houseID, inviteID) {
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
if (response.status == 200) {
|
|
1308
|
-
const result = response.data;
|
|
1309
|
-
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1310
|
-
return true;
|
|
1311
|
-
}
|
|
1312
|
-
else {
|
|
1313
|
-
logging_1.rootHTTPLogger.error("Confirm house invite - Response code not ok", { code: result.code, msg: result.msg, data: response.data, houseID: houseID, inviteID: inviteID });
|
|
1314
|
-
}
|
|
1518
|
+
const data = {
|
|
1519
|
+
house_id: houseID,
|
|
1520
|
+
invite_id: inviteID,
|
|
1521
|
+
is_inviter: 1, // 1 = true, 0 = false
|
|
1522
|
+
//user_id: "",
|
|
1523
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1524
|
+
};
|
|
1525
|
+
const response = await this.makePostRequest(this.apiConfirmHouseInvite, data);
|
|
1526
|
+
if (response !== undefined) {
|
|
1527
|
+
if (response.status == 200) {
|
|
1528
|
+
const result = response.data;
|
|
1529
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1530
|
+
return true;
|
|
1315
1531
|
}
|
|
1316
1532
|
else {
|
|
1317
|
-
logging_1.rootHTTPLogger.error("Confirm house invite -
|
|
1533
|
+
logging_1.rootHTTPLogger.error("Confirm house invite - Response code not ok", {
|
|
1534
|
+
code: result.code,
|
|
1535
|
+
msg: result.msg,
|
|
1536
|
+
data: response.data,
|
|
1537
|
+
houseID: houseID,
|
|
1538
|
+
inviteID: inviteID,
|
|
1539
|
+
});
|
|
1318
1540
|
}
|
|
1319
1541
|
}
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1542
|
+
else {
|
|
1543
|
+
logging_1.rootHTTPLogger.error("Confirm house invite - Status return code not 200", {
|
|
1544
|
+
status: response.status,
|
|
1545
|
+
statusText: response.statusText,
|
|
1546
|
+
data: response.data,
|
|
1547
|
+
houseID: houseID,
|
|
1548
|
+
inviteID: inviteID,
|
|
1549
|
+
});
|
|
1323
1550
|
}
|
|
1324
1551
|
}
|
|
1325
1552
|
return false;
|
|
@@ -1331,7 +1558,7 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1331
1558
|
try {
|
|
1332
1559
|
const response = await this.request({
|
|
1333
1560
|
method: "get",
|
|
1334
|
-
endpoint: "v2/passport/profile"
|
|
1561
|
+
endpoint: "v2/passport/profile",
|
|
1335
1562
|
});
|
|
1336
1563
|
if (response.status == 200) {
|
|
1337
1564
|
const result = response.data;
|
|
@@ -1346,11 +1573,19 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1346
1573
|
}
|
|
1347
1574
|
}
|
|
1348
1575
|
else {
|
|
1349
|
-
logging_1.rootHTTPLogger.error("Get passport profile - Response code not ok", {
|
|
1576
|
+
logging_1.rootHTTPLogger.error("Get passport profile - Response code not ok", {
|
|
1577
|
+
code: result.code,
|
|
1578
|
+
msg: result.msg,
|
|
1579
|
+
data: response.data,
|
|
1580
|
+
});
|
|
1350
1581
|
}
|
|
1351
1582
|
}
|
|
1352
1583
|
else {
|
|
1353
|
-
logging_1.rootHTTPLogger.error("Get passport profile - Status return code not 200", {
|
|
1584
|
+
logging_1.rootHTTPLogger.error("Get passport profile - Status return code not 200", {
|
|
1585
|
+
status: response.status,
|
|
1586
|
+
statusText: response.statusText,
|
|
1587
|
+
data: response.data,
|
|
1588
|
+
});
|
|
1354
1589
|
}
|
|
1355
1590
|
}
|
|
1356
1591
|
catch (err) {
|
|
@@ -1360,68 +1595,78 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1360
1595
|
return null;
|
|
1361
1596
|
}
|
|
1362
1597
|
async addUser(deviceSN, nickname, stationSN = "") {
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
const result = response.data;
|
|
1377
|
-
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1378
|
-
if (result.data)
|
|
1379
|
-
return result.data;
|
|
1380
|
-
}
|
|
1381
|
-
else {
|
|
1382
|
-
logging_1.rootHTTPLogger.error("Add user - Response code not ok", { code: result.code, msg: result.msg, data: response.data, deviceSN: deviceSN, nickname: nickname, stationSN: stationSN });
|
|
1383
|
-
}
|
|
1598
|
+
const data = {
|
|
1599
|
+
device_sn: deviceSN,
|
|
1600
|
+
nick_name: nickname,
|
|
1601
|
+
station_sn: stationSN === deviceSN ? "" : stationSN,
|
|
1602
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1603
|
+
};
|
|
1604
|
+
const response = await this.makePostRequest(this.apiAddLocalUser, data);
|
|
1605
|
+
if (response !== undefined) {
|
|
1606
|
+
if (response.status == 200) {
|
|
1607
|
+
const result = response.data;
|
|
1608
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1609
|
+
if (result.data)
|
|
1610
|
+
return result.data;
|
|
1384
1611
|
}
|
|
1385
1612
|
else {
|
|
1386
|
-
logging_1.rootHTTPLogger.error("Add user -
|
|
1613
|
+
logging_1.rootHTTPLogger.error("Add user - Response code not ok", {
|
|
1614
|
+
code: result.code,
|
|
1615
|
+
msg: result.msg,
|
|
1616
|
+
data: response.data,
|
|
1617
|
+
deviceSN: deviceSN,
|
|
1618
|
+
nickname: nickname,
|
|
1619
|
+
stationSN: stationSN,
|
|
1620
|
+
});
|
|
1387
1621
|
}
|
|
1388
1622
|
}
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1623
|
+
else {
|
|
1624
|
+
logging_1.rootHTTPLogger.error("Add user - Status return code not 200", {
|
|
1625
|
+
status: response.status,
|
|
1626
|
+
statusText: response.statusText,
|
|
1627
|
+
data: response.data,
|
|
1628
|
+
deviceSN: deviceSN,
|
|
1629
|
+
nickname: nickname,
|
|
1630
|
+
stationSN: stationSN,
|
|
1631
|
+
});
|
|
1392
1632
|
}
|
|
1393
1633
|
}
|
|
1394
1634
|
return null;
|
|
1395
1635
|
}
|
|
1396
1636
|
async deleteUser(deviceSN, shortUserId, stationSN = "") {
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
if (response.status == 200) {
|
|
1410
|
-
const result = response.data;
|
|
1411
|
-
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1412
|
-
return true;
|
|
1413
|
-
}
|
|
1414
|
-
else {
|
|
1415
|
-
logging_1.rootHTTPLogger.error("Delete user - Response code not ok", { code: result.code, msg: result.msg, data: response.data, deviceSN: deviceSN, shortUserId: shortUserId, stationSN: stationSN });
|
|
1416
|
-
}
|
|
1637
|
+
const data = {
|
|
1638
|
+
device_sn: deviceSN,
|
|
1639
|
+
short_user_ids: [shortUserId],
|
|
1640
|
+
station_sn: stationSN === deviceSN ? "" : stationSN,
|
|
1641
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1642
|
+
};
|
|
1643
|
+
const response = await this.makePostRequest(this.apiDeleteLocalUser, data);
|
|
1644
|
+
if (response !== undefined) {
|
|
1645
|
+
if (response.status == 200) {
|
|
1646
|
+
const result = response.data;
|
|
1647
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1648
|
+
return true;
|
|
1417
1649
|
}
|
|
1418
1650
|
else {
|
|
1419
|
-
logging_1.rootHTTPLogger.error("Delete user -
|
|
1651
|
+
logging_1.rootHTTPLogger.error("Delete user - Response code not ok", {
|
|
1652
|
+
code: result.code,
|
|
1653
|
+
msg: result.msg,
|
|
1654
|
+
data: response.data,
|
|
1655
|
+
deviceSN: deviceSN,
|
|
1656
|
+
shortUserId: shortUserId,
|
|
1657
|
+
stationSN: stationSN,
|
|
1658
|
+
});
|
|
1420
1659
|
}
|
|
1421
1660
|
}
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1661
|
+
else {
|
|
1662
|
+
logging_1.rootHTTPLogger.error("Delete user - Status return code not 200", {
|
|
1663
|
+
status: response.status,
|
|
1664
|
+
statusText: response.statusText,
|
|
1665
|
+
data: response.data,
|
|
1666
|
+
deviceSN: deviceSN,
|
|
1667
|
+
shortUserId: shortUserId,
|
|
1668
|
+
stationSN: stationSN,
|
|
1669
|
+
});
|
|
1425
1670
|
}
|
|
1426
1671
|
}
|
|
1427
1672
|
return false;
|
|
@@ -1430,7 +1675,7 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1430
1675
|
try {
|
|
1431
1676
|
const response = await this.request({
|
|
1432
1677
|
method: "get",
|
|
1433
|
-
endpoint: `v1/app/device/user/list?device_sn=${deviceSN}&station_sn=${stationSN}
|
|
1678
|
+
endpoint: `v1/app/device/user/list?device_sn=${deviceSN}&station_sn=${stationSN}`,
|
|
1434
1679
|
});
|
|
1435
1680
|
if (response.status == 200) {
|
|
1436
1681
|
const result = response.data;
|
|
@@ -1441,16 +1686,32 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1441
1686
|
}
|
|
1442
1687
|
}
|
|
1443
1688
|
else {
|
|
1444
|
-
logging_1.rootHTTPLogger.error("Get users - Response code not ok", {
|
|
1689
|
+
logging_1.rootHTTPLogger.error("Get users - Response code not ok", {
|
|
1690
|
+
code: result.code,
|
|
1691
|
+
msg: result.msg,
|
|
1692
|
+
data: response.data,
|
|
1693
|
+
deviceSN: deviceSN,
|
|
1694
|
+
stationSN: stationSN,
|
|
1695
|
+
});
|
|
1445
1696
|
}
|
|
1446
1697
|
}
|
|
1447
1698
|
else {
|
|
1448
|
-
logging_1.rootHTTPLogger.error("Get users - Status return code not 200", {
|
|
1699
|
+
logging_1.rootHTTPLogger.error("Get users - Status return code not 200", {
|
|
1700
|
+
status: response.status,
|
|
1701
|
+
statusText: response.statusText,
|
|
1702
|
+
data: response.data,
|
|
1703
|
+
deviceSN: deviceSN,
|
|
1704
|
+
stationSN: stationSN,
|
|
1705
|
+
});
|
|
1449
1706
|
}
|
|
1450
1707
|
}
|
|
1451
1708
|
catch (err) {
|
|
1452
1709
|
const error = (0, error_1.ensureError)(err);
|
|
1453
|
-
logging_1.rootHTTPLogger.error("Get users - Generic Error", {
|
|
1710
|
+
logging_1.rootHTTPLogger.error("Get users - Generic Error", {
|
|
1711
|
+
error: (0, utils_2.getError)(error),
|
|
1712
|
+
deviceSN: deviceSN,
|
|
1713
|
+
stationSN: stationSN,
|
|
1714
|
+
});
|
|
1454
1715
|
}
|
|
1455
1716
|
return null;
|
|
1456
1717
|
}
|
|
@@ -1467,45 +1728,57 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1467
1728
|
}
|
|
1468
1729
|
catch (err) {
|
|
1469
1730
|
const error = (0, error_1.ensureError)(err);
|
|
1470
|
-
logging_1.rootHTTPLogger.error("Get user - Generic Error", {
|
|
1731
|
+
logging_1.rootHTTPLogger.error("Get user - Generic Error", {
|
|
1732
|
+
error: (0, utils_2.getError)(error),
|
|
1733
|
+
deviceSN: deviceSN,
|
|
1734
|
+
stationSN: stationSN,
|
|
1735
|
+
shortUserId: shortUserId,
|
|
1736
|
+
});
|
|
1471
1737
|
}
|
|
1472
1738
|
return null;
|
|
1473
1739
|
}
|
|
1474
1740
|
async updateUser(deviceSN, stationSN, shortUserId, nickname) {
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
if (response.status == 200) {
|
|
1493
|
-
const result = response.data;
|
|
1494
|
-
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1495
|
-
return true;
|
|
1496
|
-
}
|
|
1497
|
-
else {
|
|
1498
|
-
logging_1.rootHTTPLogger.error("Update user - Response code not ok", { code: result.code, msg: result.msg, data: response.data, deviceSN: deviceSN, stationSN: stationSN, shortUserId: shortUserId, nickname: nickname });
|
|
1499
|
-
}
|
|
1741
|
+
const user = await this.getUser(deviceSN, stationSN, shortUserId);
|
|
1742
|
+
if (user !== null) {
|
|
1743
|
+
const data = {
|
|
1744
|
+
device_sn: deviceSN,
|
|
1745
|
+
nick_name: nickname,
|
|
1746
|
+
password_list: user.password_list,
|
|
1747
|
+
short_user_id: shortUserId,
|
|
1748
|
+
station_sn: stationSN === deviceSN ? "" : stationSN,
|
|
1749
|
+
user_type: user.user_type,
|
|
1750
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1751
|
+
};
|
|
1752
|
+
const response = await this.makePostRequest(this.apiUpdateLocalUser, data);
|
|
1753
|
+
if (response !== undefined) {
|
|
1754
|
+
if (response.status == 200) {
|
|
1755
|
+
const result = response.data;
|
|
1756
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1757
|
+
return true;
|
|
1500
1758
|
}
|
|
1501
1759
|
else {
|
|
1502
|
-
logging_1.rootHTTPLogger.error("Update user -
|
|
1760
|
+
logging_1.rootHTTPLogger.error("Update user - Response code not ok", {
|
|
1761
|
+
code: result.code,
|
|
1762
|
+
msg: result.msg,
|
|
1763
|
+
data: response.data,
|
|
1764
|
+
deviceSN: deviceSN,
|
|
1765
|
+
stationSN: stationSN,
|
|
1766
|
+
shortUserId: shortUserId,
|
|
1767
|
+
nickname: nickname,
|
|
1768
|
+
});
|
|
1503
1769
|
}
|
|
1504
1770
|
}
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1771
|
+
else {
|
|
1772
|
+
logging_1.rootHTTPLogger.error("Update user - Status return code not 200", {
|
|
1773
|
+
status: response.status,
|
|
1774
|
+
statusText: response.statusText,
|
|
1775
|
+
data: response.data,
|
|
1776
|
+
deviceSN: deviceSN,
|
|
1777
|
+
stationSN: stationSN,
|
|
1778
|
+
shortUserId: shortUserId,
|
|
1779
|
+
nickname: nickname,
|
|
1780
|
+
});
|
|
1781
|
+
}
|
|
1509
1782
|
}
|
|
1510
1783
|
}
|
|
1511
1784
|
return false;
|
|
@@ -1520,13 +1793,19 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1520
1793
|
const response = await this.request({
|
|
1521
1794
|
method: "GET",
|
|
1522
1795
|
endpoint: url,
|
|
1523
|
-
responseType: "buffer"
|
|
1796
|
+
responseType: "buffer",
|
|
1524
1797
|
}, true);
|
|
1525
1798
|
if (response.status == 200) {
|
|
1526
1799
|
return (0, utils_1.decodeImage)(station.p2p_did, response.data);
|
|
1527
1800
|
}
|
|
1528
1801
|
else {
|
|
1529
|
-
logging_1.rootHTTPLogger.error("Get Image - Status return code not 200", {
|
|
1802
|
+
logging_1.rootHTTPLogger.error("Get Image - Status return code not 200", {
|
|
1803
|
+
status: response.status,
|
|
1804
|
+
statusText: response.statusText,
|
|
1805
|
+
data: response.data,
|
|
1806
|
+
deviceSN: deviceSN,
|
|
1807
|
+
url: url,
|
|
1808
|
+
});
|
|
1530
1809
|
}
|
|
1531
1810
|
}
|
|
1532
1811
|
}
|
|
@@ -1539,45 +1818,56 @@ class HTTPApi extends tiny_typed_emitter_1.TypedEmitter {
|
|
|
1539
1818
|
return Buffer.alloc(0);
|
|
1540
1819
|
}
|
|
1541
1820
|
async updateUserPassword(deviceSN, shortUserId, passwordId, schedule, stationSN = "") {
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
}
|
|
1570
|
-
else {
|
|
1571
|
-
logging_1.rootHTTPLogger.error("Add user - Response code not ok", { code: result.code, msg: result.msg, data: response.data, deviceSN: deviceSN, shortUserId: shortUserId, schedule: schedule, stationSN: stationSN });
|
|
1572
|
-
}
|
|
1821
|
+
const data = {
|
|
1822
|
+
device_sn: deviceSN,
|
|
1823
|
+
password_list: [
|
|
1824
|
+
{
|
|
1825
|
+
password_id: passwordId,
|
|
1826
|
+
password_type: types_1.UserPasswordType.PIN,
|
|
1827
|
+
schedule: JSON.stringify({
|
|
1828
|
+
endDay: schedule !== undefined && schedule.endDateTime !== undefined ? (0, utils_1.hexDate)(schedule.endDateTime) : "ffffffff",
|
|
1829
|
+
endTime: schedule !== undefined && schedule.endDateTime !== undefined ? (0, utils_1.hexTime)(schedule.endDateTime) : "ffff",
|
|
1830
|
+
startDay: schedule !== undefined && schedule.startDateTime !== undefined
|
|
1831
|
+
? (0, utils_1.hexDate)(schedule.startDateTime)
|
|
1832
|
+
: "00000000",
|
|
1833
|
+
startTime: schedule !== undefined && schedule.startDateTime !== undefined ? (0, utils_1.hexTime)(schedule.startDateTime) : "0000",
|
|
1834
|
+
week: schedule !== undefined && schedule.week !== undefined ? (0, utils_1.hexWeek)(schedule) : "ff",
|
|
1835
|
+
}),
|
|
1836
|
+
},
|
|
1837
|
+
],
|
|
1838
|
+
short_user_id: shortUserId,
|
|
1839
|
+
station_sn: stationSN === deviceSN ? "" : stationSN,
|
|
1840
|
+
transaction: `${new Date().getTime().toString()}`,
|
|
1841
|
+
};
|
|
1842
|
+
const response = await this.makePostRequest(this.apiUpdateUserPassword, data);
|
|
1843
|
+
if (response !== undefined) {
|
|
1844
|
+
if (response.status == 200) {
|
|
1845
|
+
const result = response.data;
|
|
1846
|
+
if (result.code == types_1.ResponseErrorCode.CODE_OK) {
|
|
1847
|
+
return true;
|
|
1573
1848
|
}
|
|
1574
1849
|
else {
|
|
1575
|
-
logging_1.rootHTTPLogger.error("Add user -
|
|
1850
|
+
logging_1.rootHTTPLogger.error("Add user - Response code not ok", {
|
|
1851
|
+
code: result.code,
|
|
1852
|
+
msg: result.msg,
|
|
1853
|
+
data: response.data,
|
|
1854
|
+
deviceSN: deviceSN,
|
|
1855
|
+
shortUserId: shortUserId,
|
|
1856
|
+
schedule: schedule,
|
|
1857
|
+
stationSN: stationSN,
|
|
1858
|
+
});
|
|
1576
1859
|
}
|
|
1577
1860
|
}
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1861
|
+
else {
|
|
1862
|
+
logging_1.rootHTTPLogger.error("Add user - Status return code not 200", {
|
|
1863
|
+
status: response.status,
|
|
1864
|
+
statusText: response.statusText,
|
|
1865
|
+
data: response.data,
|
|
1866
|
+
deviceSN: deviceSN,
|
|
1867
|
+
shortUserId: shortUserId,
|
|
1868
|
+
schedule: schedule,
|
|
1869
|
+
stationSN: stationSN,
|
|
1870
|
+
});
|
|
1581
1871
|
}
|
|
1582
1872
|
}
|
|
1583
1873
|
return false;
|