incyclist-devices 3.0.37 → 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.
- package/lib/cjs/ble/base/peripheral.js +26 -7
- package/lib/cjs/ble/base/sensor.js +1 -1
- package/lib/cjs/ble/fm/adapter.js +5 -0
- package/lib/cjs/ble/fm/sensor.js +14 -5
- package/lib/cjs/ble/zwift/play/sensor.js +63 -6
- package/lib/esm/ble/base/peripheral.js +26 -7
- package/lib/esm/ble/base/sensor.js +1 -1
- package/lib/esm/ble/fm/adapter.js +5 -0
- package/lib/esm/ble/fm/sensor.js +14 -5
- package/lib/esm/ble/zwift/play/sensor.js +63 -6
- package/lib/types/ble/base/peripheral.d.ts +3 -0
- package/lib/types/ble/fm/adapter.d.ts +1 -0
- package/lib/types/ble/fm/sensor.d.ts +1 -0
- package/lib/types/ble/zwift/play/sensor.d.ts +12 -0
- package/package.json +2 -2
|
@@ -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',
|
|
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',
|
|
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',
|
|
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',
|
|
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 });
|
|
@@ -105,11 +105,11 @@ class TBleSensor extends node_events_1.EventEmitter {
|
|
|
105
105
|
return res;
|
|
106
106
|
}
|
|
107
107
|
async stopSensor() {
|
|
108
|
+
this.stopRequested = true;
|
|
108
109
|
this.onDisconnect();
|
|
109
110
|
this.removeAllListeners();
|
|
110
111
|
if (!this.peripheral)
|
|
111
112
|
return true;
|
|
112
|
-
this.stopRequested = true;
|
|
113
113
|
return await this.peripheral.disconnect();
|
|
114
114
|
}
|
|
115
115
|
isReconnectBusy() {
|
|
@@ -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;
|
package/lib/cjs/ble/fm/sensor.js
CHANGED
|
@@ -26,6 +26,7 @@ class BleFitnessMachineDevice extends sensor_js_1.TBleSensor {
|
|
|
26
26
|
rowerDataTS;
|
|
27
27
|
rowerMaxPower;
|
|
28
28
|
indoorBikeDataTruncationLogged = false;
|
|
29
|
+
hrmParseErrorLogged = false;
|
|
29
30
|
constructor(peripheral, props) {
|
|
30
31
|
super(peripheral, props);
|
|
31
32
|
this.reset();
|
|
@@ -180,16 +181,24 @@ class BleFitnessMachineDevice extends sensor_js_1.TBleSensor {
|
|
|
180
181
|
if (data.length == 0)
|
|
181
182
|
return { ...this.data, raw: '<empty>' };
|
|
182
183
|
try {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
this.data.heartrate = data.readUInt8(1);
|
|
184
|
+
if (data.length === 1) {
|
|
185
|
+
this.data.heartrate = data.readUInt8(0);
|
|
186
186
|
}
|
|
187
187
|
else {
|
|
188
|
-
|
|
188
|
+
const flags = data.readUInt8(0);
|
|
189
|
+
if ((flags & 0x1) === 0) {
|
|
190
|
+
this.data.heartrate = data.readUInt8(1);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
this.data.heartrate = data.readUInt16LE(1);
|
|
194
|
+
}
|
|
189
195
|
}
|
|
190
196
|
}
|
|
191
197
|
catch (err) {
|
|
192
|
-
this.
|
|
198
|
+
if (!this.hrmParseErrorLogged) {
|
|
199
|
+
this.hrmParseErrorLogged = true;
|
|
200
|
+
this.logEvent({ message: 'error', fn: 'parseHrm()', error: err.message, stack: err.stack, raw: `2a37:${data.toString('hex')}` });
|
|
201
|
+
}
|
|
193
202
|
}
|
|
194
203
|
return { ...this.data, raw: `2a37:${data.toString('hex')}` };
|
|
195
204
|
}
|
|
@@ -12,6 +12,7 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
12
12
|
static services = ['0000000119ca465186e5fa29dcdd09d1'];
|
|
13
13
|
static characteristics = [];
|
|
14
14
|
static detectionPriority = 1;
|
|
15
|
+
static MEASUREMENT_REASSEMBLY_TIMEOUT = 250;
|
|
15
16
|
emitter;
|
|
16
17
|
isHubPairConfirmed;
|
|
17
18
|
encrypted;
|
|
@@ -33,6 +34,8 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
33
34
|
subscribePromise;
|
|
34
35
|
prevHubSettings;
|
|
35
36
|
prevcWax10000;
|
|
37
|
+
pendingMeasurement;
|
|
38
|
+
measurementReassemblyErrorLogged = false;
|
|
36
39
|
constructor(peripheral, props) {
|
|
37
40
|
if (props?.isTrainer && peripheral) {
|
|
38
41
|
const sensor = peripheral;
|
|
@@ -148,7 +151,8 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
148
151
|
await this.requestDataUpdate(512);
|
|
149
152
|
}
|
|
150
153
|
catch (err) {
|
|
151
|
-
|
|
154
|
+
const message = this.stopRequested ? 'warning' : 'error';
|
|
155
|
+
this.logger.logEvent({ message, fn: 'setSimulationData', data, error: err.message, stack: err.stack });
|
|
152
156
|
}
|
|
153
157
|
}
|
|
154
158
|
async setIncline(incline) {
|
|
@@ -180,7 +184,8 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
180
184
|
await this.requestDataUpdate(512);
|
|
181
185
|
}
|
|
182
186
|
catch (err) {
|
|
183
|
-
|
|
187
|
+
const message = this.stopRequested ? 'warning' : 'error';
|
|
188
|
+
this.logger.logEvent({ message, fn: 'setGearRatio', gearRatio, error: err.message, stack: err.stack });
|
|
184
189
|
}
|
|
185
190
|
return gearRatio;
|
|
186
191
|
}
|
|
@@ -194,8 +199,31 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
194
199
|
console.log('Invalid click measurement data', data.toString('hex'));
|
|
195
200
|
return false;
|
|
196
201
|
}
|
|
197
|
-
|
|
198
|
-
|
|
202
|
+
let type;
|
|
203
|
+
let message;
|
|
204
|
+
try {
|
|
205
|
+
if (this.pendingMeasurement) {
|
|
206
|
+
type = this.pendingMeasurement.type;
|
|
207
|
+
message = Buffer.concat([this.pendingMeasurement.buffer, data]);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
type = data.readUInt8(0);
|
|
211
|
+
message = data.subarray(1);
|
|
212
|
+
}
|
|
213
|
+
this.dispatchMeasurement(type, message);
|
|
214
|
+
this.clearPendingMeasurement();
|
|
215
|
+
}
|
|
216
|
+
catch (err) {
|
|
217
|
+
if (err instanceof RangeError && err.message === 'premature EOF') {
|
|
218
|
+
this.bufferPendingMeasurement(type, message);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
this.clearPendingMeasurement();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
dispatchMeasurement(type, message) {
|
|
199
227
|
if (type === 0x37) {
|
|
200
228
|
this.onClickButtonMessage(message);
|
|
201
229
|
}
|
|
@@ -220,9 +248,26 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
220
248
|
else if (type === 0x15) {
|
|
221
249
|
}
|
|
222
250
|
else {
|
|
223
|
-
this.logEvent({ message: 'got hub notification', raw:
|
|
251
|
+
this.logEvent({ message: 'got hub notification', raw: message.toString('hex') });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
bufferPendingMeasurement(type, buffer) {
|
|
255
|
+
this.clearPendingMeasurement();
|
|
256
|
+
const timer = setTimeout(() => {
|
|
257
|
+
const pending = this.pendingMeasurement;
|
|
258
|
+
this.pendingMeasurement = undefined;
|
|
259
|
+
if (!this.measurementReassemblyErrorLogged) {
|
|
260
|
+
this.measurementReassemblyErrorLogged = true;
|
|
261
|
+
this.logEvent({ message: 'error', fn: 'onMeasurement', error: 'message reassembly timed out - incomplete message dropped', type: pending?.type, payload: pending?.buffer?.toString('hex') });
|
|
262
|
+
}
|
|
263
|
+
}, BleZwiftPlaySensor.MEASUREMENT_REASSEMBLY_TIMEOUT);
|
|
264
|
+
this.pendingMeasurement = { type, buffer, timer };
|
|
265
|
+
}
|
|
266
|
+
clearPendingMeasurement() {
|
|
267
|
+
if (this.pendingMeasurement) {
|
|
268
|
+
clearTimeout(this.pendingMeasurement.timer);
|
|
269
|
+
this.pendingMeasurement = undefined;
|
|
224
270
|
}
|
|
225
|
-
return true;
|
|
226
271
|
}
|
|
227
272
|
async initHubService(setSimulation = true) {
|
|
228
273
|
if (!this.isHubServiceActive && this.initHubServicePromise !== undefined) {
|
|
@@ -312,6 +357,8 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
312
357
|
this.logEvent({ message: 'trainer response received', unknown: data.unknown, text });
|
|
313
358
|
}
|
|
314
359
|
catch (err) {
|
|
360
|
+
if (err instanceof RangeError && err.message === 'premature EOF')
|
|
361
|
+
throw err;
|
|
315
362
|
let payload = 'unknown';
|
|
316
363
|
try {
|
|
317
364
|
payload = m.toString('hex');
|
|
@@ -328,6 +375,8 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
328
375
|
this.logEvent({ message: 'riding data received', power: data.power, cadence: data.cadence, Speed: data.speedX100 / 100, heartrate: data.hR, unknown1: data.unknown1, unknown2: data.unknown2 });
|
|
329
376
|
}
|
|
330
377
|
catch (err) {
|
|
378
|
+
if (err instanceof RangeError && err.message === 'premature EOF')
|
|
379
|
+
throw err;
|
|
331
380
|
this.logEvent({ message: 'Error', fn: 'onRidingData', error: err.message, stack: err.stack });
|
|
332
381
|
}
|
|
333
382
|
}
|
|
@@ -377,6 +426,8 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
377
426
|
});
|
|
378
427
|
}
|
|
379
428
|
catch (err) {
|
|
429
|
+
if (err instanceof RangeError && err.message === 'premature EOF')
|
|
430
|
+
throw err;
|
|
380
431
|
this.logEvent({ message: 'Error', fn: 'onRideKeyPadStatus', error: err.message, stack: err.stack });
|
|
381
432
|
}
|
|
382
433
|
}
|
|
@@ -440,6 +491,8 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
440
491
|
this.prevClickMessage = messageStr;
|
|
441
492
|
}
|
|
442
493
|
catch (err) {
|
|
494
|
+
if (err instanceof RangeError && err.message === 'premature EOF')
|
|
495
|
+
throw err;
|
|
443
496
|
this.logEvent({ message: 'error', fn: 'onButtonMessage', error: err.message, stack: err.stack });
|
|
444
497
|
}
|
|
445
498
|
}
|
|
@@ -611,6 +664,9 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
611
664
|
privateKeyEncoding: { type: 'pkcs8', format: 'der' }
|
|
612
665
|
});
|
|
613
666
|
}
|
|
667
|
+
resetHubState() {
|
|
668
|
+
this.setInitialState();
|
|
669
|
+
}
|
|
614
670
|
setInitialState() {
|
|
615
671
|
this.isHubPairConfirmed = false;
|
|
616
672
|
this.encrypted = false;
|
|
@@ -621,6 +677,7 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
621
677
|
delete this.initHubServicePromise;
|
|
622
678
|
delete this.prevHubSettings;
|
|
623
679
|
this.rideKeyPadStates = new Map();
|
|
680
|
+
this.clearPendingMeasurement();
|
|
624
681
|
}
|
|
625
682
|
}
|
|
626
683
|
exports.BleZwiftPlaySensor = BleZwiftPlaySensor;
|
|
@@ -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',
|
|
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',
|
|
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',
|
|
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',
|
|
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 });
|
|
@@ -102,11 +102,11 @@ export class TBleSensor extends EventEmitter {
|
|
|
102
102
|
return res;
|
|
103
103
|
}
|
|
104
104
|
async stopSensor() {
|
|
105
|
+
this.stopRequested = true;
|
|
105
106
|
this.onDisconnect();
|
|
106
107
|
this.removeAllListeners();
|
|
107
108
|
if (!this.peripheral)
|
|
108
109
|
return true;
|
|
109
|
-
this.stopRequested = true;
|
|
110
110
|
return await this.peripheral.disconnect();
|
|
111
111
|
}
|
|
112
112
|
isReconnectBusy() {
|
|
@@ -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;
|
package/lib/esm/ble/fm/sensor.js
CHANGED
|
@@ -24,6 +24,7 @@ export default class BleFitnessMachineDevice extends TBleSensor {
|
|
|
24
24
|
rowerDataTS;
|
|
25
25
|
rowerMaxPower;
|
|
26
26
|
indoorBikeDataTruncationLogged = false;
|
|
27
|
+
hrmParseErrorLogged = false;
|
|
27
28
|
constructor(peripheral, props) {
|
|
28
29
|
super(peripheral, props);
|
|
29
30
|
this.reset();
|
|
@@ -178,16 +179,24 @@ export default class BleFitnessMachineDevice extends TBleSensor {
|
|
|
178
179
|
if (data.length == 0)
|
|
179
180
|
return { ...this.data, raw: '<empty>' };
|
|
180
181
|
try {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
this.data.heartrate = data.readUInt8(1);
|
|
182
|
+
if (data.length === 1) {
|
|
183
|
+
this.data.heartrate = data.readUInt8(0);
|
|
184
184
|
}
|
|
185
185
|
else {
|
|
186
|
-
|
|
186
|
+
const flags = data.readUInt8(0);
|
|
187
|
+
if ((flags & 0x1) === 0) {
|
|
188
|
+
this.data.heartrate = data.readUInt8(1);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
this.data.heartrate = data.readUInt16LE(1);
|
|
192
|
+
}
|
|
187
193
|
}
|
|
188
194
|
}
|
|
189
195
|
catch (err) {
|
|
190
|
-
this.
|
|
196
|
+
if (!this.hrmParseErrorLogged) {
|
|
197
|
+
this.hrmParseErrorLogged = true;
|
|
198
|
+
this.logEvent({ message: 'error', fn: 'parseHrm()', error: err.message, stack: err.stack, raw: `2a37:${data.toString('hex')}` });
|
|
199
|
+
}
|
|
191
200
|
}
|
|
192
201
|
return { ...this.data, raw: `2a37:${data.toString('hex')}` };
|
|
193
202
|
}
|
|
@@ -9,6 +9,7 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
9
9
|
static services = ['0000000119ca465186e5fa29dcdd09d1'];
|
|
10
10
|
static characteristics = [];
|
|
11
11
|
static detectionPriority = 1;
|
|
12
|
+
static MEASUREMENT_REASSEMBLY_TIMEOUT = 250;
|
|
12
13
|
emitter;
|
|
13
14
|
isHubPairConfirmed;
|
|
14
15
|
encrypted;
|
|
@@ -30,6 +31,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
30
31
|
subscribePromise;
|
|
31
32
|
prevHubSettings;
|
|
32
33
|
prevcWax10000;
|
|
34
|
+
pendingMeasurement;
|
|
35
|
+
measurementReassemblyErrorLogged = false;
|
|
33
36
|
constructor(peripheral, props) {
|
|
34
37
|
if (props?.isTrainer && peripheral) {
|
|
35
38
|
const sensor = peripheral;
|
|
@@ -145,7 +148,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
145
148
|
await this.requestDataUpdate(512);
|
|
146
149
|
}
|
|
147
150
|
catch (err) {
|
|
148
|
-
|
|
151
|
+
const message = this.stopRequested ? 'warning' : 'error';
|
|
152
|
+
this.logger.logEvent({ message, fn: 'setSimulationData', data, error: err.message, stack: err.stack });
|
|
149
153
|
}
|
|
150
154
|
}
|
|
151
155
|
async setIncline(incline) {
|
|
@@ -177,7 +181,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
177
181
|
await this.requestDataUpdate(512);
|
|
178
182
|
}
|
|
179
183
|
catch (err) {
|
|
180
|
-
|
|
184
|
+
const message = this.stopRequested ? 'warning' : 'error';
|
|
185
|
+
this.logger.logEvent({ message, fn: 'setGearRatio', gearRatio, error: err.message, stack: err.stack });
|
|
181
186
|
}
|
|
182
187
|
return gearRatio;
|
|
183
188
|
}
|
|
@@ -191,8 +196,31 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
191
196
|
console.log('Invalid click measurement data', data.toString('hex'));
|
|
192
197
|
return false;
|
|
193
198
|
}
|
|
194
|
-
|
|
195
|
-
|
|
199
|
+
let type;
|
|
200
|
+
let message;
|
|
201
|
+
try {
|
|
202
|
+
if (this.pendingMeasurement) {
|
|
203
|
+
type = this.pendingMeasurement.type;
|
|
204
|
+
message = Buffer.concat([this.pendingMeasurement.buffer, data]);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
type = data.readUInt8(0);
|
|
208
|
+
message = data.subarray(1);
|
|
209
|
+
}
|
|
210
|
+
this.dispatchMeasurement(type, message);
|
|
211
|
+
this.clearPendingMeasurement();
|
|
212
|
+
}
|
|
213
|
+
catch (err) {
|
|
214
|
+
if (err instanceof RangeError && err.message === 'premature EOF') {
|
|
215
|
+
this.bufferPendingMeasurement(type, message);
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
this.clearPendingMeasurement();
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
dispatchMeasurement(type, message) {
|
|
196
224
|
if (type === 0x37) {
|
|
197
225
|
this.onClickButtonMessage(message);
|
|
198
226
|
}
|
|
@@ -217,9 +245,26 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
217
245
|
else if (type === 0x15) {
|
|
218
246
|
}
|
|
219
247
|
else {
|
|
220
|
-
this.logEvent({ message: 'got hub notification', raw:
|
|
248
|
+
this.logEvent({ message: 'got hub notification', raw: message.toString('hex') });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
bufferPendingMeasurement(type, buffer) {
|
|
252
|
+
this.clearPendingMeasurement();
|
|
253
|
+
const timer = setTimeout(() => {
|
|
254
|
+
const pending = this.pendingMeasurement;
|
|
255
|
+
this.pendingMeasurement = undefined;
|
|
256
|
+
if (!this.measurementReassemblyErrorLogged) {
|
|
257
|
+
this.measurementReassemblyErrorLogged = true;
|
|
258
|
+
this.logEvent({ message: 'error', fn: 'onMeasurement', error: 'message reassembly timed out - incomplete message dropped', type: pending?.type, payload: pending?.buffer?.toString('hex') });
|
|
259
|
+
}
|
|
260
|
+
}, BleZwiftPlaySensor.MEASUREMENT_REASSEMBLY_TIMEOUT);
|
|
261
|
+
this.pendingMeasurement = { type, buffer, timer };
|
|
262
|
+
}
|
|
263
|
+
clearPendingMeasurement() {
|
|
264
|
+
if (this.pendingMeasurement) {
|
|
265
|
+
clearTimeout(this.pendingMeasurement.timer);
|
|
266
|
+
this.pendingMeasurement = undefined;
|
|
221
267
|
}
|
|
222
|
-
return true;
|
|
223
268
|
}
|
|
224
269
|
async initHubService(setSimulation = true) {
|
|
225
270
|
if (!this.isHubServiceActive && this.initHubServicePromise !== undefined) {
|
|
@@ -309,6 +354,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
309
354
|
this.logEvent({ message: 'trainer response received', unknown: data.unknown, text });
|
|
310
355
|
}
|
|
311
356
|
catch (err) {
|
|
357
|
+
if (err instanceof RangeError && err.message === 'premature EOF')
|
|
358
|
+
throw err;
|
|
312
359
|
let payload = 'unknown';
|
|
313
360
|
try {
|
|
314
361
|
payload = m.toString('hex');
|
|
@@ -325,6 +372,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
325
372
|
this.logEvent({ message: 'riding data received', power: data.power, cadence: data.cadence, Speed: data.speedX100 / 100, heartrate: data.hR, unknown1: data.unknown1, unknown2: data.unknown2 });
|
|
326
373
|
}
|
|
327
374
|
catch (err) {
|
|
375
|
+
if (err instanceof RangeError && err.message === 'premature EOF')
|
|
376
|
+
throw err;
|
|
328
377
|
this.logEvent({ message: 'Error', fn: 'onRidingData', error: err.message, stack: err.stack });
|
|
329
378
|
}
|
|
330
379
|
}
|
|
@@ -374,6 +423,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
374
423
|
});
|
|
375
424
|
}
|
|
376
425
|
catch (err) {
|
|
426
|
+
if (err instanceof RangeError && err.message === 'premature EOF')
|
|
427
|
+
throw err;
|
|
377
428
|
this.logEvent({ message: 'Error', fn: 'onRideKeyPadStatus', error: err.message, stack: err.stack });
|
|
378
429
|
}
|
|
379
430
|
}
|
|
@@ -437,6 +488,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
437
488
|
this.prevClickMessage = messageStr;
|
|
438
489
|
}
|
|
439
490
|
catch (err) {
|
|
491
|
+
if (err instanceof RangeError && err.message === 'premature EOF')
|
|
492
|
+
throw err;
|
|
440
493
|
this.logEvent({ message: 'error', fn: 'onButtonMessage', error: err.message, stack: err.stack });
|
|
441
494
|
}
|
|
442
495
|
}
|
|
@@ -608,6 +661,9 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
608
661
|
privateKeyEncoding: { type: 'pkcs8', format: 'der' }
|
|
609
662
|
});
|
|
610
663
|
}
|
|
664
|
+
resetHubState() {
|
|
665
|
+
this.setInitialState();
|
|
666
|
+
}
|
|
611
667
|
setInitialState() {
|
|
612
668
|
this.isHubPairConfirmed = false;
|
|
613
669
|
this.encrypted = false;
|
|
@@ -618,5 +674,6 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
618
674
|
delete this.initHubServicePromise;
|
|
619
675
|
delete this.prevHubSettings;
|
|
620
676
|
this.rideKeyPadStates = new Map();
|
|
677
|
+
this.clearPendingMeasurement();
|
|
621
678
|
}
|
|
622
679
|
}
|
|
@@ -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>;
|
|
@@ -23,6 +23,7 @@ export default class BleFitnessMachineDevice extends TBleSensor {
|
|
|
23
23
|
protected rowerDataTS: number | undefined;
|
|
24
24
|
protected rowerMaxPower: number | undefined;
|
|
25
25
|
protected indoorBikeDataTruncationLogged: boolean;
|
|
26
|
+
protected hrmParseErrorLogged: boolean;
|
|
26
27
|
constructor(peripheral: IBlePeripheral, props?: any);
|
|
27
28
|
get features(): IndoorBikeFeatures;
|
|
28
29
|
reset(): void;
|
|
@@ -14,12 +14,18 @@ type BleZwiftPlaySensorProps = {
|
|
|
14
14
|
isTrainer?: boolean;
|
|
15
15
|
};
|
|
16
16
|
type DeviceType = 'left' | 'right' | 'click' | 'hub' | 'ride-left' | 'ride-right';
|
|
17
|
+
type PendingMeasurement = {
|
|
18
|
+
type: number;
|
|
19
|
+
buffer: Buffer;
|
|
20
|
+
timer: ReturnType<typeof setTimeout>;
|
|
21
|
+
};
|
|
17
22
|
export declare class BleZwiftPlaySensor extends TBleSensor {
|
|
18
23
|
static readonly profile: LegacyProfile;
|
|
19
24
|
static readonly protocol: BleProtocol;
|
|
20
25
|
static readonly services: string[];
|
|
21
26
|
static readonly characteristics: any[];
|
|
22
27
|
static readonly detectionPriority = 1;
|
|
28
|
+
static readonly MEASUREMENT_REASSEMBLY_TIMEOUT = 250;
|
|
23
29
|
protected emitter: EventEmitter;
|
|
24
30
|
protected isHubPairConfirmed: boolean;
|
|
25
31
|
protected encrypted: boolean;
|
|
@@ -41,6 +47,8 @@ export declare class BleZwiftPlaySensor extends TBleSensor {
|
|
|
41
47
|
protected subscribePromise: Promise<boolean>;
|
|
42
48
|
protected prevHubSettings: DeviceSettingsSubContent | undefined;
|
|
43
49
|
protected prevcWax10000: number;
|
|
50
|
+
protected pendingMeasurement: PendingMeasurement | undefined;
|
|
51
|
+
protected measurementReassemblyErrorLogged: boolean;
|
|
44
52
|
constructor(peripheral: IBlePeripheral | TBleSensor, props?: BleZwiftPlaySensorProps);
|
|
45
53
|
reconnectSensor(): Promise<boolean>;
|
|
46
54
|
stopSensor(): Promise<boolean>;
|
|
@@ -53,6 +61,9 @@ export declare class BleZwiftPlaySensor extends TBleSensor {
|
|
|
53
61
|
setGearRatio(gearRatio: number): Promise<number>;
|
|
54
62
|
protected sendPlayCommand(id: number, command: Buffer): Promise<Buffer<ArrayBufferLike>>;
|
|
55
63
|
protected onMeasurement(d: Buffer): boolean;
|
|
64
|
+
protected dispatchMeasurement(type: number, message: Buffer): void;
|
|
65
|
+
protected bufferPendingMeasurement(type: number, buffer: Buffer): void;
|
|
66
|
+
protected clearPendingMeasurement(): void;
|
|
56
67
|
initHubService(setSimulation?: boolean): Promise<boolean>;
|
|
57
68
|
sendHubRequest(request: HubRequest): Promise<Buffer<ArrayBufferLike>>;
|
|
58
69
|
sendHubCommand(command: HubCommand): Promise<Buffer<ArrayBufferLike>>;
|
|
@@ -70,6 +81,7 @@ export declare class BleZwiftPlaySensor extends TBleSensor {
|
|
|
70
81
|
protected getCrypto(): ICryptoBinding;
|
|
71
82
|
protected encryptedSupported(): boolean;
|
|
72
83
|
protected createKeyPair(): any;
|
|
84
|
+
resetHubState(): void;
|
|
73
85
|
protected setInitialState(): void;
|
|
74
86
|
}
|
|
75
87
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "incyclist-devices",
|
|
3
|
-
"version": "3.0.
|
|
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",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"protoc": "^33.6.0",
|
|
42
42
|
"ts-node": "^10.9.2",
|
|
43
43
|
"typescript": "^5.9.3",
|
|
44
|
-
"vitest": "^4.
|
|
44
|
+
"vitest": "^4.1.11"
|
|
45
45
|
},
|
|
46
46
|
"files": [
|
|
47
47
|
"lib/"
|