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
|
@@ -9,9 +9,14 @@ const index_js_1 = require("../../ble/index.js");
|
|
|
9
9
|
const types_js_1 = require("../../ble/base/types.js");
|
|
10
10
|
const consts_js_1 = require("../../ble/consts.js");
|
|
11
11
|
const consts_js_2 = require("../../ble/wahoo/consts.js");
|
|
12
|
+
const consts_js_3 = require("../../ble/openbikecontrol/consts.js");
|
|
12
13
|
const DC_TYPE = 'wahoo-fitness-tnp';
|
|
13
14
|
const DC_PORT = 36866;
|
|
14
15
|
const DC_EXPIRATION_TIMEOUT = 10 * 1000 * 60;
|
|
16
|
+
const DC_PROBE_TIMEOUT = 2000;
|
|
17
|
+
const DC_REPROBE_INTERVAL = 5000;
|
|
18
|
+
const DC_WAIT_LIFETIME = 5 * 60 * 1000;
|
|
19
|
+
const OBC_TYPE = 'openbikecontrol';
|
|
15
20
|
let instanceId = 0;
|
|
16
21
|
class DirectConnectInterface extends node_events_1.EventEmitter {
|
|
17
22
|
static _instance;
|
|
@@ -28,6 +33,7 @@ class DirectConnectInterface extends node_events_1.EventEmitter {
|
|
|
28
33
|
matching = [];
|
|
29
34
|
instance;
|
|
30
35
|
connected = false;
|
|
36
|
+
pendingWaits = new Map();
|
|
31
37
|
backgroundPaused = false;
|
|
32
38
|
static getInstance(props = {}) {
|
|
33
39
|
if (DirectConnectInterface._instance === undefined)
|
|
@@ -65,6 +71,8 @@ class DirectConnectInterface extends node_events_1.EventEmitter {
|
|
|
65
71
|
this.props = props;
|
|
66
72
|
}
|
|
67
73
|
createPeripheral(announcement) {
|
|
74
|
+
if (announcement.type === OBC_TYPE)
|
|
75
|
+
return peripheral_js_1.OpenBikeControlPeripheral.create(announcement);
|
|
68
76
|
return peripheral_js_1.DirectConnectPeripheral.create(announcement);
|
|
69
77
|
}
|
|
70
78
|
getSupportedServices() {
|
|
@@ -151,12 +159,20 @@ class DirectConnectInterface extends node_events_1.EventEmitter {
|
|
|
151
159
|
this.getBinding().mdns.find({ type: DC_TYPE }, (service) => {
|
|
152
160
|
this.addService(service, DC_TYPE);
|
|
153
161
|
});
|
|
162
|
+
this.getBinding().mdns.find({ type: OBC_TYPE }, (service) => {
|
|
163
|
+
if (service.type && service.type !== OBC_TYPE)
|
|
164
|
+
return;
|
|
165
|
+
if (service.serviceUUIDs?.length && !service.serviceUUIDs.some(uuid => (0, index_js_1.matches)(uuid, consts_js_3.OBC_SERVICE)))
|
|
166
|
+
return;
|
|
167
|
+
this.addService({ ...service, type: OBC_TYPE }, OBC_TYPE);
|
|
168
|
+
});
|
|
154
169
|
}
|
|
155
170
|
async pauseBackgroundActivity() {
|
|
156
171
|
if (this.backgroundPaused)
|
|
157
172
|
return;
|
|
158
173
|
this.backgroundPaused = true;
|
|
159
174
|
this.logEvent({ message: 'pausing background activity' });
|
|
175
|
+
this.cancelWaits();
|
|
160
176
|
try {
|
|
161
177
|
this.getBinding()?.mdns?.disconnect();
|
|
162
178
|
}
|
|
@@ -238,22 +254,134 @@ class DirectConnectInterface extends node_events_1.EventEmitter {
|
|
|
238
254
|
return this.logDisabled;
|
|
239
255
|
}
|
|
240
256
|
waitForPeripheral(settings) {
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
257
|
+
const key = `${settings.name}|${settings.address ?? ''}`;
|
|
258
|
+
const pending = this.pendingWaits.get(key);
|
|
259
|
+
if (pending)
|
|
260
|
+
return pending.promise;
|
|
261
|
+
let cancel = () => { };
|
|
262
|
+
const promise = new Promise((resolve, reject) => {
|
|
263
|
+
cancel = this.runWait(key, settings, resolve, reject);
|
|
264
|
+
});
|
|
265
|
+
this.pendingWaits.set(key, { promise, cancel });
|
|
266
|
+
return promise;
|
|
267
|
+
}
|
|
268
|
+
cancelWait(settings) {
|
|
269
|
+
const key = `${settings.name}|${settings.address ?? ''}`;
|
|
270
|
+
this.pendingWaits.get(key)?.cancel();
|
|
271
|
+
}
|
|
272
|
+
runWait(key, settings, resolve, reject) {
|
|
273
|
+
const start = Date.now();
|
|
274
|
+
let probes = 0;
|
|
275
|
+
let lastResult;
|
|
276
|
+
let finished = false;
|
|
277
|
+
let timer;
|
|
278
|
+
const cleanup = () => {
|
|
279
|
+
finished = true;
|
|
280
|
+
if (timer)
|
|
281
|
+
clearTimeout(timer);
|
|
282
|
+
this.off('device', onDevice);
|
|
283
|
+
this.pendingWaits.delete(key);
|
|
284
|
+
};
|
|
285
|
+
const succeed = (peripheral, source) => {
|
|
286
|
+
if (finished)
|
|
287
|
+
return;
|
|
288
|
+
cleanup();
|
|
289
|
+
this.logEvent({ message: 'wifi device reachable', device: settings.name, address: settings.address ?? '', source, waitedMs: Date.now() - start, probes });
|
|
290
|
+
resolve(peripheral);
|
|
291
|
+
};
|
|
292
|
+
const fail = (reason) => {
|
|
293
|
+
if (finished)
|
|
294
|
+
return;
|
|
295
|
+
cleanup();
|
|
296
|
+
reject(new Error(`wifi device wait ${reason}`));
|
|
297
|
+
};
|
|
298
|
+
const onDevice = (device) => {
|
|
299
|
+
if (finished || device.name !== settings.name)
|
|
300
|
+
return;
|
|
301
|
+
const peripheral = this.createPeripheralFromSettings(settings);
|
|
302
|
+
if (peripheral)
|
|
303
|
+
succeed(peripheral, 'mdns');
|
|
304
|
+
};
|
|
305
|
+
const attempt = async (trigger) => {
|
|
306
|
+
if (finished)
|
|
307
|
+
return;
|
|
308
|
+
const entry = this.findEntry(settings);
|
|
309
|
+
if (entry) {
|
|
310
|
+
const { address, port } = entry.service;
|
|
311
|
+
const t0 = Date.now();
|
|
312
|
+
const result = await this.probePort(address, port);
|
|
313
|
+
if (finished)
|
|
314
|
+
return;
|
|
315
|
+
probes++;
|
|
316
|
+
if (result !== lastResult) {
|
|
317
|
+
lastResult = result;
|
|
318
|
+
this.logEvent({ message: 'wifi device probe', device: settings.name, address, port, result, durationMs: Date.now() - t0, trigger });
|
|
319
|
+
}
|
|
320
|
+
if (result === 'open' || result === 'skipped') {
|
|
321
|
+
entry.ts = Date.now();
|
|
247
322
|
const peripheral = this.createPeripheralFromSettings(settings);
|
|
248
|
-
if (peripheral)
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
323
|
+
if (peripheral)
|
|
324
|
+
return succeed(peripheral, 'probe');
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (Date.now() - start >= DC_WAIT_LIFETIME) {
|
|
328
|
+
this.logEvent({ message: 'wifi device wait abandoned', device: settings.name, address: settings.address ?? '', waitedMs: Date.now() - start, probes });
|
|
329
|
+
fail('abandoned');
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
timer = setTimeout(() => { void attempt('reprobe'); }, DC_REPROBE_INTERVAL);
|
|
333
|
+
};
|
|
334
|
+
this.on('device', onDevice);
|
|
335
|
+
void attempt('start');
|
|
336
|
+
return () => fail('cancelled');
|
|
337
|
+
}
|
|
338
|
+
probePort(address, port, timeout = DC_PROBE_TIMEOUT) {
|
|
339
|
+
const net = this.getBinding()?.net;
|
|
340
|
+
if (!net?.createSocket || !address)
|
|
341
|
+
return Promise.resolve('skipped');
|
|
342
|
+
return new Promise(resolve => {
|
|
343
|
+
let socket;
|
|
344
|
+
let timer;
|
|
345
|
+
let settled = false;
|
|
346
|
+
const done = (result) => {
|
|
347
|
+
if (settled)
|
|
348
|
+
return;
|
|
349
|
+
settled = true;
|
|
350
|
+
if (timer)
|
|
351
|
+
clearTimeout(timer);
|
|
352
|
+
try {
|
|
353
|
+
socket?.removeAllListeners();
|
|
354
|
+
socket?.on('error', () => { });
|
|
355
|
+
socket?.destroy();
|
|
252
356
|
}
|
|
357
|
+
catch { }
|
|
358
|
+
resolve(result);
|
|
253
359
|
};
|
|
254
|
-
|
|
360
|
+
try {
|
|
361
|
+
socket = net.createSocket();
|
|
362
|
+
socket.once('connect', () => done('open'));
|
|
363
|
+
socket.once('error', (err) => done(err?.code === 'ETIMEDOUT' ? 'timeout' : 'closed'));
|
|
364
|
+
socket.once('timeout', () => done('timeout'));
|
|
365
|
+
timer = setTimeout(() => done('timeout'), timeout);
|
|
366
|
+
socket.connect(port, address);
|
|
367
|
+
}
|
|
368
|
+
catch (err) {
|
|
369
|
+
this.logError(err, 'probePort');
|
|
370
|
+
done('error');
|
|
371
|
+
}
|
|
255
372
|
});
|
|
256
373
|
}
|
|
374
|
+
findEntry(settings) {
|
|
375
|
+
const bleSettings = settings;
|
|
376
|
+
const all = this.getAll();
|
|
377
|
+
return (bleSettings.address && all.find(a => a.service.name === settings.name && a.service.address === bleSettings.address))
|
|
378
|
+
|| all.find(a => a.service.name === settings.name);
|
|
379
|
+
}
|
|
380
|
+
cancelWaits() {
|
|
381
|
+
const waits = Array.from(this.pendingWaits.values());
|
|
382
|
+
waits.forEach(w => w.cancel());
|
|
383
|
+
this.pendingWaits.clear();
|
|
384
|
+
}
|
|
257
385
|
async reconnect() {
|
|
258
386
|
await this.disconnect();
|
|
259
387
|
await this.connect(true);
|
|
@@ -270,7 +398,7 @@ class DirectConnectInterface extends node_events_1.EventEmitter {
|
|
|
270
398
|
emitCachedDevices() {
|
|
271
399
|
const announced = this.getAll();
|
|
272
400
|
this.matching = announced.map(a => a.service.name);
|
|
273
|
-
announced.forEach(a => {
|
|
401
|
+
announced.filter(a => a.source !== 'known-device').forEach(a => {
|
|
274
402
|
this.emitDevice(a.service);
|
|
275
403
|
});
|
|
276
404
|
}
|
|
@@ -287,6 +415,8 @@ class DirectConnectInterface extends node_events_1.EventEmitter {
|
|
|
287
415
|
const src = source === 'known-device' ? 'known-device' : 'mdns';
|
|
288
416
|
try {
|
|
289
417
|
service.transport = this.getName();
|
|
418
|
+
if (service.type === OBC_TYPE && !service.serviceUUIDs?.length)
|
|
419
|
+
service.serviceUUIDs = [consts_js_3.OBC_SERVICE];
|
|
290
420
|
const sameAddress = this.findByNameAndAddress(service.name, service.address);
|
|
291
421
|
if (sameAddress) {
|
|
292
422
|
this.refreshSameAddressEntry(sameAddress, service, src, source);
|
|
@@ -315,16 +445,25 @@ class DirectConnectInterface extends node_events_1.EventEmitter {
|
|
|
315
445
|
const idx = this.services.indexOf(sameAddress);
|
|
316
446
|
const wasKnownDevicePlaceholder = sameAddress.source === 'known-device';
|
|
317
447
|
const nextSource = (wasKnownDevicePlaceholder && src !== 'known-device') ? src : sameAddress.source;
|
|
318
|
-
|
|
448
|
+
const hadServices = sameAddress.service.serviceUUIDs?.length > 0;
|
|
449
|
+
const hasServices = service.serviceUUIDs?.length > 0;
|
|
450
|
+
this.services[idx] = { ts: Date.now(), service: (hadServices && !hasServices) ? sameAddress.service : service, source: nextSource };
|
|
319
451
|
if (src !== 'known-device' && wasKnownDevicePlaceholder) {
|
|
320
452
|
this.logEvent({ message: 'device re-announced', device: service.name, announcement: service, source });
|
|
321
453
|
this.emitDevice(service);
|
|
322
454
|
}
|
|
455
|
+
else if (!hadServices && hasServices) {
|
|
456
|
+
this.logEvent({ message: 'device announced', device: service.name, announcement: service, source });
|
|
457
|
+
this.emitDevice(service);
|
|
458
|
+
this.matching?.push(service.name);
|
|
459
|
+
}
|
|
323
460
|
}
|
|
324
461
|
announceNewEntry(service, src, source, message) {
|
|
325
462
|
this.services.push({ ts: Date.now(), service, source: src });
|
|
326
463
|
if (!service.serviceUUIDs?.length)
|
|
327
464
|
return;
|
|
465
|
+
if (src === 'known-device')
|
|
466
|
+
return;
|
|
328
467
|
this.logEvent({ message, device: service.name, announcement: service, source });
|
|
329
468
|
this.emitDevice(service);
|
|
330
469
|
this.matching?.push(service.name);
|
|
@@ -386,7 +525,7 @@ class DirectConnectInterface extends node_events_1.EventEmitter {
|
|
|
386
525
|
return details[0].class;
|
|
387
526
|
}
|
|
388
527
|
createAnnouncementFromSettings(settings) {
|
|
389
|
-
if (settings.protocol && settings.address) {
|
|
528
|
+
if (settings.protocol && settings.address && settings.protocol !== 'openbikecontrol') {
|
|
390
529
|
const announcement = {
|
|
391
530
|
name: settings.name,
|
|
392
531
|
address: settings.address,
|
|
@@ -3,14 +3,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.DirectConnectPeripheral = void 0;
|
|
6
|
+
exports.OpenBikeControlPeripheral = exports.DirectConnectPeripheral = void 0;
|
|
7
7
|
const task_js_1 = require("../../utils/task.js");
|
|
8
8
|
const interface_js_1 = __importDefault(require("./interface.js"));
|
|
9
9
|
const index_js_1 = require("../messages/index.js");
|
|
10
10
|
const node_events_1 = require("node:events");
|
|
11
11
|
const consts_js_1 = require("../consts.js");
|
|
12
12
|
const utils_js_1 = require("../../ble/utils.js");
|
|
13
|
+
const consts_js_2 = require("../../ble/openbikecontrol/consts.js");
|
|
13
14
|
let IDS = 0;
|
|
15
|
+
const DC_CONNECT_TIMEOUT = 2000;
|
|
16
|
+
const DC_RESPONSE_TIMEOUT = 5000;
|
|
14
17
|
class DirectConnectPeripheral {
|
|
15
18
|
announcement;
|
|
16
19
|
static instances = {};
|
|
@@ -22,6 +25,7 @@ class DirectConnectPeripheral {
|
|
|
22
25
|
return this.instances[key];
|
|
23
26
|
}
|
|
24
27
|
socket;
|
|
28
|
+
socketConnected = false;
|
|
25
29
|
connectTask;
|
|
26
30
|
msgSeqNo = 0;
|
|
27
31
|
onDataHandler = this.onData.bind(this);
|
|
@@ -58,22 +62,36 @@ class DirectConnectPeripheral {
|
|
|
58
62
|
async connect() {
|
|
59
63
|
if (this.isConnected())
|
|
60
64
|
return true;
|
|
61
|
-
if (this.isConnecting())
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
if (this.isConnecting()) {
|
|
66
|
+
try {
|
|
67
|
+
await this.connectTask.getPromise();
|
|
68
|
+
}
|
|
69
|
+
catch { }
|
|
70
|
+
if (this.isConnected())
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
67
73
|
this.connectTask = new task_js_1.InteruptableTask(this.startConnection(), {
|
|
68
|
-
timeout:
|
|
74
|
+
timeout: DC_CONNECT_TIMEOUT,
|
|
69
75
|
});
|
|
70
|
-
|
|
76
|
+
let connected = false;
|
|
77
|
+
try {
|
|
78
|
+
connected = await this.connectTask.run();
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
connected = false;
|
|
82
|
+
}
|
|
83
|
+
if (!connected || !this.isConnected()) {
|
|
84
|
+
this.discardSocket();
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
return true;
|
|
71
88
|
}
|
|
72
89
|
async disconnect(connectionLost = false) {
|
|
73
90
|
try {
|
|
74
|
-
await this.connectTask
|
|
91
|
+
await this.connectTask?.stop();
|
|
75
92
|
await this.stopConnection(connectionLost);
|
|
76
93
|
delete this.socket;
|
|
94
|
+
this.socketConnected = false;
|
|
77
95
|
}
|
|
78
96
|
catch (err) {
|
|
79
97
|
return false;
|
|
@@ -81,7 +99,20 @@ class DirectConnectPeripheral {
|
|
|
81
99
|
return true;
|
|
82
100
|
}
|
|
83
101
|
isConnected() {
|
|
84
|
-
return this.socket !== undefined;
|
|
102
|
+
return this.socket !== undefined && this.socketConnected;
|
|
103
|
+
}
|
|
104
|
+
discardSocket() {
|
|
105
|
+
const socket = this.socket;
|
|
106
|
+
delete this.socket;
|
|
107
|
+
this.socketConnected = false;
|
|
108
|
+
if (!socket)
|
|
109
|
+
return;
|
|
110
|
+
try {
|
|
111
|
+
socket.removeAllListeners();
|
|
112
|
+
socket.on('error', () => { });
|
|
113
|
+
socket.destroy();
|
|
114
|
+
}
|
|
115
|
+
catch { }
|
|
85
116
|
}
|
|
86
117
|
isConnecting() {
|
|
87
118
|
return this.connectTask?.isRunning();
|
|
@@ -368,6 +399,7 @@ class DirectConnectPeripheral {
|
|
|
368
399
|
this.stopConnection().then(() => resolve(false));
|
|
369
400
|
});
|
|
370
401
|
this.socket.once('connect', () => {
|
|
402
|
+
this.socketConnected = true;
|
|
371
403
|
this.socket.on('data', this.onDataHandler);
|
|
372
404
|
this.socket.on('error', this.onPortErrorHandler);
|
|
373
405
|
this.socket.on('close', this.onPortCloseHandler);
|
|
@@ -406,9 +438,7 @@ class DirectConnectPeripheral {
|
|
|
406
438
|
async stopConnection(connectionLost = false) {
|
|
407
439
|
this.eventEmitter.removeAllListeners();
|
|
408
440
|
if (!this.isConnected()) {
|
|
409
|
-
this.
|
|
410
|
-
this.socket.destroy();
|
|
411
|
-
delete this.socket;
|
|
441
|
+
this.discardSocket();
|
|
412
442
|
return true;
|
|
413
443
|
}
|
|
414
444
|
await this.unsubscribeAll(connectionLost);
|
|
@@ -436,12 +466,28 @@ class DirectConnectPeripheral {
|
|
|
436
466
|
return this.msgSeqNo;
|
|
437
467
|
}
|
|
438
468
|
async send(seqNo, data) {
|
|
439
|
-
await this.connect();
|
|
440
|
-
this.
|
|
441
|
-
|
|
442
|
-
|
|
469
|
+
const connected = await this.connect();
|
|
470
|
+
if (!connected || !this.isConnected())
|
|
471
|
+
throw new Error('not connected');
|
|
472
|
+
const event = `response-${seqNo}`;
|
|
473
|
+
return new Promise((done, reject) => {
|
|
474
|
+
const timer = setTimeout(() => {
|
|
475
|
+
this.eventEmitter.off(event, onResponse);
|
|
476
|
+
reject(new Error('response timeout'));
|
|
477
|
+
}, DC_RESPONSE_TIMEOUT);
|
|
478
|
+
const onResponse = (response) => {
|
|
479
|
+
clearTimeout(timer);
|
|
443
480
|
done(response);
|
|
444
|
-
}
|
|
481
|
+
};
|
|
482
|
+
this.eventEmitter.once(event, onResponse);
|
|
483
|
+
try {
|
|
484
|
+
this.socket.write(data);
|
|
485
|
+
}
|
|
486
|
+
catch (err) {
|
|
487
|
+
clearTimeout(timer);
|
|
488
|
+
this.eventEmitter.off(event, onResponse);
|
|
489
|
+
reject(err);
|
|
490
|
+
}
|
|
445
491
|
});
|
|
446
492
|
}
|
|
447
493
|
getNextMessage(data) {
|
|
@@ -510,3 +556,114 @@ class DirectConnectPeripheral {
|
|
|
510
556
|
}
|
|
511
557
|
}
|
|
512
558
|
exports.DirectConnectPeripheral = DirectConnectPeripheral;
|
|
559
|
+
const OBC_MSG_BUTTON_STATE = 0x01;
|
|
560
|
+
const OBC_MSG_DEVICE_STATUS = 0x02;
|
|
561
|
+
const OBC_DEVICE_STATUS_LENGTH = 3;
|
|
562
|
+
class OpenBikeControlPeripheral extends DirectConnectPeripheral {
|
|
563
|
+
static instances = {};
|
|
564
|
+
static create(announcement) {
|
|
565
|
+
const { address, port } = announcement;
|
|
566
|
+
const key = `${address}:${port}`;
|
|
567
|
+
if (!this.instances[key])
|
|
568
|
+
this.instances[key] = new OpenBikeControlPeripheral(announcement);
|
|
569
|
+
return this.instances[key];
|
|
570
|
+
}
|
|
571
|
+
async discoverServices() {
|
|
572
|
+
this.discoveredServiceUUIds = [(0, utils_js_1.beautifyUUID)(consts_js_2.OBC_SERVICE)];
|
|
573
|
+
return [consts_js_2.OBC_SERVICE];
|
|
574
|
+
}
|
|
575
|
+
async discoverCharacteristics(_serviceUUID) {
|
|
576
|
+
return [{ uuid: consts_js_2.OBC_BUTTON_STATE, properties: ['read', 'notify'] }];
|
|
577
|
+
}
|
|
578
|
+
async subscribe(characteristicUUID, callback) {
|
|
579
|
+
const uuid = (0, utils_js_1.parseUUID)(characteristicUUID);
|
|
580
|
+
if (this.subscribed.includes(uuid))
|
|
581
|
+
return true;
|
|
582
|
+
this.subscribed.push(uuid);
|
|
583
|
+
if (callback) {
|
|
584
|
+
this.eventEmitter.on(uuid, (data) => {
|
|
585
|
+
callback(characteristicUUID, data);
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
return true;
|
|
589
|
+
}
|
|
590
|
+
async unsubscribe(characteristicUUID) {
|
|
591
|
+
const uuid = (0, utils_js_1.parseUUID)(characteristicUUID);
|
|
592
|
+
if (this.subscribed.includes(uuid))
|
|
593
|
+
this.subscribed.splice(this.subscribed.indexOf(uuid), 1);
|
|
594
|
+
this.eventEmitter.removeAllListeners(uuid);
|
|
595
|
+
return true;
|
|
596
|
+
}
|
|
597
|
+
async read(_characteristicUUID) {
|
|
598
|
+
return Buffer.from([]);
|
|
599
|
+
}
|
|
600
|
+
async write(characteristicUUID, data, _options) {
|
|
601
|
+
await this.connect();
|
|
602
|
+
this.logEvent({ message: 'write', path: this.getPath(), characteristic: (0, utils_js_1.beautifyUUID)(characteristicUUID), raw: data.toString('hex') });
|
|
603
|
+
this.socket.write(data);
|
|
604
|
+
return Buffer.from([]);
|
|
605
|
+
}
|
|
606
|
+
pending;
|
|
607
|
+
async disconnect(connectionLost) {
|
|
608
|
+
delete this.pending;
|
|
609
|
+
return super.disconnect(connectionLost);
|
|
610
|
+
}
|
|
611
|
+
async onData(data) {
|
|
612
|
+
const buffer = this.pending ? Buffer.concat([this.pending, data]) : data;
|
|
613
|
+
delete this.pending;
|
|
614
|
+
try {
|
|
615
|
+
let offset = 0;
|
|
616
|
+
while (offset < buffer.length) {
|
|
617
|
+
const msgType = buffer[offset];
|
|
618
|
+
if (msgType === OBC_MSG_DEVICE_STATUS) {
|
|
619
|
+
if (offset + OBC_DEVICE_STATUS_LENGTH > buffer.length) {
|
|
620
|
+
this.pending = Buffer.from(buffer.subarray(offset));
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
const status = buffer.subarray(offset, offset + OBC_DEVICE_STATUS_LENGTH);
|
|
624
|
+
this.logEvent({ message: 'device status', path: this.getPath(), battery: status[1], connected: status[2] === 1, raw: status.toString('hex') });
|
|
625
|
+
offset += OBC_DEVICE_STATUS_LENGTH;
|
|
626
|
+
}
|
|
627
|
+
else if (msgType === OBC_MSG_BUTTON_STATE) {
|
|
628
|
+
const end = this.getButtonStateEnd(buffer, offset);
|
|
629
|
+
if (end < 0) {
|
|
630
|
+
this.pending = Buffer.from(buffer.subarray(offset));
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
const message = Buffer.from(buffer.subarray(offset, end));
|
|
634
|
+
this.logEvent({ message: 'button state', path: this.getPath(), raw: message.toString('hex') });
|
|
635
|
+
this.eventEmitter.emit((0, utils_js_1.parseUUID)(consts_js_2.OBC_BUTTON_STATE), message);
|
|
636
|
+
offset = end;
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
this.logEvent({ message: 'unknown message', path: this.getPath(), raw: buffer.subarray(offset).toString('hex') });
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
catch (err) {
|
|
645
|
+
this.logEvent({ message: 'error', fn: 'onData', error: err.message, stack: err.stack, path: this.getPath(), raw: buffer?.toString('hex') });
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
getButtonStateEnd(buffer, offset) {
|
|
649
|
+
const buttons = new Set();
|
|
650
|
+
let pos = offset + 1;
|
|
651
|
+
while (pos < buffer.length) {
|
|
652
|
+
const id = buffer[pos];
|
|
653
|
+
if (buttons.has(id))
|
|
654
|
+
return pos;
|
|
655
|
+
if (pos + 1 >= buffer.length)
|
|
656
|
+
return buttons.size > 0 && this.isMessageType(id) ? pos : -1;
|
|
657
|
+
const next = buffer[pos + 1];
|
|
658
|
+
if (buttons.size > 0 && this.isMessageType(id) && next > 0x01)
|
|
659
|
+
return pos;
|
|
660
|
+
buttons.add(id);
|
|
661
|
+
pos += 2;
|
|
662
|
+
}
|
|
663
|
+
return buttons.size > 0 ? pos : -1;
|
|
664
|
+
}
|
|
665
|
+
isMessageType(byte) {
|
|
666
|
+
return byte === OBC_MSG_BUTTON_STATE || byte === OBC_MSG_DEVICE_STATUS;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
exports.OpenBikeControlPeripheral = OpenBikeControlPeripheral;
|
package/lib/esm/base/adpater.js
CHANGED
|
@@ -81,6 +81,9 @@ export default class IncyclistDevice extends EventEmitter {
|
|
|
81
81
|
getInterface() {
|
|
82
82
|
return typeof this.settings.interface === 'string' ? this.settings.interface : this.settings.interface.getName();
|
|
83
83
|
}
|
|
84
|
+
isReconnecting() {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
84
87
|
async restart(pause) {
|
|
85
88
|
this.logger.logEvent({ message: 'restarting device', device: this.getName() });
|
|
86
89
|
const stopped = await this.stop();
|
|
@@ -2,8 +2,12 @@ import IncyclistDevice from "../../base/adpater.js";
|
|
|
2
2
|
import { IncyclistCapability } from "../../types/index.js";
|
|
3
3
|
import { BleMultiTransportInterfaceFactory } from "../factories/interface-factory.js";
|
|
4
4
|
import { InteruptableTask } from "../../utils/task.js";
|
|
5
|
-
import { resolveNextTick } from "../../utils/utils.js";
|
|
5
|
+
import { resolveNextTick, sleep } from "../../utils/utils.js";
|
|
6
6
|
export default class BleAdapter extends IncyclistDevice {
|
|
7
|
+
static STOP_WAIT_BOUND_MS = 10000;
|
|
8
|
+
getStopWaitBound() {
|
|
9
|
+
return BleAdapter.STOP_WAIT_BOUND_MS;
|
|
10
|
+
}
|
|
7
11
|
deviceData;
|
|
8
12
|
dataMsgCount;
|
|
9
13
|
lastDataTS;
|
|
@@ -89,6 +93,9 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
89
93
|
isConnected() {
|
|
90
94
|
return this.device?.isConnected();
|
|
91
95
|
}
|
|
96
|
+
isReconnecting() {
|
|
97
|
+
return this.getSensor()?.isReconnectBusy() ?? false;
|
|
98
|
+
}
|
|
92
99
|
resetData() {
|
|
93
100
|
super.resetData();
|
|
94
101
|
this.dataMsgCount = 0;
|
|
@@ -208,7 +215,8 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
208
215
|
timeout: startProps?.timeout ?? this.getDefaultStartupTimeout(),
|
|
209
216
|
name: 'start',
|
|
210
217
|
errorOnTimeout: false,
|
|
211
|
-
log: this.logEvent.bind(this)
|
|
218
|
+
log: this.logEvent.bind(this),
|
|
219
|
+
onCancel: () => { ble.cancelWait?.(this.settings); }
|
|
212
220
|
});
|
|
213
221
|
this.startPromise = this.runStartTask(ble);
|
|
214
222
|
return this.startPromise;
|
|
@@ -231,6 +239,9 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
231
239
|
hasData() {
|
|
232
240
|
return this.dataMsgCount > 0;
|
|
233
241
|
}
|
|
242
|
+
shouldWaitForData() {
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
234
245
|
async waitForInitialData(startupTimeout) {
|
|
235
246
|
let waitTask;
|
|
236
247
|
let iv;
|
|
@@ -309,7 +320,9 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
309
320
|
else {
|
|
310
321
|
await this.initControlBestEffort(startProps);
|
|
311
322
|
}
|
|
312
|
-
|
|
323
|
+
if (this.shouldWaitForData()) {
|
|
324
|
+
await this.waitForInitialData(timeout);
|
|
325
|
+
}
|
|
313
326
|
this.stopped = false;
|
|
314
327
|
this.started = true;
|
|
315
328
|
if (wasPaused)
|
|
@@ -324,7 +337,7 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
324
337
|
return true;
|
|
325
338
|
}
|
|
326
339
|
catch (err) {
|
|
327
|
-
this.logEvent({ message: 'start result: error', error: err.message,
|
|
340
|
+
this.logEvent({ message: 'start result: error', error: err.message, device: this.getName(), interface: this.getInterface(), protocol: this.getProtocolName() });
|
|
328
341
|
this.started = false;
|
|
329
342
|
this.stopped = true;
|
|
330
343
|
const ble = this.getBle();
|
|
@@ -341,7 +354,7 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
341
354
|
}
|
|
342
355
|
const sensor = this.getSensor();
|
|
343
356
|
let connected = await sensor.startSensor();
|
|
344
|
-
const subscribed = await sensor.subscribe();
|
|
357
|
+
const subscribed = connected ? await sensor.subscribe() : false;
|
|
345
358
|
if (connected && !subscribed) {
|
|
346
359
|
connected = false;
|
|
347
360
|
}
|
|
@@ -408,7 +421,15 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
408
421
|
if (this.isStarting()) {
|
|
409
422
|
const task = this.startTask;
|
|
410
423
|
await task.stop();
|
|
411
|
-
|
|
424
|
+
const underlying = task.getUnderlyingPromise();
|
|
425
|
+
if (underlying !== undefined) {
|
|
426
|
+
const settled = await Promise.race([
|
|
427
|
+
underlying.then(() => true).catch(() => true),
|
|
428
|
+
sleep(this.getStopWaitBound()).then(() => false),
|
|
429
|
+
]);
|
|
430
|
+
if (!settled)
|
|
431
|
+
this.logEvent({ message: 'stop(): the previous start is still running past its bound, proceeding anyway', device: this.getName(), interface: this.getInterface() });
|
|
432
|
+
}
|
|
412
433
|
}
|
|
413
434
|
this.started = false;
|
|
414
435
|
this.resetData();
|