incyclist-devices 3.0.43 → 3.0.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/ble/base/adapter.js +24 -6
- package/lib/cjs/ble/csc/adapter.js +5 -5
- package/lib/cjs/ble/index.js +2 -0
- package/lib/cjs/ble/openbikecontrol/adapter.js +81 -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 +7 -0
- package/lib/cjs/direct-connect/base/interface.js +153 -14
- package/lib/cjs/direct-connect/base/peripheral.js +176 -19
- package/lib/esm/ble/base/adapter.js +25 -7
- package/lib/esm/ble/csc/adapter.js +5 -5
- package/lib/esm/ble/index.js +2 -0
- package/lib/esm/ble/openbikecontrol/adapter.js +74 -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 +7 -0
- package/lib/esm/direct-connect/base/interface.js +155 -16
- package/lib/esm/direct-connect/base/peripheral.js +174 -18
- package/lib/types/ble/base/adapter.d.ts +3 -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/direct-connect/base/interface.d.ts +9 -0
- package/lib/types/direct-connect/base/peripheral.d.ts +17 -0
- package/package.json +2 -2
|
@@ -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;
|
|
@@ -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;
|
|
@@ -23,7 +27,7 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
23
27
|
}
|
|
24
28
|
getUniqueName() {
|
|
25
29
|
const settings = this.settings;
|
|
26
|
-
if (
|
|
30
|
+
if (this.getName().match(/\d/g) || settings.address === undefined)
|
|
27
31
|
return this.getName();
|
|
28
32
|
else {
|
|
29
33
|
const id = (settings.id ?? settings.address ?? '').replace(/[:\-]/g, '');
|
|
@@ -211,7 +215,8 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
211
215
|
timeout: startProps?.timeout ?? this.getDefaultStartupTimeout(),
|
|
212
216
|
name: 'start',
|
|
213
217
|
errorOnTimeout: false,
|
|
214
|
-
log: this.logEvent.bind(this)
|
|
218
|
+
log: this.logEvent.bind(this),
|
|
219
|
+
onCancel: () => { ble.cancelWait?.(this.settings); }
|
|
215
220
|
});
|
|
216
221
|
this.startPromise = this.runStartTask(ble);
|
|
217
222
|
return this.startPromise;
|
|
@@ -234,6 +239,9 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
234
239
|
hasData() {
|
|
235
240
|
return this.dataMsgCount > 0;
|
|
236
241
|
}
|
|
242
|
+
shouldWaitForData() {
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
237
245
|
async waitForInitialData(startupTimeout) {
|
|
238
246
|
let waitTask;
|
|
239
247
|
let iv;
|
|
@@ -312,7 +320,9 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
312
320
|
else {
|
|
313
321
|
await this.initControlBestEffort(startProps);
|
|
314
322
|
}
|
|
315
|
-
|
|
323
|
+
if (this.shouldWaitForData()) {
|
|
324
|
+
await this.waitForInitialData(timeout);
|
|
325
|
+
}
|
|
316
326
|
this.stopped = false;
|
|
317
327
|
this.started = true;
|
|
318
328
|
if (wasPaused)
|
|
@@ -327,7 +337,7 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
327
337
|
return true;
|
|
328
338
|
}
|
|
329
339
|
catch (err) {
|
|
330
|
-
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() });
|
|
331
341
|
this.started = false;
|
|
332
342
|
this.stopped = true;
|
|
333
343
|
const ble = this.getBle();
|
|
@@ -344,7 +354,7 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
344
354
|
}
|
|
345
355
|
const sensor = this.getSensor();
|
|
346
356
|
let connected = await sensor.startSensor();
|
|
347
|
-
const subscribed = await sensor.subscribe();
|
|
357
|
+
const subscribed = connected ? await sensor.subscribe() : false;
|
|
348
358
|
if (connected && !subscribed) {
|
|
349
359
|
connected = false;
|
|
350
360
|
}
|
|
@@ -411,7 +421,15 @@ export default class BleAdapter extends IncyclistDevice {
|
|
|
411
421
|
if (this.isStarting()) {
|
|
412
422
|
const task = this.startTask;
|
|
413
423
|
await task.stop();
|
|
414
|
-
|
|
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
|
+
}
|
|
415
433
|
}
|
|
416
434
|
this.started = false;
|
|
417
435
|
this.resetData();
|
|
@@ -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)
|
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,74 @@
|
|
|
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
|
+
if (err.message === 'wifi device wait cancelled') {
|
|
35
|
+
this.logEvent({ message: err.message });
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
this.logEvent({ message: 'error', fn: 'startSensor', error: err.message, stack: err.stack });
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
onKeyPressed(event) {
|
|
43
|
+
this.emit('key-pressed', this.getSettings(), event);
|
|
44
|
+
}
|
|
45
|
+
isEqual(settings) {
|
|
46
|
+
return super.isEqual(settings) && settings.address == this.settings.address;
|
|
47
|
+
}
|
|
48
|
+
isSame(adapter) {
|
|
49
|
+
if (!(adapter instanceof OpenBikeControlAdapter))
|
|
50
|
+
return false;
|
|
51
|
+
return this.isEqual(adapter.settings);
|
|
52
|
+
}
|
|
53
|
+
updateSensor(peripheral) {
|
|
54
|
+
this.device = new BleOpenBikeControlSensor(peripheral, { logger: this.logger });
|
|
55
|
+
}
|
|
56
|
+
getProfile() {
|
|
57
|
+
return OpenBikeControlAdapter.INCYCLIST_PROFILE_NAME;
|
|
58
|
+
}
|
|
59
|
+
getName() {
|
|
60
|
+
const settings = this.settings;
|
|
61
|
+
const id = (settings.id ?? settings.address ?? '').replace(/[:\-.]/g, '');
|
|
62
|
+
const addressHash = id.length > 4 ? id.slice(-4).toUpperCase() : id.toUpperCase();
|
|
63
|
+
return addressHash ? `${settings.name} ${addressHash}` : settings.name ?? 'BikeControl';
|
|
64
|
+
}
|
|
65
|
+
getDisplayName() {
|
|
66
|
+
return this.getName();
|
|
67
|
+
}
|
|
68
|
+
mapData(deviceData) {
|
|
69
|
+
return {};
|
|
70
|
+
}
|
|
71
|
+
shouldWaitForData() {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -31,6 +31,10 @@ export class ZwiftPlayAdapter extends BleAdapter {
|
|
|
31
31
|
return connected;
|
|
32
32
|
}
|
|
33
33
|
catch (err) {
|
|
34
|
+
if (err.message === 'wifi device wait cancelled') {
|
|
35
|
+
this.logEvent({ message: err.message });
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
34
38
|
this.logEvent({ message: 'error', fn: 'startSensor', error: err.message, stack: err.stack });
|
|
35
39
|
return false;
|
|
36
40
|
}
|
|
@@ -76,4 +80,7 @@ export class ZwiftPlayAdapter extends BleAdapter {
|
|
|
76
80
|
mapData(deviceData) {
|
|
77
81
|
return {};
|
|
78
82
|
}
|
|
83
|
+
shouldWaitForData() {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
79
86
|
}
|