incyclist-devices 3.0.41 → 3.0.43

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.
@@ -83,6 +83,9 @@ class IncyclistDevice extends node_events_1.EventEmitter {
83
83
  getInterface() {
84
84
  return typeof this.settings.interface === 'string' ? this.settings.interface : this.settings.interface.getName();
85
85
  }
86
+ isReconnecting() {
87
+ return false;
88
+ }
86
89
  async restart(pause) {
87
90
  this.logger.logEvent({ message: 'restarting device', device: this.getName() });
88
91
  const stopped = await this.stop();
@@ -94,6 +94,9 @@ class BleAdapter extends adpater_js_1.default {
94
94
  isConnected() {
95
95
  return this.device?.isConnected();
96
96
  }
97
+ isReconnecting() {
98
+ return this.getSensor()?.isReconnectBusy() ?? false;
99
+ }
97
100
  resetData() {
98
101
  super.resetData();
99
102
  this.dataMsgCount = 0;
@@ -8,11 +8,14 @@ const utils_js_2 = require("../utils.js");
8
8
  class TBleSensor extends node_events_1.EventEmitter {
9
9
  peripheral;
10
10
  static protocol;
11
+ static RECONNECT_ATTEMPT_TIMEOUT = 20000;
11
12
  logger;
12
13
  stopRequested = false;
13
14
  subscribeSuccess = false;
14
15
  reconnectPromise;
15
16
  connectPromise;
17
+ reconnectAttemptTimeout = TBleSensor.RECONNECT_ATTEMPT_TIMEOUT;
18
+ reconnectCount = 0;
16
19
  onDataHandler;
17
20
  logEvent(event, ...args) {
18
21
  try {
@@ -93,16 +96,22 @@ class TBleSensor extends node_events_1.EventEmitter {
93
96
  const selected = this.getRequiredCharacteristics();
94
97
  if (selected === null) {
95
98
  const res = this.peripheral?.subscribeAll ? await this.peripheral.subscribeAll(this.onDataHandler) : false;
96
- this.subscribeSuccess = res;
97
- return res;
99
+ return this.onSubscribeResult(res);
98
100
  }
99
101
  if (selected.length === 0) {
100
102
  this.subscribeSuccess = true;
101
103
  return true;
102
104
  }
103
105
  const res = await this.peripheral.subscribeSelected(selected, this.onDataHandler);
104
- this.subscribeSuccess = res;
105
- return res;
106
+ return this.onSubscribeResult(res);
107
+ }
108
+ onSubscribeResult(res) {
109
+ const success = res && this.isConnected();
110
+ if (res && !success) {
111
+ this.logEvent({ message: 'subscribe failed', reason: 'not connected' });
112
+ }
113
+ this.subscribeSuccess = success;
114
+ return success;
106
115
  }
107
116
  async stopSensor() {
108
117
  this.stopRequested = true;
@@ -124,41 +133,64 @@ class TBleSensor extends node_events_1.EventEmitter {
124
133
  delete this.reconnectPromise;
125
134
  return res;
126
135
  }
136
+ getReconnectCount() {
137
+ return this.reconnectCount;
138
+ }
127
139
  async doReconnectSensor(connectionLost = true) {
128
140
  this.onDisconnect();
141
+ this.reconnectCount++;
129
142
  const { name, address } = this.peripheral?.getInfo() ?? {};
130
143
  this.logEvent({ message: 'reconnect sensor', name, address, connectionLost });
131
- let connected = false;
132
- let subscribed = false;
133
144
  let success = false;
134
145
  if (connectionLost) {
135
146
  this.emit('connctionLost');
136
147
  }
137
148
  await (0, utils_js_1.sleep)(500);
138
- do {
139
- try {
140
- if (!connected) {
141
- connected = await this.startSensor(true);
142
- }
143
- if (connected && !subscribed) {
144
- subscribed = await this.subscribe();
145
- }
146
- }
147
- catch {
148
- }
149
- success = connected && subscribed;
150
- if (!success) {
151
- await (0, utils_js_1.sleep)(1000);
152
- }
153
- if (!this.stopRequested && !success)
154
- this.logEvent({ message: 'reconnect sensor retry', name, address });
155
- } while (!success || this.stopRequested);
149
+ success = await this.reconnectUntilDone(name, address);
156
150
  if (connectionLost && success) {
157
151
  this.emit('connctionRecovered');
158
152
  }
159
153
  this.logEvent({ message: 'reconnect sensor completed', success, stopRequested: this.stopRequested });
160
154
  return success;
161
155
  }
156
+ async reconnectUntilDone(name, address) {
157
+ const success = await this.reconnectAttempt(name, address);
158
+ if (success || this.stopRequested)
159
+ return success;
160
+ await (0, utils_js_1.sleep)(1000);
161
+ if (this.stopRequested)
162
+ return false;
163
+ this.logEvent({ message: 'reconnect sensor retry', name, address });
164
+ return this.reconnectUntilDone(name, address);
165
+ }
166
+ async reconnectAttempt(name, address) {
167
+ try {
168
+ return await (0, utils_js_1.runWithTimeout)(this.connectAndSubscribe(), this.reconnectAttemptTimeout);
169
+ }
170
+ catch (err) {
171
+ if (err?.message === 'Timeout') {
172
+ await this.abortReconnectAttempt(name, address);
173
+ }
174
+ return false;
175
+ }
176
+ }
177
+ async connectAndSubscribe() {
178
+ const connected = await this.startSensor(true);
179
+ if (!connected)
180
+ return false;
181
+ const subscribed = await this.subscribe();
182
+ return subscribed && this.isConnected();
183
+ }
184
+ async abortReconnectAttempt(name, address) {
185
+ this.logEvent({ message: 'reconnect sensor attempt timeout', name, address, timeout: this.reconnectAttemptTimeout });
186
+ delete this.connectPromise;
187
+ this.onDisconnect();
188
+ try {
189
+ await (0, utils_js_1.runWithTimeout)(this.peripheral.disconnect(), this.reconnectAttemptTimeout);
190
+ }
191
+ catch {
192
+ }
193
+ }
162
194
  reset() {
163
195
  throw new Error("Method not implemented.");
164
196
  }
@@ -342,6 +342,10 @@ class BleFmAdapter extends adapter_js_1.default {
342
342
  return;
343
343
  try {
344
344
  const update = this.getCyclingMode().buildUpdate(request);
345
+ if (this.isReconnecting()) {
346
+ this.logEvent({ message: 'send bike update skipped', reason: 'reconnect in progress', update, request });
347
+ return;
348
+ }
345
349
  this.logEvent({ message: 'send bike update requested', profile: this.getProfile(), mode: this.getCyclingMode()?.getName(), update, request });
346
350
  const device = this.getSensor();
347
351
  if (this.hasCapability(index_js_1.IncyclistCapability.Control)) {
@@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BleZwiftPlaySensor = void 0;
4
4
  const zwift_hub_js_1 = require("../../../proto/zwift_hub.js");
5
5
  const sensor_js_1 = require("../../base/sensor.js");
6
- const utils_js_1 = require("../../utils.js");
6
+ const utils_js_1 = require("../../../utils/utils.js");
7
+ const utils_js_2 = require("../../utils.js");
7
8
  const node_events_1 = require("node:events");
8
9
  const index_js_1 = require("../../../bindings/index.js");
9
10
  class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
@@ -36,31 +37,84 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
36
37
  prevcWax10000;
37
38
  pendingMeasurement;
38
39
  measurementReassemblyErrorLogged = false;
40
+ static RECONNECT_PAIR_ATTEMPTS = 3;
41
+ static RECONNECT_PAIR_RETRY_DELAY = 1000;
42
+ owner;
43
+ ownerReconnectCount = 0;
44
+ reconnectAndPairPromise;
39
45
  constructor(peripheral, props) {
40
46
  if (props?.isTrainer && peripheral) {
41
47
  const sensor = peripheral;
42
48
  super(sensor.getPeripheral(), props);
43
49
  this.isFM = true;
50
+ this.owner = sensor;
44
51
  }
45
52
  else if (peripheral &&
46
53
  'startSensor' in peripheral && typeof peripheral.startSensor === 'function' &&
47
54
  'getPeripheral' in peripheral && typeof peripheral.getPeripheral === 'function') {
48
55
  super(peripheral.getPeripheral(), props);
49
56
  this.isFM = true;
57
+ this.owner = peripheral;
50
58
  }
51
59
  else {
52
60
  super(peripheral, props);
53
61
  this.isFM = false;
54
62
  }
63
+ this.ownerReconnectCount = this.owner?.getReconnectCount?.() ?? 0;
55
64
  this.emitter = new node_events_1.EventEmitter();
56
65
  this.setInitialState();
57
66
  }
58
67
  async reconnectSensor() {
59
- this.setInitialState();
60
- let reconnected = await super.reconnectSensor();
61
- this.pair();
68
+ this.reconnectAndPairPromise = this.reconnectAndPairPromise ?? this.reconnectAndPair();
69
+ const promise = this.reconnectAndPairPromise;
70
+ const reconnected = await promise;
71
+ if (this.reconnectAndPairPromise === promise)
72
+ delete this.reconnectAndPairPromise;
62
73
  return reconnected;
63
74
  }
75
+ isReconnectBusy() {
76
+ return super.isReconnectBusy() || this.reconnectAndPairPromise !== undefined || this.isOwnerReconnecting();
77
+ }
78
+ isOwnerReconnecting() {
79
+ return this.owner?.isReconnectBusy?.() ?? false;
80
+ }
81
+ async reconnectAndPair() {
82
+ this.setInitialState();
83
+ const reconnected = await super.reconnectSensor();
84
+ if (!reconnected)
85
+ return false;
86
+ const paired = await this.pairWithRetry(1);
87
+ if (paired === undefined) {
88
+ const { name, address } = this.peripheral?.getInfo() ?? {};
89
+ this.logEvent({ message: 'reconnect sensor pairing failed', name, address, attempts: BleZwiftPlaySensor.RECONNECT_PAIR_ATTEMPTS });
90
+ return false;
91
+ }
92
+ return paired;
93
+ }
94
+ async pairWithRetry(attempt) {
95
+ if (this.stopRequested)
96
+ return false;
97
+ if (await this.pair())
98
+ return true;
99
+ if (attempt >= BleZwiftPlaySensor.RECONNECT_PAIR_ATTEMPTS)
100
+ return undefined;
101
+ await (0, utils_js_1.sleep)(BleZwiftPlaySensor.RECONNECT_PAIR_RETRY_DELAY);
102
+ return this.pairWithRetry(attempt + 1);
103
+ }
104
+ canSendHubCommand(fn) {
105
+ if (!this.owner)
106
+ return true;
107
+ const reconnectCount = this.owner.getReconnectCount?.() ?? 0;
108
+ if (reconnectCount !== this.ownerReconnectCount) {
109
+ this.ownerReconnectCount = reconnectCount;
110
+ this.setInitialState();
111
+ }
112
+ if (this.isOwnerReconnecting()) {
113
+ this.logger.logEvent({ message: 'warning', fn, reason: 'reconnect in progress', skipped: true });
114
+ return false;
115
+ }
116
+ return true;
117
+ }
64
118
  stopSensor() {
65
119
  this.emitter.removeAllListeners();
66
120
  this.setInitialState();
@@ -107,7 +161,7 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
107
161
  return this.deviceType;
108
162
  }
109
163
  onData(characteristic, data, isNotify) {
110
- const uuid = (0, utils_js_1.beautifyUUID)(characteristic).toLowerCase();
164
+ const uuid = (0, utils_js_2.beautifyUUID)(characteristic).toLowerCase();
111
165
  if (uuid === '00000002-19ca-4651-86e5-fa29dcdd09d1') {
112
166
  this.onMeasurement(data);
113
167
  }
@@ -123,6 +177,8 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
123
177
  }
124
178
  async setSimulationData(data) {
125
179
  try {
180
+ if (!this.canSendHubCommand('setSimulationData'))
181
+ return;
126
182
  if (!this.isHubServiceActive) {
127
183
  await this.initHubService(false);
128
184
  }
@@ -151,7 +207,7 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
151
207
  await this.requestDataUpdate(512);
152
208
  }
153
209
  catch (err) {
154
- const message = this.stopRequested ? 'warning' : 'error';
210
+ const message = (this.stopRequested || this.isReconnectBusy()) ? 'warning' : 'error';
155
211
  this.logger.logEvent({ message, fn: 'setSimulationData', data, error: err.message, stack: err.stack });
156
212
  }
157
213
  }
@@ -161,6 +217,8 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
161
217
  }
162
218
  async setGearRatio(gearRatio) {
163
219
  try {
220
+ if (!this.canSendHubCommand('setGearRatio'))
221
+ return;
164
222
  if (!this.isHubServiceActive) {
165
223
  await this.initHubService();
166
224
  }
@@ -184,7 +242,7 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
184
242
  await this.requestDataUpdate(512);
185
243
  }
186
244
  catch (err) {
187
- const message = this.stopRequested ? 'warning' : 'error';
245
+ const message = (this.stopRequested || this.isReconnectBusy()) ? 'warning' : 'error';
188
246
  this.logger.logEvent({ message, fn: 'setGearRatio', gearRatio, error: err.message, stack: err.stack });
189
247
  }
190
248
  return gearRatio;
@@ -270,6 +328,8 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
270
328
  }
271
329
  }
272
330
  async initHubService(setSimulation = true) {
331
+ if (!this.canSendHubCommand('initHubService'))
332
+ return false;
273
333
  if (!this.isHubServiceActive && this.initHubServicePromise !== undefined) {
274
334
  await this.initHubServicePromise;
275
335
  }
@@ -574,7 +634,7 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
574
634
  const paired = await this.pairPromise;
575
635
  this.pairPromise = undefined;
576
636
  if (paired)
577
- return;
637
+ return true;
578
638
  }
579
639
  let manufacturerData;
580
640
  try {
@@ -624,7 +684,7 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
624
684
  message = Buffer.from('RideOn');
625
685
  }
626
686
  this.logEvent({ message: `send rideOn` });
627
- await this.write((0, utils_js_1.fullUUID)('00000003-19ca-4651-86e5-fa29dcdd09d1'), message, { withoutResponse: true });
687
+ await this.write((0, utils_js_2.fullUUID)('00000003-19ca-4651-86e5-fa29dcdd09d1'), message, { withoutResponse: true });
628
688
  this.isHubServicePaired = true;
629
689
  this.logEvent({ message: 'pairing done', deviceType: this.deviceType, encrypted: this.encrypted, manufacturerData: this.getManufacturerData() });
630
690
  return true;
@@ -81,6 +81,9 @@ export default class IncyclistDevice extends EventEmitter {
81
81
  getInterface() {
82
82
  return typeof this.settings.interface === 'string' ? this.settings.interface : this.settings.interface.getName();
83
83
  }
84
+ isReconnecting() {
85
+ return false;
86
+ }
84
87
  async restart(pause) {
85
88
  this.logger.logEvent({ message: 'restarting device', device: this.getName() });
86
89
  const stopped = await this.stop();
@@ -89,6 +89,9 @@ export default class BleAdapter extends IncyclistDevice {
89
89
  isConnected() {
90
90
  return this.device?.isConnected();
91
91
  }
92
+ isReconnecting() {
93
+ return this.getSensor()?.isReconnectBusy() ?? false;
94
+ }
92
95
  resetData() {
93
96
  super.resetData();
94
97
  this.dataMsgCount = 0;
@@ -1,15 +1,18 @@
1
1
  import { EventEmitter } from "node:events";
2
2
  import { EventLogger } from "gd-eventlog";
3
- import { sleep } from "../../utils/utils.js";
3
+ import { runWithTimeout, sleep } from "../../utils/utils.js";
4
4
  import { beautifyUUID } from "../utils.js";
5
5
  export class TBleSensor extends EventEmitter {
6
6
  peripheral;
7
7
  static protocol;
8
+ static RECONNECT_ATTEMPT_TIMEOUT = 20000;
8
9
  logger;
9
10
  stopRequested = false;
10
11
  subscribeSuccess = false;
11
12
  reconnectPromise;
12
13
  connectPromise;
14
+ reconnectAttemptTimeout = TBleSensor.RECONNECT_ATTEMPT_TIMEOUT;
15
+ reconnectCount = 0;
13
16
  onDataHandler;
14
17
  logEvent(event, ...args) {
15
18
  try {
@@ -90,16 +93,22 @@ export class TBleSensor extends EventEmitter {
90
93
  const selected = this.getRequiredCharacteristics();
91
94
  if (selected === null) {
92
95
  const res = this.peripheral?.subscribeAll ? await this.peripheral.subscribeAll(this.onDataHandler) : false;
93
- this.subscribeSuccess = res;
94
- return res;
96
+ return this.onSubscribeResult(res);
95
97
  }
96
98
  if (selected.length === 0) {
97
99
  this.subscribeSuccess = true;
98
100
  return true;
99
101
  }
100
102
  const res = await this.peripheral.subscribeSelected(selected, this.onDataHandler);
101
- this.subscribeSuccess = res;
102
- return res;
103
+ return this.onSubscribeResult(res);
104
+ }
105
+ onSubscribeResult(res) {
106
+ const success = res && this.isConnected();
107
+ if (res && !success) {
108
+ this.logEvent({ message: 'subscribe failed', reason: 'not connected' });
109
+ }
110
+ this.subscribeSuccess = success;
111
+ return success;
103
112
  }
104
113
  async stopSensor() {
105
114
  this.stopRequested = true;
@@ -121,41 +130,64 @@ export class TBleSensor extends EventEmitter {
121
130
  delete this.reconnectPromise;
122
131
  return res;
123
132
  }
133
+ getReconnectCount() {
134
+ return this.reconnectCount;
135
+ }
124
136
  async doReconnectSensor(connectionLost = true) {
125
137
  this.onDisconnect();
138
+ this.reconnectCount++;
126
139
  const { name, address } = this.peripheral?.getInfo() ?? {};
127
140
  this.logEvent({ message: 'reconnect sensor', name, address, connectionLost });
128
- let connected = false;
129
- let subscribed = false;
130
141
  let success = false;
131
142
  if (connectionLost) {
132
143
  this.emit('connctionLost');
133
144
  }
134
145
  await sleep(500);
135
- do {
136
- try {
137
- if (!connected) {
138
- connected = await this.startSensor(true);
139
- }
140
- if (connected && !subscribed) {
141
- subscribed = await this.subscribe();
142
- }
143
- }
144
- catch {
145
- }
146
- success = connected && subscribed;
147
- if (!success) {
148
- await sleep(1000);
149
- }
150
- if (!this.stopRequested && !success)
151
- this.logEvent({ message: 'reconnect sensor retry', name, address });
152
- } while (!success || this.stopRequested);
146
+ success = await this.reconnectUntilDone(name, address);
153
147
  if (connectionLost && success) {
154
148
  this.emit('connctionRecovered');
155
149
  }
156
150
  this.logEvent({ message: 'reconnect sensor completed', success, stopRequested: this.stopRequested });
157
151
  return success;
158
152
  }
153
+ async reconnectUntilDone(name, address) {
154
+ const success = await this.reconnectAttempt(name, address);
155
+ if (success || this.stopRequested)
156
+ return success;
157
+ await sleep(1000);
158
+ if (this.stopRequested)
159
+ return false;
160
+ this.logEvent({ message: 'reconnect sensor retry', name, address });
161
+ return this.reconnectUntilDone(name, address);
162
+ }
163
+ async reconnectAttempt(name, address) {
164
+ try {
165
+ return await runWithTimeout(this.connectAndSubscribe(), this.reconnectAttemptTimeout);
166
+ }
167
+ catch (err) {
168
+ if (err?.message === 'Timeout') {
169
+ await this.abortReconnectAttempt(name, address);
170
+ }
171
+ return false;
172
+ }
173
+ }
174
+ async connectAndSubscribe() {
175
+ const connected = await this.startSensor(true);
176
+ if (!connected)
177
+ return false;
178
+ const subscribed = await this.subscribe();
179
+ return subscribed && this.isConnected();
180
+ }
181
+ async abortReconnectAttempt(name, address) {
182
+ this.logEvent({ message: 'reconnect sensor attempt timeout', name, address, timeout: this.reconnectAttemptTimeout });
183
+ delete this.connectPromise;
184
+ this.onDisconnect();
185
+ try {
186
+ await runWithTimeout(this.peripheral.disconnect(), this.reconnectAttemptTimeout);
187
+ }
188
+ catch {
189
+ }
190
+ }
159
191
  reset() {
160
192
  throw new Error("Method not implemented.");
161
193
  }
@@ -337,6 +337,10 @@ export default class BleFmAdapter extends BleAdapter {
337
337
  return;
338
338
  try {
339
339
  const update = this.getCyclingMode().buildUpdate(request);
340
+ if (this.isReconnecting()) {
341
+ this.logEvent({ message: 'send bike update skipped', reason: 'reconnect in progress', update, request });
342
+ return;
343
+ }
340
344
  this.logEvent({ message: 'send bike update requested', profile: this.getProfile(), mode: this.getCyclingMode()?.getName(), update, request });
341
345
  const device = this.getSensor();
342
346
  if (this.hasCapability(IncyclistCapability.Control)) {
@@ -1,5 +1,6 @@
1
1
  import { ClickKeyPadStatus, DeviceDataEnvelope, DeviceInformationContent, DeviceSettings, HubCommand, HubRequest, HubRidingData, Idle, PlayButtonStatus, RideKeyPadStatus, TrainerResponse } from "../../../proto/zwift_hub.js";
2
2
  import { TBleSensor } from "../../base/sensor.js";
3
+ import { sleep } from "../../../utils/utils.js";
3
4
  import { beautifyUUID, fullUUID } from "../../utils.js";
4
5
  import { EventEmitter } from "node:events";
5
6
  import { BindingsFactory } from "../../../bindings/index.js";
@@ -33,31 +34,84 @@ export class BleZwiftPlaySensor extends TBleSensor {
33
34
  prevcWax10000;
34
35
  pendingMeasurement;
35
36
  measurementReassemblyErrorLogged = false;
37
+ static RECONNECT_PAIR_ATTEMPTS = 3;
38
+ static RECONNECT_PAIR_RETRY_DELAY = 1000;
39
+ owner;
40
+ ownerReconnectCount = 0;
41
+ reconnectAndPairPromise;
36
42
  constructor(peripheral, props) {
37
43
  if (props?.isTrainer && peripheral) {
38
44
  const sensor = peripheral;
39
45
  super(sensor.getPeripheral(), props);
40
46
  this.isFM = true;
47
+ this.owner = sensor;
41
48
  }
42
49
  else if (peripheral &&
43
50
  'startSensor' in peripheral && typeof peripheral.startSensor === 'function' &&
44
51
  'getPeripheral' in peripheral && typeof peripheral.getPeripheral === 'function') {
45
52
  super(peripheral.getPeripheral(), props);
46
53
  this.isFM = true;
54
+ this.owner = peripheral;
47
55
  }
48
56
  else {
49
57
  super(peripheral, props);
50
58
  this.isFM = false;
51
59
  }
60
+ this.ownerReconnectCount = this.owner?.getReconnectCount?.() ?? 0;
52
61
  this.emitter = new EventEmitter();
53
62
  this.setInitialState();
54
63
  }
55
64
  async reconnectSensor() {
56
- this.setInitialState();
57
- let reconnected = await super.reconnectSensor();
58
- this.pair();
65
+ this.reconnectAndPairPromise = this.reconnectAndPairPromise ?? this.reconnectAndPair();
66
+ const promise = this.reconnectAndPairPromise;
67
+ const reconnected = await promise;
68
+ if (this.reconnectAndPairPromise === promise)
69
+ delete this.reconnectAndPairPromise;
59
70
  return reconnected;
60
71
  }
72
+ isReconnectBusy() {
73
+ return super.isReconnectBusy() || this.reconnectAndPairPromise !== undefined || this.isOwnerReconnecting();
74
+ }
75
+ isOwnerReconnecting() {
76
+ return this.owner?.isReconnectBusy?.() ?? false;
77
+ }
78
+ async reconnectAndPair() {
79
+ this.setInitialState();
80
+ const reconnected = await super.reconnectSensor();
81
+ if (!reconnected)
82
+ return false;
83
+ const paired = await this.pairWithRetry(1);
84
+ if (paired === undefined) {
85
+ const { name, address } = this.peripheral?.getInfo() ?? {};
86
+ this.logEvent({ message: 'reconnect sensor pairing failed', name, address, attempts: BleZwiftPlaySensor.RECONNECT_PAIR_ATTEMPTS });
87
+ return false;
88
+ }
89
+ return paired;
90
+ }
91
+ async pairWithRetry(attempt) {
92
+ if (this.stopRequested)
93
+ return false;
94
+ if (await this.pair())
95
+ return true;
96
+ if (attempt >= BleZwiftPlaySensor.RECONNECT_PAIR_ATTEMPTS)
97
+ return undefined;
98
+ await sleep(BleZwiftPlaySensor.RECONNECT_PAIR_RETRY_DELAY);
99
+ return this.pairWithRetry(attempt + 1);
100
+ }
101
+ canSendHubCommand(fn) {
102
+ if (!this.owner)
103
+ return true;
104
+ const reconnectCount = this.owner.getReconnectCount?.() ?? 0;
105
+ if (reconnectCount !== this.ownerReconnectCount) {
106
+ this.ownerReconnectCount = reconnectCount;
107
+ this.setInitialState();
108
+ }
109
+ if (this.isOwnerReconnecting()) {
110
+ this.logger.logEvent({ message: 'warning', fn, reason: 'reconnect in progress', skipped: true });
111
+ return false;
112
+ }
113
+ return true;
114
+ }
61
115
  stopSensor() {
62
116
  this.emitter.removeAllListeners();
63
117
  this.setInitialState();
@@ -120,6 +174,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
120
174
  }
121
175
  async setSimulationData(data) {
122
176
  try {
177
+ if (!this.canSendHubCommand('setSimulationData'))
178
+ return;
123
179
  if (!this.isHubServiceActive) {
124
180
  await this.initHubService(false);
125
181
  }
@@ -148,7 +204,7 @@ export class BleZwiftPlaySensor extends TBleSensor {
148
204
  await this.requestDataUpdate(512);
149
205
  }
150
206
  catch (err) {
151
- const message = this.stopRequested ? 'warning' : 'error';
207
+ const message = (this.stopRequested || this.isReconnectBusy()) ? 'warning' : 'error';
152
208
  this.logger.logEvent({ message, fn: 'setSimulationData', data, error: err.message, stack: err.stack });
153
209
  }
154
210
  }
@@ -158,6 +214,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
158
214
  }
159
215
  async setGearRatio(gearRatio) {
160
216
  try {
217
+ if (!this.canSendHubCommand('setGearRatio'))
218
+ return;
161
219
  if (!this.isHubServiceActive) {
162
220
  await this.initHubService();
163
221
  }
@@ -181,7 +239,7 @@ export class BleZwiftPlaySensor extends TBleSensor {
181
239
  await this.requestDataUpdate(512);
182
240
  }
183
241
  catch (err) {
184
- const message = this.stopRequested ? 'warning' : 'error';
242
+ const message = (this.stopRequested || this.isReconnectBusy()) ? 'warning' : 'error';
185
243
  this.logger.logEvent({ message, fn: 'setGearRatio', gearRatio, error: err.message, stack: err.stack });
186
244
  }
187
245
  return gearRatio;
@@ -267,6 +325,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
267
325
  }
268
326
  }
269
327
  async initHubService(setSimulation = true) {
328
+ if (!this.canSendHubCommand('initHubService'))
329
+ return false;
270
330
  if (!this.isHubServiceActive && this.initHubServicePromise !== undefined) {
271
331
  await this.initHubServicePromise;
272
332
  }
@@ -571,7 +631,7 @@ export class BleZwiftPlaySensor extends TBleSensor {
571
631
  const paired = await this.pairPromise;
572
632
  this.pairPromise = undefined;
573
633
  if (paired)
574
- return;
634
+ return true;
575
635
  }
576
636
  let manufacturerData;
577
637
  try {
@@ -37,6 +37,7 @@ export default class IncyclistDevice<P extends DeviceProperties> extends EventEm
37
37
  isSame(adapter: IAdapter): boolean;
38
38
  isEqual(settings: DeviceSettings): boolean;
39
39
  getInterface(): string;
40
+ isReconnecting(): boolean;
40
41
  restart(pause?: number): Promise<boolean>;
41
42
  check(): Promise<boolean>;
42
43
  start(props?: DeviceProperties): Promise<boolean>;
@@ -29,6 +29,7 @@ export default class BleAdapter<TDeviceData extends BleDeviceData, TDevice exten
29
29
  isEqual(settings: BleDeviceSettings): boolean;
30
30
  isSame(adapter: IAdapter): boolean;
31
31
  isConnected(): boolean;
32
+ isReconnecting(): boolean;
32
33
  resetData(): void;
33
34
  getInterface(): string;
34
35
  getProfile(): LegacyProfile;
@@ -5,11 +5,14 @@ import { BleWriteProps, IBlePeripheral, BleProtocol, IBleSensor } from "../types
5
5
  export declare class TBleSensor extends EventEmitter implements IBleSensor {
6
6
  protected peripheral: IBlePeripheral;
7
7
  static readonly protocol: BleProtocol;
8
+ static readonly RECONNECT_ATTEMPT_TIMEOUT = 20000;
8
9
  protected logger: EventLogger;
9
10
  protected stopRequested: boolean;
10
11
  protected subscribeSuccess: boolean;
11
12
  protected reconnectPromise: Promise<boolean> | undefined;
12
13
  protected connectPromise: Promise<boolean> | undefined;
14
+ protected reconnectAttemptTimeout: number;
15
+ protected reconnectCount: number;
13
16
  protected onDataHandler: any;
14
17
  logEvent(event: any, ...args: any): void;
15
18
  constructor(peripheral: IBlePeripheral, props?: {
@@ -27,10 +30,16 @@ export declare class TBleSensor extends EventEmitter implements IBleSensor {
27
30
  startSensor(reconnect?: boolean): Promise<boolean>;
28
31
  protected getRequiredCharacteristics(): Array<string> | null;
29
32
  subscribe(): Promise<boolean>;
33
+ protected onSubscribeResult(res: boolean): boolean;
30
34
  stopSensor(): Promise<boolean>;
31
35
  isReconnectBusy(): boolean;
32
36
  reconnectSensor(connectionLost?: boolean): Promise<boolean>;
37
+ getReconnectCount(): number;
33
38
  doReconnectSensor(connectionLost?: boolean): Promise<boolean>;
39
+ protected reconnectUntilDone(name: string, address: string): Promise<boolean>;
40
+ protected reconnectAttempt(name: string, address: string): Promise<boolean>;
41
+ protected connectAndSubscribe(): Promise<boolean>;
42
+ protected abortReconnectAttempt(name: string, address: string): Promise<void>;
34
43
  reset(): void;
35
44
  isConnected(): boolean;
36
45
  isReady(): boolean;
@@ -49,8 +49,18 @@ export declare class BleZwiftPlaySensor extends TBleSensor {
49
49
  protected prevcWax10000: number;
50
50
  protected pendingMeasurement: PendingMeasurement | undefined;
51
51
  protected measurementReassemblyErrorLogged: boolean;
52
+ static readonly RECONNECT_PAIR_ATTEMPTS = 3;
53
+ static readonly RECONNECT_PAIR_RETRY_DELAY = 1000;
54
+ protected owner: TBleSensor | undefined;
55
+ protected ownerReconnectCount: number;
56
+ protected reconnectAndPairPromise: Promise<boolean> | undefined;
52
57
  constructor(peripheral: IBlePeripheral | TBleSensor, props?: BleZwiftPlaySensorProps);
53
58
  reconnectSensor(): Promise<boolean>;
59
+ isReconnectBusy(): boolean;
60
+ protected isOwnerReconnecting(): boolean;
61
+ protected reconnectAndPair(): Promise<boolean>;
62
+ protected pairWithRetry(attempt: number): Promise<boolean | undefined>;
63
+ protected canSendHubCommand(fn: string): boolean;
54
64
  stopSensor(): Promise<boolean>;
55
65
  protected getRequiredCharacteristics(): Array<string>;
56
66
  getDeviceType(): DeviceType;
@@ -57,4 +57,5 @@ export interface IAdapter extends EventEmitter, ITrainer, ISensor {
57
57
  onScanStop(): void;
58
58
  createMode(ModeClass: typeof CyclingMode): ICyclingMode;
59
59
  isLogPaused(): boolean;
60
+ isReconnecting?(): boolean;
60
61
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "3.0.41",
3
+ "version": "3.0.43",
4
4
  "scripts": {
5
5
  "lint": "eslint . --ext .ts",
6
6
  "build": "npm run build:esm && npm run build:cjs",
@@ -20,7 +20,7 @@
20
20
  "@serialport/parser-delimiter": "^13.0.0",
21
21
  "@serialport/parser-readline": "^13.0.0",
22
22
  "@serialport/stream": "^13.0.0",
23
- "incyclist-ant-plus": "^0.3.6",
23
+ "incyclist-ant-plus": "^0.3.7",
24
24
  "win32filetime": "^1.0.2"
25
25
  },
26
26
  "peerDependencies": {
@@ -30,18 +30,18 @@
30
30
  "@protobuf-ts/plugin": "^2.11.1",
31
31
  "@serialport/binding-mock": "^10.2.2",
32
32
  "@serialport/bindings-cpp": "^13.0.1",
33
- "@stoprocent/noble": "^2.4.0",
33
+ "@stoprocent/noble": "^2.8.0",
34
34
  "@types/jest": "^30.0.0",
35
- "@types/node": "^25.6.0",
36
- "@typescript-eslint/eslint-plugin": "^8.59.0",
37
- "@typescript-eslint/parser": "^8.59.0",
38
- "@vitest/coverage-v8": "^4.0.18",
39
- "bonjour-service": "^1.3.0",
40
- "eslint": "^9.39.4",
35
+ "@types/node": "^26.6.5",
36
+ "@typescript-eslint/eslint-plugin": "^8.71.2",
37
+ "@typescript-eslint/parser": "^8.71.2",
38
+ "@vitest/coverage-v8": "^5.0.4",
39
+ "bonjour-service": "^1.4.4",
40
+ "eslint": "^9.39.5",
41
41
  "protoc": "^33.6.0",
42
42
  "ts-node": "^10.9.2",
43
43
  "typescript": "^5.9.3",
44
- "vitest": "^4.1.11"
44
+ "vitest": "^5.0.4"
45
45
  },
46
46
  "files": [
47
47
  "lib/"