incyclist-devices 3.0.55 → 3.0.57

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 (47) hide show
  1. package/lib/cjs/ble/base/adapter.js +11 -0
  2. package/lib/cjs/ble/base/interface.js +10 -3
  3. package/lib/cjs/ble/base/peripheral.js +3 -0
  4. package/lib/cjs/ble/base/sensor.js +9 -0
  5. package/lib/cjs/ble/characteristics/hr/measurement.js +36 -0
  6. package/lib/cjs/ble/consts.js +2 -1
  7. package/lib/cjs/ble/cp/adapter.js +7 -0
  8. package/lib/cjs/ble/cp/sensor.js +28 -3
  9. package/lib/cjs/ble/csc/adapter.js +13 -11
  10. package/lib/cjs/ble/csc/sensor.js +24 -4
  11. package/lib/cjs/ble/fm/adapter.js +6 -1
  12. package/lib/cjs/ble/fm/consts.js +3 -1
  13. package/lib/cjs/ble/fm/sensor.js +11 -20
  14. package/lib/cjs/direct-connect/base/peripheral.js +5 -0
  15. package/lib/esm/ble/base/adapter.js +11 -0
  16. package/lib/esm/ble/base/interface.js +10 -3
  17. package/lib/esm/ble/base/peripheral.js +3 -0
  18. package/lib/esm/ble/base/sensor.js +10 -1
  19. package/lib/esm/ble/characteristics/hr/measurement.js +30 -0
  20. package/lib/esm/ble/consts.js +1 -0
  21. package/lib/esm/ble/cp/adapter.js +7 -0
  22. package/lib/esm/ble/cp/sensor.js +29 -4
  23. package/lib/esm/ble/csc/adapter.js +13 -11
  24. package/lib/esm/ble/csc/sensor.js +24 -4
  25. package/lib/esm/ble/fm/adapter.js +6 -1
  26. package/lib/esm/ble/fm/consts.js +2 -0
  27. package/lib/esm/ble/fm/sensor.js +13 -22
  28. package/lib/esm/direct-connect/base/peripheral.js +5 -0
  29. package/lib/types/ble/base/adapter.d.ts +2 -0
  30. package/lib/types/ble/base/interface.d.ts +1 -0
  31. package/lib/types/ble/base/peripheral.d.ts +1 -0
  32. package/lib/types/ble/base/sensor.d.ts +1 -0
  33. package/lib/types/ble/characteristics/csc/measurement.d.ts +1 -0
  34. package/lib/types/ble/characteristics/hr/measurement.d.ts +13 -0
  35. package/lib/types/ble/consts.d.ts +1 -0
  36. package/lib/types/ble/cp/adapter.d.ts +1 -0
  37. package/lib/types/ble/cp/sensor.d.ts +6 -1
  38. package/lib/types/ble/cp/types.d.ts +1 -0
  39. package/lib/types/ble/csc/sensor.d.ts +5 -1
  40. package/lib/types/ble/csc/types.d.ts +1 -0
  41. package/lib/types/ble/fm/adapter.d.ts +1 -0
  42. package/lib/types/ble/fm/consts.d.ts +2 -0
  43. package/lib/types/ble/fm/sensor.d.ts +1 -0
  44. package/lib/types/ble/types.d.ts +1 -0
  45. package/lib/types/direct-connect/base/peripheral.d.ts +2 -0
  46. package/lib/types/features/features.d.ts +1 -1
  47. package/package.json +1 -1
@@ -8,6 +8,7 @@ const index_js_1 = require("../../types/index.js");
8
8
  const interface_factory_js_1 = require("../factories/interface-factory.js");
9
9
  const task_js_1 = require("../../utils/task.js");
10
10
  const utils_js_1 = require("../../utils/utils.js");
