incyclist-devices 1.5.2 → 1.5.3

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.
@@ -0,0 +1,90 @@
1
+ /// <reference types="node" />
2
+ import { BleDevice } from './ble-device';
3
+ import BleInterface from './ble-interface';
4
+ import BleProtocol from './incyclist-protocol';
5
+ import { BleDeviceClass } from './ble';
6
+ import DeviceAdapter, { DeviceData } from '../device';
7
+ import { DeviceProtocol } from '../protocol';
8
+ import { EventLogger } from 'gd-eventlog';
9
+ import CyclingMode from '../cycling-mode';
10
+ import { IncyclistBikeData } from '../cycling-mode';
11
+ export declare const ELITE_TRAINER_SVC = "347b0001";
12
+ declare type PowerData = {
13
+ instantaneousPower?: number;
14
+ balance?: number;
15
+ accTorque?: number;
16
+ time: number;
17
+ rpm: number;
18
+ raw?: string;
19
+ };
20
+ declare type CrankData = {
21
+ revolutions?: number;
22
+ time?: number;
23
+ cntUpdateMissing?: number;
24
+ };
25
+ export default class BleEliteDevice extends BleDevice {
26
+ static services: string[];
27
+ static characteristics: string[];
28
+ static detectionPriority: number;
29
+ instantaneousPower: number;
30
+ balance: number;
31
+ accTorque: number;
32
+ rpm: number;
33
+ timeOffset: number;
34
+ time: number;
35
+ currentCrankData: CrankData;
36
+ prevCrankData: CrankData;
37
+ constructor(props?: any);
38
+ isMatching(characteristics: string[]): boolean;
39
+ init(): Promise<boolean>;
40
+ getProfile(): string;
41
+ getServiceUUids(): string[];
42
+ parseCrankData(crankData: any): {
43
+ rpm: number;
44
+ time: any;
45
+ };
46
+ parsePower(_data: Uint8Array): PowerData;
47
+ onData(characteristic: string, data: Buffer): boolean;
48
+ reset(): void;
49
+ }
50
+ export declare class EliteAdapter extends DeviceAdapter {
51
+ device: BleEliteDevice;
52
+ ignore: boolean;
53
+ ble: BleInterface;
54
+ protocol: DeviceProtocol;
55
+ paused: boolean;
56
+ logger: EventLogger;
57
+ mode: CyclingMode;
58
+ distanceInternal: number;
59
+ prevDataTS: number;
60
+ userSettings: {
61
+ weight?: number;
62
+ };
63
+ bikeSettings: {
64
+ weight?: number;
65
+ };
66
+ constructor(device: BleDeviceClass, protocol: BleProtocol);
67
+ isBike(): boolean;
68
+ isHrm(): boolean;
69
+ isPower(): boolean;
70
+ isSame(device: DeviceAdapter): boolean;
71
+ getProfile(): string;
72
+ getName(): string;
73
+ getDisplayName(): string;
74
+ getCyclingMode(): CyclingMode;
75
+ getDefaultCyclingMode(): CyclingMode;
76
+ getSupportedCyclingModes(): any[];
77
+ getPort(): string;
78
+ getWeight(): number;
79
+ setIgnoreBike(ignore: any): void;
80
+ setIgnorePower(ignore: any): void;
81
+ onDeviceData(deviceData: PowerData): void;
82
+ mapData(deviceData: PowerData): IncyclistBikeData;
83
+ transformData(bikeData: IncyclistBikeData): DeviceData;
84
+ start(props?: any): Promise<any>;
85
+ stop(): Promise<boolean>;
86
+ sendUpdate(request: any): Promise<void>;
87
+ pause(): Promise<boolean>;
88
+ resume(): Promise<boolean>;
89
+ }
90
+ export {};
@@ -0,0 +1,322 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ var __importDefault = (this && this.__importDefault) || function (mod) {
31
+ return (mod && mod.__esModule) ? mod : { "default": mod };
32
+ };
33
+ Object.defineProperty(exports, "__esModule", { value: true });
34
+ exports.EliteAdapter = exports.ELITE_TRAINER_SVC = void 0;
35
+ const ble_device_1 = require("./ble-device");
36
+ const ble_interface_1 = __importDefault(require("./ble-interface"));
37
+ const ble_1 = require("./ble");
38
+ const device_1 = __importStar(require("../device"));
39
+ const gd_eventlog_1 = require("gd-eventlog");
40
+ const consts_1 = require("./consts");
41
+ const power_meter_1 = __importDefault(require("../modes/power-meter"));
42
+ exports.ELITE_TRAINER_SVC = '347b0001';
43
+ class BleEliteDevice extends ble_device_1.BleDevice {
44
+ constructor(props) {
45
+ super(props);
46
+ this.instantaneousPower = undefined;
47
+ this.balance = undefined;
48
+ this.accTorque = undefined;
49
+ this.rpm = undefined;
50
+ this.timeOffset = 0;
51
+ this.time = undefined;
52
+ this.currentCrankData = undefined;
53
+ this.prevCrankData = undefined;
54
+ }
55
+ isMatching(characteristics) {
56
+ if (!characteristics)
57
+ return false;
58
+ const hasCPMeasurement = characteristics.find(c => c === consts_1.CSP_MEASUREMENT) !== undefined;
59
+ const hasCPFeature = characteristics.find(c => c === consts_1.CSP_FEATURE) !== undefined;
60
+ return hasCPMeasurement && hasCPFeature;
61
+ }
62
+ init() {
63
+ const _super = Object.create(null, {
64
+ init: { get: () => super.init }
65
+ });
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ try {
68
+ yield _super.init.call(this);
69
+ }
70
+ catch (err) {
71
+ return Promise.resolve(false);
72
+ }
73
+ });
74
+ }
75
+ getProfile() {
76
+ return 'Power Meter';
77
+ }
78
+ getServiceUUids() {
79
+ return BleEliteDevice.services;
80
+ }
81
+ parseCrankData(crankData) {
82
+ if (!this.prevCrankData)
83
+ this.prevCrankData = { revolutions: 0, time: 0, cntUpdateMissing: -1 };
84
+ const c = this.currentCrankData = crankData;
85
+ const p = this.prevCrankData;
86
+ let rpm = this.rpm;
87
+ let hasUpdate = c.time !== p.time;
88
+ if (hasUpdate) {
89
+ let time = c.time - p.time;
90
+ let revs = c.revolutions - p.revolutions;
91
+ if (c.time < p.time) {
92
+ time += 0x10000;
93
+ this.timeOffset += 0x10000;
94
+ }
95
+ if (c.revolutions < p.revolutions)
96
+ revs += 0x10000;
97
+ rpm = 1024 * 60 * revs / time;
98
+ }
99
+ else {
100
+ if (p.cntUpdateMissing < 0 || p.cntUpdateMissing > 2) {
101
+ rpm = 0;
102
+ }
103
+ }
104
+ const cntUpdateMissing = p.cntUpdateMissing;
105
+ this.prevCrankData = this.currentCrankData;
106
+ if (hasUpdate)
107
+ this.prevCrankData.cntUpdateMissing = 0;
108
+ else
109
+ this.prevCrankData.cntUpdateMissing = cntUpdateMissing + 1;
110
+ return { rpm, time: this.timeOffset + c.time };
111
+ }
112
+ parsePower(_data) {
113
+ const data = Buffer.from(_data);
114
+ try {
115
+ let offset = 4;
116
+ const flags = data.readUInt16LE(0);
117
+ this.instantaneousPower = data.readUInt16LE(2);
118
+ if (flags & 0x1)
119
+ this.balance = data.readUInt8(offset++);
120
+ if (flags & 0x4) {
121
+ this.accTorque = data.readUInt16LE(offset);
122
+ offset += 2;
123
+ }
124
+ if (flags & 0x10) {
125
+ }
126
+ if (flags & 0x20) {
127
+ const crankData = {
128
+ revolutions: data.readUInt16LE(offset),
129
+ time: data.readUInt16LE(offset + 2)
130
+ };
131
+ const { rpm, time } = this.parseCrankData(crankData);
132
+ this.rpm = rpm;
133
+ this.time = time;
134
+ offset += 4;
135
+ }
136
+ }
137
+ catch (err) {
138
+ }
139
+ const { instantaneousPower, balance, accTorque, rpm, time } = this;
140
+ return { instantaneousPower, balance, accTorque, rpm, time, raw: `2a63:${data.toString('hex')}` };
141
+ }
142
+ onData(characteristic, data) {
143
+ const hasData = super.onData(characteristic, data);
144
+ if (!hasData)
145
+ return false;
146
+ if ((0, ble_1.matches)(characteristic, consts_1.CSP_MEASUREMENT)) {
147
+ const res = this.parsePower(data);
148
+ this.emit('data', res);
149
+ return false;
150
+ }
151
+ return true;
152
+ }
153
+ reset() {
154
+ this.instantaneousPower = undefined;
155
+ this.balance = undefined;
156
+ this.accTorque = undefined;
157
+ this.rpm = undefined;
158
+ this.timeOffset = 0;
159
+ this.time = undefined;
160
+ this.currentCrankData = undefined;
161
+ this.prevCrankData = undefined;
162
+ }
163
+ }
164
+ exports.default = BleEliteDevice;
165
+ BleEliteDevice.services = [exports.ELITE_TRAINER_SVC];
166
+ BleEliteDevice.characteristics = [consts_1.CSP_MEASUREMENT, consts_1.CSP_FEATURE, '2a5d', '2a3c'];
167
+ BleEliteDevice.detectionPriority = 10;
168
+ ble_interface_1.default.register('BleCyclingPowerDevice', 'cp', BleEliteDevice, BleEliteDevice.services);
169
+ class EliteAdapter extends device_1.default {
170
+ constructor(device, protocol) {
171
+ super(protocol);
172
+ this.ignore = false;
173
+ this.paused = false;
174
+ this.distanceInternal = 0;
175
+ this.device = device;
176
+ this.ble = protocol.ble;
177
+ this.mode = this.getDefaultCyclingMode();
178
+ this.logger = new gd_eventlog_1.EventLogger('Ble-Elite');
179
+ }
180
+ isBike() { return true; }
181
+ isHrm() { return false; }
182
+ isPower() { return true; }
183
+ isSame(device) {
184
+ if (!(device instanceof EliteAdapter))
185
+ return false;
186
+ const adapter = device;
187
+ return (adapter.getName() === this.getName() && adapter.getProfile() === this.getProfile());
188
+ }
189
+ getProfile() {
190
+ return 'Elite Smart Trainer';
191
+ }
192
+ getName() {
193
+ return `${this.device.name}`;
194
+ }
195
+ getDisplayName() {
196
+ const { name, instantaneousPower: power } = this.device;
197
+ const powerStr = power ? ` (${power})` : '';
198
+ return `${name}${powerStr}`;
199
+ }
200
+ getCyclingMode() {
201
+ if (!this.mode)
202
+ this.mode = this.getDefaultCyclingMode();
203
+ return this.mode;
204
+ }
205
+ getDefaultCyclingMode() {
206
+ return new power_meter_1.default(this);
207
+ }
208
+ getSupportedCyclingModes() {
209
+ return [power_meter_1.default];
210
+ }
211
+ getPort() {
212
+ return 'ble';
213
+ }
214
+ getWeight() {
215
+ let userWeight = device_1.DEFAULT_USER_WEIGHT;
216
+ let bikeWeight = device_1.DEFAULT_BIKE_WEIGHT;
217
+ if (this.userSettings && this.userSettings.weight) {
218
+ userWeight = Number(this.userSettings.weight);
219
+ }
220
+ if (this.bikeSettings && this.bikeSettings.weight) {
221
+ bikeWeight = Number(this.bikeSettings.weight);
222
+ }
223
+ return bikeWeight + userWeight;
224
+ }
225
+ setIgnoreBike(ignore) {
226
+ this.ignore = ignore;
227
+ }
228
+ setIgnorePower(ignore) {
229
+ this.ignore = ignore;
230
+ }
231
+ onDeviceData(deviceData) {
232
+ if (this.prevDataTS && Date.now() - this.prevDataTS < 1000)
233
+ return;
234
+ this.prevDataTS = Date.now();
235
+ this.logger.logEvent({ message: 'onDeviceData', data: deviceData });
236
+ let incyclistData = this.mapData(deviceData);
237
+ incyclistData = this.getCyclingMode().updateData(incyclistData);
238
+ const data = this.transformData(incyclistData);
239
+ if (this.onDataFn && !this.ignore && !this.paused)
240
+ this.onDataFn(data);
241
+ }
242
+ mapData(deviceData) {
243
+ const data = {
244
+ isPedalling: false,
245
+ power: 0,
246
+ pedalRpm: undefined,
247
+ speed: 0,
248
+ heartrate: 0,
249
+ distanceInternal: 0,
250
+ slope: undefined,
251
+ time: undefined
252
+ };
253
+ data.power = (deviceData.instantaneousPower !== undefined ? deviceData.instantaneousPower : data.power);
254
+ data.pedalRpm = (deviceData.rpm !== undefined ? deviceData.rpm : data.pedalRpm);
255
+ data.time = (deviceData.time !== undefined ? deviceData.time : data.time);
256
+ data.isPedalling = data.pedalRpm > 0 || (data.pedalRpm === undefined && data.power > 0);
257
+ return data;
258
+ }
259
+ transformData(bikeData) {
260
+ if (this.ignore) {
261
+ return {};
262
+ }
263
+ if (bikeData === undefined)
264
+ return;
265
+ let distance = 0;
266
+ if (this.distanceInternal !== undefined && bikeData.distanceInternal !== undefined) {
267
+ distance = Math.round(bikeData.distanceInternal - this.distanceInternal);
268
+ }
269
+ if (bikeData.distanceInternal !== undefined)
270
+ this.distanceInternal = bikeData.distanceInternal;
271
+ let data = {
272
+ speed: bikeData.speed,
273
+ slope: bikeData.slope,
274
+ power: bikeData.power !== undefined ? Math.round(bikeData.power) : undefined,
275
+ cadence: bikeData.pedalRpm !== undefined ? Math.round(bikeData.pedalRpm) : undefined,
276
+ distance,
277
+ timestamp: Date.now()
278
+ };
279
+ return data;
280
+ }
281
+ start(props) {
282
+ return __awaiter(this, void 0, void 0, function* () {
283
+ if (props && props.user)
284
+ this.userSettings = props.user;
285
+ if (props && props.bikeSettings)
286
+ this.bikeSettings = props.bikeSettings;
287
+ this.logger.logEvent({ message: 'csp: start requested', profile: this.getProfile(), props });
288
+ try {
289
+ const bleDevice = yield this.ble.connectDevice(this.device);
290
+ if (bleDevice) {
291
+ this.device = bleDevice;
292
+ bleDevice.on('data', (data) => {
293
+ this.onDeviceData(data);
294
+ });
295
+ return true;
296
+ }
297
+ }
298
+ catch (err) {
299
+ this.logger.logEvent({ message: 'start result: error', error: err.message, profile: this.getProfile() });
300
+ throw new Error(`could not start device, reason:${err.message}`);
301
+ }
302
+ });
303
+ }
304
+ stop() {
305
+ return __awaiter(this, void 0, void 0, function* () {
306
+ this.logger.logEvent({ message: 'stop requested', profile: this.getProfile() });
307
+ this.distanceInternal = 0;
308
+ this.device.reset();
309
+ return this.device.disconnect();
310
+ });
311
+ }
312
+ sendUpdate(request) {
313
+ return __awaiter(this, void 0, void 0, function* () {
314
+ if (this.paused)
315
+ return;
316
+ this.getCyclingMode().sendBikeUpdate(request);
317
+ });
318
+ }
319
+ pause() { this.paused = true; return Promise.resolve(true); }
320
+ resume() { this.paused = false; return Promise.resolve(true); }
321
+ }
322
+ exports.EliteAdapter = EliteAdapter;
@@ -41,6 +41,7 @@ const hrm_1 = __importStar(require("./hrm"));
41
41
  const pwr_1 = __importStar(require("./pwr"));
