incyclist-devices 3.0.40 → 3.0.42

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.
Files changed (40) hide show
  1. package/lib/cjs/ble/base/interface.js +15 -0
  2. package/lib/cjs/ble/bkool/adapter.js +49 -0
  3. package/lib/cjs/ble/bkool/consts.js +11 -0
  4. package/lib/cjs/ble/bkool/index.js +10 -0
  5. package/lib/cjs/ble/bkool/sensor.js +160 -0
  6. package/lib/cjs/ble/characteristics/csc/measurement.js +4 -37
  7. package/lib/cjs/ble/cp/sensor.js +4 -36
  8. package/lib/cjs/ble/crank-cadence.js +49 -0
  9. package/lib/cjs/ble/fe-c-pages.js +56 -0
  10. package/lib/cjs/ble/index.js +13 -10
  11. package/lib/cjs/ble/tacx/sensor.js +10 -127
  12. package/lib/cjs/ble/wahoo/sensor.js +3 -33
  13. package/lib/cjs/ble/zwift/play/sensor.js +2 -2
  14. package/lib/esm/ble/base/interface.js +15 -0
  15. package/lib/esm/ble/bkool/adapter.js +43 -0
  16. package/lib/esm/ble/bkool/consts.js +7 -0
  17. package/lib/esm/ble/bkool/index.js +3 -0
  18. package/lib/esm/ble/bkool/sensor.js +154 -0
  19. package/lib/esm/ble/characteristics/csc/measurement.js +4 -37
  20. package/lib/esm/ble/cp/sensor.js +4 -36
  21. package/lib/esm/ble/crank-cadence.js +45 -0
  22. package/lib/esm/ble/fe-c-pages.js +48 -0
  23. package/lib/esm/ble/index.js +3 -1
  24. package/lib/esm/ble/tacx/sensor.js +10 -127
  25. package/lib/esm/ble/wahoo/sensor.js +3 -33
  26. package/lib/esm/ble/zwift/play/sensor.js +2 -2
  27. package/lib/types/ble/base/interface.d.ts +1 -0
  28. package/lib/types/ble/bkool/adapter.d.ts +13 -0
  29. package/lib/types/ble/bkool/consts.d.ts +5 -0
  30. package/lib/types/ble/bkool/index.d.ts +3 -0
  31. package/lib/types/ble/bkool/sensor.d.ts +35 -0
  32. package/lib/types/ble/characteristics/csc/measurement.d.ts +2 -9
  33. package/lib/types/ble/cp/sensor.d.ts +3 -8
  34. package/lib/types/ble/crank-cadence.d.ts +20 -0
  35. package/lib/types/ble/fe-c-pages.d.ts +12 -0
  36. package/lib/types/ble/index.d.ts +2 -1
  37. package/lib/types/ble/tacx/sensor.d.ts +2 -11
  38. package/lib/types/ble/types.d.ts +2 -2
  39. package/lib/types/ble/wahoo/sensor.d.ts +2 -8
  40. package/package.json +10 -10
@@ -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
- prevCrankData = undefined;
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.parseCrankData(crankData);
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.parseCrankData(crankData);
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
- let m = userWeight === undefined ? 0xFFFF : userWeight;
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
- let res = resistance === undefined ? 0 : resistance;
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
- let p = power === undefined ? 0x00 : power;
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
- let wc = windCoeff === undefined ? 0xFF : windCoeff;
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
- let s = slope === undefined ? 0xFFFF : slope;
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
- prevCrankData = undefined;
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.parseCrankData(crankData);
181
+ const { rpm, time } = this.crankCadence.update(crankData);
212
182
  this.data.cadence = rpm;
213
183
  this.data.time = time;
214
184
  }
@@ -151,7 +151,7 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
151
151
  await this.requestDataUpdate(512);
152
152
  }
153
153
  catch (err) {
154
- const message = this.stopRequested ? 'warning' : 'error';
154
+ const message = (this.stopRequested || this.isReconnectBusy()) ? 'warning' : 'error';
155
155
  this.logger.logEvent({ message, fn: 'setSimulationData', data, error: err.message, stack: err.stack });
156
156
  }
157
157
  }
@@ -184,7 +184,7 @@ class BleZwiftPlaySensor extends sensor_js_1.TBleSensor {
184
184
  await this.requestDataUpdate(512);
185
185
  }
186
186
  catch (err) {
187
- const message = this.stopRequested ? 'warning' : 'error';
187
+ const message = (this.stopRequested || this.isReconnectBusy()) ? 'warning' : 'error';
188
188
  this.logger.logEvent({ message, fn: 'setGearRatio', gearRatio, error: err.message, stack: err.stack });
189
189
  }
190
190
  return gearRatio;
@@ -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));
@@ -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,3 @@
1
+ import BleBkoolComms from "./sensor.js";
2
+ import BleBkoolAdapter from "./adapter.js";
3
+ export { BleBkoolAdapter, BleBkoolComms };
@@ -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
+ }
@@ -1,8 +1,8 @@
1
1
  import { DEFAULT_WHEEL_CIRCUMFERENCE } from "../../../base/consts.js";
2
+ import { CrankCadenceCalculator } from "../../crank-cadence.js";
2
3
  export class CscMeasurement {
3
4
  data;
4
- prevCrankData = undefined;
5
- currentCrankData = undefined;
5
+ crankCadence = new CrankCadenceCalculator(true);
6
6
  prevWheelData = undefined;
7
7
  currentWheelData = undefined;
8
8
  timeOffset = 0;
@@ -35,44 +35,12 @@ export class CscMeasurement {
35
35
  revolutions: data.readUInt16LE(offset),
36
36
  time: data.readUInt16LE(offset + 2)
37
37
  };
38
- const { rpm } = this.parseCrankData(crankData);
38
+ const { rpm } = this.crankCadence.update(crankData);
39
39
  this.data.cadence = rpm;
40
40
  }
