incyclist-devices 1.4.48 → 1.4.51

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,372 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.WahooAdvancedFmAdapter = void 0;
39
+ const ble_interface_1 = __importDefault(require("./ble-interface"));
40
+ const Device_1 = require("../Device");
41
+ const gd_eventlog_1 = require("gd-eventlog");
42
+ const fm_1 = __importStar(require("./fm"));
43
+ const WAHOO_ADVANCED_FTMS = 'a026e005';
44
+ const WAHOO_ADVANCED_TRAINER_CP = 'a026e037';
45
+ const cwABike = {
46
+ race: 0.35,
47
+ triathlon: 0.29,
48
+ mountain: 0.57
49
+ };
50
+ const cRR = 0.0036;
51
+ const ErgWriteDelay = 2000;
52
+ class WahooAdvancedFitnessMachineDevice extends fm_1.default {
53
+ constructor(props) {
54
+ super(props);
55
+ this.prevCrankData = undefined;
56
+ this.currentCrankData = undefined;
57
+ this.timeOffset = 0;
58
+ this.tsPrevWrite = undefined;
59
+ this.data = {};
60
+ }
61
+ init() {
62
+ const _super = Object.create(null, {
63
+ init: { get: () => super.init }
64
+ });
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ try {
67
+ this.logEvent({ message: 'get device info' });
68
+ yield _super.init.call(this);
69
+ this.logEvent({ message: 'device info', deviceInfo: this.deviceInfo, features: this.features });
70
+ }
71
+ catch (err) {
72
+ return Promise.resolve(false);
73
+ }
74
+ });
75
+ }
76
+ getProfile() {
77
+ return 'Wahoo Smart Trainer';
78
+ }
79
+ getServiceUUids() {
80
+ return WahooAdvancedFitnessMachineDevice.services;
81
+ }
82
+ isBike() {
83
+ return true;
84
+ }
85
+ isPower() {
86
+ return true;
87
+ }
88
+ isHrm() {
89
+ return this.hasService('180d');
90
+ }
91
+ parseCrankData(crankData) {
92
+ if (!this.prevCrankData)
93
+ this.prevCrankData = { revolutions: 0, time: 0, cntUpdateMissing: -1 };
94
+ const c = this.currentCrankData = crankData;
95
+ const p = this.prevCrankData;
96
+ let rpm = this.data.cadence;
97
+ let hasUpdate = c.time !== p.time;
98
+ if (hasUpdate) {
99
+ let time = c.time - p.time;
100
+ let revs = c.revolutions - p.revolutions;
101
+ if (c.time < p.time) {
102
+ time += 0x10000;
103
+ this.timeOffset += 0x10000;
104
+ }
105
+ if (c.revolutions < p.revolutions)
106
+ revs += 0x10000;
107
+ rpm = 1024 * 60 * revs / time;
108
+ }
109
+ else {
110
+ if (p.cntUpdateMissing < 0 || p.cntUpdateMissing > 2) {
111
+ rpm = 0;
112
+ }
113
+ }
114
+ const cntUpdateMissing = p.cntUpdateMissing;
115
+ this.prevCrankData = this.currentCrankData;
116
+ if (hasUpdate)
117
+ this.prevCrankData.cntUpdateMissing = 0;
118
+ else
119
+ this.prevCrankData.cntUpdateMissing = cntUpdateMissing + 1;
120
+ return { rpm, time: this.timeOffset + c.time };
121
+ }
122
+ parsePower(_data) {
123
+ const data = Buffer.from(_data);
124
+ try {
125
+ let offset = 4;
126
+ const flags = data.readUInt16LE(0);
127
+ this.data.instantaneousPower = data.readUInt16LE(2);
128
+ if (flags & 0x1)
129
+ data.readUInt8(offset++);
130
+ if (flags & 0x4) {
131
+ data.readUInt16LE(offset);
132
+ offset += 2;
133
+ }
134
+ if (flags & 0x20) {
135
+ const crankData = {
136
+ revolutions: data.readUInt16LE(offset),
137
+ time: data.readUInt16LE(offset + 2)
138
+ };
139
+ const { rpm, time } = this.parseCrankData(crankData);
140
+ this.data.cadence = rpm;
141
+ this.data.time = time;
142
+ offset += 4;
143
+ }
144
+ }
145
+ catch (err) {
146
+ }
147
+ const { instantaneousPower, cadence, time } = this.data;
148
+ return { instantaneousPower, cadence, time, raw: data.toString('hex') };
149
+ }
150
+ onData(characteristic, data) {
151
+ super.onData(characteristic, data);
152
+ const uuid = characteristic.toLocaleLowerCase();
153
+ let res = undefined;
154
+ switch (uuid) {
155
+ case '2a63':
156
+ res = this.parsePower(data);
157
+ break;
158
+ case '2ad2':
159
+ res = this.parseIndoorBikeData(data);
160
+ break;
161
+ case '2a37':
162
+ res = this.parseHrm(data);
163
+ break;
164
+ case '2ada':
165
+ res = this.parseFitnessMachineStatus(data);
166
+ break;
167
+ default:
168
+ break;
169
+ }
170
+ if (res)
171
+ this.emit('data', res);
172
+ }
173
+ writeWahooFtmsMessage(requestedOpCode, data) {
174
+ return __awaiter(this, void 0, void 0, function* () {
175
+ try {
176
+ const opcode = Buffer.alloc(1);
177
+ opcode.writeUInt8(requestedOpCode, 0);
178
+ const message = Buffer.concat([opcode, data]);
179
+ const res = yield this.write(WAHOO_ADVANCED_FTMS, message);
180
+ const responseData = Buffer.from(res);
181
+ const result = responseData.readUInt8(0);
182
+ this.logEvent({ message: 'response', opCode: requestedOpCode, response: responseData.toString('hex') });
183
+ return result === 1;
184
+ }
185
+ catch (err) {
186
+ this.logEvent({ message: 'writeWahooFtmsMessage failed', opCode: requestedOpCode, reason: err.message });
187
+ return false;
188
+ }
189
+ });
190
+ }
191
+ requestControl() {
192
+ return __awaiter(this, void 0, void 0, function* () {
193
+ if (this.hasControl)
194
+ return true;
195
+ this.logEvent({ message: 'requestControl' });
196
+ const data = Buffer.alloc(2);
197
+ data.writeUInt8(0xEE, 0);
198
+ data.writeUInt8(0xFC, 1);
199
+ const res = yield this.writeWahooFtmsMessage(32, data);
200
+ if (res === true) {
201
+ this.hasControl = true;
202
+ }
203
+ else {
204
+ this.logEvent({ message: 'requestControl failed' });
205
+ }
206
+ return this.hasControl;
207
+ });
208
+ }
209
+ setPowerAdjusting() {
210
+ this.tsPrevWrite = Date.now();
211
+ }
212
+ isPowerAdjusting() {
213
+ if (this.tsPrevWrite === undefined)
214
+ return false;
215
+ if (this.tsPrevWrite < Date.now() - ErgWriteDelay) {
216
+ this.tsPrevWrite = undefined;
217
+ return false;
218
+ }
219
+ return true;
220
+ }
221
+ setErgMode(power) {
222
+ return __awaiter(this, void 0, void 0, function* () {
223
+ if (this.isPowerAdjusting())
224
+ return false;
225
+ const data = Buffer.alloc(2);
226
+ data.writeInt16LE(Math.round(power), 0);
227
+ const res = yield this.writeWahooFtmsMessage(66, data);
228
+ if (res === true) {
229
+ this.setPowerAdjusting();
230
+ this.data.targetPower = power;
231
+ }
232
+ return res;
233
+ });
234
+ }
235
+ setSimMode(weight, crr, cw) {
236
+ return __awaiter(this, void 0, void 0, function* () {
237
+ const data = Buffer.alloc(6);
238
+ data.writeInt16LE(Math.round(weight * 100), 0);
239
+ data.writeInt16LE(Math.round(crr * 10000), 2);
240
+ data.writeInt16LE(Math.round(cw * 1000), 4);
241
+ const res = yield this.writeWahooFtmsMessage(67, data);
242
+ return res;
243
+ });
244
+ }
245
+ setSimCRR(crr) {
246
+ return __awaiter(this, void 0, void 0, function* () {
247
+ const data = Buffer.alloc(2);
248
+ data.writeInt16LE(Math.round(crr * 10000), 0);
249
+ const res = yield this.writeWahooFtmsMessage(68, data);
250
+ return res;
251
+ });
252
+ }
253
+ setSimWindResistance(cw) {
254
+ return __awaiter(this, void 0, void 0, function* () {
255
+ const data = Buffer.alloc(2);
256
+ data.writeInt16LE(Math.round(cw * 1000), 0);
257
+ const res = yield this.writeWahooFtmsMessage(69, data);
258
+ return res;
259
+ });
260
+ }
261
+ setSimGrade(slope) {
262
+ return __awaiter(this, void 0, void 0, function* () {
263
+ const value = (Math.min(1, Math.max(-1, slope)) + 1.0) * 65535 / 2.0;
264
+ const data = Buffer.alloc(2);
265
+ data.writeInt16LE(Math.round(value), 0);
266
+ const res = yield this.writeWahooFtmsMessage(70, data);
267
+ return res;
268
+ });
269
+ }
270
+ setSimWindSpeed(v) {
271
+ return __awaiter(this, void 0, void 0, function* () {
272
+ const value = (Math.max(-32.767, Math.min(32.767, v)) + 32.767) * 1000;
273
+ const data = Buffer.alloc(2);
274
+ data.writeInt16LE(Math.round(value), 0);
275
+ const res = yield this.writeWahooFtmsMessage(71, data);
276
+ return res;
277
+ });
278
+ }
279
+ setTargetPower(power) {
280
+ return __awaiter(this, void 0, void 0, function* () {
281
+ this.logEvent({ message: 'setTargetPower', power, skip: (this.data.targetPower !== undefined && this.data.targetPower === power) });
282
+ if (this.data.targetPower !== undefined && this.data.targetPower === power)
283
+ return true;
284
+ const hasControl = yield this.requestControl();
285
+ if (!hasControl) {
286
+ this.logEvent({ message: 'setTargetPower failed', reason: 'control is disabled' });
287
+ return false;
288
+ }
289
+ return yield this.setErgMode(power);
290
+ });
291
+ }
292
+ setSlope(slope) {
293
+ return __awaiter(this, void 0, void 0, function* () {
294
+ this.logEvent({ message: 'setSlope', slope });
295
+ const { windSpeed, crr, cw } = this;
296
+ return yield this.setIndoorBikeSimulation(windSpeed, slope, crr, cw);
297
+ });
298
+ }
299
+ reset() {
300
+ this.data = {};
301
+ }
302
+ }
303
+ exports.default = WahooAdvancedFitnessMachineDevice;
304
+ WahooAdvancedFitnessMachineDevice.services = ['a026ee0b'];
305
+ WahooAdvancedFitnessMachineDevice.characteristics = ['2acc', '2ad2', '2ad6', '2ad8', '2ad9', '2ada', WAHOO_ADVANCED_FTMS, WAHOO_ADVANCED_TRAINER_CP];
306
+ ble_interface_1.default.register('WahooAdvancedFitnessMachineDevice', 'wahoo-fm', WahooAdvancedFitnessMachineDevice, WahooAdvancedFitnessMachineDevice.services);
307
+ class WahooAdvancedFmAdapter extends fm_1.FmAdapter {
308
+ constructor(device, protocol) {
309
+ super(device, protocol);
310
+ this.device = device;
311
+ this.ble = protocol.ble;
312
+ this.cyclingMode = this.getDefaultCyclingMode();
313
+ this.logger = new gd_eventlog_1.EventLogger('BLE-WahooFM');
314
+ if (this.device)
315
+ this.device.setLogger(this.logger);
316
+ }
317
+ isSame(device) {
318
+ if (!(device instanceof WahooAdvancedFmAdapter))
319
+ return false;
320
+ const adapter = device;
321
+ return (adapter.getName() === this.getName() && adapter.getProfile() === this.getProfile());
322
+ }
323
+ getProfile() {
324
+ return 'Wahoo Smart Trainer';
325
+ }
326
+ start(props) {
327
+ return __awaiter(this, void 0, void 0, function* () {
328
+ this.logger.logEvent({ message: 'start requested', profile: this.getProfile(), props });
329
+ if (this.ble.isScanning())
330
+ yield this.ble.stopScan();
331
+ try {
332
+ const bleDevice = yield this.ble.connectDevice(this.device);
333
+ bleDevice.setLogger(this.logger);
334
+ if (bleDevice) {
335
+ this.device = bleDevice;
336
+ const mode = this.getCyclingMode();
337
+ if (mode && mode.getSetting('bikeType')) {
338
+ const bikeType = mode.getSetting('bikeType').toLowerCase();
339
+ this.device.setCrr(cRR);
340
+ switch (bikeType) {
341
+ case 'race':
342
+ this.device.setCw(cwABike.race);
343
+ break;
344
+ case 'triathlon':
345
+ this.device.setCw(cwABike.triathlon);
346
+ break;
347
+ case 'mountain':
348
+ this.device.setCw(cwABike.mountain);
349
+ break;
350
+ }
351
+ }
352
+ const { user } = props || {};
353
+ const weight = (user && user.weight ? user.weight : Device_1.DEFAULT_USER_WEIGHT) + Device_1.DEFAULT_BIKE_WEIGHT;
354
+ this.device.setSimMode(weight, this.device.getCrr(), this.device.getCw());
355
+ const startRequest = this.getCyclingMode().getBikeInitRequest();
356
+ yield this.sendUpdate(startRequest);
357
+ bleDevice.on('data', (data) => {
358
+ this.onDeviceData(data);
359
+ });
360
+ return true;
361
+ }
362
+ }
363
+ catch (err) {
364
+ this.logger.logEvent({ message: 'start result: error', error: err.message, profile: this.getProfile() });
365
+ throw new Error(`could not start device, reason:${err.message}`);
366
+ }
367
+ });
368
+ }
369
+ pause() { this.paused = true; return Promise.resolve(true); }
370
+ resume() { this.paused = false; return Promise.resolve(true); }
371
+ }
372
+ exports.WahooAdvancedFmAdapter = WahooAdvancedFmAdapter;
@@ -37,7 +37,6 @@ class PowerBasedCyclingModeBase extends CyclingMode_1.CyclingModeBase {
37
37
  let powerToMaintainSpeed = calculations_1.default.calculatePower(m, vPrev, slope, props);
38
38
  const powerDelta = powerToMaintainSpeed - power;
39
39
  const Ekin = EkinPrev - powerDelta * t;
40
- console.log('~~~~ calculateSpeedAndDistance', Ekin, powerToMaintainSpeed, power, m, vPrev, slope);
41
40
  if (Ekin > 0) {
42
41
  const v = Math.sqrt(2 * Ekin / m);
43
42
  const speed = v * 3.6;
@@ -53,7 +53,7 @@ class PowerMeterCyclingMode extends power_base_1.default {
53
53
  power = 0;
54
54
  }
55
55
  const m = this.getWeight();
56
- let t = this.getTimeSinceLastUpdate();
56
+ const t = this.getTimeSinceLastUpdate();
57
57
  const { speed, distance } = this.calculateSpeedAndDistance(power, slope, m, t);
58
58
  data.power = Math.round(power);
59
59
  data.slope = slope;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "1.4.48",
3
+ "version": "1.4.51",
4
4
  "dependencies": {
5
5
  "@serialport/parser-byte-length": "^9.0.1",
6
6
  "@serialport/parser-delimiter": "^9.0.1",