42
42
  const wahoo_kickr_1 = __importStar(require("./wahoo-kickr"));
43
43
  const tacx_1 = __importStar(require("./tacx"));
44
+ const elite_1 = __importStar(require("./elite"));
44
45
  class BleProtocol extends protocol_1.default {
45
46
  constructor(binding) {
46
47
  super();
@@ -76,6 +77,7 @@ class BleProtocol extends protocol_1.default {
76
77
  case 'smart trainer':
77
78
  case 'wahoo smart trainer':
78
79
  case 'fitness machine':
80
+ case 'elite smart trainer':
79
81
  case tacx_1.default.PROFILE.toLowerCase():
80
82
  let device;
81
83
  if (fromDevice)
@@ -87,6 +89,10 @@ class BleProtocol extends protocol_1.default {
87
89
  device = device || new wahoo_kickr_1.default(props());
88
90
  return new wahoo_kickr_1.WahooAdvancedFmAdapter(device, this);
89
91
  }
92
+ if (profile.toLowerCase() === 'elite smart trainer') {
93
+ device = device || new elite_1.default(props());
94
+ return new elite_1.EliteAdapter(device, this);
95
+ }
90
96
  else if (profile === tacx_1.default.PROFILE) {
91
97
  device = device || new tacx_1.default(props());
92
98
  return new tacx_1.TacxBleFEAdapter(device, this);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "dependencies": {
5
5
  "@serialport/parser-byte-length": "^9.0.1",
6
6
  "@serialport/parser-delimiter": "^9.0.1",