11
+ const consts_js_1 = require("../consts.js");
11
12
  class BleAdapter extends adpater_js_1.default {
12
13
  static STOP_WAIT_BOUND_MS = 10000;
13
14
  getStopWaitBound() {
@@ -320,6 +321,16 @@ class BleAdapter extends adpater_js_1.default {
320
321
  }
321
322
  async checkCapabilities() {
322
323
  }
324
+ addHeartRateCapability() {
325
+ if (this.capabilities.includes(index_js_1.IncyclistCapability.HeartRate) || !this.getSensor()?.hasService(consts_js_1.HRS))
326
+ return false;
327
+ this.capabilities.push(index_js_1.IncyclistCapability.HeartRate);
328
+ return true;
329
+ }
330
+ emitCapabilitiesChanged() {
331
+ this.logEvent({ message: 'device capabilities updated', name: this.getSettings().name, interface: this.getSettings().interface, capabilities: this.capabilities });
332
+ this.emit('device-info', this.getSettings(), { capabilities: this.capabilities });
333
+ }
323
334
  async initControl(_props) {
324
335
  }
325
336
  async initControlBestEffort(_props) {
@@ -10,6 +10,8 @@ const types_js_1 = require("./types.js");
10
10
  const index_js_1 = require("../factories/index.js");
11
11
  const consts_js_1 = require("../consts.js");
12
12
  const consts_js_2 = require("../bkool/consts.js");
13
+ const consts_js_3 = require("../fm/consts.js");
14
+ const index_js_2 = require("../../features/index.js");
13
15
  const BLE_EXPIRATION_TIMEOUT = 10 * 1000 * 60;
14
16
  const DEFAULT_PAUSE_REASON = 'app-background';
15
17
  const TEMPORARY_SCAN_TIMEOUT = 30 * 1000;
@@ -439,13 +441,18 @@ class BleInterface extends node_events_1.EventEmitter {
439
441
  isDiscovering() {
440
442
  return this.discoverTask?.isRunning() === true;
441
443
  }
444
+ getServicesToAnnounce() {
445
+ if (!(0, index_js_2.useFeatureToggle)().has('VirtualShiftingFC82'))
446
+ return this.expectedServices;
447
+ return [...(this.expectedServices ?? []), consts_js_3.ZWIFT_FC82_UUID];
448
+ }
442
449
  discoverPeripherals() {
443
450
  return new Promise(done => {
444
451
  const ble = this.getBinding();
445
- ble.setSupportedServices?.(this.expectedServices);
452
+ const announcedServices = this.getServicesToAnnounce();
453
+ ble.setSupportedServices?.(announcedServices);
446
454
  if (ble.setSupportedServices) {
447
- const expectedServices = this.expectedServices ?? [];
448
- const expected = expectedServices.join('|');
455
+ const expected = (announcedServices ?? []).join('|');
449
456
  this.logEvent({ message: 'announce expected services', expected });
450
457
  }
451
458
  ble.startScanning([], true, (err) => {
@@ -47,6 +47,9 @@ class BlePeripheral {
47
47
  getDiscoveredServices() {
48
48
  return this.discoveredServiceUUIds ?? [];
49
49
  }
50
+ getKnownCharacteristics() {
51
+ return Object.keys(this.characteristics);
52
+ }
50
53
  getInfo() {
51
54
  return {
52
55
  id: this.announcement?.peripheral?.id,
@@ -59,6 +59,15 @@ class TBleSensor extends node_events_1.EventEmitter {
59
59
  getSupportedServiceUUids() {
60
60
  return this.peripheral?.getDiscoveredServices();
61
61
  }
62
+ hasService(serviceUUID) {
63
+ const discovered = this.peripheral?.getDiscoveredServices?.() ?? [];
64
+ const raw = this.peripheral?.services ?? [];
65
+ const services = [
66
+ ...discovered,
67
+ ...raw.map(s => typeof s === 'string' ? s : s.uuid)
68
+ ];
69
+ return services.some(s => (0, utils_js_2.matches)(s, serviceUUID));
70
+ }
62
71
  isMatching(serviceUUIDs) {
63
72
  const uuids = serviceUUIDs.map(uuid => (0, utils_js_2.beautifyUUID)(uuid));
64
73
  const required = this.getServiceUUids();
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isMeasurementStale = exports.HeartRateMeasurement = exports.parseHeartRate = exports.MEASUREMENT_STALE_TIMEOUT = void 0;
4
+ exports.MEASUREMENT_STALE_TIMEOUT = 3000;
5
+ const parseHeartRate = (data) => {
6
+ if (data.length === 0)
7
+ return undefined;
8
+ if (data.length === 1)
9
+ return data.readUInt8(0);
10
+ const is16Bit = (data.readUInt8(0) & 0x1) !== 0;
11
+ return is16Bit ? data.readUInt16LE(1) : data.readUInt8(1);
12
+ };
13
+ exports.parseHeartRate = parseHeartRate;
14
+ class HeartRateMeasurement {
15
+ heartrate;
16
+ parse(buffer) {
17
+ const data = Buffer.from(buffer);
18
+ try {
19
+ this.heartrate = (0, exports.parseHeartRate)(data) ?? this.heartrate;
20
+ }
21
+ catch {
22
+ }
23
+ return { heartrate: this.heartrate, raw: `2a37:${data.toString('hex')}` };
24
+ }
25
+ getValue() {
26
+ return this.heartrate;
27
+ }
28
+ reset() {
29
+ this.heartrate = undefined;
30
+ }
31
+ }
32
+ exports.HeartRateMeasurement = HeartRateMeasurement;
33
+ const isMeasurementStale = (tsMeasurement) => {
34
+ return tsMeasurement === undefined || Date.now() - tsMeasurement > exports.MEASUREMENT_STALE_TIMEOUT;
35
+ };
36
+ exports.isMeasurementStale = isMeasurementStale;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ELITE_TRAINER_SVC = exports.CSC_FEATURE = exports.CSC_MEASUREMENT = exports.CSC = exports.HR_MEASUREMENT = exports.CSP_FEATURE = exports.CSP_MEASUREMENT = exports.CSP = exports.POWER_RANGE = exports.RES_LEVEL_RANGE = exports.FTMS_FEATURE = exports.TREADMILL_DATA = exports.INDOOR_BIKE_DATA = exports.ROWER_DATA = exports.FTMS_STATUS = exports.FTMS_CP = exports.FTMS = void 0;
3
+ exports.ELITE_TRAINER_SVC = exports.CSC_FEATURE = exports.CSC_MEASUREMENT = exports.CSC = exports.HR_MEASUREMENT = exports.HRS = exports.CSP_FEATURE = exports.CSP_MEASUREMENT = exports.CSP = exports.POWER_RANGE = exports.RES_LEVEL_RANGE = exports.FTMS_FEATURE = exports.TREADMILL_DATA = exports.INDOOR_BIKE_DATA = exports.ROWER_DATA = exports.FTMS_STATUS = exports.FTMS_CP = exports.FTMS = void 0;
4
4
  exports.FTMS = '1826';
5
5
  exports.FTMS_CP = '2ad9';
6
6
  exports.FTMS_STATUS = '2ada';
@@ -13,6 +13,7 @@ exports.POWER_RANGE = '2ad8';
13
13
  exports.CSP = '1818';
14
14
  exports.CSP_MEASUREMENT = '2a63';
15
15
  exports.CSP_FEATURE = '2a65';
16
+ exports.HRS = '180d';
16
17
  exports.HR_MEASUREMENT = '2a37';
17
18
  exports.CSC = '1816';
18
19
  exports.CSC_MEASUREMENT = '2a5b';
@@ -40,6 +40,10 @@ class PwrAdapter extends adapter_js_1.default {
40
40
  const powerStr = power ? ` (${power})` : '';
41
41
  return `${name}${powerStr}`;
42
42
  }
43
+ async checkCapabilities() {
44
+ if (this.addHeartRateCapability())
45
+ this.emitCapabilitiesChanged();
46
+ }
43
47
  mapData(deviceData) {
44
48
  const data = {
45
49
  isPedalling: false,
@@ -54,6 +58,7 @@ class PwrAdapter extends adapter_js_1.default {
54
58
  data.power = deviceData?.instantaneousPower ?? data.power;
55
59
  data.pedalRpm = deviceData?.rpm ?? data.pedalRpm;
56
60
  data.time = deviceData?.time ?? data.time;
61
+ data.heartrate = deviceData?.heartrate ?? data.heartrate;
57
62
  data.isPedalling = data.pedalRpm > 0 || (data.pedalRpm === undefined && data.power > 0);
58
63
  return data;
59
64
  }
@@ -68,6 +73,8 @@ class PwrAdapter extends adapter_js_1.default {
68
73
  };
69
74
  if (bikeData.time)
70
75
  data.deviceTime = bikeData.time;
76
+ if (this.hasCapability(index_js_1.IncyclistCapability.HeartRate))
77
+ data.heartrate = bikeData.heartrate;
71
78
  this.data = data;
72
79
  return data;
73
80
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const consts_js_1 = require("../consts.js");
4
+ const measurement_js_1 = require("../characteristics/hr/measurement.js");
4
5
  const utils_js_1 = require("../utils.js");
5
6
  const sensor_js_1 = require("../base/sensor.js");
6
7
  const crank_cadence_js_1 = require("../crank-cadence.js");
@@ -9,13 +10,15 @@ class BleCyclingPowerDevice extends sensor_js_1.TBleSensor {
9
10
  static protocol = 'cp';
10
11
  static services = [consts_js_1.CSP];
11
12
  static characteristics = [consts_js_1.CSP_MEASUREMENT, consts_js_1.CSP_FEATURE, '2a5d', '2a3c'];
12
- static detectionPriority = 1;
13
+ static detectionPriority = 3;
13
14
  instantaneousPower = undefined;
14
15
  balance = undefined;
15
16
  accTorque = undefined;
16
17
  rpm = undefined;
17
18
  time = undefined;
18
19
  crankCadence = new crank_cadence_js_1.CrankCadenceCalculator();
20
+ hrm = new measurement_js_1.HeartRateMeasurement();
21
+ tsPower;
19
22
  parsePower(_data) {
20
23
  const data = Buffer.from(_data);
21
24
  try {
@@ -43,11 +46,26 @@ class BleCyclingPowerDevice extends sensor_js_1.TBleSensor {
43
46
  }
44
47
  catch {
45
48
  }
49
+ return { ...this.getPowerData(), raw: `2a63:${data.toString('hex')}` };
50
+ }
51
+ getPowerData() {
46
52
  const { instantaneousPower, balance, accTorque, rpm, time } = this;
47
- return { instantaneousPower, balance, accTorque, rpm, time, raw: `2a63:${data.toString('hex')}` };
53
+ const heartrate = this.hrm?.getValue();
54
+ return { instantaneousPower, balance, accTorque, rpm, time, ...(heartrate === undefined ? {} : { heartrate }) };
55
+ }
56
+ getHeartRateRecord(data) {
57
+ const { raw } = this.hrm.parse(data);
58
+ const res = { ...this.getPowerData(), raw };
59
+ if ((0, measurement_js_1.isMeasurementStale)(this.tsPower)) {
60
+ if (res.instantaneousPower !== undefined)
61
+ res.instantaneousPower = 0;
62
+ if (res.rpm !== undefined)
63
+ res.rpm = 0;
64
+ }
65
+ return res;
48
66
  }
49
67
  getRequiredCharacteristics() {
50
- return [consts_js_1.CSP_MEASUREMENT];
68
+ return [consts_js_1.CSP_MEASUREMENT, consts_js_1.HR_MEASUREMENT];
51
69
  }
52
70
  onData(characteristic, characteristicData) {
53
71
  const data = Buffer.from(characteristicData);
@@ -55,10 +73,15 @@ class BleCyclingPowerDevice extends sensor_js_1.TBleSensor {
55
73
  if (!hasData)
56
74
  return false;
57
75
  if ((0, utils_js_1.matches)(characteristic, consts_js_1.CSP_MEASUREMENT)) {
76
+ this.tsPower = Date.now();
58
77
  const res = this.parsePower(data);
59
78
  this.emit('data', res);
60
79
  return false;
61
80
  }
81
+ if ((0, utils_js_1.matches)(characteristic, consts_js_1.HR_MEASUREMENT)) {
82
+ this.emit('data', this.getHeartRateRecord(data));
83
+ return false;
84
+ }
62
85
  return true;
63
86
  }
64
87
  reset() {
@@ -68,6 +91,8 @@ class BleCyclingPowerDevice extends sensor_js_1.TBleSensor {
68
91
  this.rpm = undefined;
69
92
  this.time = undefined;
70
93
  this.crankCadence = new crank_cadence_js_1.CrankCadenceCalculator();
94
+ this.hrm?.reset();
95
+ this.tsPower = undefined;
71
96
  }
72
97
  }
73
98
  exports.default = BleCyclingPowerDevice;
@@ -20,23 +20,23 @@ class BleCSCAdapter extends adapter_js_1.default {
20
20
  super(settings, props);
21
21
  this.logger = new gd_eventlog_1.EventLogger('Ble-CSC');
22
22
  this.device = new sensor_js_1.BleCyclingSpeedCadenceDevice(this.getPeripheral(), { logger: this.logger });
23
- this.capabilities = BleCSCAdapter.CAPABILITIES;
23
+ this.capabilities = [...BleCSCAdapter.CAPABILITIES];
24
24
  }
25
25
  async checkCapabilities() {
26
26
  const before = this.capabilities.join(',');
27
27
  const sensor = this.getSensor();
28
28
  if (!sensor)
29
29
  return;
30
+ this.addHeartRateCapability();
30
31
  const features = await sensor.getFeatures();
31
- if (!features || (features.wheelRevolutionData === undefined && features.crankRevolutionData === undefined))
32
- return;
33
- this.capabilities = BleCSCAdapter.CAPABILITIES.filter(c => (c !== index_js_1.IncyclistCapability.Speed || features.wheelRevolutionData) &&
34
- (c !== index_js_1.IncyclistCapability.Cadence || features.crankRevolutionData));
35
- const after = this.capabilities.join(',');
36
- if (before !== after) {
37
- this.logEvent({ message: 'device capabilities updated', name: this.getSettings().name, interface: this.getSettings().interface, capabilities: this.capabilities });
38
- this.emit('device-info', this.getSettings(), { capabilities: this.capabilities });
32
+ if (features && (features.wheelRevolutionData !== undefined || features.crankRevolutionData !== undefined)) {
33
+ const supported = BleCSCAdapter.CAPABILITIES.filter(c => (c !== index_js_1.IncyclistCapability.Speed || features.wheelRevolutionData) &&
34
+ (c !== index_js_1.IncyclistCapability.Cadence || features.crankRevolutionData));
35
+ const heartrate = this.capabilities.filter(c => c === index_js_1.IncyclistCapability.HeartRate);
36
+ this.capabilities = [...supported, ...heartrate];
39
37
  }
38
+ if (before !== this.capabilities.join(','))
39
+ this.emitCapabilitiesChanged();
40
40
  }
41
41
  isSame(device) {
42
42
  if (!(device instanceof BleCSCAdapter))
@@ -53,8 +53,8 @@ class BleCSCAdapter extends adapter_js_1.default {
53
53
  return this.getName();
54
54
  }
55
55
  mapData(deviceData) {
56
- const { cadence, speed = 0 } = deviceData;
57
- return { cadence, pedalRpm: cadence, speed };
56
+ const { cadence, speed = 0, heartrate } = deviceData;
57
+ return { cadence, pedalRpm: cadence, speed, ...(heartrate === undefined ? {} : { heartrate }) };
58
58
  }
59
59
  transformData(bikeData) {
60
60
  if (bikeData === undefined)
@@ -67,6 +67,8 @@ class BleCSCAdapter extends adapter_js_1.default {
67
67
  };
68
68
  if (bikeData.time)
69
69
  data.deviceTime = bikeData.time;
70
+ if (this.hasCapability(index_js_1.IncyclistCapability.HeartRate))
71
+ data.heartrate = bikeData.heartrate;
70
72
  this.data = data;
71
73
  return data;
72
74
  }
@@ -3,19 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BleCyclingSpeedCadenceDevice = void 0;
4
4
  const consts_js_1 = require("../consts.js");
5
5
  const utils_js_1 = require("../utils.js");
6
+ const measurement_js_1 = require("../characteristics/hr/measurement.js");
6
7
  const sensor_js_1 = require("../base/sensor.js");
7
- const measurement_js_1 = require("../characteristics/csc/measurement.js");
8
+ const measurement_js_2 = require("../characteristics/csc/measurement.js");
8
9
  const features_js_1 = require("../characteristics/csc/features.js");
9
10
  class BleCyclingSpeedCadenceDevice extends sensor_js_1.TBleSensor {
10
11
  static profile = 'Speed + Cadence Sensor';
11
12
  static protocol = 'csc';
12
13
  static services = [consts_js_1.CSC];
13
14
  static characteristics = [consts_js_1.CSC_MEASUREMENT, consts_js_1.CSC_FEATURE];
14
- static detectionPriority = 1;
15
+ static detectionPriority = 2;
15
16
  data;
16
17
  parsers = {};
17
18
  featureParser;
18
- measurement = new measurement_js_1.CscMeasurement();
19
+ measurement = new measurement_js_2.CscMeasurement();
20
+ hrm = new measurement_js_1.HeartRateMeasurement();
21
+ tsMeasurement;
19
22
  constructor(peripheral, props) {
20
23
  super(peripheral, props);
21
24
  this.data = {};
@@ -23,7 +26,7 @@ class BleCyclingSpeedCadenceDevice extends sensor_js_1.TBleSensor {
23
26
  this.parsers[(0, utils_js_1.beautifyUUID)(consts_js_1.CSC_MEASUREMENT)] = this.measurement;
24
27
  }
25
28
  getRequiredCharacteristics() {
26
- return [consts_js_1.CSC_MEASUREMENT];
29
+ return [consts_js_1.CSC_MEASUREMENT, consts_js_1.HR_MEASUREMENT];
27
30
  }
28
31
  async getFeatures() {
29
32
  try {
@@ -42,11 +45,16 @@ class BleCyclingSpeedCadenceDevice extends sensor_js_1.TBleSensor {
42
45
  const hasData = super.onData(characteristic, data);
43
46
  if (!hasData)
44
47
  return false;
48
+ if ((0, utils_js_1.matches)(characteristic, consts_js_1.HR_MEASUREMENT)) {
49
+ this.onHeartRate(data);
50
+ return true;
51
+ }
45
52
  const uuid = (0, utils_js_1.beautifyUUID)(characteristic);
46
53
  const parser = this.parsers[uuid];
47
54
  if (!parser) {
48
55
  return false;
49
56
  }
57
+ this.tsMeasurement = Date.now();
50
58
  const parsed = parser.parse(data);
51
59
  this.data = { ...this.data, ...parsed };
52
60
  if (this.data.speed < 0) {
@@ -56,8 +64,20 @@ class BleCyclingSpeedCadenceDevice extends sensor_js_1.TBleSensor {
56
64
  this.emit('data', this.data);
57
65
  return true;
58
66
  }
67
+ onHeartRate(data) {
68
+ this.data = { ...this.data, ...this.hrm.parse(data) };
69
+ const res = { ...this.data };
70
+ const stale = (0, measurement_js_1.isMeasurementStale)(this.tsMeasurement);
71
+ if (res.speed !== undefined && (stale || res.speed < 0))
72
+ res.speed = 0;
73
+ if (res.cadence !== undefined && stale)
74
+ res.cadence = 0;
75
+ this.emit('data', res);
76
+ }
59
77
  reset() {
60
78
  this.data = {};
79
+ this.tsMeasurement = undefined;
80
+ this.hrm?.reset();
61
81
  this.measurement?.reset();
62
82
  }
63
83
  onDisconnect() {
@@ -53,7 +53,12 @@ class BleFmAdapter extends adapter_js_1.default {
53
53
  supportsVirtualShifting() {
54
54
  if (!this.getFeatureToggle().has('VirtualShifting'))
55
55
  return false;
56
- return this.device?.supportsVirtualShifting() ?? false;
56
+ return (this.device?.supportsVirtualShifting() ?? false) || this.supportsVirtualShiftingViaFC82();
57
+ }
58
+ supportsVirtualShiftingViaFC82() {
59
+ if (!this.getFeatureToggle().has('VirtualShiftingFC82'))
60
+ return false;
61
+ return this.device?.supportsVirtualShiftingViaFC82() ?? false;
57
62
  }
58
63
  getSupportedCyclingModes() {
59
64
  const modes = [power_meter_js_1.default];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ZWIFT_PLAY_UUID = exports.TargetSettingFeatureFlag = exports.FitnessMachineFeatureFlag = exports.IndoorBikeDataFlag = exports.cRR = exports.cwABike = void 0;
3
+ exports.ZWIFT_SYNC_RX_UUID = exports.ZWIFT_FC82_UUID = exports.ZWIFT_PLAY_UUID = exports.TargetSettingFeatureFlag = exports.FitnessMachineFeatureFlag = exports.IndoorBikeDataFlag = exports.cRR = exports.cwABike = void 0;
4
4
  exports.cwABike = {
5
5
  race: 0.35,
6
6
  triathlon: 0.29,
@@ -62,3 +62,5 @@ exports.TargetSettingFeatureFlag = {
62
62
  TargetedCadenceConfigurationSupported: bit(16)
63
63
  };
64
64
  exports.ZWIFT_PLAY_UUID = '0000000119ca465186e5fa29dcdd09d1';
65
+ exports.ZWIFT_FC82_UUID = 'fc82';
66
+ exports.ZWIFT_SYNC_RX_UUID = '00000003-19ca-4651-86e5-fa29dcdd09d1';
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const consts_js_1 = require("../consts.js");
4
+ const measurement_js_1 = require("../characteristics/hr/measurement.js");
4
5
  const sensor_js_1 = require("../base/sensor.js");
5
6
  const utils_js_1 = require("../utils.js");
6
7
  const consts_js_2 = require("./consts.js");
@@ -184,18 +185,7 @@ class BleFitnessMachineDevice extends sensor_js_1.TBleSensor {
184
185
  if (data.length == 0)
185
186
  return { ...this.data, raw: '<empty>' };
186
187
  try {
187
- if (data.length === 1) {
188
- this.data.heartrate = data.readUInt8(0);
189
- }
190
- else {
191
- const flags = data.readUInt8(0);
192
- if ((flags & 0x1) === 0) {
193
- this.data.heartrate = data.readUInt8(1);
194
- }
195
- else {
196
- this.data.heartrate = data.readUInt16LE(1);
197
- }
198
- }
188
+ this.data.heartrate = (0, measurement_js_1.parseHeartRate)(data);
199
189
  }
200
190
  catch (err) {
201
191
  if (!this.hrmParseErrorLogged) {
@@ -550,14 +540,8 @@ class BleFitnessMachineDevice extends sensor_js_1.TBleSensor {
550
540
  }
551
541
  const buffer = data ? Buffer.from(data) : undefined;
552
542
  try {
553
- const discovered = this.peripheral?.getDiscoveredServices?.() ?? [];
554
- const rawServices = this.peripheral?.services ?? [];
555
- const services = [
556
- ...discovered,
557
- ...rawServices.map(s => typeof s === 'string' ? s : s.uuid)
558
- ];
559
- let power = services.some(s => (0, utils_js_1.matches)(s, '1818'));
560
- let heartrate = services.some(s => (0, utils_js_1.matches)(s, '180d'));
543
+ let power = this.hasService(consts_js_1.CSP);
544
+ let heartrate = this.hasService(consts_js_1.HRS);
561
545
  const dataLength = buffer?.length ?? 0;
562
546
  const fitnessMachine = dataLength >= 4 ? buffer.readUInt32LE(0) : 0;
563
547
  const targetSettings = dataLength >= 8 ? buffer.readUInt32LE(4) : 0;
@@ -584,6 +568,13 @@ class BleFitnessMachineDevice extends sensor_js_1.TBleSensor {
584
568
  supportsVirtualShifting() {
585
569
  return this.getSupportedServiceUUids()?.some(s => (0, utils_js_1.matches)(s, consts_js_2.ZWIFT_PLAY_UUID));
586
570
  }
571
+ supportsVirtualShiftingViaFC82() {
572
+ const hasService = this.getSupportedServiceUUids()?.some(s => (0, utils_js_1.matches)(s, consts_js_2.ZWIFT_FC82_UUID));
573
+ if (!hasService)
574
+ return false;
575
+ const characteristics = this.peripheral?.getKnownCharacteristics?.() ?? [];
576
+ return characteristics.length === 0 || characteristics.some(c => (0, utils_js_1.matches)(c, consts_js_2.ZWIFT_SYNC_RX_UUID));
577
+ }
587
578
  buildFitnessMachineInfo(fitnessMachine) {
588
579
  if (fitnessMachine == null)
589
580
  return;
@@ -37,6 +37,7 @@ class DirectConnectPeripheral {
37
37
  subscribed = [];
38
38
  onDisconnectHandler;
39
39
  discoveredServiceUUIds;
40
+ knownCharacteristics = new Set();
40
41
  id;
41
42
  constructor(announcement) {
42
43
  this.announcement = announcement;
@@ -59,6 +60,9 @@ class DirectConnectPeripheral {
59
60
  getDiscoveredServices() {
60
61
  return this.discoveredServiceUUIds;
61
62
  }
63
+ getKnownCharacteristics() {
64
+ return Array.from(this.knownCharacteristics);
65
+ }
62
66
  async connect() {
63
67
  if (this.isConnected())
64
68
  return true;
@@ -161,6 +165,7 @@ class DirectConnectPeripheral {
161
165
  const service = (0, utils_js_1.beautifyUUID)(res.body.serviceUUID);
162
166
  const characteristics = res.body.characteristicDefinitions.map(cd => `${(0, utils_js_1.beautifyUUID)(cd.characteristicUUID)}:${cd.properties.join('/')}`);
163
167
  this.logEvent({ message: 'DiscoverCharacteritics response', path: this.getPath(), rc, service, characteristics, raw: response.toString('hex') });
168
+ res.body.characteristicDefinitions.forEach(cd => this.knownCharacteristics.add((0, utils_js_1.beautifyUUID)(cd.characteristicUUID)));
164
169
  return res.body.characteristicDefinitions.map(c => ({ uuid: c.characteristicUUID, properties: c.properties }));
165
170
  }
166
171
  catch (err) {
@@ -3,6 +3,7 @@ import { IncyclistCapability } from "../../types/index.js";
3
3
  import { BleMultiTransportInterfaceFactory } from "../factories/interface-factory.js";
4
4
  import { InteruptableTask } from "../../utils/task.js";
5
5
  import { resolveNextTick, sleep } from "../../utils/utils.js";
6
+ import { HRS } from "../consts.js";
6
7
  export default class BleAdapter extends IncyclistDevice {
7
8
  static STOP_WAIT_BOUND_MS = 10000;
8
9
  getStopWaitBound() {
@@ -315,6 +316,16 @@ export default class BleAdapter extends IncyclistDevice {
315
316
  }
316
317
  async checkCapabilities() {
317
318
  }
319
+ addHeartRateCapability() {
320
+ if (this.capabilities.includes(IncyclistCapability.HeartRate) || !this.getSensor()?.hasService(HRS))
321
+ return false;
322
+ this.capabilities.push(IncyclistCapability.HeartRate);
323
+ return true;
324
+ }
325
+ emitCapabilitiesChanged() {
326
+ this.logEvent({ message: 'device capabilities updated', name: this.getSettings().name, interface: this.getSettings().interface, capabilities: this.capabilities });
327
+ this.emit('device-info', this.getSettings(), { capabilities: this.capabilities });
328
+ }
318
329
  async initControl(_props) {
319
330
  }
320
331
  async initControlBestEffort(_props) {
@@ -7,6 +7,8 @@ import { InterfaceFactory } from "./types.js";
7
7
  import { BleAdapterFactory } from "../factories/index.js";
8
8
  import { CSP } from "../consts.js";
9
9
  import { BKOOL_SERVICE, isBkoolDeviceName } from "../bkool/consts.js";
10
+ import { ZWIFT_FC82_UUID } from "../fm/consts.js";
11
+ import { useFeatureToggle } from "../../features/index.js";
10
12
  const BLE_EXPIRATION_TIMEOUT = 10 * 1000 * 60;
11
13
  const DEFAULT_PAUSE_REASON = 'app-background';
12
14
  const TEMPORARY_SCAN_TIMEOUT = 30 * 1000;
@@ -436,13 +438,18 @@ export class BleInterface extends EventEmitter {
436
438
  isDiscovering() {
437
439
  return this.discoverTask?.isRunning() === true;
438
440
  }
441
+ getServicesToAnnounce() {
442
+ if (!useFeatureToggle().has('VirtualShiftingFC82'))
443
+ return this.expectedServices;
444
+ return [...(this.expectedServices ?? []), ZWIFT_FC82_UUID];
445
+ }
439
446
  discoverPeripherals() {
440
447
  return new Promise(done => {
441
448
  const ble = this.getBinding();
442
- ble.setSupportedServices?.(this.expectedServices);
449
+ const announcedServices = this.getServicesToAnnounce();
450
+ ble.setSupportedServices?.(announcedServices);
443
451
  if (ble.setSupportedServices) {
444
- const expectedServices = this.expectedServices ?? [];
445
- const expected = expectedServices.join('|');
452
+ const expected = (announcedServices ?? []).join('|');
446
453
  this.logEvent({ message: 'announce expected services', expected });
447
454
  }
448
455
  ble.startScanning([], true, (err) => {
@@ -44,6 +44,9 @@ export class BlePeripheral {
44
44
  getDiscoveredServices() {
45
45
  return this.discoveredServiceUUIds ?? [];
46
46
  }
47
+ getKnownCharacteristics() {
48
+ return Object.keys(this.characteristics);
49
+ }
47
50
  getInfo() {
48
51
  return {
49
52
  id: this.announcement?.peripheral?.id,
@@ -1,7 +1,7 @@
1
1
  import { EventEmitter } from "node:events";
2
2
  import { EventLogger } from "gd-eventlog";
3
3
  import { runWithTimeout, sleep } from "../../utils/utils.js";
4
- import { beautifyUUID } from "../utils.js";
4
+ import { beautifyUUID, matches } from "../utils.js";
5
5
  export class TBleSensor extends EventEmitter {
6
6
  peripheral;
7
7
  static protocol;
@@ -56,6 +56,15 @@ export class TBleSensor extends EventEmitter {
56
56
  getSupportedServiceUUids() {
57
57
  return this.peripheral?.getDiscoveredServices();
58
58
  }
59
+ hasService(serviceUUID) {
60
+ const discovered = this.peripheral?.getDiscoveredServices?.() ?? [];
61
+ const raw = this.peripheral?.services ?? [];
62
+ const services = [
63
+ ...discovered,
64
+ ...raw.map(s => typeof s === 'string' ? s : s.uuid)
65
+ ];
66
+ return services.some(s => matches(s, serviceUUID));
67
+ }
59
68
  isMatching(serviceUUIDs) {
60
69
  const uuids = serviceUUIDs.map(uuid => beautifyUUID(uuid));
61
70
  const required = this.getServiceUUids();
@@ -0,0 +1,30 @@
1
+ export const MEASUREMENT_STALE_TIMEOUT = 3000;
2
+ export const parseHeartRate = (data) => {
3
+ if (data.length === 0)
4
+ return undefined;
5
+ if (data.length === 1)
6
+ return data.readUInt8(0);
7
+ const is16Bit = (data.readUInt8(0) & 0x1) !== 0;
8
+ return is16Bit ? data.readUInt16LE(1) : data.readUInt8(1);
9
+ };
10
+ export class HeartRateMeasurement {
11
+ heartrate;
12
+ parse(buffer) {
13
+ const data = Buffer.from(buffer);
14
+ try {
15
+ this.heartrate = parseHeartRate(data) ?? this.heartrate;
16
+ }
17
+ catch {
18
+ }
19
+ return { heartrate: this.heartrate, raw: `2a37:${data.toString('hex')}` };
20
+ }
21
+ getValue() {
22
+ return this.heartrate;
23
+ }
24
+ reset() {
25
+ this.heartrate = undefined;
26
+ }
27
+ }
28
+ export const isMeasurementStale = (tsMeasurement) => {
29
+ return tsMeasurement === undefined || Date.now() - tsMeasurement > MEASUREMENT_STALE_TIMEOUT;
30
+ };
@@ -10,6 +10,7 @@ export const POWER_RANGE = '2ad8';
10
10
  export const CSP = '1818';
11
11
  export const CSP_MEASUREMENT = '2a63';
12
12
  export const CSP_FEATURE = '2a65';
13
+ export const HRS = '180d';
13
14
  export const HR_MEASUREMENT = '2a37';
14
15
  export const CSC = '1816';
15
16
  export const CSC_MEASUREMENT = '2a5b';
@@ -35,6 +35,10 @@ export default class PwrAdapter extends BleAdapter {
35
35
  const powerStr = power ? ` (${power})` : '';
36
36
  return `${name}${powerStr}`;
37
37
  }
38
+ async checkCapabilities() {
39
+ if (this.addHeartRateCapability())
40
+ this.emitCapabilitiesChanged();
41
+ }
38
42
  mapData(deviceData) {
39
43
  const data = {
40
44
  isPedalling: false,
@@ -49,6 +53,7 @@ export default class PwrAdapter extends BleAdapter {
49
53
  data.power = deviceData?.instantaneousPower ?? data.power;
50
54
  data.pedalRpm = deviceData?.rpm ?? data.pedalRpm;
51
55
  data.time = deviceData?.time ?? data.time;
56
+ data.heartrate = deviceData?.heartrate ?? data.heartrate;
52
57
  data.isPedalling = data.pedalRpm > 0 || (data.pedalRpm === undefined && data.power > 0);
53
58
  return data;
54
59
  }
@@ -63,6 +68,8 @@ export default class PwrAdapter extends BleAdapter {
63
68
  };
64
69
  if (bikeData.time)
65
70
  data.deviceTime = bikeData.time;
71
+ if (this.hasCapability(IncyclistCapability.HeartRate))
72
+ data.heartrate = bikeData.heartrate;
66
73
  this.data = data;
67
74
  return data;
68
75
  }
@@ -1,4 +1,5 @@
1
- import { CSP, CSP_MEASUREMENT, CSP_FEATURE } from '../consts.js';
1
+ import { CSP, CSP_MEASUREMENT, CSP_FEATURE, HR_MEASUREMENT } from '../consts.js';
2
+ import { HeartRateMeasurement, isMeasurementStale } from '../characteristics/hr/measurement.js';
2
3
  import { matches } from '../utils.js';
3
4
  import { TBleSensor } from '../base/sensor.js';
4
5
  import { CrankCadenceCalculator } from '../crank-cadence.js';
@@ -7,13 +8,15 @@ export default class BleCyclingPowerDevice extends TBleSensor {
7
8
  static protocol = 'cp';
8
9
  static services = [CSP];
9
10
  static characteristics = [CSP_MEASUREMENT, CSP_FEATURE, '2a5d', '2a3c'];
10
- static detectionPriority = 1;
11
+ static detectionPriority = 3;
11
12
  instantaneousPower = undefined;
12
13
  balance = undefined;
13
14
  accTorque = undefined;
14
15
  rpm = undefined;
15
16
  time = undefined;
16
17
  crankCadence = new CrankCadenceCalculator();
18
+ hrm = new HeartRateMeasurement();
19
+ tsPower;
17
20
  parsePower(_data) {
18
21
  const data = Buffer.from(_data);
19
22
  try {
@@ -41,11 +44,26 @@ export default class BleCyclingPowerDevice extends TBleSensor {
41
44
  }
42
45
  catch {
43
46
  }
47
+ return { ...this.getPowerData(), raw: `2a63:${data.toString('hex')}` };
48
+ }
49
+ getPowerData() {
44
50
  const { instantaneousPower, balance, accTorque, rpm, time } = this;
45
- return { instantaneousPower, balance, accTorque, rpm, time, raw: `2a63:${data.toString('hex')}` };
51
+ const heartrate = this.hrm?.getValue();
52
+ return { instantaneousPower, balance, accTorque, rpm, time, ...(heartrate === undefined ? {} : { heartrate }) };
53
+ }
54
+ getHeartRateRecord(data) {
55
+ const { raw } = this.hrm.parse(data);
56
+ const res = { ...this.getPowerData(), raw };
57
+ if (isMeasurementStale(this.tsPower)) {
58
+ if (res.instantaneousPower !== undefined)
59
+ res.instantaneousPower = 0;
60
+ if (res.rpm !== undefined)
61
+ res.rpm = 0;
62
+ }
63
+ return res;
46
64
  }
47
65
  getRequiredCharacteristics() {
48
- return [CSP_MEASUREMENT];
66
+ return [CSP_MEASUREMENT, HR_MEASUREMENT];
49
67
  }
50
68
  onData(characteristic, characteristicData) {
51
69
  const data = Buffer.from(characteristicData);
@@ -53,10 +71,15 @@ export default class BleCyclingPowerDevice extends TBleSensor {
53
71
  if (!hasData)
54
72
  return false;
55
73
  if (matches(characteristic, CSP_MEASUREMENT)) {
74
+ this.tsPower = Date.now();
56
75
  const res = this.parsePower(data);
57
76
  this.emit('data', res);
58
77
  return false;
59
78
  }
79
+ if (matches(characteristic, HR_MEASUREMENT)) {
80
+ this.emit('data', this.getHeartRateRecord(data));
81
+ return false;
82
+ }
60
83
  return true;
61
84
  }
62
85
  reset() {
@@ -66,5 +89,7 @@ export default class BleCyclingPowerDevice extends TBleSensor {
66
89
  this.rpm = undefined;
67
90
  this.time = undefined;
68
91
  this.crankCadence = new CrankCadenceCalculator();
92
+ this.hrm?.reset();
93
+ this.tsPower = undefined;
69
94
  }
70
95
  }
@@ -14,23 +14,23 @@ export class BleCSCAdapter extends BleAdapter {
14
14
  super(settings, props);
15
15
  this.logger = new EventLogger('Ble-CSC');
16
16
  this.device = new BleCyclingSpeedCadenceDevice(this.getPeripheral(), { logger: this.logger });
17
- this.capabilities = BleCSCAdapter.CAPABILITIES;
17
+ this.capabilities = [...BleCSCAdapter.CAPABILITIES];
18
18
  }
19
19
  async checkCapabilities() {
20
20
  const before = this.capabilities.join(',');
21
21
  const sensor = this.getSensor();
22
22
  if (!sensor)
23
23
  return;
24
+ this.addHeartRateCapability();
24
25
  const features = await sensor.getFeatures();
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
- const after = this.capabilities.join(',');
30
- if (before !== after) {
31
- this.logEvent({ message: 'device capabilities updated', name: this.getSettings().name, interface: this.getSettings().interface, capabilities: this.capabilities });
32
- this.emit('device-info', this.getSettings(), { capabilities: this.capabilities });
26
+ if (features && (features.wheelRevolutionData !== undefined || features.crankRevolutionData !== undefined)) {
27
+ const supported = BleCSCAdapter.CAPABILITIES.filter(c => (c !== IncyclistCapability.Speed || features.wheelRevolutionData) &&
28
+ (c !== IncyclistCapability.Cadence || features.crankRevolutionData));
29
+ const heartrate = this.capabilities.filter(c => c === IncyclistCapability.HeartRate);
30
+ this.capabilities = [...supported, ...heartrate];
33
31
  }
32
+ if (before !== this.capabilities.join(','))
33
+ this.emitCapabilitiesChanged();
34
34
  }
35
35
  isSame(device) {
36
36
  if (!(device instanceof BleCSCAdapter))
@@ -47,8 +47,8 @@ export class BleCSCAdapter extends BleAdapter {
47
47
  return this.getName();
48
48
  }
49
49
  mapData(deviceData) {
50
- const { cadence, speed = 0 } = deviceData;
51
- return { cadence, pedalRpm: cadence, speed };
50
+ const { cadence, speed = 0, heartrate } = deviceData;
51
+ return { cadence, pedalRpm: cadence, speed, ...(heartrate === undefined ? {} : { heartrate }) };
52
52
  }
53
53
  transformData(bikeData) {
54
54
  if (bikeData === undefined)
@@ -61,6 +61,8 @@ export class BleCSCAdapter extends BleAdapter {
61
61
  };
62
62
  if (bikeData.time)
63
63
  data.deviceTime = bikeData.time;
64
+ if (this.hasCapability(IncyclistCapability.HeartRate))
65
+ data.heartrate = bikeData.heartrate;
64
66
  this.data = data;
65
67
  return data;
66
68
  }
@@ -1,5 +1,6 @@
1
- import { CSC, CSC_MEASUREMENT, CSC_FEATURE } from '../consts.js';
2
- import { beautifyUUID } from '../utils.js';
1
+ import { CSC, CSC_MEASUREMENT, CSC_FEATURE, HR_MEASUREMENT } from '../consts.js';
2
+ import { beautifyUUID, matches } from '../utils.js';
3
+ import { HeartRateMeasurement, isMeasurementStale } from '../characteristics/hr/measurement.js';
3
4
  import { TBleSensor } from '../base/sensor.js';
4
5
  import { CscMeasurement } from '../characteristics/csc/measurement.js';
5
6
  import { CscFeatures } from '../characteristics/csc/features.js';
@@ -8,11 +9,13 @@ export class BleCyclingSpeedCadenceDevice extends TBleSensor {
8
9
  static protocol = 'csc';
9
10
  static services = [CSC];
10
11
  static characteristics = [CSC_MEASUREMENT, CSC_FEATURE];
11
- static detectionPriority = 1;
12
+ static detectionPriority = 2;
12
13
  data;
13
14
  parsers = {};
14
15
  featureParser;
15
16
  measurement = new CscMeasurement();
17
+ hrm = new HeartRateMeasurement();
18
+ tsMeasurement;
16
19
  constructor(peripheral, props) {
17
20
  super(peripheral, props);
18
21
  this.data = {};
@@ -20,7 +23,7 @@ export class BleCyclingSpeedCadenceDevice extends TBleSensor {
20
23
  this.parsers[beautifyUUID(CSC_MEASUREMENT)] = this.measurement;
21
24
  }
22
25
  getRequiredCharacteristics() {
23
- return [CSC_MEASUREMENT];
26
+ return [CSC_MEASUREMENT, HR_MEASUREMENT];
24
27
  }
25
28
  async getFeatures() {
26
29
  try {
@@ -39,11 +42,16 @@ export class BleCyclingSpeedCadenceDevice extends TBleSensor {
39
42
  const hasData = super.onData(characteristic, data);
40
43
  if (!hasData)
41
44
  return false;
45
+ if (matches(characteristic, HR_MEASUREMENT)) {
46
+ this.onHeartRate(data);
47
+ return true;
48
+ }
42
49
  const uuid = beautifyUUID(characteristic);
43
50
  const parser = this.parsers[uuid];
44
51
  if (!parser) {
45
52
  return false;
46
53
  }
54
+ this.tsMeasurement = Date.now();
47
55
  const parsed = parser.parse(data);
48
56
  this.data = { ...this.data, ...parsed };
49
57
  if (this.data.speed < 0) {
@@ -53,8 +61,20 @@ export class BleCyclingSpeedCadenceDevice extends TBleSensor {
53
61
  this.emit('data', this.data);
54
62
  return true;
55
63
  }
64
+ onHeartRate(data) {
65
+ this.data = { ...this.data, ...this.hrm.parse(data) };
66
+ const res = { ...this.data };
67
+ const stale = isMeasurementStale(this.tsMeasurement);
68
+ if (res.speed !== undefined && (stale || res.speed < 0))
69
+ res.speed = 0;
70
+ if (res.cadence !== undefined && stale)
71
+ res.cadence = 0;
72
+ this.emit('data', res);
73
+ }
56
74
  reset() {
57
75
  this.data = {};
76
+ this.tsMeasurement = undefined;
77
+ this.hrm?.reset();
58
78
  this.measurement?.reset();
59
79
  }
60
80
  onDisconnect() {
@@ -48,7 +48,12 @@ export default class BleFmAdapter extends BleAdapter {
48
48
  supportsVirtualShifting() {
49
49
  if (!this.getFeatureToggle().has('VirtualShifting'))
50
50
  return false;
51
- return this.device?.supportsVirtualShifting() ?? false;
51
+ return (this.device?.supportsVirtualShifting() ?? false) || this.supportsVirtualShiftingViaFC82();
52
+ }
53
+ supportsVirtualShiftingViaFC82() {
54
+ if (!this.getFeatureToggle().has('VirtualShiftingFC82'))
55
+ return false;
56
+ return this.device?.supportsVirtualShiftingViaFC82() ?? false;
52
57
  }
53
58
  getSupportedCyclingModes() {
54
59
  const modes = [PowerMeterCyclingMode];
@@ -59,3 +59,5 @@ export const TargetSettingFeatureFlag = {
59
59
  TargetedCadenceConfigurationSupported: bit(16)
60
60
  };
61
61
  export const ZWIFT_PLAY_UUID = '0000000119ca465186e5fa29dcdd09d1';
62
+ export const ZWIFT_FC82_UUID = 'fc82';
63
+ export const ZWIFT_SYNC_RX_UUID = '00000003-19ca-4651-86e5-fa29dcdd09d1';
@@ -1,7 +1,8 @@
1
- import { FTMS, FTMS_CP, FTMS_STATUS, INDOOR_BIKE_DATA, ROWER_DATA, TREADMILL_DATA } from "../consts.js";
1
+ import { CSP, FTMS, FTMS_CP, FTMS_STATUS, HRS, INDOOR_BIKE_DATA, ROWER_DATA, TREADMILL_DATA } from "../consts.js";
2
+ import { parseHeartRate } from "../characteristics/hr/measurement.js";
2
3
  import { TBleSensor } from "../base/sensor.js";
3
4
  import { beautifyUUID, bit, matches } from "../utils.js";
4
- import { IndoorBikeDataFlag, FitnessMachineFeatureFlag, TargetSettingFeatureFlag, ZWIFT_PLAY_UUID } from "./consts.js";
5
+ import { IndoorBikeDataFlag, FitnessMachineFeatureFlag, TargetSettingFeatureFlag, ZWIFT_PLAY_UUID, ZWIFT_FC82_UUID, ZWIFT_SYNC_RX_UUID } from "./consts.js";
5
6
  import { InteruptableTask } from "../../utils/task.js";
6
7
  const BLE_COMMAND_TIMEOUT = 800;
7
8
  export default class BleFitnessMachineDevice extends TBleSensor {
@@ -182,18 +183,7 @@ export default class BleFitnessMachineDevice extends TBleSensor {
182
183
  if (data.length == 0)
183
184
  return { ...this.data, raw: '<empty>' };
184
185
  try {
185
- if (data.length === 1) {
186
- this.data.heartrate = data.readUInt8(0);
187
- }
188
- else {
189
- const flags = data.readUInt8(0);
190
- if ((flags & 0x1) === 0) {
191
- this.data.heartrate = data.readUInt8(1);
192
- }
193
- else {
194
- this.data.heartrate = data.readUInt16LE(1);
195
- }
196
- }
186
+ this.data.heartrate = parseHeartRate(data);
197
187
  }
198
188
  catch (err) {
199
189
  if (!this.hrmParseErrorLogged) {
@@ -548,14 +538,8 @@ export default class BleFitnessMachineDevice extends TBleSensor {
548
538
  }
549
539
  const buffer = data ? Buffer.from(data) : undefined;
550
540
  try {
551
- const discovered = this.peripheral?.getDiscoveredServices?.() ?? [];
552
- const rawServices = this.peripheral?.services ?? [];
553
- const services = [
554
- ...discovered,
555
- ...rawServices.map(s => typeof s === 'string' ? s : s.uuid)
556
- ];
557
- let power = services.some(s => matches(s, '1818'));
558
- let heartrate = services.some(s => matches(s, '180d'));
541
+ let power = this.hasService(CSP);
542
+ let heartrate = this.hasService(HRS);
559
543
  const dataLength = buffer?.length ?? 0;
560
544
  const fitnessMachine = dataLength >= 4 ? buffer.readUInt32LE(0) : 0;
561
545
  const targetSettings = dataLength >= 8 ? buffer.readUInt32LE(4) : 0;
@@ -582,6 +566,13 @@ export default class BleFitnessMachineDevice extends TBleSensor {
582
566
  supportsVirtualShifting() {
583
567
  return this.getSupportedServiceUUids()?.some(s => matches(s, ZWIFT_PLAY_UUID));
584
568
  }
569
+ supportsVirtualShiftingViaFC82() {
570
+ const hasService = this.getSupportedServiceUUids()?.some(s => matches(s, ZWIFT_FC82_UUID));
571
+ if (!hasService)
572
+ return false;
573
+ const characteristics = this.peripheral?.getKnownCharacteristics?.() ?? [];
574
+ return characteristics.length === 0 || characteristics.some(c => matches(c, ZWIFT_SYNC_RX_UUID));
575
+ }
585
576
  buildFitnessMachineInfo(fitnessMachine) {
586
577
  if (fitnessMachine == null)
587
578
  return;
@@ -31,6 +31,7 @@ export class DirectConnectPeripheral {
31
31
  subscribed = [];
32
32
  onDisconnectHandler;
33
33
  discoveredServiceUUIds;
34
+ knownCharacteristics = new Set();
34
35
  id;
35
36
  constructor(announcement) {
36
37
  this.announcement = announcement;
@@ -53,6 +54,9 @@ export class DirectConnectPeripheral {
53
54
  getDiscoveredServices() {
54
55
  return this.discoveredServiceUUIds;
55
56
  }
57
+ getKnownCharacteristics() {
58
+ return Array.from(this.knownCharacteristics);
59
+ }
56
60
  async connect() {
57
61
  if (this.isConnected())
58
62
  return true;
@@ -155,6 +159,7 @@ export class DirectConnectPeripheral {
155
159
  const service = beautifyUUID(res.body.serviceUUID);
156
160
  const characteristics = res.body.characteristicDefinitions.map(cd => `${beautifyUUID(cd.characteristicUUID)}:${cd.properties.join('/')}`);
157
161
  this.logEvent({ message: 'DiscoverCharacteritics response', path: this.getPath(), rc, service, characteristics, raw: response.toString('hex') });
162
+ res.body.characteristicDefinitions.forEach(cd => this.knownCharacteristics.add(beautifyUUID(cd.characteristicUUID)));
158
163
  return res.body.characteristicDefinitions.map(c => ({ uuid: c.characteristicUUID, properties: c.properties }));
159
164
  }
160
165
  catch (err) {
@@ -60,6 +60,8 @@ export default class BleAdapter<TDeviceData extends BleDeviceData, TDevice exten
60
60
  protected shouldWaitForData(): boolean;
61
61
  protected waitForInitialData(startupTimeout: any): Promise<void>;
62
62
  protected checkCapabilities(): Promise<void>;
63
+ protected addHeartRateCapability(): boolean;
64
+ protected emitCapabilitiesChanged(): void;
63
65
  protected initControl(_props?: BleStartProperties): Promise<void>;
64
66
  protected initControlBestEffort(_props?: BleStartProperties): Promise<void>;
65
67
  protected getStartLogProps(props: BleStartProperties): BleStartProperties;
@@ -87,6 +87,7 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
87
87
  protected emitDisconnectAllPeripherals(): void;
88
88
  protected disconnectAllPeripherals(): Promise<void>;
89
89
  protected isDiscovering(): boolean;
90
+ protected getServicesToAnnounce(): string[];
90
91
  protected discoverPeripherals(): Promise<void>;
91
92
  protected onPeripheralFound(peripheral: BleRawPeripheral): void;
92
93
  protected checkForBkoolEnhancement(announcement: BlePeripheralAnnouncement): boolean;
@@ -28,6 +28,7 @@ export declare class BlePeripheral implements IBlePeripheral {
28
28
  getInterface(): IBleInterface<any>;
29
29
  getAnnouncedServices(): string[];
30
30
  getDiscoveredServices(): string[];
31
+ getKnownCharacteristics(): string[];
31
32
  getInfo(): BleDeviceIdentifier;
32
33
  connect(): Promise<boolean>;
33
34
  disconnect(connectionLost?: boolean): Promise<boolean>;
@@ -29,6 +29,7 @@ export declare class TBleSensor extends EventEmitter implements IBleSensor {
29
29
  getProtocol(): BleProtocol;
30
30
  getServiceUUids(): string[];
31
31
  getSupportedServiceUUids(): string[];
32
+ hasService(serviceUUID: string): boolean;
32
33
  isMatching(serviceUUIDs: string[]): boolean;
33
34
  hasPeripheral(): boolean;
34
35
  pair(): Promise<boolean>;
@@ -4,6 +4,7 @@ import { CrankCadenceCalculator } from "../../crank-cadence.js";
4
4
  export type CyclingCadenceAndSpeed = {
5
5
  cadence?: number;
6
6
  speed?: number;
7
+ heartrate?: number;
7
8
  raw?: string;
8
9
  };
9
10
  export declare class CscMeasurement implements CharacteristicParser<CyclingCadenceAndSpeed> {
@@ -0,0 +1,13 @@
1
+ export declare const MEASUREMENT_STALE_TIMEOUT = 3000;
2
+ export type HeartRateValue = {
3
+ heartrate?: number;
4
+ raw?: string;
5
+ };
6
+ export declare const parseHeartRate: (data: Buffer) => number | undefined;
7
+ export declare class HeartRateMeasurement {
8
+ protected heartrate: number | undefined;
9
+ parse(buffer: Buffer): HeartRateValue;
10
+ getValue(): number | undefined;
11
+ reset(): void;
12
+ }
13
+ export declare const isMeasurementStale: (tsMeasurement: number | undefined) => boolean;
@@ -10,6 +10,7 @@ export declare const POWER_RANGE = "2ad8";
10
10
  export declare const CSP = "1818";
11
11
  export declare const CSP_MEASUREMENT = "2a63";
12
12
  export declare const CSP_FEATURE = "2a65";
13
+ export declare const HRS = "180d";
13
14
  export declare const HR_MEASUREMENT = "2a37";
14
15
  export declare const CSC = "1816";
15
16
  export declare const CSC_MEASUREMENT = "2a5b";
@@ -14,6 +14,7 @@ export default class PwrAdapter extends BleAdapter<PowerData, BleCyclingPowerDev
14
14
  updateSensor(peripheral: IBlePeripheral): void;
15
15
  getProfile(): LegacyProfile;
16
16
  getDisplayName(): string;
17
+ protected checkCapabilities(): Promise<void>;
17
18
  mapData(deviceData: PowerData): IncyclistBikeData;
18
19
  transformData(bikeData: IncyclistBikeData): IncyclistAdapterData;
19
20
  stop(): Promise<boolean>;
@@ -1,4 +1,5 @@
1
1
  import { PowerData } from './types.js';
2
+ import { HeartRateMeasurement } from '../characteristics/hr/measurement.js';
2
3
  import { LegacyProfile } from '../../antv2/types.js';
3
4
  import { BleProtocol } from '../types.js';
4
5
  import { TBleSensor } from '../base/sensor.js';
@@ -8,14 +9,18 @@ export default class BleCyclingPowerDevice extends TBleSensor {
8
9
  static readonly protocol: BleProtocol;
9
10
  static readonly services: string[];
10
11
  static readonly characteristics: string[];
11
- static readonly detectionPriority = 1;
12
+ static readonly detectionPriority = 3;
12
13
  instantaneousPower: number;
13
14
  balance: number;
14
15
  accTorque: number;
15
16
  rpm: number;
16
17
  time: number;
17
18
  protected crankCadence: CrankCadenceCalculator;
19
+ protected hrm: HeartRateMeasurement;
20
+ protected tsPower: number | undefined;
18
21
  parsePower(_data: Uint8Array): PowerData;
22
+ protected getPowerData(): PowerData;
23
+ protected getHeartRateRecord(data: Buffer): PowerData;
19
24
  protected getRequiredCharacteristics(): Array<string>;
20
25
  onData(characteristic: string, characteristicData: Buffer): boolean;
21
26
  reset(): void;
@@ -4,6 +4,7 @@ export type PowerData = {
4
4
  accTorque?: number;
5
5
  time: number;
6
6
  rpm: number;
7
+ heartrate?: number;
7
8
  raw?: string;
8
9
  };
9
10
  export type CrankData = {
@@ -1,3 +1,4 @@
1
+ import { HeartRateMeasurement } from '../characteristics/hr/measurement.js';
1
2
  import { LegacyProfile } from '../../antv2/types.js';
2
3
  import { BleProtocol } from '../types.js';
3
4
  import { TBleSensor } from '../base/sensor.js';
@@ -10,15 +11,18 @@ export declare class BleCyclingSpeedCadenceDevice extends TBleSensor implements
10
11
  static readonly protocol: BleProtocol;
11
12
  static readonly services: string[];
12
13
  static readonly characteristics: string[];
13
- static readonly detectionPriority = 1;
14
+ static readonly detectionPriority = 2;
14
15
  protected data: CyclingCadenceAndSpeed;
15
16
  protected parsers: Record<string, CharacteristicParser<any>>;
16
17
  protected featureParser: CscFeatures;
17
18
  protected measurement: CscMeasurement;
19
+ protected hrm: HeartRateMeasurement;
20
+ protected tsMeasurement: number | undefined;
18
21
  constructor(peripheral: any, props?: any);
19
22
  protected getRequiredCharacteristics(): Array<string>;
20
23
  getFeatures(): Promise<BleCSCFeatures>;
21
24
  onData(characteristic: string, characteristicData: Buffer): boolean;
25
+ protected onHeartRate(data: Buffer): void;
22
26
  reset(): void;
23
27
  protected onDisconnect(): void;
24
28
  setWheelCircumference(wheelCircumference: number): Promise<void>;
@@ -2,5 +2,6 @@ import { BleDeviceData } from "../base/types.js";
2
2
  export interface CSCData extends BleDeviceData {
3
3
  speed?: number;
4
4
  cadence?: number;
5
+ heartrate?: number;
5
6
  raw?: string;
6
7
  }
@@ -24,6 +24,7 @@ export default class BleFmAdapter extends BleAdapter<IndoorBikeData, BleFitnessM
24
24
  isSame(device: IAdapter): boolean;
25
25
  isControllable(): boolean;
26
26
  supportsVirtualShifting(): boolean;
27
+ protected supportsVirtualShiftingViaFC82(): boolean;
27
28
  getSupportedCyclingModes(): Array<typeof CyclingMode>;
28
29
  getDefaultCyclingMode(): ICyclingMode;
29
30
  mapData(deviceData: IndoorBikeData): IncyclistBikeData;
@@ -98,3 +98,5 @@ export declare const TargetSettingFeatureFlag: {
98
98
  TargetedCadenceConfigurationSupported: number;
99
99
  };
100
100
  export declare const ZWIFT_PLAY_UUID = "0000000119ca465186e5fa29dcdd09d1";
101
+ export declare const ZWIFT_FC82_UUID = "fc82";
102
+ export declare const ZWIFT_SYNC_RX_UUID = "00000003-19ca-4651-86e5-fa29dcdd09d1";
@@ -50,6 +50,7 @@ export default class BleFitnessMachineDevice extends TBleSensor {
50
50
  protected parseServiceData(): FtmsServiceData | undefined;
51
51
  getFitnessMachineFeatures(): Promise<IndoorBikeFeatures | undefined>;
52
52
  supportsVirtualShifting(): boolean;
53
+ supportsVirtualShiftingViaFC82(): boolean;
53
54
  protected buildFitnessMachineInfo(fitnessMachine?: number): string[] | undefined;
54
55
  protected buildTargetSettingsInfo(targetSettings?: number): string[] | undefined;
55
56
  protected writeFtmsMessage(requestedOpCode: number, data: Buffer, props?: BleWriteProps): Promise<number>;
@@ -172,6 +172,7 @@ export interface IBlePeripheral {
172
172
  getInfo(): BleDeviceIdentifier;
173
173
  getAnnouncedServices(): string[];
174
174
  getDiscoveredServices(): string[];
175
+ getKnownCharacteristics?(): string[];
175
176
  getInterface(): IBleInterface<any>;
176
177
  verifyCharacteristics?(): Promise<{
177
178
  missing: string[];
@@ -20,12 +20,14 @@ export declare class DirectConnectPeripheral implements IBlePeripheral {
20
20
  protected subscribed: Array<string>;
21
21
  protected onDisconnectHandler: () => void;
22
22
  protected discoveredServiceUUIds: Array<string>;
23
+ protected knownCharacteristics: Set<string>;
23
24
  id: number;
24
25
  constructor(announcement: MulticastDnsAnnouncement);
25
26
  getInfo(): BleDeviceIdentifier;
26
27
  get services(): BleService[];
27
28
  getAnnouncedServices(): string[];
28
29
  getDiscoveredServices(): string[];
30
+ getKnownCharacteristics(): string[];
29
31
  connect(): Promise<boolean>;
30
32
  disconnect(connectionLost?: boolean): Promise<boolean>;
31
33
  isConnected(): boolean;
@@ -1,4 +1,4 @@
1
- export type Feature = 'VirtualShifting';
1
+ export type Feature = 'VirtualShifting' | 'VirtualShiftingFC82' | 'KettlerRacerBluetooth';
2
2
  export declare class FeatureToggle {
3
3
  protected static _instance: FeatureToggle;
4
4
  static getInstance(): FeatureToggle;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "3.0.55",
3
+ "version": "3.0.57",
4
4
  "scripts": {
5
5
  "lint": "eslint . --ext .ts",
6
6
  "build": "npm run build:esm && npm run build:cjs",