incyclist-devices 3.0.42 → 3.0.44
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 +3 -0
- package/lib/cjs/ble/base/adapter.js +26 -5
- package/lib/cjs/ble/base/sensor.js +56 -24
- package/lib/cjs/ble/csc/adapter.js +5 -5
- package/lib/cjs/ble/fm/adapter.js +4 -0
- package/lib/cjs/ble/index.js +2 -0
- package/lib/cjs/ble/openbikecontrol/adapter.js +77 -0
- package/lib/cjs/ble/openbikecontrol/consts.js +6 -0
- package/lib/cjs/ble/openbikecontrol/index.js +18 -0
- package/lib/cjs/ble/openbikecontrol/sensor.js +108 -0
- package/lib/cjs/ble/zwift/play/adapter.js +3 -0
- package/lib/cjs/ble/zwift/play/sensor.js +67 -7
- package/lib/cjs/direct-connect/base/interface.js +153 -14
- package/lib/cjs/direct-connect/base/peripheral.js +176 -19
- package/lib/esm/base/adpater.js +3 -0
- package/lib/esm/ble/base/adapter.js +27 -6
- package/lib/esm/ble/base/sensor.js +57 -25
- package/lib/esm/ble/csc/adapter.js +5 -5
- package/lib/esm/ble/fm/adapter.js +4 -0
- package/lib/esm/ble/index.js +2 -0
- package/lib/esm/ble/openbikecontrol/adapter.js +70 -0
- package/lib/esm/ble/openbikecontrol/consts.js +3 -0
- package/lib/esm/ble/openbikecontrol/index.js +2 -0
- package/lib/esm/ble/openbikecontrol/sensor.js +104 -0
- package/lib/esm/ble/zwift/play/adapter.js +3 -0
- package/lib/esm/ble/zwift/play/sensor.js +64 -4
- package/lib/esm/direct-connect/base/interface.js +155 -16
- package/lib/esm/direct-connect/base/peripheral.js +174 -18
- package/lib/types/base/adpater.d.ts +1 -0
- package/lib/types/ble/base/adapter.d.ts +4 -0
- package/lib/types/ble/base/sensor.d.ts +9 -0
- package/lib/types/ble/csc/adapter.d.ts +1 -1
- package/lib/types/ble/openbikecontrol/adapter.d.ts +23 -0
- package/lib/types/ble/openbikecontrol/consts.d.ts +3 -0
- package/lib/types/ble/openbikecontrol/index.d.ts +2 -0
- package/lib/types/ble/openbikecontrol/sensor.d.ts +25 -0
- package/lib/types/ble/types.d.ts +2 -1
- package/lib/types/ble/zwift/play/adapter.d.ts +1 -0
- package/lib/types/ble/zwift/play/sensor.d.ts +10 -0
- package/lib/types/direct-connect/base/interface.d.ts +9 -0
- package/lib/types/direct-connect/base/peripheral.d.ts +17 -0
- package/lib/types/types/adapter.d.ts +1 -0
- package/package.json +2 -2
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { EventEmitter } from "node:events";
|
|
2
2
|
import { EventLogger } from "gd-eventlog";
|
|
3
|
-
import { sleep } from "../../utils/utils.js";
|
|
3
|
+
import { runWithTimeout, sleep } from "../../utils/utils.js";
|
|
4
4
|
import { beautifyUUID } from "../utils.js";
|
|
5
5
|
export class TBleSensor extends EventEmitter {
|
|
6
6
|
peripheral;
|
|
7
7
|
static protocol;
|
|
8
|
+
static RECONNECT_ATTEMPT_TIMEOUT = 20000;
|
|
8
9
|
logger;
|
|
9
10
|
stopRequested = false;
|
|
10
11
|
subscribeSuccess = false;
|
|
11
12
|
reconnectPromise;
|
|
12
13
|
connectPromise;
|
|
14
|
+
reconnectAttemptTimeout = TBleSensor.RECONNECT_ATTEMPT_TIMEOUT;
|
|
15
|
+
reconnectCount = 0;
|
|
13
16
|
onDataHandler;
|
|
14
17
|
logEvent(event, ...args) {
|
|
15
18
|
try {
|
|
@@ -90,16 +93,22 @@ export class TBleSensor extends EventEmitter {
|
|
|
90
93
|
const selected = this.getRequiredCharacteristics();
|
|
91
94
|
if (selected === null) {
|
|
92
95
|
const res = this.peripheral?.subscribeAll ? await this.peripheral.subscribeAll(this.onDataHandler) : false;
|
|
93
|
-
this.
|
|
94
|
-
return res;
|
|
96
|
+
return this.onSubscribeResult(res);
|
|
95
97
|
}
|
|
96
98
|
if (selected.length === 0) {
|
|
97
99
|
this.subscribeSuccess = true;
|
|
98
100
|
return true;
|
|
99
101
|
}
|
|
100
102
|
const res = await this.peripheral.subscribeSelected(selected, this.onDataHandler);
|
|
101
|
-
this.
|
|
102
|
-
|
|
103
|
+
return this.onSubscribeResult(res);
|
|
104
|
+
}
|
|
105
|
+
onSubscribeResult(res) {
|
|
106
|
+
const success = res && this.isConnected();
|
|
107
|
+
if (res && !success) {
|
|
108
|
+
this.logEvent({ message: 'subscribe failed', reason: 'not connected' });
|
|
109
|
+
}
|
|
110
|
+
this.subscribeSuccess = success;
|
|
111
|
+
return success;
|
|
103
112
|
}
|
|
104
113
|
async stopSensor() {
|
|
105
114
|
this.stopRequested = true;
|
|
@@ -121,41 +130,64 @@ export class TBleSensor extends EventEmitter {
|
|
|
121
130
|
delete this.reconnectPromise;
|
|
122
131
|
return res;
|
|
123
132
|
}
|
|
133
|
+
getReconnectCount() {
|
|
134
|
+
return this.reconnectCount;
|
|
135
|
+
}
|
|
124
136
|
async doReconnectSensor(connectionLost = true) {
|
|
125
137
|
this.onDisconnect();
|
|
138
|
+
this.reconnectCount++;
|
|
126
139
|
const { name, address } = this.peripheral?.getInfo() ?? {};
|
|
127
140
|
this.logEvent({ message: 'reconnect sensor', name, address, connectionLost });
|
|
128
|
-
let connected = false;
|
|
129
|
-
let subscribed = false;
|
|
130
141
|
let success = false;
|
|
131
142
|
if (connectionLost) {
|
|
132
143
|
this.emit('connctionLost');
|
|
133
144
|
}
|
|
134
145
|
await sleep(500);
|
|
135
|
-
|
|
136
|
-
try {
|
|
137
|
-
if (!connected) {
|
|
138
|
-
connected = await this.startSensor(true);
|
|
139
|
-
}
|
|
140
|
-
if (connected && !subscribed) {
|
|
141
|
-
subscribed = await this.subscribe();
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
catch {
|
|
145
|
-
}
|
|
146
|
-
success = connected && subscribed;
|
|
147
|
-
if (!success) {
|
|
148
|
-
await sleep(1000);
|
|
149
|
-
}
|
|
150
|
-
if (!this.stopRequested && !success)
|
|
151
|
-
this.logEvent({ message: 'reconnect sensor retry', name, address });
|
|
152
|
-
} while (!success || this.stopRequested);
|
|
146
|
+
success = await this.reconnectUntilDone(name, address);
|
|
153
147
|
if (connectionLost && success) {
|
|
154
148
|
this.emit('connctionRecovered');
|
|
155
149
|
}
|
|
156
150
|
this.logEvent({ message: 'reconnect sensor completed', success, stopRequested: this.stopRequested });
|
|
157
151
|
return success;
|
|
158
152
|
}
|
|
153
|
+
async reconnectUntilDone(name, address) {
|
|
154
|
+
const success = await this.reconnectAttempt(name, address);
|
|
155
|
+
if (success || this.stopRequested)
|
|
156
|
+
return success;
|
|
157
|
+
await sleep(1000);
|
|
158
|
+
if (this.stopRequested)
|
|
159
|
+
return false;
|
|
160
|
+
this.logEvent({ message: 'reconnect sensor retry', name, address });
|
|
161
|
+
return this.reconnectUntilDone(name, address);
|
|
162
|
+
}
|
|
163
|
+
async reconnectAttempt(name, address) {
|
|
164
|
+
try {
|
|
165
|
+
return await runWithTimeout(this.connectAndSubscribe(), this.reconnectAttemptTimeout);
|
|
166
|
+
}
|
|
167
|
+
catch (err) {
|
|
168
|
+
if (err?.message === 'Timeout') {
|
|
169
|
+
await this.abortReconnectAttempt(name, address);
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
async connectAndSubscribe() {
|
|
175
|
+
const connected = await this.startSensor(true);
|
|
176
|
+
if (!connected)
|
|
177
|
+
return false;
|
|
178
|
+
const subscribed = await this.subscribe();
|
|
179
|
+
return subscribed && this.isConnected();
|
|
180
|
+
}
|
|
181
|
+
async abortReconnectAttempt(name, address) {
|
|
182
|
+
this.logEvent({ message: 'reconnect sensor attempt timeout', name, address, timeout: this.reconnectAttemptTimeout });
|
|
183
|
+
delete this.connectPromise;
|
|
184
|
+
this.onDisconnect();
|
|
185
|
+
try {
|
|
186
|
+
await runWithTimeout(this.peripheral.disconnect(), this.reconnectAttemptTimeout);
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
}
|
|
190
|
+
}
|
|
159
191
|
reset() {
|
|
160
192
|
throw new Error("Method not implemented.");
|
|
161
193
|
}
|
|
@@ -22,10 +22,10 @@ export class BleCSCAdapter extends BleAdapter {
|
|
|
22
22
|
if (!sensor)
|
|
23
23
|
return;
|
|
24
24
|
const features = await sensor.getFeatures();
|
|
25
|
-
if (!features
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
if (!features || (features.wheelRevolutionData === undefined && features.crankRevolutionData === undefined))
|
|
26
|
+
return;
|
|
27
|
+
this.capabilities = BleCSCAdapter.CAPABILITIES.filter(c => (c !== IncyclistCapability.Speed || features.wheelRevolutionData) &&
|
|
28
|
+
(c !== IncyclistCapability.Cadence || features.crankRevolutionData));
|
|
29
29
|
const after = this.capabilities.join(',');
|
|
30
30
|
if (before !== after) {
|
|
31
31
|
this.logEvent({ message: 'device capabilities updated', name: this.getSettings().name, interface: this.getSettings().interface, capabilities: this.capabilities });
|
|
@@ -48,7 +48,7 @@ export class BleCSCAdapter extends BleAdapter {
|
|
|
48
48
|
}
|
|
49
49
|
mapData(deviceData) {
|
|
50
50
|
const { cadence, speed = 0 } = deviceData;
|
|
51
|
-
return { cadence, speed };
|
|
51
|
+
return { cadence, pedalRpm: cadence, speed };
|
|
52
52
|
}
|
|
53
53
|
transformData(bikeData) {
|
|
54
54
|
if (bikeData === undefined)
|
|
@@ -337,6 +337,10 @@ export default class BleFmAdapter extends BleAdapter {
|
|
|
337
337
|
return;
|
|
338
338
|
try {
|
|
339
339
|
const update = this.getCyclingMode().buildUpdate(request);
|
|
340
|
+
if (this.isReconnecting()) {
|
|
341
|
+
this.logEvent({ message: 'send bike update skipped', reason: 'reconnect in progress', update, request });
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
340
344
|
this.logEvent({ message: 'send bike update requested', profile: this.getProfile(), mode: this.getCyclingMode()?.getName(), update, request });
|
|
341
345
|
const device = this.getSensor();
|
|
342
346
|
if (this.hasCapability(IncyclistCapability.Control)) {
|
package/lib/esm/ble/index.js
CHANGED
|
@@ -8,6 +8,7 @@ import { BleInterface, BleInterfaceFactory } from '../ble/base/interface.js';
|
|
|
8
8
|
import { BleMultiTransportInterfaceFactory, BleAdapterFactory } from './factories/index.js';
|
|
9
9
|
import { BleCSCAdapter, BleCyclingSpeedCadenceDevice } from './csc/index.js';
|
|
10
10
|
import { BleZwiftPlaySensor, ZwiftPlayAdapter } from './zwift/play/index.js';
|
|
11
|
+
import { BleOpenBikeControlSensor, OpenBikeControlAdapter } from './openbikecontrol/index.js';
|
|
11
12
|
['ble', 'wifi'].forEach(i => {
|
|
12
13
|
const af = BleAdapterFactory.getInstance(i);
|
|
13
14
|
af.register('hr', BleHrmAdapter, BleHrmComms);
|
|
@@ -18,6 +19,7 @@ import { BleZwiftPlaySensor, ZwiftPlayAdapter } from './zwift/play/index.js';
|
|
|
18
19
|
af.register('bkool', BleBkoolAdapter, BleBkoolComms);
|
|
19
20
|
af.register('csc', BleCSCAdapter, BleCyclingSpeedCadenceDevice);
|
|
20
21
|
af.register('zwift-play', ZwiftPlayAdapter, BleZwiftPlaySensor);
|
|
22
|
+
af.register('openbikecontrol', OpenBikeControlAdapter, BleOpenBikeControlSensor);
|
|
21
23
|
});
|
|
22
24
|
BleMultiTransportInterfaceFactory.register('ble', BleInterfaceFactory);
|
|
23
25
|
export * from './utils.js';
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { EventLogger } from 'gd-eventlog';
|
|
2
|
+
import BleAdapter from '../base/adapter.js';
|
|
3
|
+
import { BleOpenBikeControlSensor } from './sensor.js';
|
|
4
|
+
import { IncyclistCapability } from '../../types/index.js';
|
|
5
|
+
export class OpenBikeControlAdapter extends BleAdapter {
|
|
6
|
+
static INCYCLIST_PROFILE_NAME = 'Controller';
|
|
7
|
+
static CAPABILITIES = [IncyclistCapability.AppControl];
|
|
8
|
+
keyPressedHandler;
|
|
9
|
+
constructor(settings, props) {
|
|
10
|
+
super(settings, props);
|
|
11
|
+
this.logger = new EventLogger('OpenBikeControl');
|
|
12
|
+
this.device = new BleOpenBikeControlSensor(this.getPeripheral(), { logger: this.logger });
|
|
13
|
+
this.capabilities = OpenBikeControlAdapter.CAPABILITIES;
|
|
14
|
+
}
|
|
15
|
+
async checkCapabilities() {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
async startSensor() {
|
|
19
|
+
try {
|
|
20
|
+
let connected = await super.startSensor();
|
|
21
|
+
if (connected) {
|
|
22
|
+
const sensor = this.getSensor();
|
|
23
|
+
if (this.keyPressedHandler) {
|
|
24
|
+
sensor.off('key-pressed', this.keyPressedHandler);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
this.keyPressedHandler = this.onKeyPressed.bind(this);
|
|
28
|
+
}
|
|
29
|
+
sensor.on('key-pressed', this.keyPressedHandler);
|
|
30
|
+
}
|
|
31
|
+
return connected;
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
this.logEvent({ message: 'error', fn: 'startSensor', error: err.message, stack: err.stack });
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
onKeyPressed(event) {
|
|
39
|
+
this.emit('key-pressed', this.getSettings(), event);
|
|
40
|
+
}
|
|
41
|
+
isEqual(settings) {
|
|
42
|
+
return super.isEqual(settings) && settings.address == this.settings.address;
|
|
43
|
+
}
|
|
44
|
+
isSame(adapter) {
|
|
45
|
+
if (!(adapter instanceof OpenBikeControlAdapter))
|
|
46
|
+
return false;
|
|
47
|
+
return this.isEqual(adapter.settings);
|
|
48
|
+
}
|
|
49
|
+
updateSensor(peripheral) {
|
|
50
|
+
this.device = new BleOpenBikeControlSensor(peripheral, { logger: this.logger });
|
|
51
|
+
}
|
|
52
|
+
getProfile() {
|
|
53
|
+
return OpenBikeControlAdapter.INCYCLIST_PROFILE_NAME;
|
|
54
|
+
}
|
|
55
|
+
getName() {
|
|
56
|
+
const settings = this.settings;
|
|
57
|
+
const id = (settings.id ?? settings.address ?? '').replace(/[:\-.]/g, '');
|
|
58
|
+
const addressHash = id.length > 4 ? id.slice(-4).toUpperCase() : id.toUpperCase();
|
|
59
|
+
return addressHash ? `${settings.name} ${addressHash}` : settings.name;
|
|
60
|
+
}
|
|
61
|
+
getDisplayName() {
|
|
62
|
+
return this.getName();
|
|
63
|
+
}
|
|
64
|
+
mapData(deviceData) {
|
|
65
|
+
return {};
|
|
66
|
+
}
|
|
67
|
+
shouldWaitForData() {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { TBleSensor } from "../base/sensor.js";
|
|
2
|
+
import { matches } from "../utils.js";
|
|
3
|
+
import { OBC_APP_INFO, OBC_BUTTON_STATE, OBC_SERVICE } from "./consts.js";
|
|
4
|
+
const MSG_BUTTON_STATE = 0x01;
|
|
5
|
+
const MSG_APP_INFO = 0x04;
|
|
6
|
+
const BUTTON_SHIFT_UP = 0x01;
|
|
7
|
+
const BUTTON_SHIFT_DOWN = 0x02;
|
|
8
|
+
const BUTTON_INCREASE_DIFFICULTY = 0x30;
|
|
9
|
+
const BUTTON_DECREASE_DIFFICULTY = 0x31;
|
|
10
|
+
const BUTTON_SKIP_INTERVAL = 0x32;
|
|
11
|
+
const BUTTON_PREVIOUS_INTERVAL = 0x36;
|
|
12
|
+
const BUTTON_KEYS = new Map([
|
|
13
|
+
[BUTTON_SHIFT_UP, 'r-shift-up'],
|
|
14
|
+
[BUTTON_SHIFT_DOWN, 'r-shift-down'],
|
|
15
|
+
[BUTTON_INCREASE_DIFFICULTY, 'y'],
|
|
16
|
+
[BUTTON_DECREASE_DIFFICULTY, 'b'],
|
|
17
|
+
[BUTTON_SKIP_INTERVAL, 'a'],
|
|
18
|
+
[BUTTON_PREVIOUS_INTERVAL, 'z'],
|
|
19
|
+
]);
|
|
20
|
+
export class BleOpenBikeControlSensor extends TBleSensor {
|
|
21
|
+
static profile = 'Controller';
|
|
22
|
+
static protocol = 'openbikecontrol';
|
|
23
|
+
static services = [OBC_SERVICE];
|
|
24
|
+
static characteristics = [];
|
|
25
|
+
static detectionPriority = 0;
|
|
26
|
+
buttonStates = new Map();
|
|
27
|
+
onDisconnect() {
|
|
28
|
+
super.onDisconnect();
|
|
29
|
+
this.reset();
|
|
30
|
+
}
|
|
31
|
+
async subscribe() {
|
|
32
|
+
const subscribed = await super.subscribe();
|
|
33
|
+
if (subscribed)
|
|
34
|
+
await this.sendAppInfo();
|
|
35
|
+
return subscribed;
|
|
36
|
+
}
|
|
37
|
+
getRequiredCharacteristics() {
|
|
38
|
+
return [OBC_BUTTON_STATE];
|
|
39
|
+
}
|
|
40
|
+
onData(characteristic, data) {
|
|
41
|
+
if (matches(characteristic, OBC_BUTTON_STATE)) {
|
|
42
|
+
this.onButtonState(Buffer.from(data));
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
onButtonState(data) {
|
|
47
|
+
try {
|
|
48
|
+
if (data.length < 3 || data[0] !== MSG_BUTTON_STATE)
|
|
49
|
+
return;
|
|
50
|
+
const press = { buttons: [], pressed: [], timestamp: Date.now() };
|
|
51
|
+
const released = [];
|
|
52
|
+
for (let i = 1; i + 1 < data.length; i += 2) {
|
|
53
|
+
const buttonId = data[i];
|
|
54
|
+
if (!BUTTON_KEYS.has(buttonId))
|
|
55
|
+
continue;
|
|
56
|
+
const pressed = data[i + 1] !== 0x00;
|
|
57
|
+
const prev = this.buttonStates.get(buttonId);
|
|
58
|
+
if (pressed && !prev) {
|
|
59
|
+
press.buttons.push(buttonId);
|
|
60
|
+
press.pressed.push(buttonId);
|
|
61
|
+
this.buttonStates.set(buttonId, press);
|
|
62
|
+
}
|
|
63
|
+
else if (!pressed && prev) {
|
|
64
|
+
this.buttonStates.delete(buttonId);
|
|
65
|
+
prev.pressed = prev.pressed.filter(id => id !== buttonId);
|
|
66
|
+
if (prev.pressed.length === 0)
|
|
67
|
+
released.push(prev);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
released.forEach(p => {
|
|
71
|
+
const key = this.getKey(p.buttons);
|
|
72
|
+
const duration = Date.now() - p.timestamp;
|
|
73
|
+
const { name, address } = this.peripheral?.getInfo() ?? {};
|
|
74
|
+
this.logEvent({ message: 'key pressed', key, name, address, duration });
|
|
75
|
+
this.emit('key-pressed', { key, duration });
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
this.logEvent({ message: 'error', fn: 'onButtonState', error: err.message, stack: err.stack });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
getKey(buttons) {
|
|
83
|
+
if (buttons.includes(BUTTON_SHIFT_UP) && buttons.includes(BUTTON_INCREASE_DIFFICULTY))
|
|
84
|
+
return 'up';
|
|
85
|
+
if (buttons.includes(BUTTON_SHIFT_DOWN) && buttons.includes(BUTTON_DECREASE_DIFFICULTY))
|
|
86
|
+
return 'down';
|
|
87
|
+
const buttonId = Array.from(BUTTON_KEYS.keys()).find(id => buttons.includes(id));
|
|
88
|
+
return BUTTON_KEYS.get(buttonId);
|
|
89
|
+
}
|
|
90
|
+
async sendAppInfo() {
|
|
91
|
+
try {
|
|
92
|
+
const appId = Buffer.from('incyclist');
|
|
93
|
+
const buttons = Array.from(BUTTON_KEYS.keys());
|
|
94
|
+
const message = Buffer.from([MSG_APP_INFO, 0x01, appId.length, ...appId, 0x00, buttons.length, ...buttons]);
|
|
95
|
+
await this.write(OBC_APP_INFO, message, { withoutResponse: true });
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
this.logEvent({ message: 'warning', fn: 'sendAppInfo', error: err.message });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
reset() {
|
|
102
|
+
this.buttonStates = new Map();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ClickKeyPadStatus, DeviceDataEnvelope, DeviceInformationContent, DeviceSettings, HubCommand, HubRequest, HubRidingData, Idle, PlayButtonStatus, RideKeyPadStatus, TrainerResponse } from "../../../proto/zwift_hub.js";
|
|
2
2
|
import { TBleSensor } from "../../base/sensor.js";
|
|
3
|
+
import { sleep } from "../../../utils/utils.js";
|
|
3
4
|
import { beautifyUUID, fullUUID } from "../../utils.js";
|
|
4
5
|
import { EventEmitter } from "node:events";
|
|
5
6
|
import { BindingsFactory } from "../../../bindings/index.js";
|
|
@@ -33,31 +34,84 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
33
34
|
prevcWax10000;
|
|
34
35
|
pendingMeasurement;
|
|
35
36
|
measurementReassemblyErrorLogged = false;
|
|
37
|
+
static RECONNECT_PAIR_ATTEMPTS = 3;
|
|
38
|
+
static RECONNECT_PAIR_RETRY_DELAY = 1000;
|
|
39
|
+
owner;
|
|
40
|
+
ownerReconnectCount = 0;
|
|
41
|
+
reconnectAndPairPromise;
|
|
36
42
|
constructor(peripheral, props) {
|
|
37
43
|
if (props?.isTrainer && peripheral) {
|
|
38
44
|
const sensor = peripheral;
|
|
39
45
|
super(sensor.getPeripheral(), props);
|
|
40
46
|
this.isFM = true;
|
|
47
|
+
this.owner = sensor;
|
|
41
48
|
}
|
|
42
49
|
else if (peripheral &&
|
|
43
50
|
'startSensor' in peripheral && typeof peripheral.startSensor === 'function' &&
|
|
44
51
|
'getPeripheral' in peripheral && typeof peripheral.getPeripheral === 'function') {
|
|
45
52
|
super(peripheral.getPeripheral(), props);
|
|
46
53
|
this.isFM = true;
|
|
54
|
+
this.owner = peripheral;
|
|
47
55
|
}
|
|
48
56
|
else {
|
|
49
57
|
super(peripheral, props);
|
|
50
58
|
this.isFM = false;
|
|
51
59
|
}
|
|
60
|
+
this.ownerReconnectCount = this.owner?.getReconnectCount?.() ?? 0;
|
|
52
61
|
this.emitter = new EventEmitter();
|
|
53
62
|
this.setInitialState();
|
|
54
63
|
}
|
|
55
64
|
async reconnectSensor() {
|
|
56
|
-
this.
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
this.reconnectAndPairPromise = this.reconnectAndPairPromise ?? this.reconnectAndPair();
|
|
66
|
+
const promise = this.reconnectAndPairPromise;
|
|
67
|
+
const reconnected = await promise;
|
|
68
|
+
if (this.reconnectAndPairPromise === promise)
|
|
69
|
+
delete this.reconnectAndPairPromise;
|
|
59
70
|
return reconnected;
|
|
60
71
|
}
|
|
72
|
+
isReconnectBusy() {
|
|
73
|
+
return super.isReconnectBusy() || this.reconnectAndPairPromise !== undefined || this.isOwnerReconnecting();
|
|
74
|
+
}
|
|
75
|
+
isOwnerReconnecting() {
|
|
76
|
+
return this.owner?.isReconnectBusy?.() ?? false;
|
|
77
|
+
}
|
|
78
|
+
async reconnectAndPair() {
|
|
79
|
+
this.setInitialState();
|
|
80
|
+
const reconnected = await super.reconnectSensor();
|
|
81
|
+
if (!reconnected)
|
|
82
|
+
return false;
|
|
83
|
+
const paired = await this.pairWithRetry(1);
|
|
84
|
+
if (paired === undefined) {
|
|
85
|
+
const { name, address } = this.peripheral?.getInfo() ?? {};
|
|
86
|
+
this.logEvent({ message: 'reconnect sensor pairing failed', name, address, attempts: BleZwiftPlaySensor.RECONNECT_PAIR_ATTEMPTS });
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
return paired;
|
|
90
|
+
}
|
|
91
|
+
async pairWithRetry(attempt) {
|
|
92
|
+
if (this.stopRequested)
|
|
93
|
+
return false;
|
|
94
|
+
if (await this.pair())
|
|
95
|
+
return true;
|
|
96
|
+
if (attempt >= BleZwiftPlaySensor.RECONNECT_PAIR_ATTEMPTS)
|
|
97
|
+
return undefined;
|
|
98
|
+
await sleep(BleZwiftPlaySensor.RECONNECT_PAIR_RETRY_DELAY);
|
|
99
|
+
return this.pairWithRetry(attempt + 1);
|
|
100
|
+
}
|
|
101
|
+
canSendHubCommand(fn) {
|
|
102
|
+
if (!this.owner)
|
|
103
|
+
return true;
|
|
104
|
+
const reconnectCount = this.owner.getReconnectCount?.() ?? 0;
|
|
105
|
+
if (reconnectCount !== this.ownerReconnectCount) {
|
|
106
|
+
this.ownerReconnectCount = reconnectCount;
|
|
107
|
+
this.setInitialState();
|
|
108
|
+
}
|
|
109
|
+
if (this.isOwnerReconnecting()) {
|
|
110
|
+
this.logger.logEvent({ message: 'warning', fn, reason: 'reconnect in progress', skipped: true });
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
61
115
|
stopSensor() {
|
|
62
116
|
this.emitter.removeAllListeners();
|
|
63
117
|
this.setInitialState();
|
|
@@ -120,6 +174,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
120
174
|
}
|
|
121
175
|
async setSimulationData(data) {
|
|
122
176
|
try {
|
|
177
|
+
if (!this.canSendHubCommand('setSimulationData'))
|
|
178
|
+
return;
|
|
123
179
|
if (!this.isHubServiceActive) {
|
|
124
180
|
await this.initHubService(false);
|
|
125
181
|
}
|
|
@@ -158,6 +214,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
158
214
|
}
|
|
159
215
|
async setGearRatio(gearRatio) {
|
|
160
216
|
try {
|
|
217
|
+
if (!this.canSendHubCommand('setGearRatio'))
|
|
218
|
+
return;
|
|
161
219
|
if (!this.isHubServiceActive) {
|
|
162
220
|
await this.initHubService();
|
|
163
221
|
}
|
|
@@ -267,6 +325,8 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
267
325
|
}
|
|
268
326
|
}
|
|
269
327
|
async initHubService(setSimulation = true) {
|
|
328
|
+
if (!this.canSendHubCommand('initHubService'))
|
|
329
|
+
return false;
|
|
270
330
|
if (!this.isHubServiceActive && this.initHubServicePromise !== undefined) {
|
|
271
331
|
await this.initHubServicePromise;
|
|
272
332
|
}
|
|
@@ -571,7 +631,7 @@ export class BleZwiftPlaySensor extends TBleSensor {
|
|
|
571
631
|
const paired = await this.pairPromise;
|
|
572
632
|
this.pairPromise = undefined;
|
|
573
633
|
if (paired)
|
|
574
|
-
return;
|
|
634
|
+
return true;
|
|
575
635
|
}
|
|
576
636
|
let manufacturerData;
|
|
577
637
|
try {
|