incyclist-devices 1.4.9 → 1.4.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/CyclingMode.js +0 -1
- package/lib/Device.d.ts +2 -0
- package/lib/Device.js +2 -0
- package/lib/DeviceProtocol.d.ts +4 -1
- package/lib/DeviceProtocol.js +0 -1
- package/lib/DeviceSupport.d.ts +2 -0
- package/lib/DeviceSupport.js +11 -22
- package/lib/ant/AntAdapter.js +0 -1
- package/lib/ant/AntScanner.js +7 -20
- package/lib/ant/antfe/AntFEAdapter.js +8 -8
- package/lib/ant/anthrm/AntHrmAdapter.js +1 -1
- package/lib/ant/utils.js +1 -3
- package/lib/calculations.js +0 -1
- package/lib/daum/DaumAdapter.js +18 -13
- package/lib/daum/SmartTrainerCyclingMode.js +0 -1
- package/lib/daum/classic/DaumClassicAdapter.js +1 -1
- package/lib/daum/classic/DaumClassicProtocol.js +7 -19
- package/lib/daum/classic/bike.js +26 -26
- package/lib/daum/classic/utils.js +0 -1
- package/lib/daum/constants.js +0 -1
- package/lib/daum/premium/DaumPremiumAdapter.js +1 -1
- package/lib/daum/premium/DaumPremiumProtocol.js +7 -19
- package/lib/daum/premium/bike.js +17 -18
- package/lib/daum/premium/utils.js +0 -1
- package/lib/kettler/comms.d.ts +48 -0
- package/lib/kettler/comms.js +194 -0
- package/lib/kettler/ergo-racer/adapter.d.ts +99 -0
- package/lib/kettler/ergo-racer/adapter.js +659 -0
- package/lib/kettler/ergo-racer/modes/power-meter.d.ts +18 -0
- package/lib/kettler/ergo-racer/modes/power-meter.js +78 -0
- package/lib/kettler/ergo-racer/protocol.d.ts +41 -0
- package/lib/kettler/ergo-racer/protocol.js +191 -0
- package/lib/simulator/Simulator.js +2 -15
- package/lib/types/command.d.ts +8 -0
- package/lib/types/command.js +2 -0
- package/lib/types/route.js +0 -1
- package/lib/types/user.js +0 -1
- package/lib/utils.js +1 -3
- package/package.json +1 -1
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
12
|
+
if (mod && mod.__esModule) return mod;
|
|
13
|
+
var result = {};
|
|
14
|
+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
15
|
+
result["default"] = mod;
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
19
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
const Device_1 = __importStar(require("../../Device"));
|
|
23
|
+
const gd_eventlog_1 = require("gd-eventlog");
|
|
24
|
+
const comms_1 = __importDefault(require("../comms"));
|
|
25
|
+
const utils_1 = require("../../utils");
|
|
26
|
+
const power_meter_1 = __importDefault(require("./modes/power-meter"));
|
|
27
|
+
class KettlerRacerAdapter extends Device_1.default {
|
|
28
|
+
constructor(protocol, settings) {
|
|
29
|
+
super(protocol);
|
|
30
|
+
this.requests = [];
|
|
31
|
+
this.logger = new gd_eventlog_1.EventLogger('KettlerRacer');
|
|
32
|
+
this.settings = settings;
|
|
33
|
+
this.ignoreHrm = false;
|
|
34
|
+
this.ignorePower = false;
|
|
35
|
+
this.ignoreBike = false;
|
|
36
|
+
this.paused = false;
|
|
37
|
+
this.iv = null;
|
|
38
|
+
this.comms = new comms_1.default({ protocol, port: this.settings.port, logger: this.logger });
|
|
39
|
+
}
|
|
40
|
+
isBike() { return true; }
|
|
41
|
+
isPower() { return true; }
|
|
42
|
+
isHrm() { return true; }
|
|
43
|
+
setID(id) {
|
|
44
|
+
this.id = id;
|
|
45
|
+
}
|
|
46
|
+
getID() {
|
|
47
|
+
return this.id;
|
|
48
|
+
}
|
|
49
|
+
getName() {
|
|
50
|
+
return this.settings.name || this.getProtocolName();
|
|
51
|
+
}
|
|
52
|
+
getPort() {
|
|
53
|
+
return this.settings.port;
|
|
54
|
+
}
|
|
55
|
+
setIgnoreHrm(ignore) {
|
|
56
|
+
this.ignoreHrm = ignore;
|
|
57
|
+
}
|
|
58
|
+
setIgnorePower(ignore) {
|
|
59
|
+
this.ignorePower = ignore;
|
|
60
|
+
}
|
|
61
|
+
setIgnoreBike(ignore) {
|
|
62
|
+
this.ignoreBike = ignore;
|
|
63
|
+
}
|
|
64
|
+
getLogger() {
|
|
65
|
+
return this.logger;
|
|
66
|
+
}
|
|
67
|
+
getUserSettings() {
|
|
68
|
+
return this.settings.userSettings || { weight: Device_1.DEFAULT_USER_WEIGHT };
|
|
69
|
+
}
|
|
70
|
+
getWeight() {
|
|
71
|
+
let userWeight = Device_1.DEFAULT_USER_WEIGHT;
|
|
72
|
+
let bikeWeight = Device_1.DEFAULT_BIKE_WEIGHT;
|
|
73
|
+
if (this.settings.userSettings && this.settings.userSettings.weight) {
|
|
74
|
+
userWeight = this.settings.userSettings.weight;
|
|
75
|
+
}
|
|
76
|
+
if (this.settings.bikeSettings && this.settings.bikeSettings.weight) {
|
|
77
|
+
userWeight = this.settings.bikeSettings.weight;
|
|
78
|
+
}
|
|
79
|
+
return bikeWeight + userWeight;
|
|
80
|
+
}
|
|
81
|
+
setComputerMode() {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
return this.send('setComputerMode', 'CP').then(response => {
|
|
84
|
+
if (response === 'ACK' || response === 'RUN') {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
setClientMode() {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
return this.send('setClientMode', 'CM').then(response => {
|
|
96
|
+
if (response === 'ACK' || response === 'RUN') {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
reset() {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
return this.send('reset', 'RS').then(response => {
|
|
108
|
+
if (response === 'ACK' || response === 'RUN') {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
getIdentifier() {
|
|
118
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
119
|
+
return this.send('getIdentifier', 'ID').then(response => {
|
|
120
|
+
return response.substring(0, 3);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
getInterface() {
|
|
125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
return this.send('getInterface', 'KI');
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
getVersion() {
|
|
130
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
return this.send('getVersion', 'VE');
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
getCalibration() {
|
|
135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
return this.send('getCalibration', 'CA');
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
startTraining() {
|
|
140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
return this.send('startTraining', 'LB');
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
unknownSN() {
|
|
145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
+
return this.send('SN', 'SN');
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
setBaudrate(baudrate) {
|
|
150
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
151
|
+
return this.send(`setBaudrate(${baudrate})`, `BR${baudrate}`);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
setPower(power) {
|
|
155
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
156
|
+
return this.send(`setPower(${power})`, `PW${power}`);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
getExtendedStatus() {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
+
return this.send('getExtendedStatus', 'ES1').then(response => {
|
|
162
|
+
const data = this.parseExtendedStatus(response);
|
|
163
|
+
return data;
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
getStatus() {
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
return this.send('getStatus', 'ST').then(response => {
|
|
170
|
+
const data = this.parseStatus(response);
|
|
171
|
+
return data;
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
getDB() {
|
|
176
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
+
return this.send('getDB', 'DB');
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
send(logStr, message, timeout) {
|
|
181
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
182
|
+
const opened = yield this.waitForOpened();
|
|
183
|
+
if (!opened) {
|
|
184
|
+
throw new Error('connection error');
|
|
185
|
+
}
|
|
186
|
+
return new Promise((resolve, reject) => {
|
|
187
|
+
this.comms.send({ logStr, message, onError: reject, onResponse: resolve, timeout });
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
parseExtendedStatus(data) {
|
|
192
|
+
const result = {};
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
parseStatus(data) {
|
|
196
|
+
const states = data.split('\t');
|
|
197
|
+
const result = {};
|
|
198
|
+
if (states.length === 8) {
|
|
199
|
+
const hr = parseInt(states[0]);
|
|
200
|
+
if (!isNaN(hr)) {
|
|
201
|
+
result.heartrate = hr;
|
|
202
|
+
}
|
|
203
|
+
var cadence = parseInt(states[1]);
|
|
204
|
+
if (!isNaN(cadence)) {
|
|
205
|
+
result.cadence = cadence;
|
|
206
|
+
}
|
|
207
|
+
const speed = parseInt(states[2]);
|
|
208
|
+
if (!isNaN(speed)) {
|
|
209
|
+
result.speed = speed * 0.1;
|
|
210
|
+
}
|
|
211
|
+
const distance = parseInt(states[3]);
|
|
212
|
+
if (!isNaN(distance)) {
|
|
213
|
+
result.distance = distance;
|
|
214
|
+
}
|
|
215
|
+
const requestedPower = parseInt(states[4]);
|
|
216
|
+
if (!isNaN(requestedPower)) {
|
|
217
|
+
result.requestedPower = requestedPower;
|
|
218
|
+
}
|
|
219
|
+
const energy = parseInt(states[5]);
|
|
220
|
+
if (!isNaN(energy)) {
|
|
221
|
+
result.requestedPower = energy;
|
|
222
|
+
}
|
|
223
|
+
const timeStr = states[6];
|
|
224
|
+
const time = timeStr.split(':');
|
|
225
|
+
const hours = parseInt(time[0]);
|
|
226
|
+
const minutes = parseInt(time[1]);
|
|
227
|
+
if (!isNaN(hours) && !isNaN(minutes)) {
|
|
228
|
+
result.time = hours * 60 + minutes;
|
|
229
|
+
}
|
|
230
|
+
const power = parseInt(states[7]);
|
|
231
|
+
if (!isNaN(power)) {
|
|
232
|
+
result.power = power;
|
|
233
|
+
}
|
|
234
|
+
result.timestamp = Date.now();
|
|
235
|
+
}
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
check() {
|
|
239
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
240
|
+
var info = {};
|
|
241
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
242
|
+
this.logger.logEvent({ message: "check()", port: this.getPort() });
|
|
243
|
+
let iv = undefined;
|
|
244
|
+
try {
|
|
245
|
+
if (!info.opened)
|
|
246
|
+
info.opened = yield this.waitForOpened();
|
|
247
|
+
iv = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
|
|
248
|
+
reject(new Error(`timeout`));
|
|
249
|
+
}), 5000);
|
|
250
|
+
try {
|
|
251
|
+
yield this.getVersion();
|
|
252
|
+
}
|
|
253
|
+
catch (e) { }
|
|
254
|
+
try {
|
|
255
|
+
yield this.getInterface();
|
|
256
|
+
}
|
|
257
|
+
catch (e) { }
|
|
258
|
+
try {
|
|
259
|
+
yield this.getIdentifier();
|
|
260
|
+
}
|
|
261
|
+
catch (e) { }
|
|
262
|
+
try {
|
|
263
|
+
yield this.getExtendedStatus();
|
|
264
|
+
}
|
|
265
|
+
catch (e) { }
|
|
266
|
+
try {
|
|
267
|
+
yield this.getStatus();
|
|
268
|
+
}
|
|
269
|
+
catch (e) { }
|
|
270
|
+
try {
|
|
271
|
+
yield this.setClientMode();
|
|
272
|
+
}
|
|
273
|
+
catch (e) { }
|
|
274
|
+
try {
|
|
275
|
+
yield this.getVersion();
|
|
276
|
+
}
|
|
277
|
+
catch (e) { }
|
|
278
|
+
try {
|
|
279
|
+
yield this.getInterface();
|
|
280
|
+
}
|
|
281
|
+
catch (e) { }
|
|
282
|
+
try {
|
|
283
|
+
yield this.getIdentifier();
|
|
284
|
+
}
|
|
285
|
+
catch (e) { }
|
|
286
|
+
try {
|
|
287
|
+
yield this.getExtendedStatus();
|
|
288
|
+
}
|
|
289
|
+
catch (e) { }
|
|
290
|
+
try {
|
|
291
|
+
yield this.getStatus();
|
|
292
|
+
}
|
|
293
|
+
catch (e) { }
|
|
294
|
+
try {
|
|
295
|
+
yield this.setPower(100);
|
|
296
|
+
}
|
|
297
|
+
catch (e) { }
|
|
298
|
+
try {
|
|
299
|
+
yield this.reset();
|
|
300
|
+
}
|
|
301
|
+
catch (e) { }
|
|
302
|
+
try {
|
|
303
|
+
yield this.setComputerMode();
|
|
304
|
+
}
|
|
305
|
+
catch (e) { }
|
|
306
|
+
try {
|
|
307
|
+
yield this.getVersion();
|
|
308
|
+
}
|
|
309
|
+
catch (e) { }
|
|
310
|
+
try {
|
|
311
|
+
yield this.getInterface();
|
|
312
|
+
}
|
|
313
|
+
catch (e) { }
|
|
314
|
+
try {
|
|
315
|
+
yield this.getIdentifier();
|
|
316
|
+
}
|
|
317
|
+
catch (e) { }
|
|
318
|
+
try {
|
|
319
|
+
yield this.getExtendedStatus();
|
|
320
|
+
}
|
|
321
|
+
catch (e) { }
|
|
322
|
+
try {
|
|
323
|
+
yield this.getStatus();
|
|
324
|
+
}
|
|
325
|
+
catch (e) { }
|
|
326
|
+
try {
|
|
327
|
+
yield this.setPower(100);
|
|
328
|
+
}
|
|
329
|
+
catch (e) { }
|
|
330
|
+
clearTimeout(iv);
|
|
331
|
+
resolve(info);
|
|
332
|
+
}
|
|
333
|
+
catch (err) {
|
|
334
|
+
clearTimeout(iv);
|
|
335
|
+
iv = undefined;
|
|
336
|
+
reject(err);
|
|
337
|
+
}
|
|
338
|
+
}));
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
start(props) {
|
|
342
|
+
this.logger.logEvent({ message: 'start()' });
|
|
343
|
+
const opts = props || {};
|
|
344
|
+
var info = {};
|
|
345
|
+
return utils_1.runWithRetries(() => __awaiter(this, void 0, void 0, function* () {
|
|
346
|
+
try {
|
|
347
|
+
if (!info.checkDone) {
|
|
348
|
+
info.checkDone = yield this.check();
|
|
349
|
+
}
|
|
350
|
+
if (!info.started) {
|
|
351
|
+
info.started = yield this.startTraining();
|
|
352
|
+
}
|
|
353
|
+
if (!info.data) {
|
|
354
|
+
info.data = yield this.getStatus();
|
|
355
|
+
}
|
|
356
|
+
return info.data;
|
|
357
|
+
}
|
|
358
|
+
catch (err) {
|
|
359
|
+
throw (new Error(`could not start device, reason:${err.message}`));
|
|
360
|
+
}
|
|
361
|
+
}), 5, 1000)
|
|
362
|
+
.then(data => {
|
|
363
|
+
this.startUpdatePull();
|
|
364
|
+
return data;
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
startUpdatePull() {
|
|
368
|
+
if (this.iv)
|
|
369
|
+
return;
|
|
370
|
+
if (this.ignoreBike && this.ignoreHrm && this.ignorePower)
|
|
371
|
+
return;
|
|
372
|
+
const ivSync = setInterval(() => {
|
|
373
|
+
this.bikeSync();
|
|
374
|
+
}, 1000);
|
|
375
|
+
const ivUpdate = setInterval(() => {
|
|
376
|
+
this.sendData();
|
|
377
|
+
this.refreshRequests();
|
|
378
|
+
}, 1000);
|
|
379
|
+
this.iv = {
|
|
380
|
+
sync: ivSync,
|
|
381
|
+
update: ivUpdate
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
stop() {
|
|
385
|
+
this.logger.logEvent({ message: 'stop request' });
|
|
386
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
387
|
+
try {
|
|
388
|
+
if (this.iv) {
|
|
389
|
+
if (this.iv.sync)
|
|
390
|
+
clearInterval(this.iv.sync);
|
|
391
|
+
if (this.iv.update)
|
|
392
|
+
clearInterval(this.iv.update);
|
|
393
|
+
this.iv = undefined;
|
|
394
|
+
}
|
|
395
|
+
yield this.waitForClosed();
|
|
396
|
+
this.logger.logEvent({ message: 'stop request completed' });
|
|
397
|
+
this.paused = undefined;
|
|
398
|
+
resolve(true);
|
|
399
|
+
}
|
|
400
|
+
catch (err) {
|
|
401
|
+
this.logger.logEvent({ message: 'stop error', error: err.message });
|
|
402
|
+
this.paused = undefined;
|
|
403
|
+
reject(err);
|
|
404
|
+
}
|
|
405
|
+
}));
|
|
406
|
+
}
|
|
407
|
+
pause() {
|
|
408
|
+
this.logger.logEvent({ message: 'pause' });
|
|
409
|
+
return new Promise(resolve => {
|
|
410
|
+
this.paused = true;
|
|
411
|
+
resolve(true);
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
resume() {
|
|
415
|
+
this.logger.logEvent({ message: 'resume' });
|
|
416
|
+
return new Promise(resolve => {
|
|
417
|
+
this.paused = false;
|
|
418
|
+
resolve(true);
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
mapData(bikeData) {
|
|
422
|
+
let data = {};
|
|
423
|
+
data.isPedalling = bikeData.cadence > 0;
|
|
424
|
+
data.power = bikeData.power;
|
|
425
|
+
data.pedalRpm = bikeData.cadence;
|
|
426
|
+
data.speed = bikeData.speed;
|
|
427
|
+
data.heartrate = bikeData.heartrate;
|
|
428
|
+
data.distanceInternal = bikeData.distance;
|
|
429
|
+
data.time = bikeData.time;
|
|
430
|
+
return data;
|
|
431
|
+
}
|
|
432
|
+
transformData(internalData, bikeData) {
|
|
433
|
+
let data = {};
|
|
434
|
+
data.heartrate = internalData.heartrate;
|
|
435
|
+
data.timestamp = Date.now();
|
|
436
|
+
data.deviceTime = bikeData.time;
|
|
437
|
+
if (!this.ignoreBike) {
|
|
438
|
+
data.speed = internalData.speed;
|
|
439
|
+
data.power = internalData.power;
|
|
440
|
+
data.cadence = internalData.pedalRpm;
|
|
441
|
+
data.distance = internalData.distanceInternal;
|
|
442
|
+
data.deviceDistanceCounter = bikeData.distance;
|
|
443
|
+
}
|
|
444
|
+
if (this.ignoreHrm)
|
|
445
|
+
delete this.data.heartrate;
|
|
446
|
+
if (this.ignorePower) {
|
|
447
|
+
delete this.data.power;
|
|
448
|
+
delete this.data.cadence;
|
|
449
|
+
}
|
|
450
|
+
return data;
|
|
451
|
+
}
|
|
452
|
+
update() {
|
|
453
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
454
|
+
this.updateBusy = true;
|
|
455
|
+
this.getStatus()
|
|
456
|
+
.then((bikeData) => {
|
|
457
|
+
this.kettlerData = bikeData;
|
|
458
|
+
let data = this.mapData(bikeData);
|
|
459
|
+
data = this.getCyclingMode().updateData(data);
|
|
460
|
+
this.data = this.transformData(data, bikeData);
|
|
461
|
+
this.updateBusy = false;
|
|
462
|
+
})
|
|
463
|
+
.catch(err => {
|
|
464
|
+
this.logger.logEvent({ message: 'bike update error', error: err.message, stack: err.stack });
|
|
465
|
+
this.updateBusy = false;
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
sendRequest(request) {
|
|
470
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
471
|
+
this.requestBusy = true;
|
|
472
|
+
try {
|
|
473
|
+
this.logger.logEvent({ message: 'sendRequest', request });
|
|
474
|
+
const isReset = (!request || request.reset || Object.keys(request).length === 0);
|
|
475
|
+
if (isReset) {
|
|
476
|
+
this.requestBusy = false;
|
|
477
|
+
return {};
|
|
478
|
+
}
|
|
479
|
+
if (request.slope !== undefined) {
|
|
480
|
+
this.data.slope = request.slope;
|
|
481
|
+
}
|
|
482
|
+
if (request.targetPower !== undefined) {
|
|
483
|
+
yield this.setPower(request.targetPower);
|
|
484
|
+
}
|
|
485
|
+
this.requestBusy = false;
|
|
486
|
+
return request;
|
|
487
|
+
}
|
|
488
|
+
catch (err) {
|
|
489
|
+
this.requestBusy = false;
|
|
490
|
+
this.logger.logEvent({ message: 'error', fn: 'sendRequest()', error: err.message || err });
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
sendRequests() {
|
|
496
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
497
|
+
if (this.requests.length > 0) {
|
|
498
|
+
const processing = [...this.requests];
|
|
499
|
+
const cnt = processing.length;
|
|
500
|
+
processing.forEach((request, idx) => __awaiter(this, void 0, void 0, function* () {
|
|
501
|
+
if (cnt > 1 && idx < cnt - 1) {
|
|
502
|
+
this.logger.logEvent({ message: 'ignoring bike update request', request });
|
|
503
|
+
this.requests.shift();
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
}));
|
|
507
|
+
const request = processing[0];
|
|
508
|
+
try {
|
|
509
|
+
yield this.sendRequest(request);
|
|
510
|
+
this.requests.shift();
|
|
511
|
+
}
|
|
512
|
+
catch (err) {
|
|
513
|
+
this.logger.logEvent({ message: 'bike update error', error: err.message, stack: err.stack, request });
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
bikeSync() {
|
|
519
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
520
|
+
if (this.paused) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
if (this.updateBusy || this.requestBusy) {
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
this.logger.logEvent({ message: 'bikeSync' });
|
|
527
|
+
if (!this.ignoreBike) {
|
|
528
|
+
yield this.sendRequests();
|
|
529
|
+
}
|
|
530
|
+
yield this.update();
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
sendUpdate(request) {
|
|
534
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
535
|
+
if (this.paused)
|
|
536
|
+
return;
|
|
537
|
+
this.logger.logEvent({ message: 'sendUpdate', request, waiting: this.requests.length });
|
|
538
|
+
return yield this.processClientRequest(request);
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
sendData() {
|
|
542
|
+
if (this.onDataFn)
|
|
543
|
+
this.onDataFn(this.data);
|
|
544
|
+
}
|
|
545
|
+
refreshRequests() {
|
|
546
|
+
if (this.kettlerData.cadence === 0)
|
|
547
|
+
return;
|
|
548
|
+
let bikeRequest = this.getCyclingMode().sendBikeUpdate({ refresh: true }) || {};
|
|
549
|
+
const prev = this.requests[this.requests.length - 1] || {};
|
|
550
|
+
if (bikeRequest.targetPower !== undefined && bikeRequest.targetPower !== prev.targetPower) {
|
|
551
|
+
this.logger.logEvent({ message: 'add request', request: bikeRequest });
|
|
552
|
+
this.requests.push(bikeRequest);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
processClientRequest(request) {
|
|
556
|
+
if (request.slope !== undefined) {
|
|
557
|
+
this.data.slope = request.slope;
|
|
558
|
+
}
|
|
559
|
+
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
560
|
+
let bikeRequest = this.getCyclingMode().sendBikeUpdate(request);
|
|
561
|
+
this.logger.logEvent({ message: 'add request', request: bikeRequest });
|
|
562
|
+
this.requests.push(bikeRequest);
|
|
563
|
+
resolve(bikeRequest);
|
|
564
|
+
}));
|
|
565
|
+
}
|
|
566
|
+
waitForOpened() {
|
|
567
|
+
return utils_1.runWithRetries(() => {
|
|
568
|
+
return new Promise((resolve, reject) => {
|
|
569
|
+
try {
|
|
570
|
+
if (this.comms.isConnected()) {
|
|
571
|
+
resolve(true);
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const cleanup = () => {
|
|
575
|
+
this.comms.removeAllListeners();
|
|
576
|
+
};
|
|
577
|
+
const onOpen = () => {
|
|
578
|
+
resolve(true);
|
|
579
|
+
cleanup();
|
|
580
|
+
};
|
|
581
|
+
const onError = (err) => { reject(err); cleanup(); };
|
|
582
|
+
const onClose = () => { cleanup(); };
|
|
583
|
+
this.comms.on('opened', onOpen);
|
|
584
|
+
this.comms.on('closed', onClose);
|
|
585
|
+
this.comms.on('error', onError);
|
|
586
|
+
this.logger.logEvent({ message: 'opening', port: this.getPort() });
|
|
587
|
+
this.comms.open();
|
|
588
|
+
}
|
|
589
|
+
catch (err) {
|
|
590
|
+
this.logger.logEvent({ message: 'error', fn: 'waitForOpened()', error: err.message || err });
|
|
591
|
+
reject(err);
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
}, 3, 1000);
|
|
595
|
+
}
|
|
596
|
+
waitForClosed() {
|
|
597
|
+
return new Promise((resolve, reject) => {
|
|
598
|
+
try {
|
|
599
|
+
if (!this.comms.isConnected()) {
|
|
600
|
+
resolve(true);
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
const cleanup = () => {
|
|
604
|
+
this.comms.removeAllListeners();
|
|
605
|
+
};
|
|
606
|
+
const onClose = () => {
|
|
607
|
+
resolve(true);
|
|
608
|
+
cleanup();
|
|
609
|
+
};
|
|
610
|
+
const onError = (err) => { reject(err); cleanup(); };
|
|
611
|
+
const onOpen = () => { cleanup(); };
|
|
612
|
+
this.comms.on('closed', onClose);
|
|
613
|
+
this.comms.on('opened', onOpen);
|
|
614
|
+
this.comms.on('error', onError);
|
|
615
|
+
this.logger.logEvent({ message: 'closing', port: this.getPort() });
|
|
616
|
+
this.comms.close();
|
|
617
|
+
}
|
|
618
|
+
catch (err) {
|
|
619
|
+
this.logger.logEvent({ message: 'error', fn: 'waitForClosed()', error: err.message || err });
|
|
620
|
+
reject(err);
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
getSupportedCyclingModes() {
|
|
625
|
+
return [power_meter_1.default];
|
|
626
|
+
}
|
|
627
|
+
setCyclingMode(mode, settings) {
|
|
628
|
+
let selectedMode;
|
|
629
|
+
if (typeof mode === 'string') {
|
|
630
|
+
const supported = this.getSupportedCyclingModes();
|
|
631
|
+
const CyclingModeClass = supported.find(M => { const m = new M(this); return m.getName() === mode; });
|
|
632
|
+
if (CyclingModeClass) {
|
|
633
|
+
this.settings.cyclingMode = new CyclingModeClass(this, settings);
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
selectedMode = this.getDefaultCyclingMode();
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
selectedMode = mode;
|
|
640
|
+
}
|
|
641
|
+
this.settings.cyclingMode = selectedMode;
|
|
642
|
+
this.settings.cyclingMode.setSettings(settings);
|
|
643
|
+
}
|
|
644
|
+
getCyclingMode() {
|
|
645
|
+
if (!this.settings.cyclingMode)
|
|
646
|
+
this.setCyclingMode(this.getDefaultCyclingMode());
|
|
647
|
+
return this.settings.cyclingMode;
|
|
648
|
+
}
|
|
649
|
+
getDefaultCyclingMode() {
|
|
650
|
+
return new power_meter_1.default(this);
|
|
651
|
+
}
|
|
652
|
+
setUserSettings(userSettings) {
|
|
653
|
+
this.settings.userSettings = userSettings;
|
|
654
|
+
}
|
|
655
|
+
setBikeSettings(bikeSettings) {
|
|
656
|
+
this.settings.bikeSettings = bikeSettings;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
exports.default = KettlerRacerAdapter;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { EventLogger } from 'gd-eventlog';
|
|
2
|
+
import CyclingMode, { CyclingModeProperty, IncyclistBikeData, Settings, UpdateRequest, CyclingModeBase } from '../../../CyclingMode';
|
|
3
|
+
import KettlerRacerAdapter from '../adapter';
|
|
4
|
+
export default class PowerMeterCyclingMode extends CyclingModeBase implements CyclingMode {
|
|
5
|
+
logger: EventLogger;
|
|
6
|
+
data: IncyclistBikeData;
|
|
7
|
+
prevRequest: UpdateRequest;
|
|
8
|
+
prevUpdateTS: number;
|
|
9
|
+
hasBikeUpdate: boolean;
|
|
10
|
+
constructor(adapter: KettlerRacerAdapter, props?: Settings);
|
|
11
|
+
getName(): string;
|
|
12
|
+
getDescription(): string;
|
|
13
|
+
getProperties(): CyclingModeProperty[];
|
|
14
|
+
getProperty(name: string): CyclingModeProperty;
|
|
15
|
+
getBikeInitRequest(): UpdateRequest;
|
|
16
|
+
sendBikeUpdate(request: UpdateRequest): UpdateRequest;
|
|
17
|
+
updateData(data: IncyclistBikeData): IncyclistBikeData;
|
|
18
|
+
}
|