41
41
  this.data.raw = data.toString('hex');
42
42
  return this.data;
43
43
  }
44
- parseCrankData(crankData) {
45
- if (!this.prevCrankData) {
46
- this.prevCrankData = { ...crankData, cntUpdateMissing: -1 };
47
- return {};
48
- }
49
- const c = this.currentCrankData = crankData;
50
- const p = this.prevCrankData;
51
- let rpm = this.data.cadence;
52
- let hasUpdate = c.time !== p.time;
53
- if (hasUpdate) {
54
- let time = c.time - p.time;
55
- let revs = c.revolutions - p.revolutions;
56
- if (c.time < p.time) {
57
- time += 0x10000;
58
- this.timeOffset += 0x10000;
59
- }
60
- if (c.revolutions < p.revolutions)
61
- revs += 0x10000;
62
- const seconds = time / 1024;
63
- rpm = 60 * revs / seconds;
64
- }
65
- else if (p.cntUpdateMissing < 0 || p.cntUpdateMissing > 2) {
66
- rpm = 0;
67
- }
68
- const cntUpdateMissing = p.cntUpdateMissing;
69
- this.prevCrankData = this.currentCrankData;
70
- if (hasUpdate)
71
- this.prevCrankData.cntUpdateMissing = 0;
72
- else
73
- this.prevCrankData.cntUpdateMissing = cntUpdateMissing + 1;
74
- return { rpm, time: this.timeOffset + c.time };
75
- }
76
44
  parseWheelData(wheelData) {
77
45
  if (!this.prevWheelData) {
78
46
  this.prevWheelData = { ...wheelData, cntUpdateMissing: -1 };
@@ -106,8 +74,7 @@ export class CscMeasurement {
106
74
  }
107
75
  reset() {
108
76
  this.data = {};
109
- this.prevCrankData = undefined;
110
- this.currentCrankData = undefined;
77
+ this.crankCadence = new CrankCadenceCalculator(true);
111
78
  this.prevWheelData = undefined;
112
79
  this.currentWheelData = undefined;
113
80
  this.timeOffset = 0;
@@ -1,6 +1,7 @@
1
1
  import { CSP, CSP_MEASUREMENT, CSP_FEATURE } from '../consts.js';
2
2
  import { matches } from '../utils.js';
3
3
  import { TBleSensor } from '../base/sensor.js';
4
+ import { CrankCadenceCalculator } from '../crank-cadence.js';
4
5
  export default class BleCyclingPowerDevice extends TBleSensor {
5
6
  static profile = 'Power Meter';
6
7
  static protocol = 'cp';
@@ -11,39 +12,8 @@ export default class BleCyclingPowerDevice extends TBleSensor {
11
12
  balance = undefined;
12
13
  accTorque = undefined;
13
14
  rpm = undefined;
14
- timeOffset = 0;
15
15
  time = undefined;
16
- currentCrankData = undefined;
17
- prevCrankData = undefined;
18
- parseCrankData(crankData) {
19
- if (!this.prevCrankData)
20
- this.prevCrankData = { revolutions: 0, time: 0, cntUpdateMissing: -1 };
21
- const c = this.currentCrankData = crankData;
22
- const p = this.prevCrankData;
23
- let rpm = this.rpm;
24
- let hasUpdate = c.time !== p.time;
25
- if (hasUpdate) {
26
- let time = c.time - p.time;
27
- let revs = c.revolutions - p.revolutions;
28
- if (c.time < p.time) {
29
- time += 0x10000;
30
- this.timeOffset += 0x10000;
31
- }
32
- if (c.revolutions < p.revolutions)
33
- revs += 0x10000;
34
- rpm = 1024 * 60 * revs / time;
35
- }
36
- else if (p.cntUpdateMissing < 0 || p.cntUpdateMissing > 2) {
37
- rpm = 0;
38
- }
39
- const cntUpdateMissing = p.cntUpdateMissing;
40
- this.prevCrankData = this.currentCrankData;
41
- if (hasUpdate)
42
- this.prevCrankData.cntUpdateMissing = 0;
43
- else
44
- this.prevCrankData.cntUpdateMissing = cntUpdateMissing + 1;
45
- return { rpm, time: this.timeOffset + c.time };
46
- }
16
+ crankCadence = new CrankCadenceCalculator();
47
17
  parsePower(_data) {
48
18
  const data = Buffer.from(_data);
49
19
  try {
@@ -64,7 +34,7 @@ export default class BleCyclingPowerDevice extends TBleSensor {
64
34
  revolutions: data.readUInt16LE(offset),
65
35
  time: data.readUInt16LE(offset + 2)
66
36
  };
67
- const { rpm, time } = this.parseCrankData(crankData);
37
+ const { rpm, time } = this.crankCadence.update(crankData);
68
38
  this.rpm = rpm;
69
39
  this.time = time;
70
40
  }
@@ -94,9 +64,7 @@ export default class BleCyclingPowerDevice extends TBleSensor {
94
64
  this.balance = undefined;
95
65
  this.accTorque = undefined;
96
66
  this.rpm = undefined;
97
- this.timeOffset = 0;
98
67
  this.time = undefined;
99
- this.currentCrankData = undefined;
100
- this.prevCrankData = undefined;
68
+ this.crankCadence = new CrankCadenceCalculator();
101
69
  }
102
70
  }