incyclist-devices 2.3.0-beta.8 → 2.3.0-beta.9

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.
@@ -32,6 +32,7 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
32
32
  protected onDiscovered: (peripheral: BlePeripheralInfo) => void;
33
33
  protected instanceId: number;
34
34
  protected connectedPeripherals: IBlePeripheral[];
35
+ protected connectAttemptCnt: number;
35
36
  static getInstance(props?: InterfaceProps): BleInterface;
36
37
  protected constructor(props: InterfaceProps);
37
38
  getLogger(): EventLogger;
@@ -63,6 +64,8 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
63
64
  pauseDiscovery(): Promise<void>;
64
65
  resumeDiscovery(): Promise<void>;
65
66
  protected onPeripheralFound(peripheral: BleRawPeripheral): void;
67
+ protected checkForWahooEnhancement(announcement: BlePeripheralAnnouncement): boolean;
68
+ protected processWahooAnnouncement(announcement: BlePeripheralAnnouncement): void;
66
69
  protected buildAnnouncement(peripheral: BleRawPeripheral): BlePeripheralAnnouncement;
67
70
  protected updateWithServices(announcement: BlePeripheralAnnouncement): Promise<BlePeripheralAnnouncement>;
68
71
  protected discoverServices(announcement: BlePeripheralAnnouncement): Promise<string[]>;
@@ -85,6 +88,8 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
85
88
  protected waitForBleConnected(): Promise<boolean>;
86
89
  protected onBleStateChange(state: BleInterfaceState): void;
87
90
  protected getAdapterFactory(): BleAdapterFactory<TBleSensor>;
91
+ protected getConnectTimeout(): number;
92
+ protected getExpectedServices(): string[];
88
93
  logEvent(event: any): void;
89
94
  logError(err: Error, fn: string, args?: any): void;
90
95
  }
@@ -49,6 +49,7 @@ class BleInterface extends events_1.default {
49
49
  this.expectedServices = ['180d', '1818', '1826', '6e40fec1'];
50
50
  this.matching = [];
51
51
  this.connectedPeripherals = [];
52
+ this.connectAttemptCnt = 0;
52
53
  this.instanceId = ++instanceCount;
53
54
  this.props = props;
54
55
  this.logEnabled = props.log || true;
@@ -90,28 +91,28 @@ class BleInterface extends events_1.default {
90
91
  return false;
91
92
  }
92
93
  if (this.isConnecting()) {
93
- this.logEvent({ message: 'connect - already connecting' });
94
+ this.logEvent({ message: 'BLE connect - already connecting' });
94
95
  return this.connectTask.getPromise();
95
96
  }
96
97
  if (this.isConnected())
97
98
  return true;
98
- this.logEvent({ message: 'Ble connect request' });
99
+ this.logEvent({ message: 'BLE connect request' });
99
100
  this.connectTask = new task_1.InteruptableTask(this.connectBle(), {
100
- timeout: BLE_DEFAULT_CONNECT_TIMEOUT,
101
- name: 'connect',
101
+ timeout: this.getConnectTimeout(),
102
+ name: 'BLE connect',
102
103
  errorOnTimeout: false,
103
104
  log: this.logEvent.bind(this),
104
105
  });
105
- const success = yield this.connectTask.run();
106
+ const success = yield this.connectTask.run().catch(() => false);
106
107
  if (success) {
107
108
  this.startPeripheralScan();
108
109
  }
109
- this.expectedServices = factories_1.BleAdapterFactory.getInstance('ble').getAllSupportedServices();
110
110
  return success;
111
111
  });
112
112
  }
