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