incyclist-devices 3.0.46 → 3.0.48
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/base/adpater.js +4 -1
- package/lib/cjs/ble/base/adapter.js +6 -1
- package/lib/cjs/ble/base/interface.js +59 -17
- package/lib/cjs/ble/base/peripheral.js +22 -5
- package/lib/cjs/ble/fm/adapter.js +59 -3
- package/lib/cjs/ble/fm/sensor.js +6 -1
- package/lib/cjs/modes/fm-resistance.js +122 -4
- package/lib/esm/base/adpater.js +4 -1
- package/lib/esm/ble/base/adapter.js +6 -1
- package/lib/esm/ble/base/interface.js +59 -17
- package/lib/esm/ble/base/peripheral.js +23 -6
- package/lib/esm/ble/fm/adapter.js +59 -3
- package/lib/esm/ble/fm/sensor.js +6 -1
- package/lib/esm/modes/fm-resistance.js +122 -4
- package/lib/types/base/adpater.d.ts +1 -0
- package/lib/types/ble/base/interface.d.ts +9 -3
- package/lib/types/ble/base/peripheral.d.ts +1 -0
- package/lib/types/ble/fm/adapter.d.ts +6 -0
- package/lib/types/modes/fm-resistance.d.ts +28 -17
- package/lib/types/types/adapter.d.ts +11 -0
- package/lib/types/types/interface.d.ts +2 -2
- package/package.json +1 -1
package/lib/cjs/base/adpater.js
CHANGED
|
@@ -167,7 +167,7 @@ class IncyclistDevice extends node_events_1.EventEmitter {
|
|
|
167
167
|
}
|
|
168
168
|
createOrGetMode(mode, useCurrent = true) {
|
|
169
169
|
if (typeof mode === 'string') {
|
|
170
|
-
if (useCurrent && this.cyclingMode && mode === this.cyclingMode?.getName()) {
|
|
170
|
+
if (useCurrent && this.cyclingMode && mode === this.cyclingMode?.getName() && this.isModeSupported(mode)) {
|
|
171
171
|
return this.cyclingMode;
|
|
172
172
|
}
|
|
173
173
|
const supported = this.getSupportedCyclingModes();
|
|
@@ -185,6 +185,9 @@ class IncyclistDevice extends node_events_1.EventEmitter {
|
|
|
185
185
|
}
|
|
186
186
|
return mode;
|
|
187
187
|
}
|
|
188
|
+
isModeSupported(_modeName) {
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
188
191
|
getSupportedCyclingModes() {
|
|
189
192
|
if (!this.isControllable())
|
|
190
193
|
return [];
|
|
@@ -407,7 +407,12 @@ class BleAdapter extends adpater_js_1.default {
|
|
|
407
407
|
}
|
|
408
408
|
return connected;
|
|
409
409
|
}
|
|
410
|
-
|
|
410
|
+
try {
|
|
411
|
+
await sensor.getPeripheral().disconnect();
|
|
412
|
+
}
|
|
413
|
+
catch (err) {
|
|
414
|
+
this.logEvent({ message: 'restart: disconnect failed', device: this.getName(), interface: this.getInterface(), reason: err?.message });
|
|
415
|
+
}
|
|
411
416
|
const success = await super.restart(pause);
|
|
412
417
|
return success;
|
|
413
418
|
}
|
|
@@ -11,6 +11,8 @@ const index_js_1 = require("../factories/index.js");
|
|
|
11
11
|
const consts_js_1 = require("../consts.js");
|
|
12
12
|
const consts_js_2 = require("../bkool/consts.js");
|
|
13
13
|
const BLE_EXPIRATION_TIMEOUT = 10 * 1000 * 60;
|
|
14
|
+
const DEFAULT_PAUSE_REASON = 'app-background';
|
|
15
|
+
const TEMPORARY_SCAN_TIMEOUT = 30 * 1000;
|
|
14
16
|
const BLE_DEFAULT_CONNECT_TIMEOUT = 30 * 1000;
|
|
15
17
|
let instanceCount = 0;
|
|
16
18
|
class BleInterface extends node_events_1.EventEmitter {
|
|
@@ -29,6 +31,8 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
29
31
|
expectedServices = ['180d', '1818', '1826', '6e40fec1'];
|
|
30
32
|
matching = [];
|
|
31
33
|
backgroundPaused = false;
|
|
34
|
+
pauseReasons = new Set();
|
|
35
|
+
pausedSince;
|
|
32
36
|
connectTask;
|
|
33
37
|
disconnectTask;
|
|
34
38
|
scanTask;
|
|
@@ -247,18 +251,30 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
247
251
|
delete this.scanTask;
|
|
248
252
|
return this.buildDeviceSettings(this.matching);
|
|
249
253
|
}
|
|
250
|
-
async pauseBackgroundActivity() {
|
|
251
|
-
if (this.
|
|
254
|
+
async pauseBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
255
|
+
if (this.pauseReasons.has(reason))
|
|
252
256
|
return;
|
|
257
|
+
this.pauseReasons.add(reason);
|
|
258
|
+
const alreadyPaused = this.backgroundPaused;
|
|
253
259
|
this.backgroundPaused = true;
|
|
254
|
-
this.logEvent({ message: 'pausing background activity' });
|
|
260
|
+
this.logEvent({ message: 'pausing background activity', reason, reasons: Array.from(this.pauseReasons).join(','), interface: 'ble' });
|
|
261
|
+
if (alreadyPaused)
|
|
262
|
+
return;
|
|
263
|
+
this.pausedSince = Date.now();
|
|
255
264
|
await this.stopPeripheralScan();
|
|
256
265
|
}
|
|
257
|
-
async resumeBackgroundActivity() {
|
|
258
|
-
if (!this.
|
|
266
|
+
async resumeBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
267
|
+
if (!this.pauseReasons.delete(reason))
|
|
259
268
|
return;
|
|
269
|
+
const reasons = Array.from(this.pauseReasons).join(',');
|
|
270
|
+
if (this.pauseReasons.size > 0) {
|
|
271
|
+
this.logEvent({ message: 'resuming background activity skipped', reason, reasons, interface: 'ble' });
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
260
274
|
this.backgroundPaused = false;
|
|
261
|
-
this.
|
|
275
|
+
const pausedMs = this.pausedSince ? Date.now() - this.pausedSince : 0;
|
|
276
|
+
delete this.pausedSince;
|
|
277
|
+
this.logEvent({ message: 'resuming background activity', reason, reasons, pausedMs, interface: 'ble' });
|
|
262
278
|
await this.startPeripheralScan();
|
|
263
279
|
}
|
|
264
280
|
pauseLogging() {
|
|
@@ -293,22 +309,40 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
293
309
|
const peripheral = this.createPeripheralFromSettings(settings);
|
|
294
310
|
if (peripheral)
|
|
295
311
|
return Promise.resolve(peripheral);
|
|
296
|
-
return new Promise((done) => {
|
|
312
|
+
return new Promise((done, reject) => {
|
|
313
|
+
const temporary = this.backgroundPaused;
|
|
297
314
|
const wasDiscovering = this.isDiscovering();
|
|
298
|
-
|
|
299
|
-
|
|
315
|
+
const tsStart = Date.now();
|
|
316
|
+
let timer;
|
|
317
|
+
const finish = () => {
|
|
318
|
+
this.off('device', onDevice);
|
|
319
|
+
if (timer)
|
|
320
|
+
clearTimeout(timer);
|
|
321
|
+
if (temporary || !wasDiscovering)
|
|
322
|
+
this.stopPeripheralScan().catch(() => { });
|
|
323
|
+
};
|
|
300
324
|
const onDevice = (device) => {
|
|
301
325
|
if (device.address === settings.address) {
|
|
302
326
|
const peripheral = this.createPeripheralFromSettings(device);
|
|
303
327
|
if (peripheral) {
|
|
304
|
-
|
|
305
|
-
if (
|
|
306
|
-
this.
|
|
328
|
+
finish();
|
|
329
|
+
if (temporary)
|
|
330
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: true });
|
|
307
331
|
done(peripheral);
|
|
308
332
|
}
|
|
309
333
|
}
|
|
310
334
|
};
|
|
311
335
|
this.on('device', onDevice);
|
|
336
|
+
if (temporary) {
|
|
337
|
+
timer = setTimeout(() => {
|
|
338
|
+
finish();
|
|
339
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: false });
|
|
340
|
+
reject(new Error('peripheral not found'));
|
|
341
|
+
}, TEMPORARY_SCAN_TIMEOUT);
|
|
342
|
+
this.startPeripheralScan(false, true).catch(() => { });
|
|
343
|
+
}
|
|
344
|
+
else if (!wasDiscovering)
|
|
345
|
+
this.startPeripheralScan();
|
|
312
346
|
});
|
|
313
347
|
}
|
|
314
348
|
createDeviceSetting(service) {
|
|
@@ -336,8 +370,8 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
336
370
|
await this.disconnect();
|
|
337
371
|
await this.connectInternal(true);
|
|
338
372
|
}
|
|
339
|
-
async startPeripheralScan(retry = false) {
|
|
340
|
-
if (this.backgroundPaused)
|
|
373
|
+
async startPeripheralScan(retry = false, force = false) {
|
|
374
|
+
if (this.backgroundPaused && !force)
|
|
341
375
|
return;
|
|
342
376
|
if (!this.isConnected() || this.isDiscovering()) {
|
|
343
377
|
return;
|
|
@@ -357,7 +391,7 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
357
391
|
this.logError(err, 'discover');
|
|
358
392
|
}
|
|
359
393
|
if (this.discoverTask.getState().result !== 'stopped') {
|
|
360
|
-
this.startPeripheralScan(true);
|
|
394
|
+
this.startPeripheralScan(true, force).catch(err => this.logError(err, 'discover'));
|
|
361
395
|
}
|
|
362
396
|
}
|
|
363
397
|
async stopPeripheralScan() {
|
|
@@ -644,10 +678,18 @@ class BleInterface extends node_events_1.EventEmitter {
|
|
|
644
678
|
return supported.length > 0;
|
|
645
679
|
}
|
|
646
680
|
find(service) {
|
|
647
|
-
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address &&
|
|
681
|
+
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address && this.isAnnouncementValid(a));
|
|
648
682
|
}
|
|
649
683
|
getAll() {
|
|
650
|
-
return this.services.filter(a =>
|
|
684
|
+
return this.services.filter(a => this.isAnnouncementValid(a));
|
|
685
|
+
}
|
|
686
|
+
isAnnouncementValid(a) {
|
|
687
|
+
if (a.ts > Date.now() - BLE_EXPIRATION_TIMEOUT)
|
|
688
|
+
return true;
|
|
689
|
+
if (!this.backgroundPaused)
|
|
690
|
+
return false;
|
|
691
|
+
const id = a.service?.peripheral?.id;
|
|
692
|
+
return id !== undefined && this.connectedPeripherals.some(p => p.id === id);
|
|
651
693
|
}
|
|
652
694
|
setDebug(enabled) {
|
|
653
695
|
this.debug = enabled;
|
|
@@ -7,6 +7,7 @@ const interface_js_1 = require("./interface.js");
|
|
|
7
7
|
const SERVICE_COMPLETENESS_WHITELIST = ['MRK-R15'];
|
|
8
8
|
const POST_CONNECT_DISCOVERY_ATTEMPTS = 4;
|
|
9
9
|
const POST_CONNECT_DISCOVERY_BACKOFF_MS = 1000;
|
|
10
|
+
const BLE_DISCONNECT_STEP_TIMEOUT = 3000;
|
|
10
11
|
class BlePeripheral {
|
|
11
12
|
announcement;
|
|
12
13
|
connected = false;
|
|
@@ -88,8 +89,21 @@ class BlePeripheral {
|
|
|
88
89
|
}
|
|
89
90
|
async disconnect(connectionLost = false) {
|
|
90
91
|
this.disconnecting = true;
|
|
92
|
+
try {
|
|
93
|
+
return await this.doDisconnect(connectionLost);
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
this.disconnecting = false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async doDisconnect(connectionLost) {
|
|
91
100
|
if (this.isConnected() || connectionLost) {
|
|
92
|
-
|
|
101
|
+
try {
|
|
102
|
+
await (0, utils_js_1.runWithTimeout)(this.unsubscribeAll(connectionLost), BLE_DISCONNECT_STEP_TIMEOUT);
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
this.logEvent({ message: 'peripheral unsubscribe timeout', timeoutMs: BLE_DISCONNECT_STEP_TIMEOUT, reason: err?.message });
|
|
106
|
+
}
|
|
93
107
|
}
|
|
94
108
|
Object.keys(this.characteristics).forEach(uuid => {
|
|
95
109
|
const c = this.characteristics[uuid];
|
|
@@ -106,8 +120,12 @@ class BlePeripheral {
|
|
|
106
120
|
return new Promise((done) => { this.getPeripheral().disconnect(() => { done(); }); });
|
|
107
121
|
};
|
|
108
122
|
}
|
|
109
|
-
|
|
110
|
-
.
|
|
123
|
+
try {
|
|
124
|
+
await (0, utils_js_1.runWithTimeout)(this.getPeripheral().disconnectAsync(), BLE_DISCONNECT_STEP_TIMEOUT);
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
this.logEvent({ message: 'peripheral disconnect timeout', address: peripheral.address, timeoutMs: BLE_DISCONNECT_STEP_TIMEOUT, reason: err?.message });
|
|
128
|
+
}
|
|
111
129
|
}
|
|
112
130
|
peripheral.removeAllListeners();
|
|
113
131
|
this.ble.unregisterConnected(peripheral.id);
|
|
@@ -116,8 +134,7 @@ class BlePeripheral {
|
|
|
116
134
|
delete this.onDisconnectHandler;
|
|
117
135
|
}
|
|
118
136
|
this.connected = false;
|
|
119
|
-
this.
|
|
120
|
-
this.logEvent({ message: 'peripheral disconnect completed', address: peripheral.address });
|
|
137
|
+
this.logEvent({ message: 'peripheral disconnect completed', address: peripheral?.address });
|
|
121
138
|
return !this.connected;
|
|
122
139
|
}
|
|
123
140
|
isConnected() {
|
|
@@ -26,6 +26,7 @@ class BleFmAdapter extends adapter_js_1.default {
|
|
|
26
26
|
promiseSendUpdate;
|
|
27
27
|
zwiftPlay;
|
|
28
28
|
isHubInitialized = false;
|
|
29
|
+
setSlopeSkippedLogged = false;
|
|
29
30
|
constructor(settings, props) {
|
|
30
31
|
super(settings, props);
|
|
31
32
|
this.logger = new gd_eventlog_1.EventLogger('BLE-FM');
|
|
@@ -176,6 +177,7 @@ class BleFmAdapter extends adapter_js_1.default {
|
|
|
176
177
|
return;
|
|
177
178
|
await this.establishControl();
|
|
178
179
|
await this.attemptStartRequest();
|
|
180
|
+
this.getResistanceMode()?.resetSession();
|
|
179
181
|
await this.sendInitialRequest();
|
|
180
182
|
}
|
|
181
183
|
async initControlBestEffort(_startProps) {
|
|
@@ -289,6 +291,20 @@ class BleFmAdapter extends adapter_js_1.default {
|
|
|
289
291
|
mode.resetConfig();
|
|
290
292
|
mode.getConfig();
|
|
291
293
|
});
|
|
294
|
+
const autoDefault = this.getSensor()?.features?.setSlope === false;
|
|
295
|
+
const resistanceModes = [...Object.values(this.modes), this.cyclingMode];
|
|
296
|
+
resistanceModes.forEach(mode => {
|
|
297
|
+
if (mode instanceof fm_resistance_js_1.default)
|
|
298
|
+
mode.setAutoResistanceDefault(autoDefault);
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
getResistanceMode() {
|
|
302
|
+
const mode = this.getCyclingMode();
|
|
303
|
+
return mode instanceof fm_resistance_js_1.default ? mode : undefined;
|
|
304
|
+
}
|
|
305
|
+
async stop() {
|
|
306
|
+
this.getResistanceMode()?.logSummary();
|
|
307
|
+
return await super.stop();
|
|
292
308
|
}
|
|
293
309
|
async checkCapabilities() {
|
|
294
310
|
const before = this.capabilities.join(',');
|
|
@@ -304,12 +320,42 @@ class BleFmAdapter extends adapter_js_1.default {
|
|
|
304
320
|
if (sensor.features) {
|
|
305
321
|
this.updateCapabilitiesFromFeatures(sensor.features);
|
|
306
322
|
}
|
|
323
|
+
this.setSlopeSkippedLogged = false;
|
|
324
|
+
this.checkCyclingModeSupported();
|
|
325
|
+
if (sensor.features)
|
|
326
|
+
this.updateCyclingModeConfig();
|
|
307
327
|
const after = this.capabilities.join(',');
|
|
308
328
|
if (before !== after) {
|
|
309
329
|
this.logEvent({ message: 'device capabilities updated', name: this.getSettings().name, interface: this.getSettings().interface, capabilities: this.capabilities });
|
|
310
330
|
this.emit('device-info', this.getSettings(), { capabilities: this.capabilities });
|
|
311
331
|
}
|
|
312
332
|
}
|
|
333
|
+
isModeSupported(modeName) {
|
|
334
|
+
if (!this.getSensor()?.features)
|
|
335
|
+
return true;
|
|
336
|
+
return this.getSupportedCyclingModes().some(M => new M(null).getName() === modeName);
|
|
337
|
+
}
|
|
338
|
+
checkCyclingModeSupported() {
|
|
339
|
+
const sensor = this.getSensor();
|
|
340
|
+
const features = sensor?.features;
|
|
341
|
+
if (!features)
|
|
342
|
+
return;
|
|
343
|
+
const current = this.getCyclingMode();
|
|
344
|
+
const requestedMode = current?.getName();
|
|
345
|
+
if (!requestedMode || this.isModeSupported(requestedMode))
|
|
346
|
+
return;
|
|
347
|
+
const newMode = this.getDefaultCyclingMode();
|
|
348
|
+
if (!newMode)
|
|
349
|
+
return;
|
|
350
|
+
this.cyclingMode = newMode;
|
|
351
|
+
this.logEvent({
|
|
352
|
+
message: 'cycling mode not supported',
|
|
353
|
+
requestedMode, newMode: newMode.getName(),
|
|
354
|
+
setSlope: features.setSlope === true, setPower: features.setPower === true, setResistance: features.setResistance === true,
|
|
355
|
+
device: this.getName()
|
|
356
|
+
});
|
|
357
|
+
this.emit('mode-replaced', { requestedMode, newMode: newMode.getName() });
|
|
358
|
+
}
|
|
313
359
|
updateCapabilitiesFromFeatures(features) {
|
|
314
360
|
if (features.heartrate && !this.hasCapability(index_js_1.IncyclistCapability.HeartRate)) {
|
|
315
361
|
this.capabilities.push(index_js_1.IncyclistCapability.HeartRate);
|
|
@@ -352,7 +398,14 @@ class BleFmAdapter extends adapter_js_1.default {
|
|
|
352
398
|
const send = async () => {
|
|
353
399
|
const res = {};
|
|
354
400
|
if (update.slope !== undefined) {
|
|
355
|
-
|
|
401
|
+
const skipSlope = !update.isHub && device.features?.setSlope === false;
|
|
402
|
+
if (skipSlope) {
|
|
403
|
+
if (!this.setSlopeSkippedLogged) {
|
|
404
|
+
this.setSlopeSkippedLogged = true;
|
|
405
|
+
this.logEvent({ message: 'setSlope skipped', reason: 'not supported', device: this.getName() });
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
else if (update.isHub) {
|
|
356
409
|
await this.initVirtualShifting(true);
|
|
357
410
|
if (this.zwiftPlay) {
|
|
358
411
|
await this.zwiftPlay.setIncline(update.slope);
|
|
@@ -361,7 +414,8 @@ class BleFmAdapter extends adapter_js_1.default {
|
|
|
361
414
|
else {
|
|
362
415
|
await device.setSlope(update.slope);
|
|
363
416
|
}
|
|
364
|
-
|
|
417
|
+
if (!skipSlope)
|
|
418
|
+
res.slope = update.slope;
|
|
365
419
|
}
|
|
366
420
|
if (update.targetPower !== undefined && !update.isHub) {
|
|
367
421
|
const tp = update.targetPower > 0 ? update.targetPower : 0;
|
|
@@ -369,7 +423,9 @@ class BleFmAdapter extends adapter_js_1.default {
|
|
|
369
423
|
res.targetPower = tp;
|
|
370
424
|
}
|
|
371
425
|
if (update.targetResistance !== undefined && !update.isHub) {
|
|
372
|
-
await device.setTargetResistanceLevel(update.targetResistance);
|
|
426
|
+
const success = await device.setTargetResistanceLevel(update.targetResistance);
|
|
427
|
+
if (success === false)
|
|
428
|
+
this.getResistanceMode()?.registerFailedUpdate();
|
|
373
429
|
res.targetResistance = update.targetResistance;
|
|
374
430
|
}
|
|
375
431
|
if (update.gearRatio !== undefined) {
|
package/lib/cjs/ble/fm/sensor.js
CHANGED
|
@@ -167,6 +167,9 @@ class BleFitnessMachineDevice extends sensor_js_1.TBleSensor {
|
|
|
167
167
|
if (res === 5) {
|
|
168
168
|
this.hasControl = false;
|
|
169
169
|
}
|
|
170
|
+
if (res !== 1) {
|
|
171
|
+
this.logEvent({ message: 'setTargetResistanceLevel failed', device: this.getName(), resistanceLevel, result: String(res) });
|
|
172
|
+
}
|
|
170
173
|
return (res === 1);
|
|
171
174
|
}
|
|
172
175
|
async setSlope(slope) {
|
|
@@ -669,6 +672,8 @@ class BleFitnessMachineDevice extends sensor_js_1.TBleSensor {
|
|
|
669
672
|
res = await this.write(consts_js_1.FTMS_CP, data, props);
|
|
670
673
|
}
|
|
671
674
|
const responseData = Buffer.from(res);
|
|
675
|
+
if (responseData.length < 3)
|
|
676
|
+
throw new Error('not connected');
|
|
672
677
|
const opCode = responseData.readUInt8(0);
|
|
673
678
|
const request = responseData.readUInt8(1);
|
|
674
679
|
const result = responseData.readUInt8(2);
|
|
@@ -701,7 +706,7 @@ class BleFitnessMachineDevice extends sensor_js_1.TBleSensor {
|
|
|
701
706
|
return (res === 1);
|
|
702
707
|
}
|
|
703
708
|
async setIndoorBikeSimulation(windSpeed, gradient, crr, cw) {
|
|
704
|
-
if (this.features?.
|
|
709
|
+
if (this.features?.setSlope === false)
|
|
705
710
|
return true;
|
|
706
711
|
this.logEvent({ message: 'setIndoorBikeSimulation', device: this.getName(), windSpeed, gradient, crr, cw });
|
|
707
712
|
const hasControl = await this.requestControl();
|
|
@@ -5,6 +5,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const power_base_js_1 = __importDefault(require("./power-base.js"));
|
|
7
7
|
const types_js_1 = require("./types.js");
|
|
8
|
+
const MIN_GEAR = 1;
|
|
9
|
+
const MAX_GEAR = 26;
|
|
10
|
+
const LEVELS_PER_GEAR = 4;
|
|
11
|
+
const DEFAULT_MAX_SLOPE = 10;
|
|
12
|
+
const DOWNHILL_FACTOR = 0.5;
|
|
13
|
+
const UPDATE_THROTTLE_MS = 1000;
|
|
8
14
|
class FMResistanceMode extends power_base_js_1.default {
|
|
9
15
|
static config = {
|
|
10
16
|
isERG: false,
|
|
@@ -12,20 +18,61 @@ class FMResistanceMode extends power_base_js_1.default {
|
|
|
12
18
|
isResistance: true,
|
|
13
19
|
name: "Resistance",
|
|
14
20
|
description: "Resistance levels are set by the app based on selected gear. Calculates speed based on power and slope.",
|
|
15
|
-
properties:
|
|
16
|
-
{ key: 'startGear', name: 'Initial Gear', description: 'Initial Gear', type: types_js_1.CyclingModeProperyType.Integer, default: 5, min: 1, max: 26 }
|
|
17
|
-
]
|
|
21
|
+
properties: FMResistanceMode.buildProperties(false)
|
|
18
22
|
};
|
|
23
|
+
static buildProperties(autoDefault) {
|
|
24
|
+
const isAuto = (s) => (s?.autoResistance ?? autoDefault) !== false && s?.autoResistance !== 'false';
|
|
25
|
+
return [
|
|
26
|
+
{ key: 'startGear', name: 'Initial Gear', description: 'Initial Gear', type: types_js_1.CyclingModeProperyType.Integer, default: 5, min: 1, max: 26 },
|
|
27
|
+
{ key: 'autoResistance', name: 'Auto Resistance', description: 'Adjusts the resistance automatically to the slope of the route. Gear shifts will increase/decrease the resistance on top of that', type: types_js_1.CyclingModeProperyType.Boolean, default: autoDefault },
|
|
28
|
+
{ key: 'maxSlope', name: 'Max Slope', description: 'Slope (in %) at which the maximum resistance is reached. Downhill sections reduce the resistance at half of that rate', type: types_js_1.CyclingModeProperyType.Integer, default: DEFAULT_MAX_SLOPE, min: 1, max: 30, condition: isAuto }
|
|
29
|
+
];
|
|
30
|
+
}
|
|
19
31
|
confirmedResistance;
|
|
20
32
|
requestedResistance;
|
|
33
|
+
gearOffset = 0;
|
|
34
|
+
lastAutoSendTs = 0;
|
|
35
|
+
autoStats = { updatesSent: 0, updatesFailed: 0, gearChanges: 0 };
|
|
21
36
|
constructor(adapter, props) {
|
|
22
37
|
super(adapter, props);
|
|
23
38
|
this.initLogger('FMResistanceMode');
|
|
24
39
|
this.data.slope = 0;
|
|
25
40
|
}
|
|
41
|
+
setAutoResistanceDefault(autoDefault) {
|
|
42
|
+
const base = this.constructor.config;
|
|
43
|
+
this.setConfig({ ...base, properties: FMResistanceMode.buildProperties(autoDefault) });
|
|
44
|
+
}
|
|
45
|
+
isAutoResistance() {
|
|
46
|
+
const value = this.getSetting('autoResistance');
|
|
47
|
+
return value === true || value === 'true';
|
|
48
|
+
}
|
|
26
49
|
getBikeInitRequest() {
|
|
50
|
+
if (this.isAutoResistance()) {
|
|
51
|
+
return { targetResistance: this.calculateAutoResistance(this.data.slope ?? 0) };
|
|
52
|
+
}
|
|
27
53
|
return { targetResistance: this.calculateTargetResistance(this.getInitialGear()) };
|
|
28
54
|
}
|
|
55
|
+
resetSession() {
|
|
56
|
+
this.gearOffset = 0;
|
|
57
|
+
this.lastAutoSendTs = 0;
|
|
58
|
+
this.autoStats = { updatesSent: 0, updatesFailed: 0, gearChanges: 0 };
|
|
59
|
+
}
|
|
60
|
+
registerFailedUpdate() {
|
|
61
|
+
if (this.isAutoResistance())
|
|
62
|
+
this.autoStats.updatesFailed++;
|
|
63
|
+
}
|
|
64
|
+
logSummary() {
|
|
65
|
+
const stats = this.autoStats;
|
|
66
|
+
if (stats.updatesSent === 0)
|
|
67
|
+
return;
|
|
68
|
+
const durationSec = stats.tsStart ? Math.round((Date.now() - stats.tsStart) / 1000) : 0;
|
|
69
|
+
this.logEvent({
|
|
70
|
+
message: 'auto resistance summary',
|
|
71
|
+
updatesSent: stats.updatesSent, updatesFailed: stats.updatesFailed, gearChanges: stats.gearChanges,
|
|
72
|
+
targetMin: stats.targetMin ?? 0, targetMax: stats.targetMax ?? 0, durationSec
|
|
73
|
+
});
|
|
74
|
+
this.autoStats = { updatesSent: 0, updatesFailed: 0, gearChanges: 0 };
|
|
75
|
+
}
|
|
29
76
|
sendBikeUpdate(incoming) {
|
|
30
77
|
if (this.logger)
|
|
31
78
|
this.logEvent({ message: "processing update request", request: incoming, prev: this.prevRequest, data: this.getData() });
|
|
@@ -38,8 +85,16 @@ class FMResistanceMode extends power_base_js_1.default {
|
|
|
38
85
|
return req;
|
|
39
86
|
}
|
|
40
87
|
this.checkSlope(request);
|
|
41
|
-
this.
|
|
88
|
+
if (this.isAutoResistance()) {
|
|
89
|
+
this.checkAutoGearDeltaSet(request, newRequest);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
this.checkGearDeltaSet(request, newRequest);
|
|
93
|
+
}
|
|
42
94
|
this.checkTargetResistanceSet(request, newRequest);
|
|
95
|
+
if (this.isAutoResistance()) {
|
|
96
|
+
this.checkAutoResistanceSet(newRequest);
|
|
97
|
+
}
|
|
43
98
|
this.checkRefresh(request, newRequest);
|
|
44
99
|
this.checkEmptyRequest(newRequest);
|
|
45
100
|
this.prevRequest = JSON.parse(JSON.stringify(newRequest));
|
|
@@ -50,6 +105,9 @@ class FMResistanceMode extends power_base_js_1.default {
|
|
|
50
105
|
}
|
|
51
106
|
if (newRequest.targetResistance !== undefined) {
|
|
52
107
|
this.requestedResistance = newRequest.targetResistance;
|
|
108
|
+
if (this.isAutoResistance()) {
|
|
109
|
+
this.recordAutoUpdate(newRequest.targetResistance);
|
|
110
|
+
}
|
|
53
111
|
}
|
|
54
112
|
return newRequest;
|
|
55
113
|
}
|
|
@@ -89,8 +147,68 @@ class FMResistanceMode extends power_base_js_1.default {
|
|
|
89
147
|
delete request.gearDelta;
|
|
90
148
|
}
|
|
91
149
|
}
|
|
150
|
+
getStartGear() {
|
|
151
|
+
const gear = Math.round(this.getInitialGear());
|
|
152
|
+
if (Number.isNaN(gear))
|
|
153
|
+
return MIN_GEAR;
|
|
154
|
+
return Math.min(Math.max(gear, MIN_GEAR), MAX_GEAR);
|
|
155
|
+
}
|
|
156
|
+
getMaxSlope() {
|
|
157
|
+
const maxSlope = Number(this.getSetting('maxSlope'));
|
|
158
|
+
return (Number.isNaN(maxSlope) || maxSlope <= 0) ? DEFAULT_MAX_SLOPE : maxSlope;
|
|
159
|
+
}
|
|
160
|
+
getGearOffset() {
|
|
161
|
+
const startGear = this.getStartGear();
|
|
162
|
+
return Math.min(Math.max(this.gearOffset, MIN_GEAR - startGear), MAX_GEAR - startGear);
|
|
163
|
+
}
|
|
164
|
+
getVirtualGear() {
|
|
165
|
+
return this.getStartGear() + this.getGearOffset();
|
|
166
|
+
}
|
|
167
|
+
calculateAutoResistance(slope) {
|
|
168
|
+
const base = this.calculateTargetResistance(this.getStartGear());
|
|
169
|
+
const maxSlope = this.getMaxSlope();
|
|
170
|
+
const s = Number.isFinite(slope) ? slope : 0;
|
|
171
|
+
const effective = s < 0 ? s * DOWNHILL_FACTOR : s;
|
|
172
|
+
const resistance = base + (100 - base) * effective / maxSlope + LEVELS_PER_GEAR * this.getGearOffset();
|
|
173
|
+
return Math.round(Math.min(Math.max(resistance, 0), 100));
|
|
174
|
+
}
|
|
175
|
+
checkAutoGearDeltaSet(request, newRequest) {
|
|
176
|
+
if (request.gearDelta === undefined)
|
|
177
|
+
return;
|
|
178
|
+
const startGear = this.getStartGear();
|
|
179
|
+
const prevOffset = this.getGearOffset();
|
|
180
|
+
const offset = Math.min(Math.max(prevOffset + request.gearDelta, MIN_GEAR - startGear), MAX_GEAR - startGear);
|
|
181
|
+
delete request.gearDelta;
|
|
182
|
+
if (offset !== prevOffset)
|
|
183
|
+
this.autoStats.gearChanges++;
|
|
184
|
+
this.gearOffset = offset;
|
|
185
|
+
newRequest.targetResistance = this.calculateAutoResistance(this.data.slope ?? 0);
|
|
186
|
+
}
|
|
187
|
+
checkAutoResistanceSet(newRequest) {
|
|
188
|
+
if (newRequest.targetResistance !== undefined)
|
|
189
|
+
return;
|
|
190
|
+
if (Date.now() - this.lastAutoSendTs < UPDATE_THROTTLE_MS)
|
|
191
|
+
return;
|
|
192
|
+
const target = this.calculateAutoResistance(this.data.slope ?? 0);
|
|
193
|
+
if (target !== this.requestedResistance)
|
|
194
|
+
newRequest.targetResistance = target;
|
|
195
|
+
}
|
|
196
|
+
recordAutoUpdate(target) {
|
|
197
|
+
const now = Date.now();
|
|
198
|
+
const stats = this.autoStats;
|
|
199
|
+
this.lastAutoSendTs = now;
|
|
200
|
+
stats.tsStart = stats.tsStart ?? now;
|
|
201
|
+
stats.updatesSent++;
|
|
202
|
+
stats.targetMin = stats.targetMin === undefined ? target : Math.min(stats.targetMin, target);
|
|
203
|
+
stats.targetMax = stats.targetMax === undefined ? target : Math.max(stats.targetMax, target);
|
|
204
|
+
}
|
|
92
205
|
updateData(bikeData, log) {
|
|
93
206
|
const data = super.updateData(bikeData, log);
|
|
207
|
+
if (this.isAutoResistance()) {
|
|
208
|
+
data.gear = this.getVirtualGear();
|
|
209
|
+
data.gearStr = `${data.gear}`;
|
|
210
|
+
return data;
|
|
211
|
+
}
|
|
94
212
|
if (data.resistance !== undefined) {
|
|
95
213
|
data.gear = this.getGear(data.resistance);
|
|
96
214
|
data.gearStr = `${data.gear}`;
|
package/lib/esm/base/adpater.js
CHANGED
|
@@ -165,7 +165,7 @@ export default class IncyclistDevice extends EventEmitter {
|
|
|
165
165
|
}
|
|
166
166
|
createOrGetMode(mode, useCurrent = true) {
|
|
167
167
|
if (typeof mode === 'string') {
|
|
168
|
-
if (useCurrent && this.cyclingMode && mode === this.cyclingMode?.getName()) {
|
|
168
|
+
if (useCurrent && this.cyclingMode && mode === this.cyclingMode?.getName() && this.isModeSupported(mode)) {
|
|
169
169
|
return this.cyclingMode;
|
|
170
170
|
}
|
|
171
171
|
const supported = this.getSupportedCyclingModes();
|
|
@@ -183,6 +183,9 @@ export default class IncyclistDevice extends EventEmitter {
|
|
|
183
183
|
}
|
|
184
184
|
return mode;
|
|
185
185
|
}
|
|
186
|
+
isModeSupported(_modeName) {
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
186
189
|
getSupportedCyclingModes() {
|
|
187
190
|
if (!this.isControllable())
|
|
188
191
|
return [];
|
|
@@ -402,7 +402,12 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
402
402
|
}
|
|
403
403
|
return connected;
|
|
404
404
|
}
|
|
405
|
-
|
|
405
|
+
try {
|
|
406
|
+
await sensor.getPeripheral().disconnect();
|
|
407
|
+
}
|
|
408
|
+
catch (err) {
|
|
409
|
+
this.logEvent({ message: 'restart: disconnect failed', device: this.getName(), interface: this.getInterface(), reason: err?.message });
|
|
410
|
+
}
|
|
406
411
|
const success = await super.restart(pause);
|
|
407
412
|
return success;
|
|
408
413
|
}
|
|
@@ -8,6 +8,8 @@ import { BleAdapterFactory } from "../factories/index.js";
|
|
|
8
8
|
import { CSP } from "../consts.js";
|
|
9
9
|
import { BKOOL_SERVICE, isBkoolDeviceName } from "../bkool/consts.js";
|
|
10
10
|
const BLE_EXPIRATION_TIMEOUT = 10 * 1000 * 60;
|
|
11
|
+
const DEFAULT_PAUSE_REASON = 'app-background';
|
|
12
|
+
const TEMPORARY_SCAN_TIMEOUT = 30 * 1000;
|
|
11
13
|
const BLE_DEFAULT_CONNECT_TIMEOUT = 30 * 1000;
|
|
12
14
|
let instanceCount = 0;
|
|
13
15
|
export class BleInterface extends EventEmitter {
|
|
@@ -26,6 +28,8 @@ export class BleInterface extends EventEmitter {
|
|
|
26
28
|
expectedServices = ['180d', '1818', '1826', '6e40fec1'];
|
|
27
29
|
matching = [];
|
|
28
30
|
backgroundPaused = false;
|
|
31
|
+
pauseReasons = new Set();
|
|
32
|
+
pausedSince;
|
|
29
33
|
connectTask;
|
|
30
34
|
disconnectTask;
|
|
31
35
|
scanTask;
|
|
@@ -244,18 +248,30 @@ export class BleInterface extends EventEmitter {
|
|
|
244
248
|
delete this.scanTask;
|
|
245
249
|
return this.buildDeviceSettings(this.matching);
|
|
246
250
|
}
|
|
247
|
-
async pauseBackgroundActivity() {
|
|
248
|
-
if (this.
|
|
251
|
+
async pauseBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
252
|
+
if (this.pauseReasons.has(reason))
|
|
249
253
|
return;
|
|
254
|
+
this.pauseReasons.add(reason);
|
|
255
|
+
const alreadyPaused = this.backgroundPaused;
|
|
250
256
|
this.backgroundPaused = true;
|
|
251
|
-
this.logEvent({ message: 'pausing background activity' });
|
|
257
|
+
this.logEvent({ message: 'pausing background activity', reason, reasons: Array.from(this.pauseReasons).join(','), interface: 'ble' });
|
|
258
|
+
if (alreadyPaused)
|
|
259
|
+
return;
|
|
260
|
+
this.pausedSince = Date.now();
|
|
252
261
|
await this.stopPeripheralScan();
|
|
253
262
|
}
|
|
254
|
-
async resumeBackgroundActivity() {
|
|
255
|
-
if (!this.
|
|
263
|
+
async resumeBackgroundActivity(reason = DEFAULT_PAUSE_REASON) {
|
|
264
|
+
if (!this.pauseReasons.delete(reason))
|
|
256
265
|
return;
|
|
266
|
+
const reasons = Array.from(this.pauseReasons).join(',');
|
|
267
|
+
if (this.pauseReasons.size > 0) {
|
|
268
|
+
this.logEvent({ message: 'resuming background activity skipped', reason, reasons, interface: 'ble' });
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
257
271
|
this.backgroundPaused = false;
|
|
258
|
-
this.
|
|
272
|
+
const pausedMs = this.pausedSince ? Date.now() - this.pausedSince : 0;
|
|
273
|
+
delete this.pausedSince;
|
|
274
|
+
this.logEvent({ message: 'resuming background activity', reason, reasons, pausedMs, interface: 'ble' });
|
|
259
275
|
await this.startPeripheralScan();
|
|
260
276
|
}
|
|
261
277
|
pauseLogging() {
|
|
@@ -290,22 +306,40 @@ export class BleInterface extends EventEmitter {
|
|
|
290
306
|
const peripheral = this.createPeripheralFromSettings(settings);
|
|
291
307
|
if (peripheral)
|
|
292
308
|
return Promise.resolve(peripheral);
|
|
293
|
-
return new Promise((done) => {
|
|
309
|
+
return new Promise((done, reject) => {
|
|
310
|
+
const temporary = this.backgroundPaused;
|
|
294
311
|
const wasDiscovering = this.isDiscovering();
|
|
295
|
-
|
|
296
|
-
|
|
312
|
+
const tsStart = Date.now();
|
|
313
|
+
let timer;
|
|
314
|
+
const finish = () => {
|
|
315
|
+
this.off('device', onDevice);
|
|
316
|
+
if (timer)
|
|
317
|
+
clearTimeout(timer);
|
|
318
|
+
if (temporary || !wasDiscovering)
|
|
319
|
+
this.stopPeripheralScan().catch(() => { });
|
|
320
|
+
};
|
|
297
321
|
const onDevice = (device) => {
|
|
298
322
|
if (device.address === settings.address) {
|
|
299
323
|
const peripheral = this.createPeripheralFromSettings(device);
|
|
300
324
|
if (peripheral) {
|
|
301
|
-
|
|
302
|
-
if (
|
|
303
|
-
this.
|
|
325
|
+
finish();
|
|
326
|
+
if (temporary)
|
|
327
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: true });
|
|
304
328
|
done(peripheral);
|
|
305
329
|
}
|
|
306
330
|
}
|
|
307
331
|
};
|
|
308
332
|
this.on('device', onDevice);
|
|
333
|
+
if (temporary) {
|
|
334
|
+
timer = setTimeout(() => {
|
|
335
|
+
finish();
|
|
336
|
+
this.logEvent({ message: 'temporary discovery for peripheral', interface: 'ble', device: settings.name, waitedMs: Date.now() - tsStart, found: false });
|
|
337
|
+
reject(new Error('peripheral not found'));
|
|
338
|
+
}, TEMPORARY_SCAN_TIMEOUT);
|
|
339
|
+
this.startPeripheralScan(false, true).catch(() => { });
|
|
340
|
+
}
|
|
341
|
+
else if (!wasDiscovering)
|
|
342
|
+
this.startPeripheralScan();
|
|
309
343
|
});
|
|
310
344
|
}
|
|
311
345
|
createDeviceSetting(service) {
|
|
@@ -333,8 +367,8 @@ export class BleInterface extends EventEmitter {
|
|
|
333
367
|
await this.disconnect();
|
|
334
368
|
await this.connectInternal(true);
|
|
335
369
|
}
|
|
336
|
-
async startPeripheralScan(retry = false) {
|
|
337
|
-
if (this.backgroundPaused)
|
|
370
|
+
async startPeripheralScan(retry = false, force = false) {
|
|
371
|
+
if (this.backgroundPaused && !force)
|
|
338
372
|
return;
|
|
339
373
|
if (!this.isConnected() || this.isDiscovering()) {
|
|
340
374
|
return;
|
|
@@ -354,7 +388,7 @@ export class BleInterface extends EventEmitter {
|
|
|
354
388
|
this.logError(err, 'discover');
|
|
355
389
|
}
|
|
356
390
|
if (this.discoverTask.getState().result !== 'stopped') {
|
|
357
|
-
this.startPeripheralScan(true);
|
|
391
|
+
this.startPeripheralScan(true, force).catch(err => this.logError(err, 'discover'));
|
|
358
392
|
}
|
|
359
393
|
}
|
|
360
394
|
async stopPeripheralScan() {
|
|
@@ -641,10 +675,18 @@ export class BleInterface extends EventEmitter {
|
|
|
641
675
|
return supported.length > 0;
|
|
642
676
|
}
|
|
643
677
|
find(service) {
|
|
644
|
-
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address &&
|
|
678
|
+
return this.services.find(a => a.service.name === service.name && a.service?.peripheral?.address && this.isAnnouncementValid(a));
|
|
645
679
|
}
|
|
646
680
|
getAll() {
|
|
647
|
-
return this.services.filter(a =>
|
|
681
|
+
return this.services.filter(a => this.isAnnouncementValid(a));
|
|
682
|
+
}
|
|
683
|
+
isAnnouncementValid(a) {
|
|
684
|
+
if (a.ts > Date.now() - BLE_EXPIRATION_TIMEOUT)
|
|
685
|
+
return true;
|
|
686
|
+
if (!this.backgroundPaused)
|
|
687
|
+
return false;
|
|
688
|
+
const id = a.service?.peripheral?.id;
|
|
689
|
+
return id !== undefined && this.connectedPeripherals.some(p => p.id === id);
|
|
648
690
|
}
|
|
649
691
|
setDebug(enabled) {
|
|
650
692
|
this.debug = enabled;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { sleep } from "../../utils/utils.js";
|
|
1
|
+
import { runWithTimeout, 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
5
|
const POST_CONNECT_DISCOVERY_ATTEMPTS = 4;
|
|
6
6
|
const POST_CONNECT_DISCOVERY_BACKOFF_MS = 1000;
|
|
7
|
+
const BLE_DISCONNECT_STEP_TIMEOUT = 3000;
|
|
7
8
|
export class BlePeripheral {
|
|
8
9
|
announcement;
|
|
9
10
|
connected = false;
|
|
@@ -85,8 +86,21 @@ export class BlePeripheral {
|
|
|
85
86
|
}
|
|
86
87
|
async disconnect(connectionLost = false) {
|
|
87
88
|
this.disconnecting = true;
|
|
89
|
+
try {
|
|
90
|
+
return await this.doDisconnect(connectionLost);
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
this.disconnecting = false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async doDisconnect(connectionLost) {
|
|
88
97
|
if (this.isConnected() || connectionLost) {
|
|
89
|
-
|
|
98
|
+
try {
|
|
99
|
+
await runWithTimeout(this.unsubscribeAll(connectionLost), BLE_DISCONNECT_STEP_TIMEOUT);
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
this.logEvent({ message: 'peripheral unsubscribe timeout', timeoutMs: BLE_DISCONNECT_STEP_TIMEOUT, reason: err?.message });
|
|
103
|
+
}
|
|
90
104
|
}
|
|
91
105
|
Object.keys(this.characteristics).forEach(uuid => {
|
|
92
106
|
const c = this.characteristics[uuid];
|
|
@@ -103,8 +117,12 @@ export class BlePeripheral {
|
|
|
103
117
|
return new Promise((done) => { this.getPeripheral().disconnect(() => { done(); }); });
|
|
104
118
|
};
|
|
105
119
|
}
|
|
106
|
-
|
|
107
|
-
.
|
|
120
|
+
try {
|
|
121
|
+
await runWithTimeout(this.getPeripheral().disconnectAsync(), BLE_DISCONNECT_STEP_TIMEOUT);
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
this.logEvent({ message: 'peripheral disconnect timeout', address: peripheral.address, timeoutMs: BLE_DISCONNECT_STEP_TIMEOUT, reason: err?.message });
|
|
125
|
+
}
|
|
108
126
|
}
|
|
109
127
|
peripheral.removeAllListeners();
|
|
110
128
|
this.ble.unregisterConnected(peripheral.id);
|
|
@@ -113,8 +131,7 @@ export class BlePeripheral {
|
|
|
113
131
|
delete this.onDisconnectHandler;
|
|
114
132
|
}
|
|
115
133
|
this.connected = false;
|
|
116
|
-
this.
|
|
117
|
-
this.logEvent({ message: 'peripheral disconnect completed', address: peripheral.address });
|
|
134
|
+
this.logEvent({ message: 'peripheral disconnect completed', address: peripheral?.address });
|
|
118
135
|
return !this.connected;
|
|
119
136
|
}
|
|
120
137
|
isConnected() {
|
|
@@ -21,6 +21,7 @@ export default class BleFmAdapter extends BleAdapter {
|
|
|
21
21
|
promiseSendUpdate;
|
|
22
22
|
zwiftPlay;
|
|
23
23
|
isHubInitialized = false;
|
|
24
|
+
setSlopeSkippedLogged = false;
|
|
24
25
|
constructor(settings, props) {
|
|
25
26
|
super(settings, props);
|
|
26
27
|
this.logger = new EventLogger('BLE-FM');
|
|
@@ -171,6 +172,7 @@ export default class BleFmAdapter extends BleAdapter {
|
|
|
171
172
|
return;
|
|
172
173
|
await this.establishControl();
|
|
173
174
|
await this.attemptStartRequest();
|
|
175
|
+
this.getResistanceMode()?.resetSession();
|
|
174
176
|
await this.sendInitialRequest();
|
|
175
177
|
}
|
|
176
178
|
async initControlBestEffort(_startProps) {
|
|
@@ -284,6 +286,20 @@ export default class BleFmAdapter extends BleAdapter {
|
|
|
284
286
|
mode.resetConfig();
|
|
285
287
|
mode.getConfig();
|
|
286
288
|
});
|
|
289
|
+
const autoDefault = this.getSensor()?.features?.setSlope === false;
|
|
290
|
+
const resistanceModes = [...Object.values(this.modes), this.cyclingMode];
|
|
291
|
+
resistanceModes.forEach(mode => {
|
|
292
|
+
if (mode instanceof FMResistanceMode)
|
|
293
|
+
mode.setAutoResistanceDefault(autoDefault);
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
getResistanceMode() {
|
|
297
|
+
const mode = this.getCyclingMode();
|
|
298
|
+
return mode instanceof FMResistanceMode ? mode : undefined;
|
|
299
|
+
}
|
|
300
|
+
async stop() {
|
|
301
|
+
this.getResistanceMode()?.logSummary();
|
|
302
|
+
return await super.stop();
|
|
287
303
|
}
|
|
288
304
|
async checkCapabilities() {
|
|
289
305
|
const before = this.capabilities.join(',');
|
|
@@ -299,12 +315,42 @@ export default class BleFmAdapter extends BleAdapter {
|
|
|
299
315
|
if (sensor.features) {
|
|
300
316
|
this.updateCapabilitiesFromFeatures(sensor.features);
|
|
301
317
|
}
|
|
318
|
+
this.setSlopeSkippedLogged = false;
|
|
319
|
+
this.checkCyclingModeSupported();
|
|
320
|
+
if (sensor.features)
|
|
321
|
+
this.updateCyclingModeConfig();
|
|
302
322
|
const after = this.capabilities.join(',');
|
|
303
323
|
if (before !== after) {
|
|
304
324
|
this.logEvent({ message: 'device capabilities updated', name: this.getSettings().name, interface: this.getSettings().interface, capabilities: this.capabilities });
|
|
305
325
|
this.emit('device-info', this.getSettings(), { capabilities: this.capabilities });
|
|
306
326
|
}
|
|
307
327
|
}
|
|
328
|
+
isModeSupported(modeName) {
|
|
329
|
+
if (!this.getSensor()?.features)
|
|
330
|
+
return true;
|
|
331
|
+
return this.getSupportedCyclingModes().some(M => new M(null).getName() === modeName);
|
|
332
|
+
}
|
|
333
|
+
checkCyclingModeSupported() {
|
|
334
|
+
const sensor = this.getSensor();
|
|
335
|
+
const features = sensor?.features;
|
|
336
|
+
if (!features)
|
|
337
|
+
return;
|
|
338
|
+
const current = this.getCyclingMode();
|
|
339
|
+
const requestedMode = current?.getName();
|
|
340
|
+
if (!requestedMode || this.isModeSupported(requestedMode))
|
|
341
|
+
return;
|
|
342
|
+
const newMode = this.getDefaultCyclingMode();
|
|
343
|
+
if (!newMode)
|
|
344
|
+
return;
|
|
345
|
+
this.cyclingMode = newMode;
|
|
346
|
+
this.logEvent({
|
|
347
|
+
message: 'cycling mode not supported',
|
|
348
|
+
requestedMode, newMode: newMode.getName(),
|
|
349
|
+
setSlope: features.setSlope === true, setPower: features.setPower === true, setResistance: features.setResistance === true,
|
|
350
|
+
device: this.getName()
|
|
351
|
+
});
|
|
352
|
+
this.emit('mode-replaced', { requestedMode, newMode: newMode.getName() });
|
|
353
|
+
}
|
|
308
354
|
updateCapabilitiesFromFeatures(features) {
|
|
309
355
|
if (features.heartrate && !this.hasCapability(IncyclistCapability.HeartRate)) {
|
|
310
356
|
this.capabilities.push(IncyclistCapability.HeartRate);
|
|
@@ -347,7 +393,14 @@ export default class BleFmAdapter extends BleAdapter {
|
|
|
347
393
|
const send = async () => {
|
|
348
394
|
const res = {};
|
|
349
395
|
if (update.slope !== undefined) {
|
|
350
|
-
|
|
396
|
+
const skipSlope = !update.isHub && device.features?.setSlope === false;
|
|
397
|
+
if (skipSlope) {
|
|
398
|
+
if (!this.setSlopeSkippedLogged) {
|
|
399
|
+
this.setSlopeSkippedLogged = true;
|
|
400
|
+
this.logEvent({ message: 'setSlope skipped', reason: 'not supported', device: this.getName() });
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
else if (update.isHub) {
|
|
351
404
|
await this.initVirtualShifting(true);
|
|
352
405
|
if (this.zwiftPlay) {
|
|
353
406
|
await this.zwiftPlay.setIncline(update.slope);
|
|
@@ -356,7 +409,8 @@ export default class BleFmAdapter extends BleAdapter {
|
|
|
356
409
|
else {
|
|
357
410
|
await device.setSlope(update.slope);
|
|
358
411
|
}
|
|
359
|
-
|
|
412
|
+
if (!skipSlope)
|
|
413
|
+
res.slope = update.slope;
|
|
360
414
|
}
|
|
361
415
|
if (update.targetPower !== undefined && !update.isHub) {
|
|
362
416
|
const tp = update.targetPower > 0 ? update.targetPower : 0;
|
|
@@ -364,7 +418,9 @@ export default class BleFmAdapter extends BleAdapter {
|
|
|
364
418
|
res.targetPower = tp;
|
|
365
419
|
}
|
|
366
420
|
if (update.targetResistance !== undefined && !update.isHub) {
|
|
367
|
-
await device.setTargetResistanceLevel(update.targetResistance);
|
|
421
|
+
const success = await device.setTargetResistanceLevel(update.targetResistance);
|
|
422
|
+
if (success === false)
|
|
423
|
+
this.getResistanceMode()?.registerFailedUpdate();
|
|
368
424
|
res.targetResistance = update.targetResistance;
|
|
369
425
|
}
|
|
370
426
|
if (update.gearRatio !== undefined) {
|
package/lib/esm/ble/fm/sensor.js
CHANGED
|
@@ -165,6 +165,9 @@ export default class BleFitnessMachineDevice extends TBleSensor {
|
|
|
165
165
|
if (res === 5) {
|
|
166
166
|
this.hasControl = false;
|
|
167
167
|
}
|
|
168
|
+
if (res !== 1) {
|
|
169
|
+
this.logEvent({ message: 'setTargetResistanceLevel failed', device: this.getName(), resistanceLevel, result: String(res) });
|
|
170
|
+
}
|
|
168
171
|
return (res === 1);
|
|
169
172
|
}
|
|
170
173
|
async setSlope(slope) {
|
|
@@ -667,6 +670,8 @@ export default class BleFitnessMachineDevice extends TBleSensor {
|
|
|
667
670
|
res = await this.write(FTMS_CP, data, props);
|
|
668
671
|
}
|
|
669
672
|
const responseData = Buffer.from(res);
|
|
673
|
+
if (responseData.length < 3)
|
|
674
|
+
throw new Error('not connected');
|
|
670
675
|
const opCode = responseData.readUInt8(0);
|
|
671
676
|
const request = responseData.readUInt8(1);
|
|
672
677
|
const result = responseData.readUInt8(2);
|
|
@@ -699,7 +704,7 @@ export default class BleFitnessMachineDevice extends TBleSensor {
|
|
|
699
704
|
return (res === 1);
|
|
700
705
|
}
|
|
701
706
|
async setIndoorBikeSimulation(windSpeed, gradient, crr, cw) {
|
|
702
|
-
if (this.features?.
|
|
707
|
+
if (this.features?.setSlope === false)
|
|
703
708
|
return true;
|
|
704
709
|
this.logEvent({ message: 'setIndoorBikeSimulation', device: this.getName(), windSpeed, gradient, crr, cw });
|
|
705
710
|
const hasControl = await this.requestControl();
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import PowerBasedCyclingModeBase from "./power-base.js";
|
|
2
2
|
import { CyclingModeProperyType } from "./types.js";
|
|
3
|
+
const MIN_GEAR = 1;
|
|
4
|
+
const MAX_GEAR = 26;
|
|
5
|
+
const LEVELS_PER_GEAR = 4;
|
|
6
|
+
const DEFAULT_MAX_SLOPE = 10;
|
|
7
|
+
const DOWNHILL_FACTOR = 0.5;
|
|
8
|
+
const UPDATE_THROTTLE_MS = 1000;
|
|
3
9
|
export default class FMResistanceMode extends PowerBasedCyclingModeBase {
|
|
4
10
|
static config = {
|
|
5
11
|
isERG: false,
|
|
@@ -7,20 +13,61 @@ export default class FMResistanceMode extends PowerBasedCyclingModeBase {
|
|
|
7
13
|
isResistance: true,
|
|
8
14
|
name: "Resistance",
|
|
9
15
|
description: "Resistance levels are set by the app based on selected gear. Calculates speed based on power and slope.",
|
|
10
|
-
properties:
|
|
11
|
-
{ key: 'startGear', name: 'Initial Gear', description: 'Initial Gear', type: CyclingModeProperyType.Integer, default: 5, min: 1, max: 26 }
|
|
12
|
-
]
|
|
16
|
+
properties: FMResistanceMode.buildProperties(false)
|
|
13
17
|
};
|
|
18
|
+
static buildProperties(autoDefault) {
|
|
19
|
+
const isAuto = (s) => (s?.autoResistance ?? autoDefault) !== false && s?.autoResistance !== 'false';
|
|
20
|
+
return [
|
|
21
|
+
{ key: 'startGear', name: 'Initial Gear', description: 'Initial Gear', type: CyclingModeProperyType.Integer, default: 5, min: 1, max: 26 },
|
|
22
|
+
{ key: 'autoResistance', name: 'Auto Resistance', description: 'Adjusts the resistance automatically to the slope of the route. Gear shifts will increase/decrease the resistance on top of that', type: CyclingModeProperyType.Boolean, default: autoDefault },
|
|
23
|
+
{ key: 'maxSlope', name: 'Max Slope', description: 'Slope (in %) at which the maximum resistance is reached. Downhill sections reduce the resistance at half of that rate', type: CyclingModeProperyType.Integer, default: DEFAULT_MAX_SLOPE, min: 1, max: 30, condition: isAuto }
|
|
24
|
+
];
|
|
25
|
+
}
|
|
14
26
|
confirmedResistance;
|
|
15
27
|
requestedResistance;
|
|
28
|
+
gearOffset = 0;
|
|
29
|
+
lastAutoSendTs = 0;
|
|
30
|
+
autoStats = { updatesSent: 0, updatesFailed: 0, gearChanges: 0 };
|
|
16
31
|
constructor(adapter, props) {
|
|
17
32
|
super(adapter, props);
|
|
18
33
|
this.initLogger('FMResistanceMode');
|
|
19
34
|
this.data.slope = 0;
|
|
20
35
|
}
|
|
36
|
+
setAutoResistanceDefault(autoDefault) {
|
|
37
|
+
const base = this.constructor.config;
|
|
38
|
+
this.setConfig({ ...base, properties: FMResistanceMode.buildProperties(autoDefault) });
|
|
39
|
+
}
|
|
40
|
+
isAutoResistance() {
|
|
41
|
+
const value = this.getSetting('autoResistance');
|
|
42
|
+
return value === true || value === 'true';
|
|
43
|
+
}
|
|
21
44
|
getBikeInitRequest() {
|
|
45
|
+
if (this.isAutoResistance()) {
|
|
46
|
+
return { targetResistance: this.calculateAutoResistance(this.data.slope ?? 0) };
|
|
47
|
+
}
|
|
22
48
|
return { targetResistance: this.calculateTargetResistance(this.getInitialGear()) };
|
|
23
49
|
}
|
|
50
|
+
resetSession() {
|
|
51
|
+
this.gearOffset = 0;
|
|
52
|
+
this.lastAutoSendTs = 0;
|
|
53
|
+
this.autoStats = { updatesSent: 0, updatesFailed: 0, gearChanges: 0 };
|
|
54
|
+
}
|
|
55
|
+
registerFailedUpdate() {
|
|
56
|
+
if (this.isAutoResistance())
|
|
57
|
+
this.autoStats.updatesFailed++;
|
|
58
|
+
}
|
|
59
|
+
logSummary() {
|
|
60
|
+
const stats = this.autoStats;
|
|
61
|
+
if (stats.updatesSent === 0)
|
|
62
|
+
return;
|
|
63
|
+
const durationSec = stats.tsStart ? Math.round((Date.now() - stats.tsStart) / 1000) : 0;
|
|
64
|
+
this.logEvent({
|
|
65
|
+
message: 'auto resistance summary',
|
|
66
|
+
updatesSent: stats.updatesSent, updatesFailed: stats.updatesFailed, gearChanges: stats.gearChanges,
|
|
67
|
+
targetMin: stats.targetMin ?? 0, targetMax: stats.targetMax ?? 0, durationSec
|
|
68
|
+
});
|
|
69
|
+
this.autoStats = { updatesSent: 0, updatesFailed: 0, gearChanges: 0 };
|
|
70
|
+
}
|
|
24
71
|
sendBikeUpdate(incoming) {
|
|
25
72
|
if (this.logger)
|
|
26
73
|
this.logEvent({ message: "processing update request", request: incoming, prev: this.prevRequest, data: this.getData() });
|
|
@@ -33,8 +80,16 @@ export default class FMResistanceMode extends PowerBasedCyclingModeBase {
|
|
|
33
80
|
return req;
|
|
34
81
|
}
|
|
35
82
|
this.checkSlope(request);
|
|
36
|
-
this.
|
|
83
|
+
if (this.isAutoResistance()) {
|
|
84
|
+
this.checkAutoGearDeltaSet(request, newRequest);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
this.checkGearDeltaSet(request, newRequest);
|
|
88
|
+
}
|
|
37
89
|
this.checkTargetResistanceSet(request, newRequest);
|
|
90
|
+
if (this.isAutoResistance()) {
|
|
91
|
+
this.checkAutoResistanceSet(newRequest);
|
|
92
|
+
}
|
|
38
93
|
this.checkRefresh(request, newRequest);
|
|
39
94
|
this.checkEmptyRequest(newRequest);
|
|
40
95
|
this.prevRequest = JSON.parse(JSON.stringify(newRequest));
|
|
@@ -45,6 +100,9 @@ export default class FMResistanceMode extends PowerBasedCyclingModeBase {
|
|
|
45
100
|
}
|
|
46
101
|
if (newRequest.targetResistance !== undefined) {
|
|
47
102
|
this.requestedResistance = newRequest.targetResistance;
|
|
103
|
+
if (this.isAutoResistance()) {
|
|
104
|
+
this.recordAutoUpdate(newRequest.targetResistance);
|
|
105
|
+
}
|
|
48
106
|
}
|
|
49
107
|
return newRequest;
|
|
50
108
|
}
|
|
@@ -84,8 +142,68 @@ export default class FMResistanceMode extends PowerBasedCyclingModeBase {
|
|
|
84
142
|
delete request.gearDelta;
|
|
85
143
|
}
|
|
86
144
|
}
|
|
145
|
+
getStartGear() {
|
|
146
|
+
const gear = Math.round(this.getInitialGear());
|
|
147
|
+
if (Number.isNaN(gear))
|
|
148
|
+
return MIN_GEAR;
|
|
149
|
+
return Math.min(Math.max(gear, MIN_GEAR), MAX_GEAR);
|
|
150
|
+
}
|
|
151
|
+
getMaxSlope() {
|
|
152
|
+
const maxSlope = Number(this.getSetting('maxSlope'));
|
|
153
|
+
return (Number.isNaN(maxSlope) || maxSlope <= 0) ? DEFAULT_MAX_SLOPE : maxSlope;
|
|
154
|
+
}
|
|
155
|
+
getGearOffset() {
|
|
156
|
+
const startGear = this.getStartGear();
|
|
157
|
+
return Math.min(Math.max(this.gearOffset, MIN_GEAR - startGear), MAX_GEAR - startGear);
|
|
158
|
+
}
|
|
159
|
+
getVirtualGear() {
|
|
160
|
+
return this.getStartGear() + this.getGearOffset();
|
|
161
|
+
}
|
|
162
|
+
calculateAutoResistance(slope) {
|
|
163
|
+
const base = this.calculateTargetResistance(this.getStartGear());
|
|
164
|
+
const maxSlope = this.getMaxSlope();
|
|
165
|
+
const s = Number.isFinite(slope) ? slope : 0;
|
|
166
|
+
const effective = s < 0 ? s * DOWNHILL_FACTOR : s;
|
|
167
|
+
const resistance = base + (100 - base) * effective / maxSlope + LEVELS_PER_GEAR * this.getGearOffset();
|
|
168
|
+
return Math.round(Math.min(Math.max(resistance, 0), 100));
|
|
169
|
+
}
|
|
170
|
+
checkAutoGearDeltaSet(request, newRequest) {
|
|
171
|
+
if (request.gearDelta === undefined)
|
|
172
|
+
return;
|
|
173
|
+
const startGear = this.getStartGear();
|
|
174
|
+
const prevOffset = this.getGearOffset();
|
|
175
|
+
const offset = Math.min(Math.max(prevOffset + request.gearDelta, MIN_GEAR - startGear), MAX_GEAR - startGear);
|
|
176
|
+
delete request.gearDelta;
|
|
177
|
+
if (offset !== prevOffset)
|
|
178
|
+
this.autoStats.gearChanges++;
|
|
179
|
+
this.gearOffset = offset;
|
|
180
|
+
newRequest.targetResistance = this.calculateAutoResistance(this.data.slope ?? 0);
|
|
181
|
+
}
|
|
182
|
+
checkAutoResistanceSet(newRequest) {
|
|
183
|
+
if (newRequest.targetResistance !== undefined)
|
|
184
|
+
return;
|
|
185
|
+
if (Date.now() - this.lastAutoSendTs < UPDATE_THROTTLE_MS)
|
|
186
|
+
return;
|
|
187
|
+
const target = this.calculateAutoResistance(this.data.slope ?? 0);
|
|
188
|
+
if (target !== this.requestedResistance)
|
|
189
|
+
newRequest.targetResistance = target;
|
|
190
|
+
}
|
|
191
|
+
recordAutoUpdate(target) {
|
|
192
|
+
const now = Date.now();
|
|
193
|
+
const stats = this.autoStats;
|
|
194
|
+
this.lastAutoSendTs = now;
|
|
195
|
+
stats.tsStart = stats.tsStart ?? now;
|
|
196
|
+
stats.updatesSent++;
|
|
197
|
+
stats.targetMin = stats.targetMin === undefined ? target : Math.min(stats.targetMin, target);
|
|
198
|
+
stats.targetMax = stats.targetMax === undefined ? target : Math.max(stats.targetMax, target);
|
|
199
|
+
}
|
|
87
200
|
updateData(bikeData, log) {
|
|
88
201
|
const data = super.updateData(bikeData, log);
|
|
202
|
+
if (this.isAutoResistance()) {
|
|
203
|
+
data.gear = this.getVirtualGear();
|
|
204
|
+
data.gearStr = `${data.gear}`;
|
|
205
|
+
return data;
|
|
206
|
+
}
|
|
89
207
|
if (data.resistance !== undefined) {
|
|
90
208
|
data.gear = this.getGear(data.resistance);
|
|
91
209
|
data.gearStr = `${data.gear}`;
|
|
@@ -58,6 +58,7 @@ export default class IncyclistDevice<P extends DeviceProperties> extends EventEm
|
|
|
58
58
|
setCyclingMode(mode: string | ICyclingMode, settings?: any, sendInitCommands?: boolean): void;
|
|
59
59
|
createMode(ModeClass: typeof CyclingMode): ICyclingMode;
|
|
60
60
|
protected createOrGetMode(mode: string | ICyclingMode, useCurrent?: boolean): ICyclingMode;
|
|
61
|
+
protected isModeSupported(_modeName: string): boolean;
|
|
61
62
|
getSupportedCyclingModes(): Array<typeof CyclingMode>;
|
|
62
63
|
getCyclingMode(): ICyclingMode;
|
|
63
64
|
getDefaultCyclingMode(): ICyclingMode;
|
|
@@ -28,6 +28,8 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
28
28
|
protected expectedServices: string[];
|
|
29
29
|
protected matching: string[];
|
|
30
30
|
protected backgroundPaused: boolean;
|
|
31
|
+
protected pauseReasons: Set<string>;
|
|
32
|
+
protected pausedSince?: number;
|
|
31
33
|
protected connectTask: InteruptableTask<TaskState, boolean>;
|
|
32
34
|
protected disconnectTask: InteruptableTask<TaskState, boolean>;
|
|
33
35
|
protected scanTask: InteruptableTask<TaskState, void>;
|
|
@@ -64,8 +66,8 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
64
66
|
scan(props: BleScanProps): Promise<DeviceSettings[]>;
|
|
65
67
|
stopScan(): Promise<boolean>;
|
|
66
68
|
onScanDone(): DeviceSettings[];
|
|
67
|
-
pauseBackgroundActivity(): Promise<void>;
|
|
68
|
-
resumeBackgroundActivity(): Promise<void>;
|
|
69
|
+
pauseBackgroundActivity(reason?: string): Promise<void>;
|
|
70
|
+
resumeBackgroundActivity(reason?: string): Promise<void>;
|
|
69
71
|
pauseLogging(): void;
|
|
70
72
|
resumeLogging(): void;
|
|
71
73
|
isLoggingPaused(): boolean;
|
|
@@ -74,7 +76,7 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
74
76
|
waitForPeripheral(settings: BleDeviceSettings): Promise<IBlePeripheral>;
|
|
75
77
|
createDeviceSetting(service: BlePeripheralAnnouncement): BleDeviceSettings;
|
|
76
78
|
protected reconnect(): Promise<void>;
|
|
77
|
-
protected startPeripheralScan(retry?: boolean): Promise<void>;
|
|
79
|
+
protected startPeripheralScan(retry?: boolean, force?: boolean): Promise<void>;
|
|
78
80
|
protected stopPeripheralScan(): Promise<void>;
|
|
79
81
|
protected emitDisconnectAllPeripherals(): void;
|
|
80
82
|
protected disconnectAllPeripherals(): Promise<void>;
|
|
@@ -103,6 +105,10 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
|
|
|
103
105
|
protected isSupportedPeripheral(service: BlePeripheralAnnouncement): boolean;
|
|
104
106
|
protected find(service: BlePeripheralAnnouncement): Announcement;
|
|
105
107
|
protected getAll(): Announcement[];
|
|
108
|
+
protected isAnnouncementValid(a: {
|
|
109
|
+
ts: number;
|
|
110
|
+
service: BlePeripheralAnnouncement;
|
|
111
|
+
}): boolean;
|
|
106
112
|
setDebug(enabled: boolean): void;
|
|
107
113
|
addKnownDevice(_settings: BleDeviceSettings): void;
|
|
108
114
|
protected connectBle(): Promise<boolean>;
|
|
@@ -28,6 +28,7 @@ export declare class BlePeripheral implements IBlePeripheral {
|
|
|
28
28
|
getInfo(): BleDeviceIdentifier;
|
|
29
29
|
connect(): Promise<boolean>;
|
|
30
30
|
disconnect(connectionLost?: boolean): Promise<boolean>;
|
|
31
|
+
protected doDisconnect(connectionLost: boolean): Promise<boolean>;
|
|
31
32
|
isConnected(): boolean;
|
|
32
33
|
isConnecting(): boolean;
|
|
33
34
|
onDisconnect(callback: () => void): void;
|
|
@@ -6,6 +6,7 @@ import { BleDeviceProperties, BleDeviceSettings, BleStartProperties, IBlePeriphe
|
|
|
6
6
|
import { IAdapter, IncyclistAdapterData, IncyclistBikeData, ITrainer } from '../../types/index.js';
|
|
7
7
|
import { LegacyProfile } from '../../antv2/types.js';
|
|
8
8
|
import { BleZwiftPlaySensor } from '../zwift/play/index.js';
|
|
9
|
+
import FMResistanceMode from '../../modes/fm-resistance.js';
|
|
9
10
|
import { Sport } from '../../types/sport.js';
|
|
10
11
|
export default class BleFmAdapter extends BleAdapter<IndoorBikeData, BleFitnessMachineDevice> implements ITrainer {
|
|
11
12
|
protected static INCYCLIST_PROFILE_NAME: LegacyProfile;
|
|
@@ -16,6 +17,7 @@ export default class BleFmAdapter extends BleAdapter<IndoorBikeData, BleFitnessM
|
|
|
16
17
|
protected promiseSendUpdate: Promise<UpdateRequest | void> | undefined;
|
|
17
18
|
protected zwiftPlay: BleZwiftPlaySensor | undefined;
|
|
18
19
|
protected isHubInitialized: boolean;
|
|
20
|
+
protected setSlopeSkippedLogged: boolean;
|
|
19
21
|
constructor(settings: BleDeviceSettings, props?: BleDeviceProperties);
|
|
20
22
|
getSupportedSports(): Array<Sport>;
|
|
21
23
|
updateSensor(peripheral: IBlePeripheral): void;
|
|
@@ -36,7 +38,11 @@ export default class BleFmAdapter extends BleAdapter<IndoorBikeData, BleFitnessM
|
|
|
36
38
|
protected establishControl(): Promise<boolean>;
|
|
37
39
|
protected sendInitialRequest(): Promise<void>;
|
|
38
40
|
protected updateCyclingModeConfig(): void;
|
|
41
|
+
protected getResistanceMode(): FMResistanceMode | undefined;
|
|
42
|
+
stop(): Promise<boolean>;
|
|
39
43
|
protected checkCapabilities(): Promise<void>;
|
|
44
|
+
protected isModeSupported(modeName: string): boolean;
|
|
45
|
+
protected checkCyclingModeSupported(): void;
|
|
40
46
|
protected updateCapabilitiesFromFeatures(features: IndoorBikeFeatures): void;
|
|
41
47
|
sendUpdate(request: UpdateRequest, enforced?: boolean): Promise<UpdateRequest | void>;
|
|
42
48
|
sendInitCommands(): Promise<boolean>;
|
|
@@ -1,33 +1,43 @@
|
|
|
1
1
|
import { IncyclistDeviceAdapter } from "../base/adpater.js";
|
|
2
2
|
import { IncyclistBikeData } from "../types/index.js";
|
|
3
3
|
import PowerBasedCyclingModeBase from "./power-base.js";
|
|
4
|
-
import ICyclingMode, {
|
|
4
|
+
import ICyclingMode, { CyclingModeConfig, UpdateRequest } from "./types.js";
|
|
5
|
+
type AutoResistanceStats = {
|
|
6
|
+
updatesSent: number;
|
|
7
|
+
updatesFailed: number;
|
|
8
|
+
gearChanges: number;
|
|
9
|
+
targetMin?: number;
|
|
10
|
+
targetMax?: number;
|
|
11
|
+
tsStart?: number;
|
|
12
|
+
};
|
|
5
13
|
export default class FMResistanceMode extends PowerBasedCyclingModeBase implements ICyclingMode {
|
|
6
|
-
protected static config:
|
|
7
|
-
|
|
8
|
-
isSIM: boolean;
|
|
9
|
-
isResistance: boolean;
|
|
10
|
-
name: string;
|
|
11
|
-
description: string;
|
|
12
|
-
properties: {
|
|
13
|
-
key: string;
|
|
14
|
-
name: string;
|
|
15
|
-
description: string;
|
|
16
|
-
type: CyclingModeProperyType;
|
|
17
|
-
default: number;
|
|
18
|
-
min: number;
|
|
19
|
-
max: number;
|
|
20
|
-
}[];
|
|
21
|
-
};
|
|
14
|
+
protected static config: CyclingModeConfig;
|
|
15
|
+
protected static buildProperties(autoDefault: boolean): CyclingModeConfig['properties'];
|
|
22
16
|
protected confirmedResistance?: number;
|
|
23
17
|
protected requestedResistance?: number;
|
|
18
|
+
protected gearOffset: number;
|
|
19
|
+
protected lastAutoSendTs: number;
|
|
20
|
+
protected autoStats: AutoResistanceStats;
|
|
24
21
|
constructor(adapter: IncyclistDeviceAdapter, props?: any);
|
|
22
|
+
setAutoResistanceDefault(autoDefault: boolean): void;
|
|
23
|
+
isAutoResistance(): boolean;
|
|
25
24
|
getBikeInitRequest(): UpdateRequest;
|
|
25
|
+
resetSession(): void;
|
|
26
|
+
registerFailedUpdate(): void;
|
|
27
|
+
logSummary(): void;
|
|
26
28
|
sendBikeUpdate(incoming: UpdateRequest): UpdateRequest;
|
|
27
29
|
checkForResetOrEmpty(request: UpdateRequest): UpdateRequest | undefined;
|
|
28
30
|
protected checkSlope(request: UpdateRequest): void;
|
|
29
31
|
protected checkTargetResistanceSet(request: UpdateRequest, newRequest: UpdateRequest): void;
|
|
30
32
|
protected checkGearDeltaSet(request: UpdateRequest, newRequest: UpdateRequest): void;
|
|
33
|
+
protected getStartGear(): number;
|
|
34
|
+
protected getMaxSlope(): number;
|
|
35
|
+
protected getGearOffset(): number;
|
|
36
|
+
protected getVirtualGear(): number;
|
|
37
|
+
protected calculateAutoResistance(slope: number): number;
|
|
38
|
+
protected checkAutoGearDeltaSet(request: UpdateRequest, newRequest: UpdateRequest): void;
|
|
39
|
+
protected checkAutoResistanceSet(newRequest: UpdateRequest): void;
|
|
40
|
+
protected recordAutoUpdate(target: number): void;
|
|
31
41
|
updateData(bikeData: IncyclistBikeData, log?: boolean): IncyclistBikeData;
|
|
32
42
|
confirmed(request: UpdateRequest): void;
|
|
33
43
|
protected getCurrentResistanceTarget(): number;
|
|
@@ -37,3 +47,4 @@ export default class FMResistanceMode extends PowerBasedCyclingModeBase implemen
|
|
|
37
47
|
protected getGear(resistance: number): number;
|
|
38
48
|
protected calculateTargetResistance(gear: number): number;
|
|
39
49
|
}
|
|
50
|
+
export {};
|
|
@@ -13,6 +13,13 @@ export type ControllerConfig = {
|
|
|
13
13
|
export type UpdateRequestInput = UpdateRequest & {
|
|
14
14
|
enforced?: boolean;
|
|
15
15
|
};
|
|
16
|
+
export type ModeReplacedInfo = {
|
|
17
|
+
requestedMode: string;
|
|
18
|
+
newMode: string;
|
|
19
|
+
};
|
|
20
|
+
export interface AdapterEvents {
|
|
21
|
+
'mode-replaced': (info: ModeReplacedInfo) => void;
|
|
22
|
+
}
|
|
16
23
|
export interface ITrainer {
|
|
17
24
|
setCyclingMode(mode: ICyclingMode | string, settings?: any, sendInitCommands?: boolean): void;
|
|
18
25
|
getSupportedCyclingModes(): Array<typeof CyclingMode>;
|
|
@@ -36,6 +43,10 @@ export interface ISensor {
|
|
|
36
43
|
update(): void;
|
|
37
44
|
}
|
|
38
45
|
export interface IAdapter extends EventEmitter, ITrainer, ISensor {
|
|
46
|
+
on<E extends keyof AdapterEvents>(event: E, listener: AdapterEvents[E]): this;
|
|
47
|
+
on(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
48
|
+
emit<E extends keyof AdapterEvents>(event: E, ...args: Parameters<AdapterEvents[E]>): boolean;
|
|
49
|
+
emit(event: string | symbol, ...args: any[]): boolean;
|
|
39
50
|
getName(): string;
|
|
40
51
|
getID(): string;
|
|
41
52
|
getUniqueName(): string;
|
|
@@ -19,6 +19,6 @@ export interface IncyclistInterface extends EventEmitter {
|
|
|
19
19
|
addKnownDevice?(settings: DeviceSettings): void;
|
|
20
20
|
pauseLogging(): void;
|
|
21
21
|
resumeLogging(): void;
|
|
22
|
-
pauseBackgroundActivity?(): Promise<void>;
|
|
23
|
-
resumeBackgroundActivity?(): Promise<void>;
|
|
22
|
+
pauseBackgroundActivity?(reason?: string): Promise<void>;
|
|
23
|
+
resumeBackgroundActivity?(reason?: string): Promise<void>;
|
|
24
24
|
}
|