113
113
  disconnect() {
114
114
  return __awaiter(this, void 0, void 0, function* () {
115
+ var _a;
115
116
  if (!this.getBinding()) {
116
117
  return false;
117
118
  }
@@ -122,14 +123,16 @@ class BleInterface extends events_1.default {
122
123
  const promises = this.connectedPeripherals.map(p => p.disconnect());
123
124
  yield Promise.allSettled(promises);
124
125
  this.connectedPeripherals = [];
125
- yield this.connectTask.stop();
126
+ if (this.isConnecting())
127
+ yield ((_a = this.connectTask) === null || _a === void 0 ? void 0 : _a.stop());
126
128
  this.getBinding().removeAllListeners();
129
+ this.connectAttemptCnt = 0;
127
130
  return true;
128
131
  });
129
132
  }
130
133
  isConnected() {
131
134
  var _a;
132
- return ((_a = this.getBinding()) === null || _a === void 0 ? void 0 : _a.state) === 'poweredOn';
135
+ return this.connectAttemptCnt > 0 && ((_a = this.getBinding()) === null || _a === void 0 ? void 0 : _a.state) === 'poweredOn';
133
136
  }
134
137
  registerConnected(peripheral) {
135
138
  this.connectedPeripherals.push(peripheral);
@@ -228,6 +231,7 @@ class BleInterface extends events_1.default {
228
231
  }
229
232
  startPeripheralScan() {
230
233
  return __awaiter(this, arguments, void 0, function* (retry = false) {
234
+ this.expectedServices = this.getExpectedServices();
231
235
  if (!retry)
232
236
  this.logEvent({ message: 'starting peripheral discovery ...' });
233
237
  if (!this.isConnected() || this.isDiscovering()) {
@@ -254,9 +258,10 @@ class BleInterface extends events_1.default {
254
258
  return;
255
259
  this.logEvent({ message: 'stopping peripheral discovery ...' });
256
260
  this.discoverTask.stop();
257
- this.getBinding().off('discover', this.onDiscovered);
261
+ const ble = this.getBinding();
262
+ ble.off('discover', this.onDiscovered);
258
263
  return new Promise(done => {
259
- this.getBinding().stopScanning(() => {
264
+ ble.stopScanning(() => {
260
265
  done();
261
266
  });
262
267
  });
@@ -313,24 +318,32 @@ class BleInterface extends events_1.default {
313
318
  }
314
319
  if (announcement.serviceUUIDs.length === 0) {
315
320
  return;
316
- if (this.isCompleting(announcement)) {
317
- return;
318
- }
319
- this.addCompleting(announcement);
320
- setTimeout(() => {
321
- if (this.find(announcement)) {
322
- this.removeCompleting(announcement);
323
- return;
324
- }
325
- this.updateWithServices(announcement)
326
- .then(() => {
327
- if (this.isSupportedPeripheral(announcement))
328
- this.addService(announcement);
329
- });
330
- }, 1000);
321
+ }
322
+ const isWahoo = this.checkForWahooEnhancement(announcement);
323
+ if (isWahoo) {
324
+ this.processWahooAnnouncement(announcement);
325
+ return;
331
326
  }
332
327
  this.addService(announcement);
333
328
  }
329
+ checkForWahooEnhancement(announcement) {
330
+ if (announcement.name.includes('KICKR')) {
331
+ const supported = announcement.serviceUUIDs.map(s => (0, utils_1.beautifyUUID)(s));
332
+ if (supported.length === 1 && supported[0] === '1818')
333
+ return true;
334
+ }
335
+ return false;
336
+ }
337
+ processWahooAnnouncement(announcement) {
338
+ if (this.isCompleting(announcement)) {
339
+ return;
340
+ }
341
+ this.updateWithServices(announcement)
342
+ .then(() => {
343
+ if (this.isSupportedPeripheral(announcement))
344
+ this.addService(announcement);
345
+ });
346
+ }
334
347
  buildAnnouncement(peripheral) {
335
348
  var _a;
336
349
  return {
@@ -379,11 +392,11 @@ class BleInterface extends events_1.default {
379
392
  const res = yield peripheral.discoverSomeServicesAndCharacteristicsAsync([], []);
380
393
  announcement.serviceUUIDs = res.services.map(s => s.uuid);
381
394
  }
382
- peripheral.removeAllListeners();
383
395
  }
384
396
  catch (err) {
385
397
  this.logEvent({ message: 'discover services failed', reason: err.message, device });
386
398
  }
399
+ peripheral === null || peripheral === void 0 ? void 0 : peripheral.removeAllListeners();
387
400
  if (paused) {
388
401
  yield this.resumeDiscovery();
389
402
  }
@@ -498,8 +511,10 @@ class BleInterface extends events_1.default {
498
511
  }
499
512
  connectBle() {
500
513
  return __awaiter(this, void 0, void 0, function* () {
514
+ this.connectAttemptCnt++;
501
515
  const state = this.getBinding().state;
502
516
  if (state === 'poweredOn') {
517
+ this.logEvent({ message: 'BLE connected' });
503
518
  return true;
504
519
  }
505
520
  const res = yield this.waitForBleConnected();
@@ -514,7 +529,7 @@ class BleInterface extends events_1.default {
514
529
  });
515
530
  this.getBinding().on('stateChange', (state) => {
516
531
  if (state === 'poweredOn') {
517
- this.logEvent({ message: 'Ble connect result: success' });
532
+ this.logEvent({ message: 'BLE connected' });
518
533
  this.getBinding().removeAllListeners('stateChange');
519
534
  this.getBinding().on('stateChange', this.onBleStateChange.bind(this));
520
535
  this.getBinding().on('error', console.log);
@@ -531,6 +546,12 @@ class BleInterface extends events_1.default {
531
546
  getAdapterFactory() {
532
547
  return factories_1.BleAdapterFactory.getInstance('ble');
533
548
  }
549
+ getConnectTimeout() {
550
+ return BLE_DEFAULT_CONNECT_TIMEOUT;
551
+ }
552
+ getExpectedServices() {
553
+ return this.getAdapterFactory().getAllSupportedServices();
554
+ }
534
555
  logEvent(event) {
535
556
  if (this.logDisabled && event.message !== 'Error')
536
557
  return;
@@ -471,7 +471,7 @@ class DirectConnectPeripheral {
471
471
  }
472
472
  if (incoming.length > header.length + 6) {
473
473
  this.remainingBuffer = Buffer.from(incoming.subarray(header.length + 6));
474
- incoming = incoming.subarray(0, header.length + 6);
474
+ incoming = Buffer.from(incoming.subarray(0, header.length + 6));
475
475
  }
476
476
  return incoming;
477
477
  }
@@ -499,7 +499,6 @@ class DirectConnectPeripheral {
499
499
  this.eventEmitter.emit(uuid, notification.body.characteristicData);
500
500
  }
501
501
  else {
502
- this.logEvent({ message: 'incoming message', path: this.getPath(), raw: incoming.toString('hex'), header });
503
502
  this.eventEmitter.emit(`response-${header.seqNum}`, incoming);
504
503
  }
505
504
  }
package/lib/utils/task.js CHANGED
@@ -66,6 +66,7 @@ class InteruptableTask {
66
66
  this.sendStopNotification();
67
67
  if (this.getState().result === 'completed' || this.getState().result === 'error')
68
68
  return;
69
+ this.getState().result = 'stopped';
69
70
  if (this.props.onDone)
70
71
  resolve(this.props.onDone(this.getState()));
71
72
  else
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "2.3.0-beta.8",
3
+ "version": "2.3.0-beta.9",
4
4
  "dependencies": {
5
5
  "@serialport/bindings-interface": "^1.2.2",
6
6
  "@serialport/parser-byte-length": "^9.0.1",