incyclist-devices 1.5.11 → 1.5.12

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