incyclist-devices 1.4.100 → 1.5.0
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/LICENSE +0 -0
- package/lib/DeviceSupport.d.ts +36 -34
- package/lib/DeviceSupport.js +82 -78
- package/lib/ant/AntAdapter.d.ts +50 -50
- package/lib/ant/AntAdapter.js +109 -109
- package/lib/ant/AntScanner.d.ts +60 -60
- package/lib/ant/AntScanner.js +651 -651
- package/lib/ant/antfe/AntFEAdapter.d.ts +83 -83
- package/lib/ant/antfe/AntFEAdapter.js +652 -652
- package/lib/ant/antfe/ant-fe-adv-st-mode.d.ts +9 -9
- package/lib/ant/antfe/ant-fe-adv-st-mode.js +51 -51
- package/lib/ant/antfe/ant-fe-erg-mode.d.ts +6 -6
- package/lib/ant/antfe/ant-fe-erg-mode.js +14 -14
- package/lib/ant/antfe/ant-fe-st-mode.d.ts +5 -5
- package/lib/ant/antfe/ant-fe-st-mode.js +13 -13
- package/lib/ant/anthrm/AntHrmAdapter.d.ts +16 -16
- package/lib/ant/anthrm/AntHrmAdapter.js +130 -130
- package/lib/ant/antpwr/pwr-adapter.d.ts +49 -49
- package/lib/ant/antpwr/pwr-adapter.js +251 -251
- package/lib/ant/utils.d.ts +1 -1
- package/lib/ant/utils.js +23 -23
- package/lib/antv2/adapter-factory.d.ts +11 -0
- package/lib/antv2/adapter-factory.js +40 -0
- package/lib/antv2/ant-binding.d.ts +13 -0
- package/lib/antv2/ant-binding.js +27 -0
- package/lib/antv2/ant-device.d.ts +51 -0
- package/lib/antv2/ant-device.js +115 -0
- package/lib/antv2/ant-interface.d.ts +37 -0
- package/lib/antv2/ant-interface.js +239 -0
- package/lib/antv2/fe.d.ts +28 -0
- package/lib/antv2/fe.js +237 -0
- package/lib/antv2/hr.d.ts +18 -0
- package/lib/antv2/hr.js +93 -0
- package/lib/antv2/incyclist-protocol.d.ts +37 -0
- package/lib/antv2/incyclist-protocol.js +126 -0
- package/lib/antv2/pwr.d.ts +28 -0
- package/lib/antv2/pwr.js +163 -0
- package/lib/antv2/sensor-factory.d.ts +5 -0
- package/lib/antv2/sensor-factory.js +20 -0
- package/lib/ble/ble-device.d.ts +63 -63
- package/lib/ble/ble-device.js +444 -442
- package/lib/ble/ble-erg-mode.d.ts +18 -18
- package/lib/ble/ble-erg-mode.js +132 -132
- package/lib/ble/ble-interface.d.ts +100 -100
- package/lib/ble/ble-interface.js +721 -717
- package/lib/ble/ble-peripheral.d.ts +36 -36
- package/lib/ble/ble-peripheral.js +200 -200
- package/lib/ble/ble-st-mode.d.ts +15 -15
- package/lib/ble/ble-st-mode.js +95 -102
- package/lib/ble/ble.d.ts +129 -129
- package/lib/ble/ble.js +86 -86
- package/lib/ble/consts.d.ts +14 -14
- package/lib/ble/consts.js +17 -17
- package/lib/ble/fm.d.ts +125 -125
- package/lib/ble/fm.js +745 -739
- package/lib/ble/hrm.d.ts +48 -48
- package/lib/ble/hrm.js +134 -134
- package/lib/ble/incyclist-protocol.d.ts +31 -31
- package/lib/ble/incyclist-protocol.js +147 -147
- package/lib/ble/pwr.d.ts +89 -89
- package/lib/ble/pwr.js +321 -321
- package/lib/ble/tacx.d.ts +90 -90
- package/lib/ble/tacx.js +731 -731
- package/lib/ble/wahoo-kickr.d.ts +98 -98
- package/lib/ble/wahoo-kickr.js +496 -496
- package/lib/calculations.d.ts +13 -13
- package/lib/calculations.js +150 -150
- package/lib/{CyclingMode.d.ts → cycling-mode.d.ts} +76 -76
- package/lib/{CyclingMode.js → cycling-mode.js} +79 -79
- package/lib/daum/DaumAdapter.d.ts +66 -66
- package/lib/daum/DaumAdapter.js +396 -396
- package/lib/daum/DaumPowerMeterCyclingMode.d.ts +8 -8
- package/lib/daum/DaumPowerMeterCyclingMode.js +21 -21
- package/lib/daum/ERGCyclingMode.d.ts +26 -26
- package/lib/daum/ERGCyclingMode.js +201 -201
- package/lib/daum/SmartTrainerCyclingMode.d.ts +41 -41
- package/lib/daum/SmartTrainerCyclingMode.js +344 -344
- package/lib/daum/classic/DaumClassicAdapter.d.ts +18 -18
- package/lib/daum/classic/DaumClassicAdapter.js +146 -146
- package/lib/daum/classic/DaumClassicCyclingMode.d.ts +13 -13
- package/lib/daum/classic/DaumClassicCyclingMode.js +97 -97
- package/lib/daum/classic/DaumClassicProtocol.d.ts +27 -27
- package/lib/daum/classic/DaumClassicProtocol.js +185 -185
- package/lib/daum/classic/bike.d.ts +64 -64
- package/lib/daum/classic/bike.js +456 -456
- package/lib/daum/classic/utils.d.ts +13 -13
- package/lib/daum/classic/utils.js +143 -143
- package/lib/daum/constants.d.ts +19 -19
- package/lib/daum/constants.js +22 -22
- package/lib/daum/premium/DaumClassicCyclingMode.d.ts +14 -14
- package/lib/daum/premium/DaumClassicCyclingMode.js +86 -86
- package/lib/daum/premium/DaumPremiumAdapter.d.ts +12 -12
- package/lib/daum/premium/DaumPremiumAdapter.js +131 -131
- package/lib/daum/premium/DaumPremiumProtocol.d.ts +32 -32
- package/lib/daum/premium/DaumPremiumProtocol.js +207 -207
- package/lib/daum/premium/bike.d.ts +123 -123
- package/lib/daum/premium/bike.js +894 -894
- package/lib/daum/premium/tcpserial.d.ts +33 -33
- package/lib/daum/premium/tcpserial.js +123 -123
- package/lib/daum/premium/utils.d.ts +62 -62
- package/lib/daum/premium/utils.js +376 -376
- package/lib/{Device.d.ts → device.d.ts} +92 -92
- package/lib/{Device.js → device.js} +71 -71
- package/lib/kettler/comms.d.ts +59 -59
- package/lib/kettler/comms.js +242 -242
- package/lib/kettler/ergo-racer/ERGCyclingMode.d.ts +25 -25
- package/lib/kettler/ergo-racer/ERGCyclingMode.js +144 -144
- package/lib/kettler/ergo-racer/adapter.d.ts +101 -101
- package/lib/kettler/ergo-racer/adapter.js +639 -639
- package/lib/kettler/ergo-racer/protocol.d.ts +41 -41
- package/lib/kettler/ergo-racer/protocol.js +203 -203
- package/lib/modes/power-base.d.ts +20 -20
- package/lib/modes/power-base.js +70 -70
- package/lib/modes/power-meter.d.ts +20 -20
- package/lib/modes/power-meter.js +78 -78
- package/lib/modes/simulator.d.ts +29 -29
- package/lib/modes/simulator.js +140 -140
- package/lib/{DeviceProtocol.d.ts → protocol.d.ts} +74 -74
- package/lib/{DeviceProtocol.js → protocol.js} +41 -41
- package/lib/{DeviceRegistry.d.ts → registry.d.ts} +8 -8
- package/lib/{DeviceRegistry.js → registry.js} +33 -33
- package/lib/simulator/Simulator.d.ts +69 -69
- package/lib/simulator/Simulator.js +288 -288
- package/lib/types/command.d.ts +8 -8
- package/lib/types/command.js +2 -2
- package/lib/types/route.d.ts +24 -24
- package/lib/types/route.js +9 -9
- package/lib/types/user.d.ts +11 -11
- package/lib/types/user.js +9 -9
- package/lib/utils.d.ts +14 -14
- package/lib/utils.js +114 -114
- package/package.json +47 -46
package/lib/ble/ble-interface.js
CHANGED
|
@@ -1,717 +1,721 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const gd_eventlog_1 = require("gd-eventlog");
|
|
16
|
-
const utils_1 = require("../utils");
|
|
17
|
-
const ble_1 = require("./ble");
|
|
18
|
-
const ble_2 = require("./ble");
|
|
19
|
-
const ble_peripheral_1 = __importDefault(require("./ble-peripheral"));
|
|
20
|
-
const CONNECT_TIMEOUT = 5000;
|
|
21
|
-
const DEFAULT_SCAN_TIMEOUT = 20000;
|
|
22
|
-
class BleInterface extends ble_1.BleInterfaceClass {
|
|
23
|
-
constructor(props = {}) {
|
|
24
|
-
super(props);
|
|
25
|
-
this.scanState = { isScanning: false, isConnecting: false, timeout: undefined, isBackgroundScan: false };
|
|
26
|
-
this.connectState = { isConnecting: false, isConnected: false, isInitSuccess: false };
|
|
27
|
-
this.devices = [];
|
|
28
|
-
this.peripheralCache = [];
|
|
29
|
-
if (props.logger)
|
|
30
|
-
this.logger = props.logger;
|
|
31
|
-
this.logger = new gd_eventlog_1.EventLogger('BLE');
|
|
32
|
-
}
|
|
33
|
-
static getInstance(props = {}) {
|
|
34
|
-
if (!BleInterface._instance) {
|
|
35
|
-
BleInterface._instance = new BleInterface(props);
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
if (props.binding) {
|
|
39
|
-
BleInterface._instance.setBinding(props.binding);
|
|
40
|
-
}
|
|
41
|
-
if (props.logger) {
|
|
42
|
-
BleInterface._instance.logger = props.logger;
|
|
43
|
-
}
|
|
44
|
-
if (props.log && !BleInterface._instance.logger) {
|
|
45
|
-
BleInterface._instance.logger = new gd_eventlog_1.EventLogger('BLE');
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return BleInterface._instance;
|
|
49
|
-
}
|
|
50
|
-
static register(id, type, Class, services) {
|
|
51
|
-
if (this.deviceClasses.find(i => i.id === id))
|
|
52
|
-
return;
|
|
53
|
-
this.deviceClasses.push({ id, type, Class, services });
|
|
54
|
-
}
|
|
55
|
-
logEvent(event) {
|
|
56
|
-
if (this.logger) {
|
|
57
|
-
this.logger.logEvent(event);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
this.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
this.connectState.
|
|
85
|
-
this.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
self.connectState.
|
|
104
|
-
self.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
this.
|
|
117
|
-
this.
|
|
118
|
-
this.
|
|
119
|
-
this.
|
|
120
|
-
this.
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
this.
|
|
129
|
-
this.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
this.
|
|
137
|
-
this.
|
|
138
|
-
this.
|
|
139
|
-
this.
|
|
140
|
-
this.
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
this.connectState.timeout
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
this.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
this.peripheralCache.
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
yield connector.
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
const
|
|
367
|
-
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
this.
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
let
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
this.
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
opStr = '
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
this.logEvent({ message: `${opStr}
|
|
530
|
-
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
this.
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
this.logEvent({ message: '
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
const
|
|
583
|
-
const
|
|
584
|
-
const
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
const
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
return;
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
this.logEvent({ message: `${opStr}:
|
|
599
|
-
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
this.logEvent({ message:
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
this.
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
this.
|
|
631
|
-
|
|
632
|
-
this.
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
}
|
|
668
|
-
if (!this.
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
if (
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
this.
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
this.devices.
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
return
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
const
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const gd_eventlog_1 = require("gd-eventlog");
|
|
16
|
+
const utils_1 = require("../utils");
|
|
17
|
+
const ble_1 = require("./ble");
|
|
18
|
+
const ble_2 = require("./ble");
|
|
19
|
+
const ble_peripheral_1 = __importDefault(require("./ble-peripheral"));
|
|
20
|
+
const CONNECT_TIMEOUT = 5000;
|
|
21
|
+
const DEFAULT_SCAN_TIMEOUT = 20000;
|
|
22
|
+
class BleInterface extends ble_1.BleInterfaceClass {
|
|
23
|
+
constructor(props = {}) {
|
|
24
|
+
super(props);
|
|
25
|
+
this.scanState = { isScanning: false, isConnecting: false, timeout: undefined, isBackgroundScan: false };
|
|
26
|
+
this.connectState = { isConnecting: false, isConnected: false, isInitSuccess: false };
|
|
27
|
+
this.devices = [];
|
|
28
|
+
this.peripheralCache = [];
|
|
29
|
+
if (props.logger)
|
|
30
|
+
this.logger = props.logger;
|
|
31
|
+
this.logger = new gd_eventlog_1.EventLogger('BLE');
|
|
32
|
+
}
|
|
33
|
+
static getInstance(props = {}) {
|
|
34
|
+
if (!BleInterface._instance) {
|
|
35
|
+
BleInterface._instance = new BleInterface(props);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
if (props.binding) {
|
|
39
|
+
BleInterface._instance.setBinding(props.binding);
|
|
40
|
+
}
|
|
41
|
+
if (props.logger) {
|
|
42
|
+
BleInterface._instance.logger = props.logger;
|
|
43
|
+
}
|
|
44
|
+
if (props.log && !BleInterface._instance.logger) {
|
|
45
|
+
BleInterface._instance.logger = new gd_eventlog_1.EventLogger('BLE');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return BleInterface._instance;
|
|
49
|
+
}
|
|
50
|
+
static register(id, type, Class, services) {
|
|
51
|
+
if (this.deviceClasses.find(i => i.id === id))
|
|
52
|
+
return;
|
|
53
|
+
this.deviceClasses.push({ id, type, Class, services });
|
|
54
|
+
}
|
|
55
|
+
logEvent(event) {
|
|
56
|
+
if (this.logger) {
|
|
57
|
+
this.logger.logEvent(event);
|
|
58
|
+
}
|
|
59
|
+
if (process.env.BLE_DEBUG) {
|
|
60
|
+
console.log('~~BLE:', event);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
onStateChange(state) {
|
|
64
|
+
if (state !== ble_1.BleState.POWERED_ON) {
|
|
65
|
+
this.connectState.isConnected = false;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
this.connectState.isConnected = true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
onError(err) {
|
|
72
|
+
this.logEvent({ message: 'error', error: err.message, stack: err.stack });
|
|
73
|
+
}
|
|
74
|
+
connect(props = {}) {
|
|
75
|
+
const timeout = props.timeout || 2000;
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
if (this.connectState.isConnected) {
|
|
78
|
+
return resolve(true);
|
|
79
|
+
}
|
|
80
|
+
this.logEvent({ message: 'connect request', });
|
|
81
|
+
if (!this.getBinding())
|
|
82
|
+
return Promise.reject(new Error('no binding defined'));
|
|
83
|
+
this.connectState.timeout = setTimeout(() => {
|
|
84
|
+
this.connectState.isConnected = false;
|
|
85
|
+
this.connectState.isConnecting = false;
|
|
86
|
+
this.connectState.timeout = null;
|
|
87
|
+
this.logEvent({ message: 'connect result: timeout' });
|
|
88
|
+
reject(new Error('timeout'));
|
|
89
|
+
}, timeout);
|
|
90
|
+
try {
|
|
91
|
+
if (!this.connectState.isInitSuccess) {
|
|
92
|
+
const binding = this.getBinding()._bindings;
|
|
93
|
+
if (binding) {
|
|
94
|
+
const binding_init_original = binding.init.bind(binding);
|
|
95
|
+
const self = this;
|
|
96
|
+
binding.on('error', (err) => { this.getBinding().emit('error', err); });
|
|
97
|
+
binding.init = function () {
|
|
98
|
+
try {
|
|
99
|
+
binding_init_original();
|
|
100
|
+
self.connectState.isInitSuccess = true;
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
self.connectState.isInitSuccess = false;
|
|
104
|
+
self.connectState.isConnected = false;
|
|
105
|
+
self.connectState.isConnecting = false;
|
|
106
|
+
self.logEvent({ message: 'connect result: error', error: err.message });
|
|
107
|
+
return reject(new Error(err.message));
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const state = this.getBinding().state;
|
|
115
|
+
if (state === ble_1.BleState.POWERED_ON) {
|
|
116
|
+
clearTimeout(this.connectState.timeout);
|
|
117
|
+
this.connectState.timeout = null;
|
|
118
|
+
this.getBinding().removeAllListeners('stateChange');
|
|
119
|
+
this.getBinding().on('stateChange', this.onStateChange.bind(this));
|
|
120
|
+
this.connectState.isConnected = true;
|
|
121
|
+
this.connectState.isConnecting = false;
|
|
122
|
+
this.logEvent({ message: 'connect result: success' });
|
|
123
|
+
resolve(true);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
this.getBinding().once('error', (err) => {
|
|
128
|
+
this.connectState.isConnected = true;
|
|
129
|
+
this.connectState.isConnecting = false;
|
|
130
|
+
this.logEvent({ message: 'connect result: error', error: err.message });
|
|
131
|
+
this.getBinding().on('error', this.onError.bind(this));
|
|
132
|
+
return reject(err);
|
|
133
|
+
});
|
|
134
|
+
this.getBinding().on('stateChange', (state) => {
|
|
135
|
+
if (state === ble_1.BleState.POWERED_ON) {
|
|
136
|
+
clearTimeout(this.connectState.timeout);
|
|
137
|
+
this.connectState.timeout = null;
|
|
138
|
+
this.getBinding().removeAllListeners('stateChange');
|
|
139
|
+
this.getBinding().on('stateChange', this.onStateChange.bind(this));
|
|
140
|
+
this.connectState.isConnected = true;
|
|
141
|
+
this.connectState.isConnecting = false;
|
|
142
|
+
this.logEvent({ message: 'connect result: success' });
|
|
143
|
+
return resolve(true);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
this.logEvent({ message: 'BLE state change', state });
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
this.connectState.isConnected = false;
|
|
153
|
+
this.connectState.isConnecting = false;
|
|
154
|
+
if (this.connectState.timeout)
|
|
155
|
+
clearTimeout(this.connectState.timeout);
|
|
156
|
+
this.connectState.timeout = null;
|
|
157
|
+
this.logEvent({ message: 'connect result: error', error: err.message });
|
|
158
|
+
return reject(new Error('bluetooth unavailable, cause: ' + err.message));
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
disconnect() {
|
|
163
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
164
|
+
if (!this.connectState.isConnected) {
|
|
165
|
+
return Promise.resolve(true);
|
|
166
|
+
}
|
|
167
|
+
if (!this.getBinding())
|
|
168
|
+
return Promise.reject(new Error('no binding defined'));
|
|
169
|
+
this.logEvent({ message: 'disconnect request' });
|
|
170
|
+
if (this.scanState.isScanning) {
|
|
171
|
+
yield this.stopScan();
|
|
172
|
+
}
|
|
173
|
+
const connectedDevices = this.devices.filter(d => d.isConnected);
|
|
174
|
+
for (let i = 0; i < connectedDevices.length; i++) {
|
|
175
|
+
const d = connectedDevices[i];
|
|
176
|
+
const device = d.device;
|
|
177
|
+
yield device.disconnect();
|
|
178
|
+
}
|
|
179
|
+
this.connectState.isConnected = false;
|
|
180
|
+
this.connectState.isConnecting = false;
|
|
181
|
+
if (this.connectState.timeout) {
|
|
182
|
+
clearTimeout(this.connectState.timeout);
|
|
183
|
+
this.connectState.timeout = null;
|
|
184
|
+
}
|
|
185
|
+
this.logEvent({ message: 'disconnect result: success' });
|
|
186
|
+
return true;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
isConnected() {
|
|
190
|
+
return this.connectState.isConnected;
|
|
191
|
+
}
|
|
192
|
+
getDevicesFromServices(deviceTypes, services) {
|
|
193
|
+
if (!deviceTypes || !Array.isArray(deviceTypes) || deviceTypes.length === 0) {
|
|
194
|
+
return [];
|
|
195
|
+
}
|
|
196
|
+
const get = (deviceTypes, fnCompare) => {
|
|
197
|
+
const types = deviceTypes.filter(DeviceType => {
|
|
198
|
+
const C = DeviceType;
|
|
199
|
+
let found = false;
|
|
200
|
+
if (C.services)
|
|
201
|
+
found = C.services.find((s) => fnCompare(s));
|
|
202
|
+
return found;
|
|
203
|
+
});
|
|
204
|
+
return types;
|
|
205
|
+
};
|
|
206
|
+
if (typeof services === 'string') {
|
|
207
|
+
return get(deviceTypes, (s) => (0, ble_2.matches)(s, services));
|
|
208
|
+
}
|
|
209
|
+
if (Array.isArray(services)) {
|
|
210
|
+
const sids = services.map(ble_1.uuid);
|
|
211
|
+
return get(deviceTypes, s => {
|
|
212
|
+
const res = sids.find((service) => (0, ble_2.matches)(s, service));
|
|
213
|
+
return res !== undefined;
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return [];
|
|
217
|
+
}
|
|
218
|
+
getAllSupportedServices() {
|
|
219
|
+
const supported = BleInterface.deviceClasses;
|
|
220
|
+
const res = [];
|
|
221
|
+
if (supported && supported.length > 0) {
|
|
222
|
+
supported.forEach(dc => {
|
|
223
|
+
if (dc && dc.services) {
|
|
224
|
+
dc.services.forEach(s => {
|
|
225
|
+
if (!res.includes(s))
|
|
226
|
+
res.push(s);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
return res;
|
|
232
|
+
}
|
|
233
|
+
getAllSupportedDeviceTypes() {
|
|
234
|
+
const supported = BleInterface.deviceClasses;
|
|
235
|
+
return supported.map(dc => dc.Class);
|
|
236
|
+
}
|
|
237
|
+
getServicesFromDeviceTypes(deviceTypes) {
|
|
238
|
+
let services = [];
|
|
239
|
+
try {
|
|
240
|
+
if (!deviceTypes || !Array.isArray(deviceTypes) || deviceTypes.length === 0) {
|
|
241
|
+
return [];
|
|
242
|
+
}
|
|
243
|
+
deviceTypes.forEach(DeviceType => {
|
|
244
|
+
if (DeviceType.services) {
|
|
245
|
+
const dtServices = DeviceType.services;
|
|
246
|
+
dtServices.forEach(s => {
|
|
247
|
+
if (!services.find(s2 => s2 === s))
|
|
248
|
+
services.push(s);
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
console.log(err);
|
|
255
|
+
}
|
|
256
|
+
return services;
|
|
257
|
+
}
|
|
258
|
+
getServicesFromDevice(device) {
|
|
259
|
+
if (!device)
|
|
260
|
+
return [];
|
|
261
|
+
const services = [];
|
|
262
|
+
const dServices = device.getServiceUUids();
|
|
263
|
+
dServices.forEach(s => {
|
|
264
|
+
if (!services.find(s2 => s2 === s))
|
|
265
|
+
services.push(s);
|
|
266
|
+
});
|
|
267
|
+
return services;
|
|
268
|
+
}
|
|
269
|
+
waitForConnectFinished(timeout) {
|
|
270
|
+
const waitStart = Date.now();
|
|
271
|
+
const waitTimeout = waitStart + timeout;
|
|
272
|
+
return new Promise((resolve, reject) => {
|
|
273
|
+
const waitIv = setInterval(() => {
|
|
274
|
+
if (this.scanState.isConnecting && Date.now() > waitTimeout) {
|
|
275
|
+
clearInterval(waitIv);
|
|
276
|
+
return reject(new Error('Connecting already in progress'));
|
|
277
|
+
}
|
|
278
|
+
if (!this.scanState.isConnecting) {
|
|
279
|
+
clearInterval(waitIv);
|
|
280
|
+
return resolve(true);
|
|
281
|
+
}
|
|
282
|
+
}, 100);
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
addPeripheralToCache(peripheral, props = {}) {
|
|
286
|
+
const info = this.peripheralCache.find(i => i.address === peripheral.address);
|
|
287
|
+
const connector = info && info.connector ? info.connector : new ble_peripheral_1.default(this, peripheral);
|
|
288
|
+
this.peripheralCache.push(Object.assign({ address: peripheral.address, ts: Date.now(), peripheral, connector }, props));
|
|
289
|
+
}
|
|
290
|
+
onDisconnect(peripheral) {
|
|
291
|
+
const idx = this.peripheralCache.findIndex(i => i.address === peripheral.address);
|
|
292
|
+
if (idx !== -1)
|
|
293
|
+
this.peripheralCache.splice(idx, 1);
|
|
294
|
+
}
|
|
295
|
+
getConnector(peripheral) {
|
|
296
|
+
const info = this.peripheralCache.find(i => i.address === peripheral.address);
|
|
297
|
+
if (!info) {
|
|
298
|
+
const connector = new ble_peripheral_1.default(this, peripheral);
|
|
299
|
+
this.peripheralCache.push({ address: peripheral.address, ts: Date.now(), peripheral, connector });
|
|
300
|
+
return connector;
|
|
301
|
+
}
|
|
302
|
+
return info.connector;
|
|
303
|
+
}
|
|
304
|
+
findPeripheral(peripheral) {
|
|
305
|
+
const info = this.peripheralCache.find(i => i.address === peripheral.address || peripheral.address === i.peripheral.address || peripheral.name === i.peripheral.name || peripheral.id === i.peripheral.id);
|
|
306
|
+
return info ? info.peripheral : undefined;
|
|
307
|
+
}
|
|
308
|
+
getCharacteristics(peripheral) {
|
|
309
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
310
|
+
let characteristics = undefined;
|
|
311
|
+
let chachedPeripheralInfo = this.peripheralCache.find(i => i.address === peripheral.address);
|
|
312
|
+
if (chachedPeripheralInfo && Date.now() - chachedPeripheralInfo.ts > 600000) {
|
|
313
|
+
chachedPeripheralInfo.ts = Date.now();
|
|
314
|
+
}
|
|
315
|
+
if (!chachedPeripheralInfo) {
|
|
316
|
+
this.addPeripheralToCache(peripheral);
|
|
317
|
+
chachedPeripheralInfo = this.peripheralCache.find(i => i.address === peripheral.address);
|
|
318
|
+
}
|
|
319
|
+
const connector = chachedPeripheralInfo.connector;
|
|
320
|
+
if (!chachedPeripheralInfo.characteristics) {
|
|
321
|
+
try {
|
|
322
|
+
chachedPeripheralInfo.state = { isConfigured: false, isLoading: true, isInterrupted: false };
|
|
323
|
+
yield connector.connect();
|
|
324
|
+
peripheral.state = connector.getState();
|
|
325
|
+
yield connector.initialize();
|
|
326
|
+
characteristics = connector.getCharachteristics();
|
|
327
|
+
this.logEvent({ message: 'characteristic info (+):', info: characteristics.map(c => `${peripheral.address} ${c.uuid} ${c.properties}`) });
|
|
328
|
+
chachedPeripheralInfo.characteristics = characteristics;
|
|
329
|
+
chachedPeripheralInfo.state = { isConfigured: true, isLoading: false, isInterrupted: false };
|
|
330
|
+
}
|
|
331
|
+
catch (err) {
|
|
332
|
+
console.log(err);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
characteristics = chachedPeripheralInfo.characteristics;
|
|
337
|
+
this.logEvent({ message: 'characteristic info (*):', info: characteristics.map(c => `${peripheral.address} ${c.uuid} ${c.properties}`) });
|
|
338
|
+
}
|
|
339
|
+
if (!characteristics)
|
|
340
|
+
this.logEvent({ message: 'characteristic info:', info: 'none' });
|
|
341
|
+
return characteristics;
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
getDeviceClasses(peripheral, props = {}) {
|
|
345
|
+
let DeviceClasses;
|
|
346
|
+
const { deviceTypes, profile, services = peripheral.advertisement.serviceUuids } = props;
|
|
347
|
+
if ((!deviceTypes || deviceTypes.length === 0)) {
|
|
348
|
+
const classes = BleInterface.deviceClasses.map(c => c.Class);
|
|
349
|
+
DeviceClasses = this.getDevicesFromServices(classes, services);
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
DeviceClasses = this.getDevicesFromServices(deviceTypes, services);
|
|
353
|
+
}
|
|
354
|
+
if (profile && DeviceClasses && DeviceClasses.length > 0) {
|
|
355
|
+
DeviceClasses = DeviceClasses.filter(C => {
|
|
356
|
+
const device = new C({ peripheral });
|
|
357
|
+
if (device.getProfile() !== profile)
|
|
358
|
+
return false;
|
|
359
|
+
return true;
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
return DeviceClasses;
|
|
363
|
+
}
|
|
364
|
+
createDevice(DeviceClass, peripheral, characteristics) {
|
|
365
|
+
try {
|
|
366
|
+
const C = DeviceClass;
|
|
367
|
+
const device = new C({ peripheral });
|
|
368
|
+
const cids = characteristics ? characteristics.map(c => (0, ble_1.uuid)(c.uuid)) : [];
|
|
369
|
+
this.logEvent({ message: 'trying to create device', peripheral: peripheral.address, characteristics: cids, profile: device.getProfile() });
|
|
370
|
+
const existingDevice = this.devices.find(i => i.device.id === device.id && i.device.getProfile() === device.getProfile());
|
|
371
|
+
if (existingDevice)
|
|
372
|
+
return existingDevice.device;
|
|
373
|
+
device.setInterface(this);
|
|
374
|
+
if (characteristics && device.isMatching(cids)) {
|
|
375
|
+
device.characteristics = characteristics;
|
|
376
|
+
device.setCharacteristicUUIDs(characteristics.map(c => c.uuid));
|
|
377
|
+
return device;
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
this.logEvent({ message: 'failed to create device', peripheral: peripheral.address, profile: device.getProfile() });
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
catch (err) {
|
|
384
|
+
this.logEvent({ message: 'error', fn: '', error: err.message || err, stack: err.stack });
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
connectDevice(requested, timeout = DEFAULT_SCAN_TIMEOUT + CONNECT_TIMEOUT) {
|
|
388
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
389
|
+
const { id, name, address } = requested;
|
|
390
|
+
const profile = requested instanceof ble_1.BleDeviceClass ?
|
|
391
|
+
(requested.getProfile && typeof (requested.getProfile) === 'function' ? requested.getProfile() : undefined) :
|
|
392
|
+
requested.profile;
|
|
393
|
+
this.logEvent({ message: 'connectDevice', id, name, address, profile, isbusy: this.scanState.isConnecting });
|
|
394
|
+
if (this.scanState.isConnecting) {
|
|
395
|
+
yield this.waitForConnectFinished(CONNECT_TIMEOUT);
|
|
396
|
+
}
|
|
397
|
+
this.scanState.isConnecting = true;
|
|
398
|
+
const existing = this.devices.find(i => (!profile || i.device.getProfile() === profile) && (i.device.address === requested.address || i.device.id === requested.id || i.device.name === requested.name));
|
|
399
|
+
if (existing) {
|
|
400
|
+
this.logEvent({ message: 'connect existing device' });
|
|
401
|
+
yield existing.device.connect();
|
|
402
|
+
this.scanState.isConnecting = false;
|
|
403
|
+
return existing.device;
|
|
404
|
+
}
|
|
405
|
+
const peripheralInfo = this.peripheralCache.find(i => (i.address === requested.address || (i.periphal && i.peripheral.id === requested.id)));
|
|
406
|
+
if (peripheralInfo) {
|
|
407
|
+
if (!peripheralInfo.characteristic) {
|
|
408
|
+
yield this.getCharacteristics(peripheralInfo.periphal);
|
|
409
|
+
const DeviceClasses = this.getDeviceClasses(peripheralInfo.peripheral, { profile });
|
|
410
|
+
if (!DeviceClasses || DeviceClasses.length === 0)
|
|
411
|
+
return;
|
|
412
|
+
const devices = DeviceClasses.map(C => this.createDevice(C, peripheralInfo.periphal, peripheralInfo.characteristics));
|
|
413
|
+
if (devices && devices.length > 0) {
|
|
414
|
+
for (let i = 0; i < devices.length; i++) {
|
|
415
|
+
const idx = this.devices.push({ device: devices[i], isConnected: false }) - 1;
|
|
416
|
+
if (!devices[i].isConnected())
|
|
417
|
+
yield devices[i].connect();
|
|
418
|
+
this.devices[idx].isConnected = true;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
const connectedDevice = this.devices.find(d => d.isConnected);
|
|
423
|
+
if (connectedDevice)
|
|
424
|
+
return connectedDevice.device;
|
|
425
|
+
}
|
|
426
|
+
let devices = [];
|
|
427
|
+
let retry = false;
|
|
428
|
+
let retryCount = 0;
|
|
429
|
+
do {
|
|
430
|
+
if (retryCount > 0) {
|
|
431
|
+
this.logEvent({ message: 'retry connect device', id, name, address, profile, retryCount });
|
|
432
|
+
}
|
|
433
|
+
try {
|
|
434
|
+
devices = yield this.scan({ timeout: DEFAULT_SCAN_TIMEOUT, requested: requested });
|
|
435
|
+
if (devices.length === 0) {
|
|
436
|
+
retryCount++;
|
|
437
|
+
retry = retryCount < 5;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
catch (err) {
|
|
441
|
+
if (err.message === 'scanning already in progress') {
|
|
442
|
+
yield (0, utils_1.sleep)(1000);
|
|
443
|
+
retryCount++;
|
|
444
|
+
retry = retryCount < 5;
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
this.scanState.isConnecting = false;
|
|
448
|
+
throw err;
|
|
449
|
+
}
|
|
450
|
+
} while (devices.length === 0 && retry);
|
|
451
|
+
if (devices.length === 0) {
|
|
452
|
+
this.logEvent({ message: 'connectDevice failure', id, name, address, profile, error: 'device not found' });
|
|
453
|
+
this.scanState.isConnecting = false;
|
|
454
|
+
throw new Error('device not found');
|
|
455
|
+
}
|
|
456
|
+
if (devices[0]) {
|
|
457
|
+
this.logEvent({ message: 'connectDevice connecting', id, name, address, profile });
|
|
458
|
+
const connected = yield devices[0].connect();
|
|
459
|
+
this.scanState.isConnecting = false;
|
|
460
|
+
if (connected) {
|
|
461
|
+
this.logEvent({ message: 'connectDevice success', id, name, address, profile });
|
|
462
|
+
return devices[0];
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
this.logEvent({ message: 'connectDevice failure', id, name, address, profile });
|
|
466
|
+
throw new Error('connect failed');
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
waitForScanFinished(timeout) {
|
|
472
|
+
const waitStart = Date.now();
|
|
473
|
+
const waitTimeout = waitStart + timeout;
|
|
474
|
+
return new Promise((resolve, reject) => {
|
|
475
|
+
const waitIv = setInterval(() => {
|
|
476
|
+
if (this.scanState.isScanning && Date.now() > waitTimeout) {
|
|
477
|
+
clearInterval(waitIv);
|
|
478
|
+
return reject(new Error('scanning already in progress'));
|
|
479
|
+
}
|
|
480
|
+
if (!this.scanState.isScanning) {
|
|
481
|
+
clearInterval(waitIv);
|
|
482
|
+
return resolve(true);
|
|
483
|
+
}
|
|
484
|
+
}, 100);
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
getBestDeviceMatch(DeviceClasses) {
|
|
488
|
+
if (!DeviceClasses || DeviceClasses.length === 0)
|
|
489
|
+
return;
|
|
490
|
+
const details = DeviceClasses.map(c => ({ name: c.prototype.constructor.name, priority: c.detectionPriority || 0, class: c }));
|
|
491
|
+
details.sort((a, b) => b.priority - a.priority);
|
|
492
|
+
return details[0].class;
|
|
493
|
+
}
|
|
494
|
+
scan(props) {
|
|
495
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
496
|
+
const { timeout = DEFAULT_SCAN_TIMEOUT, deviceTypes = [], requested } = props;
|
|
497
|
+
let profile;
|
|
498
|
+
if (requested)
|
|
499
|
+
profile = requested instanceof ble_1.BleDeviceClass ?
|
|
500
|
+
(requested.getProfile && typeof (requested.getProfile) === 'function' ? requested.getProfile() : undefined) :
|
|
501
|
+
requested.profile;
|
|
502
|
+
const { id, address, name } = requested || {};
|
|
503
|
+
const scanForDevice = (requested !== null && requested !== undefined);
|
|
504
|
+
const services = (!deviceTypes || deviceTypes.length === 0) ? this.getAllSupportedServices() : this.getServicesFromDeviceTypes(deviceTypes);
|
|
505
|
+
const bleBinding = this.getBinding();
|
|
506
|
+
if (!bleBinding)
|
|
507
|
+
return Promise.reject(new Error('no binding defined'));
|
|
508
|
+
if (!this.isConnected()) {
|
|
509
|
+
yield this.connect();
|
|
510
|
+
}
|
|
511
|
+
const peripheralsProcessed = [];
|
|
512
|
+
const devicesProcessed = [];
|
|
513
|
+
this.logEvent({ message: 'scan()', props: { timeout }, scanState: this.scanState,
|
|
514
|
+
peripheralCache: this.peripheralCache.map(i => ({ address: i.address, ts: i.ts, name: i.peripheral ? i.peripheral.advertisement.localName : '' })),
|
|
515
|
+
deviceCache: this.devices.map(i => ({ address: i.device.address, profile: i.device.getProfile(), isConnected: i.isConnected }))
|
|
516
|
+
});
|
|
517
|
+
let opStr;
|
|
518
|
+
if (scanForDevice) {
|
|
519
|
+
opStr = 'search device';
|
|
520
|
+
this.logEvent({ message: 'search device request', services, device: { id, address, name }, deviceTypes });
|
|
521
|
+
}
|
|
522
|
+
else {
|
|
523
|
+
opStr = 'scan';
|
|
524
|
+
const supported = BleInterface.deviceClasses.map(dc => ({ id: dc.id, type: dc.type, services: dc.services }));
|
|
525
|
+
this.logEvent({ message: 'scan start', services, supported });
|
|
526
|
+
}
|
|
527
|
+
if (this.scanState.isScanning) {
|
|
528
|
+
try {
|
|
529
|
+
this.logEvent({ message: `${opStr}: waiting for previous scan to finish` });
|
|
530
|
+
yield this.waitForScanFinished(timeout);
|
|
531
|
+
}
|
|
532
|
+
catch (err) {
|
|
533
|
+
this.logEvent({ message: `${opStr} result: already scanning` });
|
|
534
|
+
return Promise.reject(err);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return new Promise((resolve, reject) => {
|
|
538
|
+
this.scanState.isScanning = true;
|
|
539
|
+
if (scanForDevice) {
|
|
540
|
+
if (this.devices && this.devices.length > 0) {
|
|
541
|
+
const knownDevices = this.devices.map(i => ({ name: i.device.name, address: i.device.address, isConnected: i.isConnected, connectState: i.device.getConnectState() }));
|
|
542
|
+
this.logEvent({ message: `${opStr}: check if already registered`, device: { name, address }, knownDevices });
|
|
543
|
+
const existing = this.devices.find(i => (i.device.address === address || i.device.name === name || i.device.id === id));
|
|
544
|
+
if (existing)
|
|
545
|
+
this.logEvent({ message: `${opStr}: device already registered`, device: { name, address } });
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
const onTimeout = () => {
|
|
549
|
+
if (!this.scanState.isScanning || !this.scanState.timeout)
|
|
550
|
+
return;
|
|
551
|
+
this.scanState.timeout = null;
|
|
552
|
+
this.logEvent({ message: `${opStr} result: devices found`, requested: scanForDevice ? { name, address, profile } : undefined, devices: this.devices.map(i => i.device.name + (!i.device.name || i.device.name === '') ? `addr=${i.device.address}` : '') });
|
|
553
|
+
this.getBinding().removeAllListeners('discover');
|
|
554
|
+
this.logEvent({ message: `${opStr}: stop scanning`, requested: scanForDevice ? { name, address, profile } : undefined, });
|
|
555
|
+
bleBinding.stopScanning(() => {
|
|
556
|
+
this.scanState.isScanning = false;
|
|
557
|
+
if (scanForDevice) {
|
|
558
|
+
reject(new Error('device not found'));
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
resolve(this.devices.map(i => i.device));
|
|
562
|
+
});
|
|
563
|
+
};
|
|
564
|
+
const onPeripheralFound = (peripheral, fromCache = false) => __awaiter(this, void 0, void 0, function* () {
|
|
565
|
+
if (!peripheral || !peripheral.advertisement || !peripheral.advertisement.localName || !peripheral.advertisement.serviceUuids)
|
|
566
|
+
return;
|
|
567
|
+
if (fromCache) {
|
|
568
|
+
this.logEvent({ message: 'adding from Cache', peripheral: peripheral.address });
|
|
569
|
+
}
|
|
570
|
+
else {
|
|
571
|
+
const { id, name, address, advertisement = {} } = peripheral;
|
|
572
|
+
this.logEvent({ message: 'BLE scan: found device', peripheral: { id, name: advertisement.localName, address, services: advertisement.serviceUuids } });
|
|
573
|
+
}
|
|
574
|
+
if (peripheral.address === undefined || peripheral.address === '')
|
|
575
|
+
peripheral.address = peripheral.id;
|
|
576
|
+
const isPeripheralProcessed = peripheralsProcessed.find(p => p === peripheral.address) !== undefined;
|
|
577
|
+
if (isPeripheralProcessed)
|
|
578
|
+
return;
|
|
579
|
+
peripheralsProcessed.push(peripheral.address);
|
|
580
|
+
if (scanForDevice && requested.name && requested.name !== peripheral.advertisement.localName)
|
|
581
|
+
return;
|
|
582
|
+
const connector = this.getConnector(peripheral);
|
|
583
|
+
const characteristics = yield this.getCharacteristics(peripheral);
|
|
584
|
+
const connectedServices = connector.getServices();
|
|
585
|
+
const services = connectedServices ? connectedServices.map(cs => cs.uuid) : undefined;
|
|
586
|
+
const connectedPeripheral = connector.getPeripheral();
|
|
587
|
+
const { id, name, address, advertisement = {} } = connectedPeripheral;
|
|
588
|
+
const DeviceClasses = this.getDeviceClasses(connectedPeripheral, { profile, services }) || [];
|
|
589
|
+
this.logEvent({ message: 'BLE scan: device connected', peripheral: { id, name, address, services: advertisement.serviceUuids }, services, classes: DeviceClasses.map(c => c.prototype.constructor.name) });
|
|
590
|
+
let cntFound = 0;
|
|
591
|
+
const DeviceClass = this.getBestDeviceMatch(DeviceClasses);
|
|
592
|
+
if (!DeviceClass)
|
|
593
|
+
return;
|
|
594
|
+
if (scanForDevice && cntFound > 0)
|
|
595
|
+
return;
|
|
596
|
+
const d = this.createDevice(DeviceClass, peripheral, characteristics);
|
|
597
|
+
if (!d) {
|
|
598
|
+
this.logEvent({ message: `${opStr}: could not create device `, DeviceClass });
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
try {
|
|
602
|
+
this.logEvent({ message: `${opStr}: connecting `, device: d.name, profile: d.getProfile(), address: d.address });
|
|
603
|
+
yield d.connect();
|
|
604
|
+
}
|
|
605
|
+
catch (err) {
|
|
606
|
+
this.logEvent({ message: 'error', fn: 'onPeripheralFound()', error: err.message || err, stack: err.stack });
|
|
607
|
+
}
|
|
608
|
+
if (scanForDevice) {
|
|
609
|
+
if ((id && id !== '' && d.id === id) ||
|
|
610
|
+
(address && address !== '' && d.address === address) ||
|
|
611
|
+
(name && name !== '' && d.name === name))
|
|
612
|
+
cntFound++;
|
|
613
|
+
}
|
|
614
|
+
else
|
|
615
|
+
cntFound++;
|
|
616
|
+
const existing = devicesProcessed.find(device => device.id === d.id && device.getProfile() === d.getProfile());
|
|
617
|
+
if (!scanForDevice && cntFound > 0 && !existing) {
|
|
618
|
+
this.logEvent({ message: `${opStr}: device found`, device: d.name, profile: d.getProfile(), address: d.address, services: d.services.join(',') });
|
|
619
|
+
this.addDeviceToCache(d, peripheral.state === 'connected');
|
|
620
|
+
devicesProcessed.push(d);
|
|
621
|
+
this.emit('device', d);
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
if (scanForDevice && cntFound > 0) {
|
|
625
|
+
this.logEvent({ message: `${opStr}: device found`, device: d.name, profile: d.getProfile(), address: d.address, services: d.services.join(',') });
|
|
626
|
+
this.addDeviceToCache(d, peripheral.state === 'connected');
|
|
627
|
+
devicesProcessed.push(d);
|
|
628
|
+
this.emit('device', d);
|
|
629
|
+
process.nextTick(() => {
|
|
630
|
+
if (this.scanState.timeout) {
|
|
631
|
+
clearTimeout(this.scanState.timeout);
|
|
632
|
+
this.scanState.timeout = null;
|
|
633
|
+
}
|
|
634
|
+
this.logEvent({ message: `${opStr}: stop scanning`, requested: scanForDevice ? { name, address, profile } : undefined, });
|
|
635
|
+
bleBinding.stopScanning(() => {
|
|
636
|
+
this.getBinding().removeAllListeners('discover');
|
|
637
|
+
this.scanState.isScanning = false;
|
|
638
|
+
resolve([d]);
|
|
639
|
+
});
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
this.logEvent({ message: `${opStr}: start scanning`, requested: scanForDevice ? { name, address, profile } : undefined, timeout });
|
|
644
|
+
let services = [];
|
|
645
|
+
if (scanForDevice && name && !name.toLowerCase().startsWith('tacx')) {
|
|
646
|
+
if (props.requested instanceof ble_1.BleDeviceClass) {
|
|
647
|
+
const device = props.requested;
|
|
648
|
+
services = (device.getServices()) || [];
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
bleBinding.startScanning(services, false, (err) => {
|
|
652
|
+
if (err) {
|
|
653
|
+
this.logEvent({ message: `${opStr} result: error`, requested: scanForDevice ? { name, address, profile } : undefined, error: err.message });
|
|
654
|
+
this.scanState.isScanning = false;
|
|
655
|
+
return reject(err);
|
|
656
|
+
}
|
|
657
|
+
bleBinding.on('discover', (p) => {
|
|
658
|
+
onPeripheralFound(p);
|
|
659
|
+
});
|
|
660
|
+
});
|
|
661
|
+
this.scanState.timeout = setTimeout(onTimeout, timeout);
|
|
662
|
+
});
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
stopScan() {
|
|
666
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
667
|
+
this.logEvent({ message: 'scan stop request' });
|
|
668
|
+
if (!this.scanState.isScanning) {
|
|
669
|
+
this.logEvent({ message: 'scan stop result: not scanning' });
|
|
670
|
+
return true;
|
|
671
|
+
}
|
|
672
|
+
if (!this.getBinding())
|
|
673
|
+
throw new Error('no binding defined');
|
|
674
|
+
this.getBinding().removeAllListeners('discover');
|
|
675
|
+
const ongoing = this.peripheralCache.filter(i => i.state.isLoading);
|
|
676
|
+
if (ongoing)
|
|
677
|
+
ongoing.forEach(i => { i.isInterrupted = true; });
|
|
678
|
+
yield this.getBinding().stopScanning();
|
|
679
|
+
this.scanState.isScanning = false;
|
|
680
|
+
this.logEvent({ message: 'scan stop result: success' });
|
|
681
|
+
return true;
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
isScanning() {
|
|
685
|
+
return this.scanState.isScanning === true;
|
|
686
|
+
}
|
|
687
|
+
addConnectedDevice(device) {
|
|
688
|
+
const existigDevice = this.devices.find(i => i.device.id === device.id && i.device.getProfile() === device.getProfile());
|
|
689
|
+
if (existigDevice) {
|
|
690
|
+
existigDevice.isConnected = true;
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
this.devices.push({ device, isConnected: true });
|
|
694
|
+
}
|
|
695
|
+
addDeviceToCache(device, isConnected) {
|
|
696
|
+
const existigDevice = this.devices.find(i => i.device.id === device.id && i.device.getProfile() === device.getProfile());
|
|
697
|
+
if (existigDevice) {
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
this.devices.push({ device, isConnected });
|
|
701
|
+
}
|
|
702
|
+
findConnected(device) {
|
|
703
|
+
const connected = this.devices.find(i => i.device.id === device.id && i.isConnected);
|
|
704
|
+
if (connected)
|
|
705
|
+
return connected.device;
|
|
706
|
+
return undefined;
|
|
707
|
+
}
|
|
708
|
+
findDeviceInCache(device) {
|
|
709
|
+
const existing = this.devices.find(i => (i.device.id === device.id || i.device.address === device.address || i.device.name === device.name) && i.device.getProfile() === device.profile);
|
|
710
|
+
return existing ? existing.device : undefined;
|
|
711
|
+
}
|
|
712
|
+
removeConnectedDevice(device) {
|
|
713
|
+
const existigDevice = this.devices.find(i => i.device.id === device.id);
|
|
714
|
+
if (existigDevice) {
|
|
715
|
+
existigDevice.isConnected = false;
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
exports.default = BleInterface;
|
|
721
|
+
BleInterface.deviceClasses = [];
|