incyclist-devices 3.0.38 → 3.0.41
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/interface.js +15 -0
- package/lib/cjs/ble/base/peripheral.js +26 -7
- package/lib/cjs/ble/bkool/adapter.js +49 -0
- package/lib/cjs/ble/bkool/consts.js +11 -0
- package/lib/cjs/ble/bkool/index.js +10 -0
- package/lib/cjs/ble/bkool/sensor.js +160 -0
- package/lib/cjs/ble/characteristics/csc/measurement.js +4 -37
- package/lib/cjs/ble/cp/sensor.js +4 -36
- package/lib/cjs/ble/crank-cadence.js +49 -0
- package/lib/cjs/ble/fe-c-pages.js +56 -0
- package/lib/cjs/ble/fm/adapter.js +5 -0
- package/lib/cjs/ble/index.js +13 -10
- package/lib/cjs/ble/tacx/sensor.js +10 -127
- package/lib/cjs/ble/wahoo/sensor.js +3 -33
- package/lib/cjs/ble/zwift/play/sensor.js +3 -0
- package/lib/esm/ble/base/interface.js +15 -0
- package/lib/esm/ble/base/peripheral.js +26 -7
- package/lib/esm/ble/bkool/adapter.js +43 -0
- package/lib/esm/ble/bkool/consts.js +7 -0
- package/lib/esm/ble/bkool/index.js +3 -0
- package/lib/esm/ble/bkool/sensor.js +154 -0
- package/lib/esm/ble/characteristics/csc/measurement.js +4 -37
- package/lib/esm/ble/cp/sensor.js +4 -36
- package/lib/esm/ble/crank-cadence.js +45 -0
- package/lib/esm/ble/fe-c-pages.js +48 -0
- package/lib/esm/ble/fm/adapter.js +5 -0
- package/lib/esm/ble/index.js +3 -1
- package/lib/esm/ble/tacx/sensor.js +10 -127
- package/lib/esm/ble/wahoo/sensor.js +3 -33
- package/lib/esm/ble/zwift/play/sensor.js +3 -0
- package/lib/types/ble/base/interface.d.ts +1 -0
- package/lib/types/ble/base/peripheral.d.ts +3 -0
- package/lib/types/ble/bkool/adapter.d.ts +13 -0
- package/lib/types/ble/bkool/consts.d.ts +5 -0
- package/lib/types/ble/bkool/index.d.ts +3 -0
- package/lib/types/ble/bkool/sensor.d.ts +35 -0
- package/lib/types/ble/characteristics/csc/measurement.d.ts +2 -9
- package/lib/types/ble/cp/sensor.d.ts +3 -8
- package/lib/types/ble/crank-cadence.d.ts +20 -0
- package/lib/types/ble/fe-c-pages.d.ts +12 -0
- package/lib/types/ble/fm/adapter.d.ts +1 -0
- package/lib/types/ble/index.d.ts +2 -1
- package/lib/types/ble/tacx/sensor.d.ts +2 -11
- package/lib/types/ble/types.d.ts +2 -2
- package/lib/types/ble/wahoo/sensor.d.ts +2 -8
- package/lib/types/ble/zwift/play/sensor.d.ts +1 -0
- package/package.json +1 -1
|
@@ -5,17 +5,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const consts_js_1 = require("../consts.js");
|
|
7
7
|
const consts_js_2 = require("./consts.js");
|
|
8
|
+
const fe_c_pages_js_1 = require("../fe-c-pages.js");
|
|
8
9
|
const sensor_js_1 = __importDefault(require("../fm/sensor.js"));
|
|
9
10
|
const utils_js_1 = require("../utils.js");
|
|
11
|
+
const crank_cadence_js_1 = require("../crank-cadence.js");
|
|
10
12
|
class TacxAdvancedFitnessMachineDevice extends sensor_js_1.default {
|
|
11
13
|
static profile = 'Smart Trainer';
|
|
12
14
|
static protocol = 'tacx';
|
|
13
15
|
static services = [consts_js_2.TACX_FE_C_BLE];
|
|
14
16
|
static characteristics = ['2acc', '2ad2', '2ad6', '2ad8', '2ad9', '2ada', consts_js_2.TACX_FE_C_RX, consts_js_2.TACX_FE_C_TX];
|
|
15
17
|
static detectionPriority = 10;
|
|
16
|
-
|
|
17
|
-
currentCrankData = undefined;
|
|
18
|
-
timeOffset = 0;
|
|
18
|
+
crankCadence = new crank_cadence_js_1.CrankCadenceCalculator(true);
|
|
19
19
|
tsPrevWrite = undefined;
|
|
20
20
|
hasFECData;
|
|
21
21
|
messageCnt;
|
|
@@ -101,37 +101,6 @@ class TacxAdvancedFitnessMachineDevice extends sensor_js_1.default {
|
|
|
101
101
|
async requestControl() {
|
|
102
102
|
return true;
|
|
103
103
|
}
|
|
104
|
-
parseCrankData(crankData) {
|
|
105
|
-
if (!this.prevCrankData) {
|
|
106
|
-
this.prevCrankData = { ...crankData, cntUpdateMissing: -1 };
|
|
107
|
-
return {};
|
|
108
|
-
}
|
|
109
|
-
const c = this.currentCrankData = crankData;
|
|
110
|
-
const p = this.prevCrankData;
|
|
111
|
-
let rpm = this.data.cadence;
|
|
112
|
-
let hasUpdate = c.time !== p.time;
|
|
113
|
-
if (hasUpdate) {
|
|
114
|
-
let time = c.time - p.time;
|
|
115
|
-
let revs = c.revolutions - p.revolutions;
|
|
116
|
-
if (c.time < p.time) {
|
|
117
|
-
time += 0x10000;
|
|
118
|
-
this.timeOffset += 0x10000;
|
|
119
|
-
}
|
|
120
|
-
if (c.revolutions < p.revolutions)
|
|
121
|
-
revs += 0x10000;
|
|
122
|
-
rpm = 1024 * 60 * revs / time;
|
|
123
|
-
}
|
|
124
|
-
else if (p.cntUpdateMissing < 0 || p.cntUpdateMissing > 2) {
|
|
125
|
-
rpm = 0;
|
|
126
|
-
}
|
|
127
|
-
const cntUpdateMissing = p.cntUpdateMissing;
|
|
128
|
-
this.prevCrankData = this.currentCrankData;
|
|
129
|
-
if (hasUpdate)
|
|
130
|
-
this.prevCrankData.cntUpdateMissing = 0;
|
|
131
|
-
else
|
|
132
|
-
this.prevCrankData.cntUpdateMissing = cntUpdateMissing + 1;
|
|
133
|
-
return { rpm, time: this.timeOffset + c.time };
|
|
134
|
-
}
|
|
135
104
|
parseCSC(_data, logOnly = false) {
|
|
136
105
|
const data = Buffer.from(_data);
|
|
137
106
|
this.logEvent({ message: 'BLE CSC message', data: data.toString('hex') });
|
|
@@ -148,7 +117,7 @@ class TacxAdvancedFitnessMachineDevice extends sensor_js_1.default {
|
|
|
148
117
|
revolutions: data.readUInt16LE(offset),
|
|
149
118
|
time: data.readUInt16LE(offset + 2)
|
|
150
119
|
};
|
|
151
|
-
const { rpm, time } = this.
|
|
120
|
+
const { rpm, time } = this.crankCadence.update(crankData);
|
|
152
121
|
this.data.cadence = rpm;
|
|
153
122
|
this.data.time = time;
|
|
154
123
|
}
|
|
@@ -176,7 +145,7 @@ class TacxAdvancedFitnessMachineDevice extends sensor_js_1.default {
|
|
|
176
145
|
revolutions: data.readUInt16LE(offset),
|
|
177
146
|
time: data.readUInt16LE(offset + 2)
|
|
178
147
|
};
|
|
179
|
-
const { rpm, time } = this.
|
|
148
|
+
const { rpm, time } = this.crankCadence.update(crankData);
|
|
180
149
|
this.data.cadence = rpm;
|
|
181
150
|
this.data.time = time;
|
|
182
151
|
}
|
|
@@ -460,121 +429,35 @@ class TacxAdvancedFitnessMachineDevice extends sensor_js_1.default {
|
|
|
460
429
|
async sendUserConfiguration(userWeight, bikeWeight, wheelDiameter, gearRatio) {
|
|
461
430
|
const logStr = `sendUserConfiguration(${userWeight},${bikeWeight},${wheelDiameter},${gearRatio})`;
|
|
462
431
|
this.logEvent({ message: logStr });
|
|
463
|
-
|
|
464
|
-
let mb = bikeWeight === undefined ? 0xFFF : bikeWeight;
|
|
465
|
-
let d = wheelDiameter === undefined ? 0xFF : wheelDiameter;
|
|
466
|
-
let gr = gearRatio === undefined ? 0x00 : gearRatio;
|
|
467
|
-
let dOffset = 0xFF;
|
|
468
|
-
if (m !== 0xFFFF)
|
|
469
|
-
m = Math.trunc(m * 100);
|
|
470
|
-
if (mb !== 0xFFF)
|
|
471
|
-
mb = Math.trunc(mb * 20);
|
|
472
|
-
if (d !== 0xFF) {
|
|
473
|
-
d = d * 1000;
|
|
474
|
-
dOffset = d % 10;
|
|
475
|
-
d = Math.trunc(d / 10);
|
|
476
|
-
}
|
|
477
|
-
if (gr !== 0x00) {
|
|
478
|
-
gr = Math.trunc(gr / 0.03);
|
|
479
|
-
}
|
|
480
|
-
const payload = [];
|
|
481
|
-
payload.push(consts_js_2.DEFAULT_CHANNEL);
|
|
482
|
-
payload.push(0x37);
|
|
483
|
-
payload.push(m & 0xFF);
|
|
484
|
-
payload.push((m >> 8) & 0xFF);
|
|
485
|
-
payload.push(0xFF);
|
|
486
|
-
payload.push(((mb & 0xF) << 4) | (dOffset & 0xF));
|
|
487
|
-
payload.push((mb >> 4) & 0xF);
|
|
488
|
-
payload.push(d & 0xFF);
|
|
489
|
-
payload.push(gr & 0xFF);
|
|
432
|
+
const payload = [consts_js_2.DEFAULT_CHANNEL, ...(0, fe_c_pages_js_1.buildUserConfigurationPage)(userWeight, bikeWeight, wheelDiameter, gearRatio)];
|
|
490
433
|
const data = this.buildMessage(payload, consts_js_2.ACKNOWLEDGED_DATA);
|
|
491
434
|
return await this.sendMessage(data);
|
|
492
435
|
}
|
|
493
436
|
async sendBasicResistance(resistance) {
|
|
494
437
|
const logStr = `sendBasicResistance(${resistance})`;
|
|
495
438
|
this.logEvent({ message: logStr });
|
|
496
|
-
|
|
497
|
-
res = res / 0.5;
|
|
498
|
-
const payload = [];
|
|
499
|
-
payload.push(consts_js_2.DEFAULT_CHANNEL);
|
|
500
|
-
payload.push(0x30);
|
|
501
|
-
payload.push(0xFF);
|
|
502
|
-
payload.push(0xFF);
|
|
503
|
-
payload.push(0xFF);
|
|
504
|
-
payload.push(0xFF);
|
|
505
|
-
payload.push(0xFF);
|
|
506
|
-
payload.push(0xFF);
|
|
507
|
-
payload.push(res & 0xFF);
|
|
439
|
+
const payload = [consts_js_2.DEFAULT_CHANNEL, ...(0, fe_c_pages_js_1.buildBasicResistancePage)(resistance ?? 0)];
|
|
508
440
|
const data = this.buildMessage(payload, consts_js_2.ACKNOWLEDGED_DATA);
|
|
509
441
|
return await this.sendMessage(data);
|
|
510
442
|
}
|
|
511
443
|
async sendTargetPower(power) {
|
|
512
444
|
const logStr = `sendTargetPower(${power})`;
|
|
513
445
|
this.logEvent({ message: logStr });
|
|
514
|
-
|
|
515
|
-
p = p * 4;
|
|
516
|
-
const payload = [];
|
|
517
|
-
payload.push(consts_js_2.DEFAULT_CHANNEL);
|
|
518
|
-
payload.push(0x31);
|
|
519
|
-
payload.push(0xFF);
|
|
520
|
-
payload.push(0xFF);
|
|
521
|
-
payload.push(0xFF);
|
|
522
|
-
payload.push(0xFF);
|
|
523
|
-
payload.push(0xFF);
|
|
524
|
-
payload.push(p & 0xFF);
|
|
525
|
-
payload.push((p >> 8) & 0xFF);
|
|
446
|
+
const payload = [consts_js_2.DEFAULT_CHANNEL, ...(0, fe_c_pages_js_1.buildTargetPowerPage)(power ?? 0)];
|
|
526
447
|
const data = this.buildMessage(payload, consts_js_2.ACKNOWLEDGED_DATA);
|
|
527
448
|
return await this.sendMessage(data);
|
|
528
449
|
}
|
|
529
450
|
async sendWindResistance(windCoeff, windSpeed, draftFactor) {
|
|
530
451
|
const logStr = `sendWindResistance(${windCoeff},${windSpeed},${draftFactor})`;
|
|
531
452
|
this.logEvent({ message: logStr });
|
|
532
|
-
|
|
533
|
-
let ws = windSpeed === undefined ? 0xFF : windSpeed;
|
|
534
|
-
let df = draftFactor === undefined ? 0xFF : draftFactor;
|
|
535
|
-
if (wc !== 0xFF) {
|
|
536
|
-
wc = Math.trunc(wc / 0.01);
|
|
537
|
-
}
|
|
538
|
-
if (ws !== 0xFF) {
|
|
539
|
-
ws = Math.trunc(ws + 127);
|
|
540
|
-
}
|
|
541
|
-
if (df !== 0xFF) {
|
|
542
|
-
df = Math.trunc(df / 0.01);
|
|
543
|
-
}
|
|
544
|
-
const payload = [];
|
|
545
|
-
payload.push(consts_js_2.DEFAULT_CHANNEL);
|
|
546
|
-
payload.push(0x32);
|
|
547
|
-
payload.push(0xFF);
|
|
548
|
-
payload.push(0xFF);
|
|
549
|
-
payload.push(0xFF);
|
|
550
|
-
payload.push(0xFF);
|
|
551
|
-
payload.push(wc & 0xFF);
|
|
552
|
-
payload.push(ws & 0xFF);
|
|
553
|
-
payload.push(df & 0xFF);
|
|
453
|
+
const payload = [consts_js_2.DEFAULT_CHANNEL, ...(0, fe_c_pages_js_1.buildWindResistancePage)(windCoeff, windSpeed, draftFactor)];
|
|
554
454
|
const data = this.buildMessage(payload, consts_js_2.ACKNOWLEDGED_DATA);
|
|
555
455
|
return await this.sendMessage(data);
|
|
556
456
|
}
|
|
557
457
|
async sendTrackResistance(slope, rrCoeff) {
|
|
558
458
|
const logStr = `sendTrackResistance(${slope},${rrCoeff})`;
|
|
559
459
|
this.logEvent({ message: logStr });
|
|
560
|
-
|
|
561
|
-
let rr = rrCoeff === undefined ? 0xFF : rrCoeff;
|
|
562
|
-
if (s !== 0xFFFF) {
|
|
563
|
-
s = Math.trunc((s + 200) / 0.01);
|
|
564
|
-
}
|
|
565
|
-
if (rr !== 0xFF) {
|
|
566
|
-
rr = Math.trunc(rr / 0.00005);
|
|
567
|
-
}
|
|
568
|
-
const payload = [];
|
|
569
|
-
payload.push(consts_js_2.DEFAULT_CHANNEL);
|
|
570
|
-
payload.push(0x33);
|
|
571
|
-
payload.push(0xFF);
|
|
572
|
-
payload.push(0xFF);
|
|
573
|
-
payload.push(0xFF);
|
|
574
|
-
payload.push(0xFF);
|
|
575
|
-
payload.push(s & 0xFF);
|
|
576
|
-
payload.push((s >> 8) & 0xFF);
|
|
577
|
-
payload.push(rr & 0xFF);
|
|
460
|
+
const payload = [consts_js_2.DEFAULT_CHANNEL, ...(0, fe_c_pages_js_1.buildTrackResistancePage)(slope, rrCoeff)];
|
|
578
461
|
const data = this.buildMessage(payload, consts_js_2.ACKNOWLEDGED_DATA);
|
|
579
462
|
return await this.sendMessage(data);
|
|
580
463
|
}
|
|
@@ -9,6 +9,7 @@ const consts_js_3 = require("./consts.js");
|
|
|
9
9
|
const sensor_js_1 = __importDefault(require("../fm/sensor.js"));
|
|
10
10
|
const utils_js_1 = require("../utils.js");
|
|
11
11
|
const task_js_1 = require("../../utils/task.js");
|
|
12
|
+
const crank_cadence_js_1 = require("../crank-cadence.js");
|
|
12
13
|
const WAHOO_CP_WRITE_TIMEOUT = 800;
|
|
13
14
|
class BleWahooDevice extends sensor_js_1.default {
|
|
14
15
|
static profile = 'Smart Trainer';
|
|
@@ -16,9 +17,7 @@ class BleWahooDevice extends sensor_js_1.default {
|
|
|
16
17
|
static services = [consts_js_2.CSP, consts_js_3.WAHOO_ADVANCED_FTMS];
|
|
17
18
|
static characteristics = [consts_js_2.FTMS_FEATURE, consts_js_2.INDOOR_BIKE_DATA, consts_js_2.RES_LEVEL_RANGE, consts_js_2.POWER_RANGE, consts_js_2.FTMS_CP, consts_js_2.FTMS_STATUS, consts_js_3.WAHOO_ADVANCED_TRAINER_CP];
|
|
18
19
|
static detectionPriority = 5;
|
|
19
|
-
|
|
20
|
-
currentCrankData = undefined;
|
|
21
|
-
timeOffset = 0;
|
|
20
|
+
crankCadence = new crank_cadence_js_1.CrankCadenceCalculator();
|
|
22
21
|
tsPrevWrite = undefined;
|
|
23
22
|
prevSlope = undefined;
|
|
24
23
|
wahooCP;
|
|
@@ -137,35 +136,6 @@ class BleWahooDevice extends sensor_js_1.default {
|
|
|
137
136
|
return false;
|
|
138
137
|
}
|
|
139
138
|
}
|
|
140
|
-
parseCrankData(crankData) {
|
|
141
|
-
if (!this.prevCrankData)
|
|
142
|
-
this.prevCrankData = { revolutions: 0, time: 0, cntUpdateMissing: -1 };
|
|
143
|
-
const c = this.currentCrankData = crankData;
|
|
144
|
-
const p = this.prevCrankData;
|
|
145
|
-
let rpm = this.data.cadence;
|
|
146
|
-
let hasUpdate = c.time !== p.time;
|
|
147
|
-
if (hasUpdate) {
|
|
148
|
-
let time = c.time - p.time;
|
|
149
|
-
let revs = c.revolutions - p.revolutions;
|
|
150
|
-
if (c.time < p.time) {
|
|
151
|
-
time += 0x10000;
|
|
152
|
-
this.timeOffset += 0x10000;
|
|
153
|
-
}
|
|
154
|
-
if (c.revolutions < p.revolutions)
|
|
155
|
-
revs += 0x10000;
|
|
156
|
-
rpm = 1024 * 60 * revs / time;
|
|
157
|
-
}
|
|
158
|
-
else if (p.cntUpdateMissing < 0 || p.cntUpdateMissing > 2) {
|
|
159
|
-
rpm = 0;
|
|
160
|
-
}
|
|
161
|
-
const cntUpdateMissing = p.cntUpdateMissing;
|
|
162
|
-
this.prevCrankData = this.currentCrankData;
|
|
163
|
-
if (hasUpdate)
|
|
164
|
-
this.prevCrankData.cntUpdateMissing = 0;
|
|
165
|
-
else
|
|
166
|
-
this.prevCrankData.cntUpdateMissing = cntUpdateMissing + 1;
|
|
167
|
-
return { rpm, time: this.timeOffset + c.time };
|
|
168
|
-
}
|
|
169
139
|
parsePower(_data) {
|
|
170
140
|
const data = Buffer.from(_data);
|
|
171
141
|
let balance;
|
|
@@ -208,7 +178,7 @@ class BleWahooDevice extends sensor_js_1.default {
|
|
|
208
178
|
revolutions: data.readUInt16LE(offset),
|
|
209
179
|
time: data.readUInt16LE(offset + 2)
|
|
210
180
|
};
|
|
211
|
-
const { rpm, time } = this.
|
|
181
|
+
const { rpm, time } = this.crankCadence.update(crankData);
|
|
212
182
|
this.data.cadence = rpm;
|
|
213
183
|
this.data.time = time;
|
|
214
184
|
}
|
|
@@ -664,6 +664,9 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
|
|
|
664
664
|
privateKeyEncoding: { type: 'pkcs8', format: 'der' }
|
|
665
665
|
});
|
|
666
666
|
}
|
|
667
|
+
resetHubState() {
|
|
668
|
+
this.setInitialState();
|
|
669
|
+
}
|
|
667
670
|
setInitialState() {
|
|
668
671
|
this.isHubPairConfirmed = false;
|
|
669
672
|
this.encrypted = false;
|
|
@@ -5,6 +5,8 @@ import { BlePeripheral } from "./peripheral.js";
|
|
|
5
5
|
import { beautifyUUID, getPeripheralInfo, matches, parseUUID } from "../utils.js";
|
|
6
6
|
import { InterfaceFactory } from "./types.js";
|
|
7
7
|
import { BleAdapterFactory } from "../factories/index.js";
|
|
8
|
+
import { CSP } from "../consts.js";
|
|
9
|
+
import { BKOOL_SERVICE, isBkoolDeviceName } from "../bkool/consts.js";
|
|
8
10
|
const BLE_EXPIRATION_TIMEOUT = 10 * 1000 * 60;
|
|
9
11
|
const BLE_DEFAULT_CONNECT_TIMEOUT = 30 * 1000;
|
|
10
12
|
let instanceCount = 0;
|
|
@@ -433,8 +435,21 @@ export class BleInterface extends EventEmitter {
|
|
|
433
435
|
this.processEnrichedAnnouncement(announcement);
|
|
434
436
|
return;
|
|
435
437
|
}
|
|
438
|
+
const isBkool = this.checkForBkoolEnhancement(announcement);
|
|
439
|
+
if (isBkool) {
|
|
440
|
+
this.processEnrichedAnnouncement(announcement);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
436
443
|
this.addService(announcement);
|
|
437
444
|
}
|
|
445
|
+
checkForBkoolEnhancement(announcement) {
|
|
446
|
+
if (!isBkoolDeviceName(announcement.name))
|
|
447
|
+
return false;
|
|
448
|
+
const supported = new Set(announcement.serviceUUIDs.map(s => beautifyUUID(s)));
|
|
449
|
+
const cp = beautifyUUID(CSP);
|
|
450
|
+
const bkoolService = beautifyUUID(BKOOL_SERVICE);
|
|
451
|
+
return supported.has(cp) && !supported.has(bkoolService);
|
|
452
|
+
}
|
|
438
453
|
checkForWahooEnhancement(announcement) {
|
|
439
454
|
if (announcement.name.includes('KICKR') || announcement.name.includes('Zwift')) {
|
|
440
455
|
const supported = announcement.serviceUUIDs.map(s => beautifyUUID(s));
|
|
@@ -2,6 +2,8 @@ import { sleep } from "../../utils/utils.js";
|
|
|
2
2
|
import { beautifyUUID, fullUUID, isSameServiceFamily, matches } from "../utils.js";
|
|
3
3
|
import { BleInterface } from "./interface.js";
|
|
4
4
|
const SERVICE_COMPLETENESS_WHITELIST = ['MRK-R15'];
|
|
5
|
+
const POST_CONNECT_DISCOVERY_ATTEMPTS = 4;
|
|
6
|
+
const POST_CONNECT_DISCOVERY_BACKOFF_MS = 1000;
|
|
5
7
|
export class BlePeripheral {
|
|
6
8
|
announcement;
|
|
7
9
|
connected = false;
|
|
@@ -16,6 +18,7 @@ export class BlePeripheral {
|
|
|
16
18
|
serviceDiscoveryIncomplete = false;
|
|
17
19
|
discoverServicesPromise;
|
|
18
20
|
discoverCharacteristicsPromise = {};
|
|
21
|
+
awaitingPostConnectDiscovery = false;
|
|
19
22
|
onErrorHandler = this.onPeripheralError.bind(this);
|
|
20
23
|
constructor(announcement) {
|
|
21
24
|
this.announcement = announcement;
|
|
@@ -67,6 +70,7 @@ export class BlePeripheral {
|
|
|
67
70
|
peripheral.once('disconnect', () => { this.onPeripheralDisconnect(); });
|
|
68
71
|
peripheral.on('error', this.onErrorHandler);
|
|
69
72
|
this.connected = true;
|
|
73
|
+
this.awaitingPostConnectDiscovery = true;
|
|
70
74
|
done();
|
|
71
75
|
})
|
|
72
76
|
.catch(() => {
|
|
@@ -230,7 +234,7 @@ export class BlePeripheral {
|
|
|
230
234
|
async subscribe(characteristicUUID, callback) {
|
|
231
235
|
try {
|
|
232
236
|
if (this.disconnecting || !this.connected) {
|
|
233
|
-
this.logEvent({ message: 'peripheral subscribe failed',
|
|
237
|
+
this.logEvent({ message: 'peripheral subscribe failed', characteristic: beautifyUUID(characteristicUUID), reason: 'not connected',
|
|
234
238
|
disconnecting: this.disconnecting, connected: this.connected
|
|
235
239
|
});
|
|
236
240
|
return false;
|
|
@@ -244,7 +248,7 @@ export class BlePeripheral {
|
|
|
244
248
|
};
|
|
245
249
|
const subscription = this.subscribed.find(s => s.uuid === uuid);
|
|
246
250
|
if (subscription) {
|
|
247
|
-
this.logEvent({ message: 'peripheral subscribe skipped',
|
|
251
|
+
this.logEvent({ message: 'peripheral subscribe skipped', characteristic: beautifyUUID(characteristicUUID), reason: 'already subscribed' });
|
|
248
252
|
const c = this.getRawCharacteristic(characteristicUUID);
|
|
249
253
|
if (c) {
|
|
250
254
|
c.off('data', onData);
|
|
@@ -254,13 +258,13 @@ export class BlePeripheral {
|
|
|
254
258
|
}
|
|
255
259
|
let c = await this.queryRawCharacteristic(characteristicUUID).catch(() => null);
|
|
256
260
|
if (!c) {
|
|
257
|
-
this.logEvent({ message: 'peripheral subscribe failed',
|
|
261
|
+
this.logEvent({ message: 'peripheral subscribe failed', characteristic: beautifyUUID(characteristicUUID), reason: 'not found' });
|
|
258
262
|
return false;
|
|
259
263
|
}
|
|
260
264
|
return new Promise((resolve, reject) => {
|
|
261
265
|
const info = this.subscribed.find(s => s.uuid === characteristicUUID);
|
|
262
266
|
if (info) {
|
|
263
|
-
this.logEvent({ message: 'peripheral subscribe skipped',
|
|
267
|
+
this.logEvent({ message: 'peripheral subscribe skipped', characteristic: beautifyUUID(characteristicUUID), reason: 'already subscribed' });
|
|
264
268
|
return Promise.resolve(true);
|
|
265
269
|
}
|
|
266
270
|
this.logEvent({ message: 'subscribe request', address: this.getPeripheral().address, characteristic: uuid, success: true });
|
|
@@ -354,10 +358,10 @@ export class BlePeripheral {
|
|
|
354
358
|
else {
|
|
355
359
|
const uuid = beautifyUUID(element);
|
|
356
360
|
if (c?.properties) {
|
|
357
|
-
this.logEvent({ message: 'cannot subscribe', uuid, reason: 'invalid type', properties: c.properties.join('|') });
|
|
361
|
+
this.logEvent({ message: 'cannot subscribe', characteristics: beautifyUUID(uuid), reason: 'invalid type', properties: c.properties.join('|') });
|
|
358
362
|
}
|
|
359
363
|
else {
|
|
360
|
-
this.logEvent({ message: 'cannot subscribe', uuid, reason: 'not found' });
|
|
364
|
+
this.logEvent({ message: 'cannot subscribe', characteristics: beautifyUUID(uuid), reason: 'not found' });
|
|
361
365
|
}
|
|
362
366
|
}
|
|
363
367
|
}
|
|
@@ -368,11 +372,26 @@ export class BlePeripheral {
|
|
|
368
372
|
return true;
|
|
369
373
|
}
|
|
370
374
|
catch (err) {
|
|
371
|
-
this.logEvent({ message: 'Error', fn: 'subscribeSelected', error: err.message, stack: err.stack });
|
|
375
|
+
this.logEvent({ message: 'Error', fn: 'subscribeSelected', uuids, error: err.message, stack: err.stack });
|
|
372
376
|
return false;
|
|
373
377
|
}
|
|
374
378
|
}
|
|
375
379
|
async discoverAllCharacteristics() {
|
|
380
|
+
const attempts = this.awaitingPostConnectDiscovery ? POST_CONNECT_DISCOVERY_ATTEMPTS : 1;
|
|
381
|
+
return this.discoverAllCharacteristicsAttempt(1, attempts);
|
|
382
|
+
}
|
|
383
|
+
async discoverAllCharacteristicsAttempt(attempt, maxAttempts) {
|
|
384
|
+
const found = await this.queryAllCharacteristics();
|
|
385
|
+
if (found.length > 0 || attempt >= maxAttempts) {
|
|
386
|
+
this.awaitingPostConnectDiscovery = false;
|
|
387
|
+
return found;
|
|
388
|
+
}
|
|
389
|
+
const { name, address } = this.getInfo();
|
|
390
|
+
this.logEvent({ message: 'discover all characteristics empty after connect - retrying', name, address, attempt });
|
|
391
|
+
await sleep(POST_CONNECT_DISCOVERY_BACKOFF_MS * attempt);
|
|
392
|
+
return this.discoverAllCharacteristicsAttempt(attempt + 1, maxAttempts);
|
|
393
|
+
}
|
|
394
|
+
async queryAllCharacteristics() {
|
|
376
395
|
try {
|
|
377
396
|
const { name, address } = this.getInfo();
|
|
378
397
|
this.logEvent({ message: 'discover all characteristics', name, address });
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { EventLogger } from 'gd-eventlog';
|
|
2
|
+
import { BleFmAdapter } from '../fm/index.js';
|
|
3
|
+
import BleBkoolDevice from './sensor.js';
|
|
4
|
+
import { DEFAULT_BIKE_WEIGHT, DEFAULT_USER_WEIGHT } from "../../base/consts.js";
|
|
5
|
+
import { IncyclistCapability } from '../../types/index.js';
|
|
6
|
+
export default class BleBkoolAdapter extends BleFmAdapter {
|
|
7
|
+
static INCYCLIST_PROFILE_NAME = 'Smart Trainer';
|
|
8
|
+
constructor(settings, props) {
|
|
9
|
+
super(settings, props);
|
|
10
|
+
this.logger = new EventLogger('BLE-BkoolFM');
|
|
11
|
+
this.device = new BleBkoolDevice(this.getPeripheral(), { logger: this.logger });
|
|
12
|
+
this.capabilities = [
|
|
13
|
+
IncyclistCapability.Power, IncyclistCapability.Speed, IncyclistCapability.Cadence,
|
|
14
|
+
IncyclistCapability.Control
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
isSame(device) {
|
|
18
|
+
if (!(device instanceof BleBkoolAdapter))
|
|
19
|
+
return false;
|
|
20
|
+
return this.isEqual(device.settings);
|
|
21
|
+
}
|
|
22
|
+
getProfile() {
|
|
23
|
+
return 'Smart Trainer';
|
|
24
|
+
}
|
|
25
|
+
updateSensor(peripheral) {
|
|
26
|
+
this.device = new BleBkoolDevice(peripheral, { logger: this.logger });
|
|
27
|
+
}
|
|
28
|
+
async initControl(props) {
|
|
29
|
+
const sensor = this.getSensor();
|
|
30
|
+
const { user, wheelDiameter, gearRatio, bikeWeight = DEFAULT_BIKE_WEIGHT } = props || {};
|
|
31
|
+
const userWeight = user?.weight ?? DEFAULT_USER_WEIGHT;
|
|
32
|
+
await sensor.sendBasicResistance(0);
|
|
33
|
+
await sensor.sendWindResistance(sensor.getCw(), sensor.getWindSpeed(), 0);
|
|
34
|
+
await sensor.sendTrackResistance(0, sensor.getCrr());
|
|
35
|
+
await sensor.sendUserConfiguration(userWeight, bikeWeight, wheelDiameter, gearRatio);
|
|
36
|
+
const startRequest = this.getCyclingMode().getBikeInitRequest();
|
|
37
|
+
await this.sendUpdate(startRequest);
|
|
38
|
+
}
|
|
39
|
+
checkCapabilities() {
|
|
40
|
+
this.logEvent({ message: 'device capabilities updated', name: this.getSettings().name, interface: this.getSettings().interface, capabilities: this.capabilities });
|
|
41
|
+
return Promise.resolve();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const BKOOL_SERVICE = 'f03eee01-4910-473c-be46-960948c2f59c';
|
|
2
|
+
export const BKOOL_CONTROL = 'f03ee002-4910-473c-be46-960948c2f59c';
|
|
3
|
+
export const BKOOL_NAME_PREFIX = 'BKOOLSMARTPRO';
|
|
4
|
+
export function isBkoolDeviceName(name) {
|
|
5
|
+
return !!name && name.toUpperCase().startsWith(BKOOL_NAME_PREFIX);
|
|
6
|
+
}
|
|
7
|
+
export const BKOOL_FRAME_MARKER = 0x25;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { CSP, CSP_MEASUREMENT } from "../consts.js";
|
|
2
|
+
import { BKOOL_SERVICE, BKOOL_CONTROL, BKOOL_FRAME_MARKER } from './consts.js';
|
|
3
|
+
import { buildBasicResistancePage, buildTargetPowerPage, buildWindResistancePage, buildTrackResistancePage, buildUserConfigurationPage } from "../fe-c-pages.js";
|
|
4
|
+
import BleFitnessMachineDevice from "../fm/sensor.js";
|
|
5
|
+
import { beautifyUUID } from "../utils.js";
|
|
6
|
+
import { CrankCadenceCalculator } from "../crank-cadence.js";
|
|
7
|
+
export default class BleBkoolDevice extends BleFitnessMachineDevice {
|
|
8
|
+
static profile = 'Smart Trainer';
|
|
9
|
+
static protocol = 'bkool';
|
|
10
|
+
static services = [CSP, BKOOL_SERVICE];
|
|
11
|
+
static characteristics = [CSP_MEASUREMENT, BKOOL_CONTROL];
|
|
12
|
+
static detectionPriority = 5;
|
|
13
|
+
crankCadence = new CrankCadenceCalculator();
|
|
14
|
+
constructor(peripheral, props) {
|
|
15
|
+
super(peripheral, props);
|
|
16
|
+
this.data = {};
|
|
17
|
+
this._features = {
|
|
18
|
+
fitnessMachine: 0, targetSettings: 0,
|
|
19
|
+
power: true, cadence: true, heartrate: this.supportsHeartRate(),
|
|
20
|
+
setPower: true, setSlope: true, setResistance: true
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
isMatching(serviceUUIDs) {
|
|
24
|
+
const uuids = new Set(serviceUUIDs.map(uuid => beautifyUUID(uuid)));
|
|
25
|
+
return uuids.has(beautifyUUID(CSP)) && uuids.has(beautifyUUID(BKOOL_SERVICE));
|
|
26
|
+
}
|
|
27
|
+
reset() {
|
|
28
|
+
this.data = {};
|
|
29
|
+
}
|
|
30
|
+
getRequiredCharacteristics() {
|
|
31
|
+
return [CSP_MEASUREMENT, BKOOL_CONTROL];
|
|
32
|
+
}
|
|
33
|
+
onData(characteristic, characteristicData) {
|
|
34
|
+
const data = Buffer.from(characteristicData);
|
|
35
|
+
const uuid = beautifyUUID(characteristic).toLowerCase();
|
|
36
|
+
let res;
|
|
37
|
+
if (uuid === CSP_MEASUREMENT) {
|
|
38
|
+
res = this.parsePower(data);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.logEvent({ message: 'data', uuid, data: Buffer.from(data).toString('hex') });
|
|
42
|
+
}
|
|
43
|
+
if (res) {
|
|
44
|
+
this.emit('data', res);
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
requestControl() {
|
|
50
|
+
return Promise.resolve(true);
|
|
51
|
+
}
|
|
52
|
+
async setTargetPower(power) {
|
|
53
|
+
this.logEvent({ message: 'setTargetPower', power, skip: (this.data.targetPower !== undefined && this.data.targetPower === power) });
|
|
54
|
+
if (this.data.targetPower !== undefined && this.data.targetPower === power)
|
|
55
|
+
return true;
|
|
56
|
+
const res = await this.sendTargetPower(power);
|
|
57
|
+
if (res)
|
|
58
|
+
this.data.targetPower = power;
|
|
59
|
+
return res;
|
|
60
|
+
}
|
|
61
|
+
async setSlope(slope) {
|
|
62
|
+
this.logEvent({ message: 'setSlope', slope });
|
|
63
|
+
if (this.data.targetInclination !== undefined && this.data.targetInclination === slope)
|
|
64
|
+
return true;
|
|
65
|
+
const res = await this.sendTrackResistance(slope, this.crr);
|
|
66
|
+
if (res)
|
|
67
|
+
this.data.targetInclination = slope;
|
|
68
|
+
return res;
|
|
69
|
+
}
|
|
70
|
+
async setTargetResistanceLevel(resistanceLevel) {
|
|
71
|
+
return await this.setSlope(resistanceLevel / 10);
|
|
72
|
+
}
|
|
73
|
+
async sendBasicResistance(resistance) {
|
|
74
|
+
return await this.sendFrame(buildBasicResistancePage(resistance, BKOOL_FRAME_MARKER));
|
|
75
|
+
}
|
|
76
|
+
async sendTargetPower(power) {
|
|
77
|
+
return await this.sendFrame(buildTargetPowerPage(power, BKOOL_FRAME_MARKER));
|
|
78
|
+
}
|
|
79
|
+
async sendWindResistance(windCoeff, windSpeed, draftFactor) {
|
|
80
|
+
return await this.sendFrame(buildWindResistancePage(windCoeff, windSpeed, draftFactor, BKOOL_FRAME_MARKER));
|
|
81
|
+
}
|
|
82
|
+
async sendTrackResistance(slope, rrCoeff) {
|
|
83
|
+
return await this.sendFrame(buildTrackResistancePage(slope, rrCoeff, BKOOL_FRAME_MARKER));
|
|
84
|
+
}
|
|
85
|
+
async sendUserConfiguration(userWeight, bikeWeight, wheelDiameter, gearRatio) {
|
|
86
|
+
return await this.sendFrame(buildUserConfigurationPage(userWeight, bikeWeight, wheelDiameter, gearRatio));
|
|
87
|
+
}
|
|
88
|
+
async sendFrame(frame) {
|
|
89
|
+
const data = Buffer.from(frame);
|
|
90
|
+
this.logEvent({ message: 'bkool:write', data: data.toString('hex') });
|
|
91
|
+
try {
|
|
92
|
+
await this.write(BKOOL_CONTROL, data, { withoutResponse: true });
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
this.logEvent({ message: 'bkool:write failed', reason: err.message });
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
parsePower(_data) {
|
|
101
|
+
const data = Buffer.from(_data);
|
|
102
|
+
try {
|
|
103
|
+
let offset = 4;
|
|
104
|
+
const flags = data.readUInt16LE(0);
|
|
105
|
+
this.data.instantaneousPower = data.readUInt16LE(2);
|
|
106
|
+
if (flags & 0x1)
|
|
107
|
+
offset++;
|
|
108
|
+
if (flags & 0x4)
|
|
109
|
+
offset += 2;
|
|
110
|
+
if (flags & 0x10)
|
|
111
|
+
offset += 6;
|
|
112
|
+
if (flags & 0x20) {
|
|
113
|
+
const crankData = {
|
|
114
|
+
revolutions: data.readUInt16LE(offset),
|
|
115
|
+
time: data.readUInt16LE(offset + 2)
|
|
116
|
+
};
|
|
117
|
+
const { rpm, time } = this.crankCadence.update(crankData);
|
|
118
|
+
this.data.cadence = rpm;
|
|
119
|
+
this.data.time = time;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
this.logEvent({ message: 'error', fn: 'parsePower', error: err.message, stack: err.stack });
|
|
124
|
+
}
|
|
125
|
+
const { instantaneousPower, cadence, time } = this.data;
|
|
126
|
+
return { instantaneousPower, cadence, time, raw: '2a63:' + data.toString('hex') };
|
|
127
|
+
}
|
|
128
|
+
supportsHeartRate() {
|
|
129
|
+
try {
|
|
130
|
+
if (!this.peripheral)
|
|
131
|
+
return false;
|
|
132
|
+
return this.peripheral.services?.find(s => beautifyUUID(s.uuid) === beautifyUUID('180d')) !== undefined;
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
this.logEvent({ message: 'error', fn: 'supportsHeartRate', error: err.message, stack: err.stack });
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
startRequest() {
|
|
140
|
+
return Promise.resolve(true);
|
|
141
|
+
}
|
|
142
|
+
stopRequest() {
|
|
143
|
+
return Promise.resolve(true);
|
|
144
|
+
}
|
|
145
|
+
PauseRequest() {
|
|
146
|
+
return Promise.resolve(true);
|
|
147
|
+
}
|
|
148
|
+
setTargetInclination(_inclination) {
|
|
149
|
+
return Promise.resolve(true);
|
|
150
|
+
}
|
|
151
|
+
getName() {
|
|
152
|
+
return this.peripheral?.getInfo().name ?? 'ble-bkool-device';
|
|
153
|
+
}
|
|
154
|
+
}
|