homey-api 1.5.10 → 1.5.13
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/lib/APIErrorTimeout.js +21 -0
- package/lib/EventEmitter.js +19 -1
- package/lib/HomeyAPI/HomeyAPIV2/Manager.js +30 -8
- package/lib/HomeyAPI/HomeyAPIV2.js +4 -0
- package/lib/Util.js +3 -1
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const APIError = require('./APIError');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @class
|
|
9
|
+
* @hideconstructor
|
|
10
|
+
* @extends APIError
|
|
11
|
+
* @memberof HomeyAPI
|
|
12
|
+
*/
|
|
13
|
+
class APIErrorTimeout extends APIError {
|
|
14
|
+
|
|
15
|
+
constructor(message, statusCode = 408) {
|
|
16
|
+
super(message, statusCode);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = APIErrorTimeout;
|
package/lib/EventEmitter.js
CHANGED
|
@@ -26,6 +26,16 @@ class EventEmitter {
|
|
|
26
26
|
listener(...data);
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
+
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {string} event
|
|
35
|
+
* @param {function} callback
|
|
36
|
+
*/
|
|
37
|
+
addListener(event, callback) {
|
|
38
|
+
return this.on(event, callback);
|
|
29
39
|
}
|
|
30
40
|
|
|
31
41
|
/**
|
|
@@ -37,6 +47,8 @@ class EventEmitter {
|
|
|
37
47
|
|
|
38
48
|
this.__listeners[event] = this.__listeners[event] || [];
|
|
39
49
|
this.__listeners[event].push(callback);
|
|
50
|
+
|
|
51
|
+
return this;
|
|
40
52
|
}
|
|
41
53
|
|
|
42
54
|
/**
|
|
@@ -53,6 +65,8 @@ class EventEmitter {
|
|
|
53
65
|
|
|
54
66
|
this.__listeners[event] = this.__listeners[event] || [];
|
|
55
67
|
this.__listeners[event].push(callback_);
|
|
68
|
+
|
|
69
|
+
return this;
|
|
56
70
|
}
|
|
57
71
|
|
|
58
72
|
/**
|
|
@@ -63,6 +77,8 @@ class EventEmitter {
|
|
|
63
77
|
this.__debug(`.off('${event}')`);
|
|
64
78
|
|
|
65
79
|
this.__listeners[event] = this.__listeners[event].filter(cb => cb !== callback);
|
|
80
|
+
|
|
81
|
+
return this;
|
|
66
82
|
}
|
|
67
83
|
|
|
68
84
|
/**
|
|
@@ -71,7 +87,7 @@ class EventEmitter {
|
|
|
71
87
|
* @param {function} callback
|
|
72
88
|
*/
|
|
73
89
|
removeListener(event, callback) {
|
|
74
|
-
this.off(event, callback);
|
|
90
|
+
return this.off(event, callback);
|
|
75
91
|
}
|
|
76
92
|
|
|
77
93
|
/**
|
|
@@ -79,6 +95,8 @@ class EventEmitter {
|
|
|
79
95
|
*/
|
|
80
96
|
removeAllListeners(event) {
|
|
81
97
|
this.__listeners[event] = [];
|
|
98
|
+
|
|
99
|
+
return this;
|
|
82
100
|
}
|
|
83
101
|
|
|
84
102
|
}
|
|
@@ -119,6 +119,7 @@ class Manager extends EventEmitter {
|
|
|
119
119
|
const query = { ...$query };
|
|
120
120
|
const headers = { ...$headers };
|
|
121
121
|
|
|
122
|
+
// Verify & Transform parameters
|
|
122
123
|
if (operation.parameters) {
|
|
123
124
|
// Parse Parameters
|
|
124
125
|
for (const [parameterId, parameter] of Object.entries(operation.parameters)) {
|
|
@@ -194,8 +195,9 @@ class Manager extends EventEmitter {
|
|
|
194
195
|
let result;
|
|
195
196
|
const benchmark = Util.benchmark();
|
|
196
197
|
|
|
197
|
-
//
|
|
198
|
-
|
|
198
|
+
// If connected to Socket.io,
|
|
199
|
+
// try to get the CRUD Item from Cache.
|
|
200
|
+
if (this.isConnected() && operation.crud && $cache === true) {
|
|
199
201
|
const itemId = items[operation.crud.item].id;
|
|
200
202
|
const itemType = items[operation.crud.item].type;
|
|
201
203
|
|
|
@@ -234,8 +236,8 @@ class Manager extends EventEmitter {
|
|
|
234
236
|
}
|
|
235
237
|
}
|
|
236
238
|
|
|
237
|
-
// If Homey is connected to Socket.io
|
|
238
|
-
//
|
|
239
|
+
// If Homey is connected to Socket.io,
|
|
240
|
+
// send the API request to socket.io.
|
|
239
241
|
// This is about ~2x faster than HTTP
|
|
240
242
|
if (this.homey.isConnected()) {
|
|
241
243
|
result = await Util.timeout(new Promise((resolve, reject) => {
|
|
@@ -273,13 +275,17 @@ class Manager extends EventEmitter {
|
|
|
273
275
|
case 'getOne': {
|
|
274
276
|
const key = getItemKey(result);
|
|
275
277
|
|
|
276
|
-
result =
|
|
278
|
+
result = new ItemClass({
|
|
277
279
|
key,
|
|
278
280
|
homey: this.homey,
|
|
279
281
|
manager: this,
|
|
280
282
|
properties: { ...result },
|
|
281
283
|
});
|
|
282
284
|
|
|
285
|
+
if (this.isConnected()) {
|
|
286
|
+
this.__cache[itemId][key] = result;
|
|
287
|
+
}
|
|
288
|
+
|
|
283
289
|
break;
|
|
284
290
|
}
|
|
285
291
|
case 'getAll': {
|
|
@@ -290,16 +296,20 @@ class Manager extends EventEmitter {
|
|
|
290
296
|
if (this.__cache[itemId][key]) {
|
|
291
297
|
result[resultKey] = this.__cache[itemId][key].__update(item);
|
|
292
298
|
} else {
|
|
293
|
-
result[resultKey] =
|
|
299
|
+
result[resultKey] = new ItemClass({
|
|
294
300
|
key,
|
|
295
301
|
homey: this.homey,
|
|
296
302
|
manager: this,
|
|
297
303
|
properties: { ...item },
|
|
298
304
|
});
|
|
305
|
+
|
|
306
|
+
if (this.isConnected()) {
|
|
307
|
+
this.__cache[itemId][key] = result[resultKey];
|
|
308
|
+
}
|
|
299
309
|
}
|
|
300
310
|
}
|
|
301
311
|
|
|
302
|
-
// Find and delete deleted from cache
|
|
312
|
+
// Find and delete deleted items from cache
|
|
303
313
|
if (this.__cache[itemId]) {
|
|
304
314
|
for (const cachedItem of Object.values(this.__cache[itemId])) {
|
|
305
315
|
const key = getItemKey(cachedItem);
|
|
@@ -321,11 +331,15 @@ class Manager extends EventEmitter {
|
|
|
321
331
|
if (this.__cache[itemId][key]) {
|
|
322
332
|
result = this.__cache[itemId][key].__update(result);
|
|
323
333
|
} else {
|
|
324
|
-
result =
|
|
334
|
+
result = new ItemClass({
|
|
325
335
|
key,
|
|
326
336
|
manager: this,
|
|
327
337
|
properties: result,
|
|
328
338
|
});
|
|
339
|
+
|
|
340
|
+
if (this.isConnected()) {
|
|
341
|
+
this.__cache[itemId][key] = result;
|
|
342
|
+
}
|
|
329
343
|
}
|
|
330
344
|
break;
|
|
331
345
|
}
|
|
@@ -354,6 +368,14 @@ class Manager extends EventEmitter {
|
|
|
354
368
|
this.homey.__debug(`[${this.name}]`, ...props);
|
|
355
369
|
}
|
|
356
370
|
|
|
371
|
+
/**
|
|
372
|
+
* If this manager's namespace is connected to Socket.io.
|
|
373
|
+
* @returns {Boolean}
|
|
374
|
+
*/
|
|
375
|
+
isConnected() {
|
|
376
|
+
return this.__connected === true;
|
|
377
|
+
}
|
|
378
|
+
|
|
357
379
|
/**
|
|
358
380
|
* Connect to the realtime namespace.
|
|
359
381
|
* @returns {Promise<void>}
|
package/lib/Util.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
5
|
+
const APIErrorTimeout = require('./APIErrorTimeout');
|
|
6
|
+
|
|
5
7
|
/**
|
|
6
8
|
* Helper Utility Class
|
|
7
9
|
* @class
|
|
@@ -43,7 +45,7 @@ class Util {
|
|
|
43
45
|
* @returns {Promise}
|
|
44
46
|
*/
|
|
45
47
|
static async timeout(promise, timeoutMillis = 5000, message = `Timeout after ${timeoutMillis}ms`) {
|
|
46
|
-
const timeoutError = new
|
|
48
|
+
const timeoutError = new APIErrorTimeout(message);
|
|
47
49
|
let timeoutRef;
|
|
48
50
|
return Promise.race([
|
|
49
51
|
promise,
|