incyclist-devices 1.4.98 → 1.4.102
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/CyclingMode.d.ts +76 -76
- package/lib/CyclingMode.js +79 -79
- package/lib/Device.d.ts +92 -92
- package/lib/Device.js +71 -71
- package/lib/DeviceProtocol.d.ts +74 -74
- package/lib/DeviceProtocol.js +41 -41
- package/lib/DeviceRegistry.d.ts +8 -8
- package/lib/DeviceRegistry.js +33 -33
- package/lib/DeviceSupport.d.ts +34 -34
- package/lib/DeviceSupport.js +78 -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/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 -127
- package/lib/ble/ble-interface.d.ts +100 -99
- package/lib/ble/ble-interface.js +719 -712
- 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 -730
- 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/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/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 -145
- 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/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 +46 -46
- package/lib/ant/antfe/ant-fe-st-mode copy.d.ts +0 -7
- package/lib/ant/antfe/ant-fe-st-mode copy.js +0 -54
- package/lib/ant/antpwr/AntPWRAdapter.d.ts +0 -24
- package/lib/ant/antpwr/AntPWRAdapter.js +0 -252
- package/lib/daum/PowerMeterCyclingMode.d.ts +0 -18
- package/lib/daum/PowerMeterCyclingMode.js +0 -78
- package/lib/daum/classic/ERGCyclingMode.d.ts +0 -23
- package/lib/daum/classic/ERGCyclingMode.js +0 -171
- package/lib/daum/indoorbike.d.ts +0 -24
- package/lib/daum/indoorbike.js +0 -178
- package/lib/kettler/ergo-racer/modes/power-meter.d.ts +0 -18
- package/lib/kettler/ergo-racer/modes/power-meter.js +0 -86
- package/lib/simulator/simulator-mode.d.ts +0 -28
- package/lib/simulator/simulator-mode.js +0 -120
package/lib/ble/ble-device.js
CHANGED
|
@@ -1,442 +1,444 @@
|
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.BleDevice = void 0;
|
|
13
|
-
const gd_eventlog_1 = require("gd-eventlog");
|
|
14
|
-
const ble_1 = require("./ble");
|
|
15
|
-
const ble_2 = require("./ble");
|
|
16
|
-
const CONNECT_WAIT_TIMEOUT = 10000;
|
|
17
|
-
const BLE_TIMEOUT = 1000;
|
|
18
|
-
class BleDevice extends ble_1.BleDeviceClass {
|
|
19
|
-
constructor(props) {
|
|
20
|
-
super();
|
|
21
|
-
this.characteristics = [];
|
|
22
|
-
this.deviceInfo = {};
|
|
23
|
-
this.id = props.id;
|
|
24
|
-
this.address = props.address;
|
|
25
|
-
this.name = props.name;
|
|
26
|
-
this.services = props.services;
|
|
27
|
-
this.ble = props.ble;
|
|
28
|
-
this.characteristics = [];
|
|
29
|
-
this.subscribedCharacteristics = [];
|
|
30
|
-
this.isInitialized = false;
|
|
31
|
-
this.writeQueue = [];
|
|
32
|
-
this.workerIv = null;
|
|
33
|
-
this.prevMessages = [];
|
|
34
|
-
if (props.peripheral) {
|
|
35
|
-
const { id, address, advertisement, state } = props.peripheral;
|
|
36
|
-
this.peripheral = props.peripheral;
|
|
37
|
-
this.id = id;
|
|
38
|
-
this.address = address;
|
|
39
|
-
this.name = advertisement.localName;
|
|
40
|
-
this.services = advertisement.serviceUuids;
|
|
41
|
-
this.state = state;
|
|
42
|
-
}
|
|
43
|
-
if (props.logger) {
|
|
44
|
-
this.logger = props.logger;
|
|
45
|
-
}
|
|
46
|
-
else if (props.log !== false) {
|
|
47
|
-
this.logger = new gd_eventlog_1.EventLogger('BleDevice');
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
getServices() {
|
|
51
|
-
return this.services;
|
|
52
|
-
}
|
|
53
|
-
logEvent(event) {
|
|
54
|
-
if (this.logger) {
|
|
55
|
-
this.logger.logEvent(event);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
this.
|
|
86
|
-
this.
|
|
87
|
-
this.
|
|
88
|
-
this.
|
|
89
|
-
this.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
this.
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
yield
|
|
140
|
-
|
|
141
|
-
this.
|
|
142
|
-
this.
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
this.logEvent({ message: 'connect
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
this.
|
|
214
|
-
this.
|
|
215
|
-
this.
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
this.
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
this.
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
this.connectState.
|
|
250
|
-
this.
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
this.
|
|
257
|
-
this.connectState.
|
|
258
|
-
this.
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
this.
|
|
265
|
-
this.
|
|
266
|
-
this.
|
|
267
|
-
this.connectState.
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
connector.
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
this.
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
this.
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
reject(
|
|
413
|
-
else
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
info.
|
|
434
|
-
info.
|
|
435
|
-
info.
|
|
436
|
-
info.
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
}
|
|
442
|
-
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BleDevice = void 0;
|
|
13
|
+
const gd_eventlog_1 = require("gd-eventlog");
|
|
14
|
+
const ble_1 = require("./ble");
|
|
15
|
+
const ble_2 = require("./ble");
|
|
16
|
+
const CONNECT_WAIT_TIMEOUT = 10000;
|
|
17
|
+
const BLE_TIMEOUT = 1000;
|
|
18
|
+
class BleDevice extends ble_1.BleDeviceClass {
|
|
19
|
+
constructor(props) {
|
|
20
|
+
super();
|
|
21
|
+
this.characteristics = [];
|
|
22
|
+
this.deviceInfo = {};
|
|
23
|
+
this.id = props.id;
|
|
24
|
+
this.address = props.address;
|
|
25
|
+
this.name = props.name;
|
|
26
|
+
this.services = props.services;
|
|
27
|
+
this.ble = props.ble;
|
|
28
|
+
this.characteristics = [];
|
|
29
|
+
this.subscribedCharacteristics = [];
|
|
30
|
+
this.isInitialized = false;
|
|
31
|
+
this.writeQueue = [];
|
|
32
|
+
this.workerIv = null;
|
|
33
|
+
this.prevMessages = [];
|
|
34
|
+
if (props.peripheral) {
|
|
35
|
+
const { id, address, advertisement, state } = props.peripheral;
|
|
36
|
+
this.peripheral = props.peripheral;
|
|
37
|
+
this.id = id;
|
|
38
|
+
this.address = address;
|
|
39
|
+
this.name = advertisement.localName;
|
|
40
|
+
this.services = advertisement.serviceUuids;
|
|
41
|
+
this.state = state;
|
|
42
|
+
}
|
|
43
|
+
if (props.logger) {
|
|
44
|
+
this.logger = props.logger;
|
|
45
|
+
}
|
|
46
|
+
else if (props.log !== false) {
|
|
47
|
+
this.logger = new gd_eventlog_1.EventLogger('BleDevice');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
getServices() {
|
|
51
|
+
return this.services;
|
|
52
|
+
}
|
|
53
|
+
logEvent(event) {
|
|
54
|
+
if (this.logger) {
|
|
55
|
+
this.logger.logEvent(event);
|
|
56
|
+
}
|
|
57
|
+
if (process.env.BLE_DEBUG) {
|
|
58
|
+
console.log('~~~BLE:', event);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
setLogger(logger) {
|
|
62
|
+
this.logger = logger;
|
|
63
|
+
}
|
|
64
|
+
setInterface(ble) {
|
|
65
|
+
this.ble = ble;
|
|
66
|
+
}
|
|
67
|
+
isMatching(characteristics) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
cleanupListeners() {
|
|
71
|
+
if (this.characteristics === undefined) {
|
|
72
|
+
this.characteristics = [];
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const connector = this.ble.getConnector(this.peripheral);
|
|
76
|
+
this.characteristics.forEach(c => {
|
|
77
|
+
connector.removeAllListeners((0, ble_1.uuid)(c.uuid));
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
onDisconnect() {
|
|
82
|
+
this.logEvent({ message: 'device disconnected', address: this.address, profile: this.getProfile() });
|
|
83
|
+
this.state = "disconnected";
|
|
84
|
+
if (!this.connectState.isDisconnecting) {
|
|
85
|
+
this.peripheral.state = 'disconnected';
|
|
86
|
+
this.connectState.isConnecting = false;
|
|
87
|
+
this.connectState.isConnected = false;
|
|
88
|
+
this.cleanupListeners();
|
|
89
|
+
this.subscribedCharacteristics = [];
|
|
90
|
+
this.ble.onDisconnect(this.peripheral);
|
|
91
|
+
this.connect({ reconnect: true });
|
|
92
|
+
}
|
|
93
|
+
this.emit('disconnected');
|
|
94
|
+
}
|
|
95
|
+
waitForConnectFinished(timeout) {
|
|
96
|
+
const waitStart = Date.now();
|
|
97
|
+
const waitTimeout = waitStart + timeout;
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
const waitIv = setInterval(() => {
|
|
100
|
+
try {
|
|
101
|
+
if (this.connectState.isConnecting && Date.now() > waitTimeout) {
|
|
102
|
+
clearInterval(waitIv);
|
|
103
|
+
return reject(new Error('connection already in progress'));
|
|
104
|
+
}
|
|
105
|
+
if (!this.connectState.isConnecting) {
|
|
106
|
+
clearInterval(waitIv);
|
|
107
|
+
return resolve(true);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
console.log('~~~ error', err);
|
|
112
|
+
}
|
|
113
|
+
}, 100);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
hasService(serviceUuid) {
|
|
117
|
+
return this.services && this.services.find(s => s === serviceUuid || (0, ble_1.uuid)(serviceUuid)) !== undefined;
|
|
118
|
+
}
|
|
119
|
+
init() {
|
|
120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
return yield this.initDevice();
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
initDevice() {
|
|
125
|
+
this.logEvent({ message: 'get device info' });
|
|
126
|
+
return this.getDeviceInfo().then(() => {
|
|
127
|
+
this.emit('deviceInfo', this.deviceInfo);
|
|
128
|
+
this.logEvent(Object.assign({ message: 'device init done' }, this.deviceInfo));
|
|
129
|
+
this.isInitialized = true;
|
|
130
|
+
return true;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
connectPeripheral(peripheral) {
|
|
134
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
135
|
+
this.connectState.isConnecting = true;
|
|
136
|
+
try {
|
|
137
|
+
const connector = this.ble.getConnector(peripheral);
|
|
138
|
+
connector.on('disconnect', () => { this.onDisconnect(); });
|
|
139
|
+
yield connector.connect();
|
|
140
|
+
yield connector.initialize();
|
|
141
|
+
yield this.subscribeAll(connector);
|
|
142
|
+
this.connectState.isConnected = true;
|
|
143
|
+
this.state = "connected";
|
|
144
|
+
this.emit('connected');
|
|
145
|
+
yield this.init();
|
|
146
|
+
}
|
|
147
|
+
catch (err) {
|
|
148
|
+
this.logEvent({ message: 'Error', fn: 'connectPeripheral()', error: err.message, stack: err.stack });
|
|
149
|
+
}
|
|
150
|
+
this.connectState.isConnecting = false;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
subscribeAll(conn) {
|
|
154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
155
|
+
try {
|
|
156
|
+
const connector = conn || this.ble.getConnector(this.peripheral);
|
|
157
|
+
const subscribed = yield connector.subscribeAll((uuid, data) => { this.onData(uuid, data); });
|
|
158
|
+
subscribed.forEach(c => this.subscribedCharacteristics.push(c));
|
|
159
|
+
}
|
|
160
|
+
catch (err) {
|
|
161
|
+
this.logEvent({ message: 'Error', fn: 'subscribeAll()', error: err.message, stack: err.stack });
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
connect(props) {
|
|
166
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
167
|
+
const { reconnect } = props || {};
|
|
168
|
+
try {
|
|
169
|
+
this.logEvent({ message: reconnect ? 'reconnect' : 'connect', address: this.peripheral ? this.peripheral.address : this.address, state: this.connectState });
|
|
170
|
+
if (!reconnect && this.connectState.isConnecting) {
|
|
171
|
+
yield this.waitForConnectFinished(CONNECT_WAIT_TIMEOUT);
|
|
172
|
+
}
|
|
173
|
+
if (this.connectState.isConnected) {
|
|
174
|
+
try {
|
|
175
|
+
yield this.subscribeAll();
|
|
176
|
+
yield this.init();
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
this.logEvent({ message: 'cannot reconnect', error: err.message || err });
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
this.connectState.isConnecting = true;
|
|
185
|
+
if (!this.peripheral) {
|
|
186
|
+
const { id, name, address } = this;
|
|
187
|
+
try {
|
|
188
|
+
this.peripheral = this.ble.findPeripheral({ id, name, address });
|
|
189
|
+
}
|
|
190
|
+
catch (err) {
|
|
191
|
+
console.log('~~~ error', err);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (this.peripheral) {
|
|
195
|
+
const { id, address, advertisement } = this.peripheral;
|
|
196
|
+
const name = advertisement === null || advertisement === void 0 ? void 0 : advertisement.localName;
|
|
197
|
+
this.logEvent({ message: 'connect requested', mode: 'peripheral', device: { id, name, address: address } });
|
|
198
|
+
yield this.connectPeripheral(this.peripheral);
|
|
199
|
+
this.logEvent({ message: 'connect result: success', mode: 'peripheral', device: { id, name, address } });
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
const { id, name, address } = this;
|
|
204
|
+
let error;
|
|
205
|
+
if (this.address || this.id || this.name) {
|
|
206
|
+
this.logEvent({ message: 'connect requested', mode: 'device', device: { id, name, address } });
|
|
207
|
+
try {
|
|
208
|
+
if (this.ble.isScanning()) {
|
|
209
|
+
yield this.ble.stopScan();
|
|
210
|
+
}
|
|
211
|
+
const devices = yield this.ble.scan({ requested: this });
|
|
212
|
+
if (devices && devices.length > 0) {
|
|
213
|
+
this.peripheral = devices[0].peripheral;
|
|
214
|
+
yield this.connectPeripheral(this.peripheral);
|
|
215
|
+
this.logEvent({ message: 'connect result: success', mode: 'device', device: { id, name, address } });
|
|
216
|
+
this.connectState.isConnecting = false;
|
|
217
|
+
this.connectState.isConnected = true;
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
catch (err) {
|
|
222
|
+
console.log('~~~ error', err);
|
|
223
|
+
error = err;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
this.logEvent({ message: 'connect result: failure', mode: 'device', device: { id, name, address }, error: error.message, stack: error.stack });
|
|
227
|
+
this.connectState.isConnecting = false;
|
|
228
|
+
this.connectState.isConnected = false;
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
catch (err) {
|
|
233
|
+
this.connectState.isConnecting = false;
|
|
234
|
+
this.connectState.isConnected = false;
|
|
235
|
+
this.logEvent({ message: 'connect result: error', error: err.message });
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
disconnect() {
|
|
241
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
242
|
+
const { id, name, address } = this;
|
|
243
|
+
this.logEvent({ message: 'disconnect requested', device: { id, name, address } });
|
|
244
|
+
this.connectState.isDisconnecting = true;
|
|
245
|
+
if (this.workerIv) {
|
|
246
|
+
this.stopWorker();
|
|
247
|
+
}
|
|
248
|
+
if (!this.connectState.isConnecting && !this.connectState.isConnected) {
|
|
249
|
+
this.connectState.isDisconnecting = false;
|
|
250
|
+
this.connectState.isConnecting = false;
|
|
251
|
+
this.connectState.isConnected = false;
|
|
252
|
+
this.logEvent({ message: 'disconnect result: success', device: { id, name, address } });
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
if (this.connectState.isConnecting) {
|
|
256
|
+
this.cleanupListeners();
|
|
257
|
+
setTimeout(() => { this.connectState.isDisconnecting = false; }, 1000);
|
|
258
|
+
this.logEvent({ message: 'disconnect result: unclear - connect ongoing', device: { id, name, address } });
|
|
259
|
+
this.connectState.isConnecting = false;
|
|
260
|
+
this.connectState.isConnected = false;
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
if (this.connectState.isConnected) {
|
|
264
|
+
this.ble.removeConnectedDevice(this);
|
|
265
|
+
this.cleanupListeners();
|
|
266
|
+
this.logEvent({ message: 'disconnect result: success', device: { id, name, address } });
|
|
267
|
+
this.connectState.isDisconnecting = false;
|
|
268
|
+
this.connectState.isConnecting = false;
|
|
269
|
+
this.connectState.isConnected = false;
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
checkForDuplicate(characteristic, data) {
|
|
275
|
+
const prev = this.prevMessages.find(i => i.uuid === characteristic);
|
|
276
|
+
if (prev) {
|
|
277
|
+
if (prev.data === data.toString('hex') && prev.timestamp > Date.now() - 500) {
|
|
278
|
+
prev.timestamp = Date.now();
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
prev.data = data.toString('hex');
|
|
283
|
+
prev.timestamp = Date.now();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
this.prevMessages.push({ uuid: characteristic, timestamp: Date.now(), data: data.toString('hex') });
|
|
288
|
+
}
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
onData(characteristic, _data) {
|
|
292
|
+
const data = Buffer.from(_data);
|
|
293
|
+
const isDuplicate = this.checkForDuplicate(characteristic, data);
|
|
294
|
+
if (isDuplicate) {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
this.logEvent({ message: 'got data', characteristic, data: data.toString('hex'), writeQueue: this.writeQueue.length });
|
|
298
|
+
if (this.writeQueue.length > 0) {
|
|
299
|
+
const writeIdx = this.writeQueue.findIndex(i => (0, ble_2.matches)(i.uuid, characteristic));
|
|
300
|
+
if (writeIdx !== -1) {
|
|
301
|
+
const writeItem = this.writeQueue[writeIdx];
|
|
302
|
+
this.writeQueue.splice(writeIdx, 1);
|
|
303
|
+
if (writeItem.resolve)
|
|
304
|
+
writeItem.resolve(data);
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
timeoutCheck() {
|
|
311
|
+
const now = Date.now();
|
|
312
|
+
const updatedQueue = [];
|
|
313
|
+
let hasTimeout = false;
|
|
314
|
+
this.writeQueue.forEach(writeItem => {
|
|
315
|
+
if (writeItem.timeout && writeItem.timeout < now) {
|
|
316
|
+
if (writeItem.reject) {
|
|
317
|
+
hasTimeout = true;
|
|
318
|
+
writeItem.reject(new Error('timeout'));
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
updatedQueue.push(writeItem);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
if (hasTimeout)
|
|
326
|
+
this.writeQueue = updatedQueue;
|
|
327
|
+
}
|
|
328
|
+
startWorker() {
|
|
329
|
+
if (this.workerIv)
|
|
330
|
+
return;
|
|
331
|
+
this.workerIv = setInterval(() => { this.timeoutCheck(); }, 100);
|
|
332
|
+
}
|
|
333
|
+
stopWorker() {
|
|
334
|
+
if (!this.workerIv)
|
|
335
|
+
return;
|
|
336
|
+
clearInterval(this.workerIv);
|
|
337
|
+
this.workerIv = null;
|
|
338
|
+
}
|
|
339
|
+
write(characteristicUuid, data, props) {
|
|
340
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
341
|
+
if (!this.isConnected())
|
|
342
|
+
throw new Error('not connected');
|
|
343
|
+
try {
|
|
344
|
+
const { withoutResponse, timeout } = props || {};
|
|
345
|
+
const connector = this.ble.getConnector(this.peripheral);
|
|
346
|
+
const isAlreadySubscribed = connector.isSubscribed(characteristicUuid);
|
|
347
|
+
if (!withoutResponse && !this.workerIv) {
|
|
348
|
+
this.startWorker();
|
|
349
|
+
}
|
|
350
|
+
if (!withoutResponse && !isAlreadySubscribed) {
|
|
351
|
+
const connector = this.ble.getConnector(this.peripheral);
|
|
352
|
+
connector.removeAllListeners(characteristicUuid);
|
|
353
|
+
connector.on(characteristicUuid, (uuid, data) => {
|
|
354
|
+
this.onData(uuid, data);
|
|
355
|
+
});
|
|
356
|
+
this.logEvent({ message: 'write:subscribing ', characteristic: characteristicUuid });
|
|
357
|
+
yield connector.subscribe(characteristicUuid);
|
|
358
|
+
this.subscribedCharacteristics.push(characteristicUuid);
|
|
359
|
+
}
|
|
360
|
+
return new Promise((resolve, reject) => {
|
|
361
|
+
const characteristic = this.characteristics.find(c => c.uuid === characteristicUuid || (0, ble_1.uuid)(c.uuid) === characteristicUuid);
|
|
362
|
+
if (!characteristic) {
|
|
363
|
+
reject(new Error('Characteristic not found'));
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (withoutResponse) {
|
|
367
|
+
this.logEvent({ message: 'writing', data: data.toString('hex'), withoutResponse });
|
|
368
|
+
characteristic.write(data, withoutResponse);
|
|
369
|
+
resolve(new ArrayBuffer(0));
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
const writeId = this.writeQueue.length;
|
|
374
|
+
let messageDeleted = false;
|
|
375
|
+
const writeTimeout = timeout !== undefined ? timeout : BLE_TIMEOUT;
|
|
376
|
+
this.writeQueue.push({ uuid: characteristicUuid.toLocaleLowerCase(), data, timeout: Date.now() + writeTimeout, resolve, reject });
|
|
377
|
+
const to = setTimeout(() => {
|
|
378
|
+
if (this.writeQueue.length > writeId && !messageDeleted)
|
|
379
|
+
this.writeQueue.splice(writeId, 1);
|
|
380
|
+
this.logEvent({ message: 'writing response', err: 'timeout' });
|
|
381
|
+
reject(new Error('timeout'));
|
|
382
|
+
}, 5000);
|
|
383
|
+
this.logEvent({ message: 'writing' });
|
|
384
|
+
characteristic.write(data, withoutResponse, (err) => {
|
|
385
|
+
clearTimeout(to);
|
|
386
|
+
this.logEvent({ message: 'writing response', err });
|
|
387
|
+
if (err) {
|
|
388
|
+
this.writeQueue.splice(writeId, 1);
|
|
389
|
+
messageDeleted = true;
|
|
390
|
+
reject(err);
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
catch (err) {
|
|
397
|
+
this.logEvent({ message: 'error', fn: 'write', error: err.message || err, stack: err.stack });
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
read(characteristicUuid) {
|
|
402
|
+
return new Promise((resolve, reject) => {
|
|
403
|
+
if (!this.isConnected())
|
|
404
|
+
return reject(new Error('not connected'));
|
|
405
|
+
const characteristic = this.characteristics.find(c => c.uuid === characteristicUuid || (0, ble_1.uuid)(c.uuid) === characteristicUuid);
|
|
406
|
+
if (!characteristic) {
|
|
407
|
+
reject(new Error('Characteristic not found'));
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
characteristic.read((err, data) => {
|
|
411
|
+
if (err && data instanceof Error)
|
|
412
|
+
reject(err);
|
|
413
|
+
else if (data instanceof Error)
|
|
414
|
+
reject(data);
|
|
415
|
+
else
|
|
416
|
+
resolve(data);
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
getDeviceInfo() {
|
|
421
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
422
|
+
const info = this.deviceInfo;
|
|
423
|
+
const readValue = (c) => __awaiter(this, void 0, void 0, function* () {
|
|
424
|
+
try {
|
|
425
|
+
const b = yield this.read(c);
|
|
426
|
+
const buffer = b ? Buffer.from(b) : undefined;
|
|
427
|
+
return buffer ? buffer.toString() : undefined;
|
|
428
|
+
}
|
|
429
|
+
catch (_a) {
|
|
430
|
+
return undefined;
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
info.model = info.model || (yield readValue('2a24'));
|
|
434
|
+
info.serialNo = info.serialNo || (yield readValue('2a25'));
|
|
435
|
+
info.fwRevision = info.fwRevision || (yield readValue('2a26'));
|
|
436
|
+
info.hwRevision = info.hwRevision || (yield readValue('2a27'));
|
|
437
|
+
info.swRevision = info.swRevision || (yield readValue('2a28'));
|
|
438
|
+
info.manufacturer = info.manufacturer || (yield readValue('2a29'));
|
|
439
|
+
this.deviceInfo = info;
|
|
440
|
+
return info;
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
exports.BleDevice = BleDevice;
|