incyclist-devices 3.0.38 → 3.0.40

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.
@@ -5,6 +5,8 @@ const utils_js_1 = require("../../utils/utils.js");
5
5
  const utils_js_2 = require("../utils.js");
6
6
  const interface_js_1 = require("./interface.js");
7
7
  const SERVICE_COMPLETENESS_WHITELIST = ['MRK-R15'];
8
+ const POST_CONNECT_DISCOVERY_ATTEMPTS = 4;
9
+ const POST_CONNECT_DISCOVERY_BACKOFF_MS = 1000;
8
10
  class BlePeripheral {
9
11
  announcement;
10
12
  connected = false;
@@ -19,6 +21,7 @@ class BlePeripheral {
19
21
  serviceDiscoveryIncomplete = false;
20
22
  discoverServicesPromise;
21
23
  discoverCharacteristicsPromise = {};
24
+ awaitingPostConnectDiscovery = false;
22
25
  onErrorHandler = this.onPeripheralError.bind(this);
23
26
  constructor(announcement) {
24
27
  this.announcement = announcement;
@@ -70,6 +73,7 @@ class BlePeripheral {
70
73
  peripheral.once('disconnect', () => { this.onPeripheralDisconnect(); });
71
74
  peripheral.on('error', this.onErrorHandler);
72
75
  this.connected = true;
76
+ this.awaitingPostConnectDiscovery = true;
73
77
  done();
74
78
  })
75
79
  .catch(() => {
@@ -233,7 +237,7 @@ class BlePeripheral {
233
237
  async subscribe(characteristicUUID, callback) {
234
238
  try {
235
239
  if (this.disconnecting || !this.connected) {
236
- this.logEvent({ message: 'peripheral subscribe failed', uuid: characteristicUUID, reason: 'not connected',
240
+ this.logEvent({ message: 'peripheral subscribe failed', characteristic: (0, utils_js_2.beautifyUUID)(characteristicUUID), reason: 'not connected',
237
241
  disconnecting: this.disconnecting, connected: this.connected
238
242
  });
239
243
  return false;
@@ -247,7 +251,7 @@ class BlePeripheral {
247
251
  };
248
252
  const subscription = this.subscribed.find(s => s.uuid === uuid);
249
253
  if (subscription) {
250
- this.logEvent({ message: 'peripheral subscribe skipped', uuid: characteristicUUID, reason: 'already subscribed' });
254
+ this.logEvent({ message: 'peripheral subscribe skipped', characteristic: (0, utils_js_2.beautifyUUID)(characteristicUUID), reason: 'already subscribed' });
251
255
  const c = this.getRawCharacteristic(characteristicUUID);
252
256
  if (c) {
253
257
  c.off('data', onData);
@@ -257,13 +261,13 @@ class BlePeripheral {
257
261
  }
258
262
  let c = await this.queryRawCharacteristic(characteristicUUID).catch(() => null);
259
263
  if (!c) {
260
- this.logEvent({ message: 'peripheral subscribe failed', uuid: characteristicUUID, reason: 'not found' });
264
+ this.logEvent({ message: 'peripheral subscribe failed', characteristic: (0, utils_js_2.beautifyUUID)(characteristicUUID), reason: 'not found' });
261
265
  return false;
262
266
  }
263
267
  return new Promise((resolve, reject) => {
264
268
  const info = this.subscribed.find(s => s.uuid === characteristicUUID);
265
269
  if (info) {
266
- this.logEvent({ message: 'peripheral subscribe skipped', uuid: characteristicUUID, reason: 'already subscribed' });
270
+ this.logEvent({ message: 'peripheral subscribe skipped', characteristic: (0, utils_js_2.beautifyUUID)(characteristicUUID), reason: 'already subscribed' });
267
271
  return Promise.resolve(true);
268
272
  }
269
273
  this.logEvent({ message: 'subscribe request', address: this.getPeripheral().address, characteristic: uuid, success: true });
@@ -357,10 +361,10 @@ class BlePeripheral {
357
361
  else {
358
362
  const uuid = (0, utils_js_2.beautifyUUID)(element);
359
363
  if (c?.properties) {
360
- this.logEvent({ message: 'cannot subscribe', uuid, reason: 'invalid type', properties: c.properties.join('|') });
364
+ this.logEvent({ message: 'cannot subscribe', characteristics: (0, utils_js_2.beautifyUUID)(uuid), reason: 'invalid type', properties: c.properties.join('|') });
361
365
  }
362
366
  else {
363
- this.logEvent({ message: 'cannot subscribe', uuid, reason: 'not found' });
367
+ this.logEvent({ message: 'cannot subscribe', characteristics: (0, utils_js_2.beautifyUUID)(uuid), reason: 'not found' });
364
368
  }
365
369
  }
366
370
  }
@@ -371,11 +375,26 @@ class BlePeripheral {
371
375
  return true;
372
376
  }
373
377
  catch (err) {
374
- this.logEvent({ message: 'Error', fn: 'subscribeSelected', error: err.message, stack: err.stack });
378
+ this.logEvent({ message: 'Error', fn: 'subscribeSelected', uuids, error: err.message, stack: err.stack });
375
379
  return false;
376
380
  }
377
381
  }
378
382
  async discoverAllCharacteristics() {
383
+ const attempts = this.awaitingPostConnectDiscovery ? POST_CONNECT_DISCOVERY_ATTEMPTS : 1;
384
+ return this.discoverAllCharacteristicsAttempt(1, attempts);
385
+ }
386
+ async discoverAllCharacteristicsAttempt(attempt, maxAttempts) {
387
+ const found = await this.queryAllCharacteristics();
388
+ if (found.length > 0 || attempt >= maxAttempts) {
389
+ this.awaitingPostConnectDiscovery = false;
390
+ return found;
391
+ }
392
+ const { name, address } = this.getInfo();
393
+ this.logEvent({ message: 'discover all characteristics empty after connect - retrying', name, address, attempt });
394
+ await (0, utils_js_1.sleep)(POST_CONNECT_DISCOVERY_BACKOFF_MS * attempt);
395
+ return this.discoverAllCharacteristicsAttempt(attempt + 1, maxAttempts);
396
+ }
397
+ async queryAllCharacteristics() {
379
398
  try {
380
399
  const { name, address } = this.getInfo();
381
400
  this.logEvent({ message: 'discover all characteristics', name, address });
@@ -163,6 +163,11 @@ class BleFmAdapter extends adapter_js_1.default {
163
163
  this.isHubInitialized = false;
164
164
  }
165
165
  }
166
+ async restart(pause) {
167
+ this.zwiftPlay?.resetHubState();
168
+ this.isHubInitialized = false;
169
+ return await super.restart(pause);
170
+ }
166
171
  async initControl(_startProps) {
167
172
  if (!this.isStarting())
168
173
  return;
@@ -664,6 +664,9 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
664
664
  privateKeyEncoding: { type: 'pkcs8', format: 'der' }
665
665
  });
666
666
  }
667
+ resetHubState() {
668
+ this.setInitialState();
669
+ }
667
670
  setInitialState() {
668
671
  this.isHubPairConfirmed = false;
669
672
  this.encrypted = false;
@@ -2,6 +2,8 @@ import { sleep } from "../../utils/utils.js";
2
2
  import { beautifyUUID, fullUUID, isSameServiceFamily, matches } from "../utils.js";
3
3
  import { BleInterface } from "./interface.js";
4
4
  const SERVICE_COMPLETENESS_WHITELIST = ['MRK-R15'];
5
+ const POST_CONNECT_DISCOVERY_ATTEMPTS = 4;
6
+ const POST_CONNECT_DISCOVERY_BACKOFF_MS = 1000;
5
7
  export class BlePeripheral {
6
8
  announcement;
7
9
  connected = false;
@@ -16,6 +18,7 @@ export class BlePeripheral {
16
18
  serviceDiscoveryIncomplete = false;
17
19
  discoverServicesPromise;
18
20
  discoverCharacteristicsPromise = {};
21
+ awaitingPostConnectDiscovery = false;
19
22
  onErrorHandler = this.onPeripheralError.bind(this);
20
23
  constructor(announcement) {
21
24
  this.announcement = announcement;
@@ -67,6 +70,7 @@ export class BlePeripheral {
67
70
  peripheral.once('disconnect', () => { this.onPeripheralDisconnect(); });
68
71
  peripheral.on('error', this.onErrorHandler);
69
72
  this.connected = true;
73
+ this.awaitingPostConnectDiscovery = true;
70
74
  done();
71
75
  })
72
76
  .catch(() => {
@@ -230,7 +234,7 @@ export class BlePeripheral {
230
234
  async subscribe(characteristicUUID, callback) {
231
235
  try {
232
236
  if (this.disconnecting || !this.connected) {
233
- this.logEvent({ message: 'peripheral subscribe failed', uuid: characteristicUUID, reason: 'not connected',
237
+ this.logEvent({ message: 'peripheral subscribe failed', characteristic: beautifyUUID(characteristicUUID), reason: 'not connected',
234
238
  disconnecting: this.disconnecting, connected: this.connected
235
239
  });
236
240
  return false;
@@ -244,7 +248,7 @@ export class BlePeripheral {
244
248
  };
245
249
  const subscription = this.subscribed.find(s => s.uuid === uuid);
246
250
  if (subscription) {
247
- this.logEvent({ message: 'peripheral subscribe skipped', uuid: characteristicUUID, reason: 'already subscribed' });
251
+ this.logEvent({ message: 'peripheral subscribe skipped', characteristic: beautifyUUID(characteristicUUID), reason: 'already subscribed' });
248
252
  const c = this.getRawCharacteristic(characteristicUUID);
249
253
  if (c) {
250
254
  c.off('data', onData);
@@ -254,13 +258,13 @@ export class BlePeripheral {
254
258
  }
255
259
  let c = await this.queryRawCharacteristic(characteristicUUID).catch(() => null);
256
260
  if (!c) {
257
- this.logEvent({ message: 'peripheral subscribe failed', uuid: characteristicUUID, reason: 'not found' });
261
+ this.logEvent({ message: 'peripheral subscribe failed', characteristic: beautifyUUID(characteristicUUID), reason: 'not found' });
258
262
  return false;
259
263
  }
260
264
  return new Promise((resolve, reject) => {
261
265
  const info = this.subscribed.find(s => s.uuid === characteristicUUID);
262
266
  if (info) {
263
- this.logEvent({ message: 'peripheral subscribe skipped', uuid: characteristicUUID, reason: 'already subscribed' });
267
+ this.logEvent({ message: 'peripheral subscribe skipped', characteristic: beautifyUUID(characteristicUUID), reason: 'already subscribed' });
264
268
  return Promise.resolve(true);
265
269
  }
266
270
  this.logEvent({ message: 'subscribe request', address: this.getPeripheral().address, characteristic: uuid, success: true });
@@ -354,10 +358,10 @@ export class BlePeripheral {
354
358
  else {
355
359
  const uuid = beautifyUUID(element);
356
360
  if (c?.properties) {
357
- this.logEvent({ message: 'cannot subscribe', uuid, reason: 'invalid type', properties: c.properties.join('|') });
361
+ this.logEvent({ message: 'cannot subscribe', characteristics: beautifyUUID(uuid), reason: 'invalid type', properties: c.properties.join('|') });
358
362
  }
359
363
  else {
360
- this.logEvent({ message: 'cannot subscribe', uuid, reason: 'not found' });
364
+ this.logEvent({ message: 'cannot subscribe', characteristics: beautifyUUID(uuid), reason: 'not found' });
361
365
  }
362
366
  }
363
367
  }
@@ -368,11 +372,26 @@ export class BlePeripheral {
368
372
  return true;
369
373
  }
370
374
  catch (err) {
371
- this.logEvent({ message: 'Error', fn: 'subscribeSelected', error: err.message, stack: err.stack });
375
+ this.logEvent({ message: 'Error', fn: 'subscribeSelected', uuids, error: err.message, stack: err.stack });
372
376
  return false;
373
377
  }
374
378
  }
375
379
  async discoverAllCharacteristics() {
380
+ const attempts = this.awaitingPostConnectDiscovery ? POST_CONNECT_DISCOVERY_ATTEMPTS : 1;
381
+ return this.discoverAllCharacteristicsAttempt(1, attempts);
382
+ }
383
+ async discoverAllCharacteristicsAttempt(attempt, maxAttempts) {
384
+ const found = await this.queryAllCharacteristics();
385
+ if (found.length > 0 || attempt >= maxAttempts) {
386
+ this.awaitingPostConnectDiscovery = false;
387
+ return found;
388
+ }
389
+ const { name, address } = this.getInfo();
390
+ this.logEvent({ message: 'discover all characteristics empty after connect - retrying', name, address, attempt });
391
+ await sleep(POST_CONNECT_DISCOVERY_BACKOFF_MS * attempt);
392
+ return this.discoverAllCharacteristicsAttempt(attempt + 1, maxAttempts);
393
+ }
394
+ async queryAllCharacteristics() {
376
395
  try {
377
396
  const { name, address } = this.getInfo();
378
397
  this.logEvent({ message: 'discover all characteristics', name, address });
@@ -158,6 +158,11 @@ export default class BleFmAdapter extends BleAdapter {
158
158
  this.isHubInitialized = false;
159
159
  }
160
160
  }
161
+ async restart(pause) {
162
+ this.zwiftPlay?.resetHubState();
163
+ this.isHubInitialized = false;
164
+ return await super.restart(pause);
165
+ }
161
166
  async initControl(_startProps) {
162
167
  if (!this.isStarting())
163
168
  return;
@@ -661,6 +661,9 @@ export class BleZwiftPlaySensor extends TBleSensor {
661
661
  privateKeyEncoding: { type: 'pkcs8', format: 'der' }
662
662
  });
663
663
  }
664
+ resetHubState() {
665
+ this.setInitialState();
666
+ }
664
667
  setInitialState() {
665
668
  this.isHubPairConfirmed = false;
666
669
  this.encrypted = false;
@@ -17,6 +17,7 @@ export declare class BlePeripheral implements IBlePeripheral {
17
17
  protected serviceDiscoveryIncomplete: boolean;
18
18
  protected discoverServicesPromise: Promise<string[]> | undefined;
19
19
  protected discoverCharacteristicsPromise: Record<string, Promise<BleCharacteristic[]> | undefined>;
20
+ protected awaitingPostConnectDiscovery: boolean;
20
21
  protected onErrorHandler: any;
21
22
  constructor(announcement: BlePeripheralAnnouncement);
22
23
  get services(): BleService[];
@@ -44,6 +45,8 @@ export declare class BlePeripheral implements IBlePeripheral {
44
45
  unsubscribe(characteristicUUID: string): Promise<boolean>;
45
46
  subscribeSelected(characteristics: string[], callback: (characteristicUuid: string, data: Buffer, isNotify?: boolean) => void): Promise<boolean>;
46
47
  discoverAllCharacteristics(): Promise<string[]>;
48
+ protected discoverAllCharacteristicsAttempt(attempt: number, maxAttempts: number): Promise<string[]>;
49
+ protected queryAllCharacteristics(): Promise<string[]>;
47
50
  discoverSomeCharacteristics(characteristics: string[]): Promise<string[]>;
48
51
  subscribeAll(callback: (characteristicUuid: string, data: Buffer) => void): Promise<boolean>;
49
52
  unsubscribeAll(connectionLost?: boolean): Promise<void>;
@@ -28,6 +28,7 @@ export default class BleFmAdapter extends BleAdapter<IndoorBikeData, BleFitnessM
28
28
  transformData(bikeData: IncyclistBikeData): IncyclistAdapterData;
29
29
  protected checkResume(): boolean[];
30
30
  protected initVirtualShifting(initHub?: boolean): Promise<void>;
31
+ restart(pause?: number): Promise<boolean>;
31
32
  protected initControl(_startProps?: BleStartProperties): Promise<void>;
32
33
  protected initControlBestEffort(_startProps?: BleStartProperties): Promise<void>;
33
34
  protected attemptStartRequest(): Promise<void>;
@@ -81,6 +81,7 @@ export declare class BleZwiftPlaySensor extends TBleSensor {
81
81
  protected getCrypto(): ICryptoBinding;
82
82
  protected encryptedSupported(): boolean;
83
83
  protected createKeyPair(): any;
84
+ resetHubState(): void;
84
85
  protected setInitialState(): void;
85
86
  }
86
87
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "3.0.38",
3
+ "version": "3.0.40",
4
4
  "scripts": {
5
5
  "lint": "eslint . --ext .ts",
6
6
  "build": "npm run build:esm && npm run build:cjs",