incyclist-devices 1.4.98 → 1.4.102

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 (128) hide show
  1. package/LICENSE +0 -0
  2. package/lib/CyclingMode.d.ts +76 -76
  3. package/lib/CyclingMode.js +79 -79
  4. package/lib/Device.d.ts +92 -92
  5. package/lib/Device.js +71 -71
  6. package/lib/DeviceProtocol.d.ts +74 -74
  7. package/lib/DeviceProtocol.js +41 -41
  8. package/lib/DeviceRegistry.d.ts +8 -8
  9. package/lib/DeviceRegistry.js +33 -33
  10. package/lib/DeviceSupport.d.ts +34 -34
  11. package/lib/DeviceSupport.js +78 -78
  12. package/lib/ant/AntAdapter.d.ts +50 -50
  13. package/lib/ant/AntAdapter.js +109 -109
  14. package/lib/ant/AntScanner.d.ts +60 -60
  15. package/lib/ant/AntScanner.js +651 -651
  16. package/lib/ant/antfe/AntFEAdapter.d.ts +83 -83
  17. package/lib/ant/antfe/AntFEAdapter.js +652 -652
  18. package/lib/ant/antfe/ant-fe-adv-st-mode.d.ts +9 -9
  19. package/lib/ant/antfe/ant-fe-adv-st-mode.js +51 -51
  20. package/lib/ant/antfe/ant-fe-erg-mode.d.ts +6 -6
  21. package/lib/ant/antfe/ant-fe-erg-mode.js +14 -14
  22. package/lib/ant/antfe/ant-fe-st-mode.d.ts +5 -5
  23. package/lib/ant/antfe/ant-fe-st-mode.js +13 -13
  24. package/lib/ant/anthrm/AntHrmAdapter.d.ts +16 -16
  25. package/lib/ant/anthrm/AntHrmAdapter.js +130 -130
  26. package/lib/ant/antpwr/pwr-adapter.d.ts +49 -49
  27. package/lib/ant/antpwr/pwr-adapter.js +251 -251
  28. package/lib/ant/utils.d.ts +1 -1
  29. package/lib/ant/utils.js +23 -23
  30. package/lib/ble/ble-device.d.ts +63 -63
  31. package/lib/ble/ble-device.js +444 -442
  32. package/lib/ble/ble-erg-mode.d.ts +18 -18
  33. package/lib/ble/ble-erg-mode.js +132 -127
  34. package/lib/ble/ble-interface.d.ts +100 -99
  35. package/lib/ble/ble-interface.js +719 -712
  36. package/lib/ble/ble-peripheral.d.ts +36 -36
  37. package/lib/ble/ble-peripheral.js +200 -200
  38. package/lib/ble/ble-st-mode.d.ts +15 -15
  39. package/lib/ble/ble-st-mode.js +95 -102
  40. package/lib/ble/ble.d.ts +129 -129
  41. package/lib/ble/ble.js +86 -86
  42. package/lib/ble/consts.d.ts +14 -14
  43. package/lib/ble/consts.js +17 -17
  44. package/lib/ble/fm.d.ts +125 -125
  45. package/lib/ble/fm.js +745 -739
  46. package/lib/ble/hrm.d.ts +48 -48
  47. package/lib/ble/hrm.js +134 -134
  48. package/lib/ble/incyclist-protocol.d.ts +31 -31
  49. package/lib/ble/incyclist-protocol.js +147 -147
  50. package/lib/ble/pwr.d.ts +89 -89
  51. package/lib/ble/pwr.js +321 -321
  52. package/lib/ble/tacx.d.ts +90 -90
  53. package/lib/ble/tacx.js +731 -730
  54. package/lib/ble/wahoo-kickr.d.ts +98 -98
  55. package/lib/ble/wahoo-kickr.js +496 -496
  56. package/lib/calculations.d.ts +13 -13
  57. package/lib/calculations.js +150 -150
  58. package/lib/daum/DaumAdapter.d.ts +66 -66
  59. package/lib/daum/DaumAdapter.js +396 -396
  60. package/lib/daum/DaumPowerMeterCyclingMode.d.ts +8 -8
  61. package/lib/daum/DaumPowerMeterCyclingMode.js +21 -21
  62. package/lib/daum/ERGCyclingMode.d.ts +26 -26
  63. package/lib/daum/ERGCyclingMode.js +201 -201
  64. package/lib/daum/SmartTrainerCyclingMode.d.ts +41 -41
  65. package/lib/daum/SmartTrainerCyclingMode.js +344 -344
  66. package/lib/daum/classic/DaumClassicAdapter.d.ts +18 -18
  67. package/lib/daum/classic/DaumClassicAdapter.js +146 -146
  68. package/lib/daum/classic/DaumClassicCyclingMode.d.ts +13 -13
  69. package/lib/daum/classic/DaumClassicCyclingMode.js +97 -97
  70. package/lib/daum/classic/DaumClassicProtocol.d.ts +27 -27
  71. package/lib/daum/classic/DaumClassicProtocol.js +185 -185
  72. package/lib/daum/classic/bike.d.ts +64 -64
  73. package/lib/daum/classic/bike.js +456 -456
  74. package/lib/daum/classic/utils.d.ts +13 -13
  75. package/lib/daum/classic/utils.js +143 -143
  76. package/lib/daum/constants.d.ts +19 -19
  77. package/lib/daum/constants.js +22 -22
  78. package/lib/daum/premium/DaumClassicCyclingMode.d.ts +14 -14
  79. package/lib/daum/premium/DaumClassicCyclingMode.js +86 -86
  80. package/lib/daum/premium/DaumPremiumAdapter.d.ts +12 -12
  81. package/lib/daum/premium/DaumPremiumAdapter.js +131 -131
  82. package/lib/daum/premium/DaumPremiumProtocol.d.ts +32 -32
  83. package/lib/daum/premium/DaumPremiumProtocol.js +207 -207
  84. package/lib/daum/premium/bike.d.ts +123 -123
  85. package/lib/daum/premium/bike.js +894 -894
  86. package/lib/daum/premium/tcpserial.d.ts +33 -33
  87. package/lib/daum/premium/tcpserial.js +123 -123
  88. package/lib/daum/premium/utils.d.ts +62 -62
  89. package/lib/daum/premium/utils.js +376 -376
  90. package/lib/kettler/comms.d.ts +59 -59
  91. package/lib/kettler/comms.js +242 -242
  92. package/lib/kettler/ergo-racer/ERGCyclingMode.d.ts +25 -25
  93. package/lib/kettler/ergo-racer/ERGCyclingMode.js +144 -145
  94. package/lib/kettler/ergo-racer/adapter.d.ts +101 -101
  95. package/lib/kettler/ergo-racer/adapter.js +639 -639
  96. package/lib/kettler/ergo-racer/protocol.d.ts +41 -41
  97. package/lib/kettler/ergo-racer/protocol.js +203 -203
  98. package/lib/modes/power-base.d.ts +20 -20
  99. package/lib/modes/power-base.js +70 -70
  100. package/lib/modes/power-meter.d.ts +20 -20
  101. package/lib/modes/power-meter.js +78 -78
  102. package/lib/modes/simulator.d.ts +29 -29
  103. package/lib/modes/simulator.js +140 -140
  104. package/lib/simulator/Simulator.d.ts +69 -69
  105. package/lib/simulator/Simulator.js +288 -288
  106. package/lib/types/command.d.ts +8 -8
  107. package/lib/types/command.js +2 -2
  108. package/lib/types/route.d.ts +24 -24
  109. package/lib/types/route.js +9 -9
  110. package/lib/types/user.d.ts +11 -11
  111. package/lib/types/user.js +9 -9
  112. package/lib/utils.d.ts +14 -14
  113. package/lib/utils.js +114 -114
  114. package/package.json +46 -46
  115. package/lib/ant/antfe/ant-fe-st-mode copy.d.ts +0 -7
  116. package/lib/ant/antfe/ant-fe-st-mode copy.js +0 -54
  117. package/lib/ant/antpwr/AntPWRAdapter.d.ts +0 -24
  118. package/lib/ant/antpwr/AntPWRAdapter.js +0 -252
  119. package/lib/daum/PowerMeterCyclingMode.d.ts +0 -18
  120. package/lib/daum/PowerMeterCyclingMode.js +0 -78
  121. package/lib/daum/classic/ERGCyclingMode.d.ts +0 -23
  122. package/lib/daum/classic/ERGCyclingMode.js +0 -171
  123. package/lib/daum/indoorbike.d.ts +0 -24
  124. package/lib/daum/indoorbike.js +0 -178
  125. package/lib/kettler/ergo-racer/modes/power-meter.d.ts +0 -18
  126. package/lib/kettler/ergo-racer/modes/power-meter.js +0 -86
  127. package/lib/simulator/simulator-mode.d.ts +0 -28
  128. package/lib/simulator/simulator-mode.js +0 -120
@@ -1,396 +1,396 @@
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
- const Device_1 = __importStar(require("../Device"));
35
- const ERGCyclingMode_1 = __importDefault(require("./ERGCyclingMode"));
36
- const SmartTrainerCyclingMode_1 = __importDefault(require("./SmartTrainerCyclingMode"));
37
- const DaumPowerMeterCyclingMode_1 = __importDefault(require("./DaumPowerMeterCyclingMode"));
38
- const utils_1 = require("../utils");
39
- class DaumAdapterBase extends Device_1.default {
40
- constructor(props, bike) {
41
- super(props);
42
- this.requests = [];
43
- this.adapterTime = 0;
44
- this.requestBusy = false;
45
- this.updateBusy = false;
46
- this.bike = bike;
47
- this.stopped = false;
48
- this.paused = false;
49
- this.cyclingData = {
50
- isPedalling: false,
51
- time: 0,
52
- power: 0,
53
- pedalRpm: 0,
54
- speed: 0,
55
- distanceInternal: 0,
56
- heartrate: 0
57
- };
58
- this.deviceData = {};
59
- const options = props || {};
60
- this.cyclingMode = options.cyclingMode;
61
- this.setUserSettings(options.userSettings);
62
- this.setBikeSettings(options.bikeSettings);
63
- }
64
- setCyclingMode(mode, settings) {
65
- let selectedMode;
66
- if (typeof mode === 'string') {
67
- const supported = this.getSupportedCyclingModes();
68
- const CyclingModeClass = supported.find(M => { const m = new M(this); return m.getName() === mode; });
69
- if (CyclingModeClass) {
70
- this.cyclingMode = new CyclingModeClass(this, settings);
71
- return;
72
- }
73
- selectedMode = this.getDefaultCyclingMode();
74
- }
75
- else {
76
- selectedMode = mode;
77
- }
78
- this.cyclingMode = selectedMode;
79
- this.cyclingMode.setSettings(settings);
80
- }
81
- getSupportedCyclingModes() {
82
- return [ERGCyclingMode_1.default, SmartTrainerCyclingMode_1.default, DaumPowerMeterCyclingMode_1.default];
83
- }
84
- getCyclingMode() {
85
- if (!this.cyclingMode)
86
- this.setCyclingMode(this.getDefaultCyclingMode());
87
- return this.cyclingMode;
88
- }
89
- getDefaultCyclingMode() {
90
- return new ERGCyclingMode_1.default(this);
91
- }
92
- setUserSettings(userSettings) {
93
- this.userSettings = userSettings || {};
94
- if (this.bike) {
95
- if (!this.bike.settings)
96
- this.bike.settings = { user: {} };
97
- if (!this.bike.settings.user)
98
- this.bike.settings.user = {};
99
- this.bike.settings.user.weight = this.userSettings.weight || Device_1.DEFAULT_USER_WEIGHT;
100
- }
101
- }
102
- setBikeSettings(bikeSettings) {
103
- this.bikeSettings = bikeSettings || {};
104
- if (this.bike) {
105
- if (!this.bike.settings)
106
- this.bike.settings = {};
107
- this.bike.settings.weight = this.userSettings.weight || Device_1.DEFAULT_BIKE_WEIGHT;
108
- }
109
- }
110
- getWeight() {
111
- const userWeight = Number(this.userSettings.weight || Device_1.DEFAULT_USER_WEIGHT);
112
- const bikeWeight = Number(this.bikeSettings.weight || Device_1.DEFAULT_BIKE_WEIGHT);
113
- return bikeWeight + userWeight;
114
- }
115
- getCurrentBikeData() {
116
- throw new Error('Method not implemented.');
117
- }
118
- getBike() {
119
- return this.bike;
120
- }
121
- isBike() {
122
- return true;
123
- }
124
- isPower() {
125
- return true;
126
- }
127
- isHrm() {
128
- return true;
129
- }
130
- isSame(device) {
131
- if (!(device instanceof DaumAdapterBase))
132
- return false;
133
- const adapter = device;
134
- return (adapter.getName() === this.getName() && adapter.getPort() === this.getPort());
135
- }
136
- setIgnoreHrm(ignore) {
137
- this.ignoreHrm = ignore;
138
- }
139
- setIgnoreBike(ignore) {
140
- this.ignoreBike = ignore;
141
- }
142
- isStopped() {
143
- return this.stopped;
144
- }
145
- initData() {
146
- this.distanceInternal = undefined;
147
- this.paused = false;
148
- this.stopped = false;
149
- this.cyclingData = {
150
- isPedalling: false,
151
- time: 0,
152
- power: 0,
153
- pedalRpm: 0,
154
- speed: 0,
155
- distanceInternal: 0,
156
- heartrate: 0
157
- };
158
- this.deviceData = {};
159
- this.currentRequest = {};
160
- this.requests = [];
161
- const name = this.getCyclingMode().getName();
162
- const settings = this.getCyclingMode().getSettings();
163
- this.setCyclingMode(name, settings);
164
- }
165
- start(props) {
166
- throw new Error('Method not implemented.');
167
- }
168
- startUpdatePull() {
169
- if (this.iv)
170
- return;
171
- if (this.ignoreBike && this.ignoreHrm && this.ignorePower)
172
- return;
173
- const ivSync = setInterval(() => {
174
- this.bikeSync();
175
- }, 1000);
176
- const ivUpdate = setInterval(() => {
177
- this.sendData();
178
- this.refreshRequests();
179
- }, 1000);
180
- this.iv = {
181
- sync: ivSync,
182
- update: ivUpdate
183
- };
184
- }
185
- connect() {
186
- if (!this.bike.isConnected())
187
- this.bike.connect();
188
- }
189
- close() {
190
- return this.bike.saveClose();
191
- }
192
- logEvent(event) {
193
- if (!this.logger)
194
- return;
195
- this.logger.logEvent(event);
196
- }
197
- stop() {
198
- this.logEvent({ message: 'stop request' });
199
- this.stopped = true;
200
- return new Promise((resolve, reject) => {
201
- try {
202
- if (this.iv) {
203
- if (this.iv.sync)
204
- clearInterval(this.iv.sync);
205
- if (this.iv.update)
206
- clearInterval(this.iv.update);
207
- this.iv = undefined;
208
- }
209
- this.logEvent({ message: 'stop request completed' });
210
- this.paused = undefined;
211
- resolve(true);
212
- }
213
- catch (err) {
214
- this.logEvent({ message: 'stop error', error: err.message });
215
- reject(err);
216
- }
217
- });
218
- }
219
- pause() {
220
- this.logEvent({ message: 'pause' });
221
- return new Promise(resolve => {
222
- this.paused = true;
223
- resolve(true);
224
- });
225
- }
226
- resume() {
227
- this.logEvent({ message: 'resume' });
228
- return new Promise(resolve => {
229
- this.paused = false;
230
- resolve(true);
231
- });
232
- }
233
- sendUpdate(request) {
234
- return __awaiter(this, void 0, void 0, function* () {
235
- if (this.paused)
236
- return;
237
- this.logEvent({ message: 'sendUpdate', request, waiting: this.requests.length });
238
- return yield this.processClientRequest(request);
239
- });
240
- }
241
- sendData() {
242
- if (this.onDataFn)
243
- this.onDataFn(this.deviceData);
244
- }
245
- update() {
246
- return __awaiter(this, void 0, void 0, function* () {
247
- this.updateBusy = true;
248
- this.getCurrentBikeData()
249
- .then(bikeData => {
250
- this.updateData(this.cyclingData, bikeData);
251
- this.transformData();
252
- this.updateBusy = false;
253
- })
254
- .catch(err => {
255
- this.logEvent({ message: 'bike update error', error: err.message, stack: err.stack });
256
- const { isPedalling, power, pedalRpm, speed, distanceInternal, heartrate, slope } = this.cyclingData;
257
- this.updateData(this.cyclingData, { isPedalling, power, pedalRpm, speed, distanceInternal, heartrate, slope });
258
- this.transformData();
259
- this.updateBusy = false;
260
- });
261
- });
262
- }
263
- sendRequests() {
264
- return __awaiter(this, void 0, void 0, function* () {
265
- if (this.requests.length > 0) {
266
- const processing = [...this.requests];
267
- const cnt = processing.length;
268
- processing.forEach((request, idx) => __awaiter(this, void 0, void 0, function* () {
269
- if (cnt > 1 && idx < cnt - 1) {
270
- this.logEvent({ message: 'ignoring bike update request', request });
271
- this.requests.shift();
272
- return;
273
- }
274
- }));
275
- const request = processing[0];
276
- try {
277
- yield this.sendRequest(request);
278
- this.requests.shift();
279
- }
280
- catch (err) {
281
- this.logEvent({ message: 'bike update error', error: err.message, stack: err.stack, request });
282
- }
283
- }
284
- });
285
- }
286
- bikeSync() {
287
- return __awaiter(this, void 0, void 0, function* () {
288
- if (this.paused) {
289
- return;
290
- }
291
- if (this.updateBusy || this.requestBusy) {
292
- return;
293
- }
294
- this.logEvent({ message: 'bikeSync' });
295
- if (!this.ignoreBike) {
296
- yield this.sendRequests();
297
- }
298
- yield this.update();
299
- });
300
- }
301
- updateData(prev, bikeData) {
302
- let data = {};
303
- data.isPedalling = bikeData.cadence > 0;
304
- data.power = bikeData.power;
305
- data.pedalRpm = bikeData.cadence;
306
- data.speed = bikeData.speed;
307
- data.heartrate = bikeData.heartrate;
308
- data.distanceInternal = bikeData.distanceInternal;
309
- data.gear = bikeData.gear;
310
- data.time = bikeData.time;
311
- if (bikeData.slope)
312
- data.slope = bikeData.slope;
313
- this.cyclingData = this.getCyclingMode().updateData(data);
314
- return this.cyclingData;
315
- }
316
- transformData() {
317
- if (this.cyclingData === undefined)
318
- return;
319
- let distance = 0;
320
- if (this.distanceInternal !== undefined && this.cyclingData.distanceInternal !== undefined) {
321
- distance = this.cyclingData.distanceInternal - this.distanceInternal;
322
- }
323
- if (this.cyclingData.distanceInternal !== undefined)
324
- this.distanceInternal = this.cyclingData.distanceInternal;
325
- let data = {
326
- speed: this.cyclingData.speed,
327
- slope: this.cyclingData.slope,
328
- power: (0, utils_1.intVal)(this.cyclingData.power),
329
- cadence: (0, utils_1.intVal)(this.cyclingData.pedalRpm),
330
- heartrate: (0, utils_1.intVal)(this.cyclingData.heartrate),
331
- distance,
332
- timestamp: Date.now(),
333
- deviceTime: this.cyclingData.time,
334
- deviceDistanceCounter: this.cyclingData.distanceInternal
335
- };
336
- if (this.ignoreHrm)
337
- delete data.heartrate;
338
- if (this.ignorePower) {
339
- delete data.power;
340
- delete data.cadence;
341
- }
342
- if (this.ignoreBike) {
343
- data = { heartrate: data.heartrate };
344
- }
345
- this.deviceData = data;
346
- }
347
- sendRequest(request) {
348
- return __awaiter(this, void 0, void 0, function* () {
349
- this.requestBusy = true;
350
- try {
351
- this.logEvent({ message: 'sendRequest', request });
352
- const bike = this.getBike();
353
- const isReset = (!request || request.reset || Object.keys(request).length === 0);
354
- if (isReset) {
355
- this.requestBusy = false;
356
- return {};
357
- }
358
- if (request.slope !== undefined) {
359
- yield bike.setSlope(request.slope);
360
- }
361
- if (request.targetPower !== undefined) {
362
- yield bike.setPower(request.targetPower);
363
- }
364
- this.requestBusy = false;
365
- return request;
366
- }
367
- catch (err) {
368
- this.requestBusy = false;
369
- this.logEvent({ message: 'sendRequest error', error: err.message || err });
370
- return;
371
- }
372
- });
373
- }
374
- refreshRequests() {
375
- if (!this.cyclingData.isPedalling || this.cyclingData.pedalRpm === 0)
376
- return;
377
- let bikeRequest = this.getCyclingMode().sendBikeUpdate({ refresh: true }) || {};
378
- const prev = this.requests[this.requests.length - 1] || {};
379
- if (bikeRequest.targetPower !== undefined && bikeRequest.targetPower !== prev.targetPower) {
380
- this.logEvent({ message: 'add request', request: bikeRequest });
381
- this.requests.push(bikeRequest);
382
- }
383
- }
384
- processClientRequest(request) {
385
- if (request.slope !== undefined) {
386
- this.cyclingData.slope = request.slope;
387
- }
388
- return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
389
- let bikeRequest = this.getCyclingMode().sendBikeUpdate(request);
390
- this.logEvent({ message: 'add request', request: bikeRequest });
391
- this.requests.push(bikeRequest);
392
- resolve(bikeRequest);
393
- }));
394
- }
395
- }
396
- exports.default = DaumAdapterBase;
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
+ const Device_1 = __importStar(require("../Device"));
35
+ const ERGCyclingMode_1 = __importDefault(require("./ERGCyclingMode"));
36
+ const SmartTrainerCyclingMode_1 = __importDefault(require("./SmartTrainerCyclingMode"));
37
+ const DaumPowerMeterCyclingMode_1 = __importDefault(require("./DaumPowerMeterCyclingMode"));
38
+ const utils_1 = require("../utils");
39
+ class DaumAdapterBase extends Device_1.default {
40
+ constructor(props, bike) {
41
+ super(props);
42
+ this.requests = [];
43
+ this.adapterTime = 0;
44
+ this.requestBusy = false;
45
+ this.updateBusy = false;
46
+ this.bike = bike;
47
+ this.stopped = false;
48
+ this.paused = false;
49
+ this.cyclingData = {
50
+ isPedalling: false,
51
+ time: 0,
52
+ power: 0,
53
+ pedalRpm: 0,
54
+ speed: 0,
55
+ distanceInternal: 0,
56
+ heartrate: 0
57
+ };
58
+ this.deviceData = {};
59
+ const options = props || {};
60
+ this.cyclingMode = options.cyclingMode;
61
+ this.setUserSettings(options.userSettings);
62
+ this.setBikeSettings(options.bikeSettings);
63
+ }
64
+ setCyclingMode(mode, settings) {
65
+ let selectedMode;
66
+ if (typeof mode === 'string') {
67
+ const supported = this.getSupportedCyclingModes();
68
+ const CyclingModeClass = supported.find(M => { const m = new M(this); return m.getName() === mode; });
69
+ if (CyclingModeClass) {
70
+ this.cyclingMode = new CyclingModeClass(this, settings);
71
+ return;
72
+ }
73
+ selectedMode = this.getDefaultCyclingMode();
74
+ }
75
+ else {
76
+ selectedMode = mode;
77
+ }
78
+ this.cyclingMode = selectedMode;
79
+ this.cyclingMode.setSettings(settings);
80
+ }
81
+ getSupportedCyclingModes() {
82
+ return [ERGCyclingMode_1.default, SmartTrainerCyclingMode_1.default, DaumPowerMeterCyclingMode_1.default];
83
+ }
84
+ getCyclingMode() {
85
+ if (!this.cyclingMode)
86
+ this.setCyclingMode(this.getDefaultCyclingMode());
87
+ return this.cyclingMode;
88
+ }
89
+ getDefaultCyclingMode() {
90
+ return new ERGCyclingMode_1.default(this);
91
+ }
92
+ setUserSettings(userSettings) {
93
+ this.userSettings = userSettings || {};
94
+ if (this.bike) {
95
+ if (!this.bike.settings)
96
+ this.bike.settings = { user: {} };
97
+ if (!this.bike.settings.user)
98
+ this.bike.settings.user = {};
99
+ this.bike.settings.user.weight = this.userSettings.weight || Device_1.DEFAULT_USER_WEIGHT;
100
+ }
101
+ }
102
+ setBikeSettings(bikeSettings) {
103
+ this.bikeSettings = bikeSettings || {};
104
+ if (this.bike) {
105
+ if (!this.bike.settings)
106
+ this.bike.settings = {};
107
+ this.bike.settings.weight = this.userSettings.weight || Device_1.DEFAULT_BIKE_WEIGHT;
108
+ }
109
+ }
110
+ getWeight() {
111
+ const userWeight = Number(this.userSettings.weight || Device_1.DEFAULT_USER_WEIGHT);
112
+ const bikeWeight = Number(this.bikeSettings.weight || Device_1.DEFAULT_BIKE_WEIGHT);
113
+ return bikeWeight + userWeight;
114
+ }
115
+ getCurrentBikeData() {
116
+ throw new Error('Method not implemented.');
117
+ }
118
+ getBike() {
119
+ return this.bike;
120
+ }
121
+ isBike() {
122
+ return true;
123
+ }
124
+ isPower() {
125
+ return true;
126
+ }
127
+ isHrm() {
128
+ return true;
129
+ }
130
+ isSame(device) {
131
+ if (!(device instanceof DaumAdapterBase))
132
+ return false;
133
+ const adapter = device;
134
+ return (adapter.getName() === this.getName() && adapter.getPort() === this.getPort());
135
+ }
136
+ setIgnoreHrm(ignore) {
137
+ this.ignoreHrm = ignore;
138
+ }
139
+ setIgnoreBike(ignore) {
140
+ this.ignoreBike = ignore;
141
+ }
142
+ isStopped() {
143
+ return this.stopped;
144
+ }
145
+ initData() {
146
+ this.distanceInternal = undefined;
147
+ this.paused = false;
148
+ this.stopped = false;
149
+ this.cyclingData = {
150
+ isPedalling: false,
151
+ time: 0,
152
+ power: 0,
153
+ pedalRpm: 0,
154
+ speed: 0,
155
+ distanceInternal: 0,
156
+ heartrate: 0
157
+ };
158
+ this.deviceData = {};
159
+ this.currentRequest = {};
160
+ this.requests = [];
161
+ const name = this.getCyclingMode().getName();
162
+ const settings = this.getCyclingMode().getSettings();
163
+ this.setCyclingMode(name, settings);
164
+ }
165
+ start(props) {
166
+ throw new Error('Method not implemented.');
167
+ }
168
+ startUpdatePull() {
169
+ if (this.iv)
170
+ return;
171
+ if (this.ignoreBike && this.ignoreHrm && this.ignorePower)
172
+ return;
173
+ const ivSync = setInterval(() => {
174
+ this.bikeSync();
175
+ }, 1000);
176
+ const ivUpdate = setInterval(() => {
177
+ this.sendData();
178
+ this.refreshRequests();
179
+ }, 1000);
180
+ this.iv = {
181
+ sync: ivSync,
182
+ update: ivUpdate
183
+ };
184
+ }
185
+ connect() {
186
+ if (!this.bike.isConnected())
187
+ this.bike.connect();
188
+ }
189
+ close() {
190
+ return this.bike.saveClose();
191
+ }
192
+ logEvent(event) {
193
+ if (!this.logger)
194
+ return;
195
+ this.logger.logEvent(event);
196
+ }
197
+ stop() {
198
+ this.logEvent({ message: 'stop request' });
199
+ this.stopped = true;
200
+ return new Promise((resolve, reject) => {
201
+ try {
202
+ if (this.iv) {
203
+ if (this.iv.sync)
204
+ clearInterval(this.iv.sync);
205
+ if (this.iv.update)
206
+ clearInterval(this.iv.update);
207
+ this.iv = undefined;
208
+ }
209
+ this.logEvent({ message: 'stop request completed' });
210
+ this.paused = undefined;
211
+ resolve(true);
212
+ }
213
+ catch (err) {
214
+ this.logEvent({ message: 'stop error', error: err.message });
215
+ reject(err);
216
+ }
217
+ });
218
+ }
219
+ pause() {
220
+ this.logEvent({ message: 'pause' });
221
+ return new Promise(resolve => {
222
+ this.paused = true;
223
+ resolve(true);
224
+ });
225
+ }
226
+ resume() {
227
+ this.logEvent({ message: 'resume' });
228
+ return new Promise(resolve => {
229
+ this.paused = false;
230
+ resolve(true);
231
+ });
232
+ }
233
+ sendUpdate(request) {
234
+ return __awaiter(this, void 0, void 0, function* () {
235
+ if (this.paused)
236
+ return;
237
+ this.logEvent({ message: 'sendUpdate', request, waiting: this.requests.length });
238
+ return yield this.processClientRequest(request);
239
+ });
240
+ }
241
+ sendData() {
242
+ if (this.onDataFn)
243
+ this.onDataFn(this.deviceData);
244
+ }
245
+ update() {
246
+ return __awaiter(this, void 0, void 0, function* () {
247
+ this.updateBusy = true;
248
+ this.getCurrentBikeData()
249
+ .then(bikeData => {
250
+ this.updateData(this.cyclingData, bikeData);
251
+ this.transformData();
252
+ this.updateBusy = false;
253
+ })
254
+ .catch(err => {
255
+ this.logEvent({ message: 'bike update error', error: err.message, stack: err.stack });
256
+ const { isPedalling, power, pedalRpm, speed, distanceInternal, heartrate, slope } = this.cyclingData;
257
+ this.updateData(this.cyclingData, { isPedalling, power, pedalRpm, speed, distanceInternal, heartrate, slope });
258
+ this.transformData();
259
+ this.updateBusy = false;
260
+ });
261
+ });
262
+ }
263
+ sendRequests() {
264
+ return __awaiter(this, void 0, void 0, function* () {
265
+ if (this.requests.length > 0) {
266
+ const processing = [...this.requests];
267
+ const cnt = processing.length;
268
+ processing.forEach((request, idx) => __awaiter(this, void 0, void 0, function* () {
269
+ if (cnt > 1 && idx < cnt - 1) {
270
+ this.logEvent({ message: 'ignoring bike update request', request });
271
+ this.requests.shift();
272
+ return;
273
+ }
274
+ }));
275
+ const request = processing[0];
276
+ try {
277
+ yield this.sendRequest(request);
278
+ this.requests.shift();
279
+ }
280
+ catch (err) {
281
+ this.logEvent({ message: 'bike update error', error: err.message, stack: err.stack, request });
282
+ }
283
+ }
284
+ });
285
+ }
286
+ bikeSync() {
287
+ return __awaiter(this, void 0, void 0, function* () {
288
+ if (this.paused) {
289
+ return;
290
+ }
291
+ if (this.updateBusy || this.requestBusy) {
292
+ return;
293
+ }
294
+ this.logEvent({ message: 'bikeSync' });
295
+ if (!this.ignoreBike) {
296
+ yield this.sendRequests();
297
+ }
298
+ yield this.update();
299
+ });
300
+ }
301
+ updateData(prev, bikeData) {
302
+ let data = {};
303
+ data.isPedalling = bikeData.cadence > 0;
304
+ data.power = bikeData.power;
305
+ data.pedalRpm = bikeData.cadence;
306
+ data.speed = bikeData.speed;
307
+ data.heartrate = bikeData.heartrate;
308
+ data.distanceInternal = bikeData.distanceInternal;
309
+ data.gear = bikeData.gear;
310
+ data.time = bikeData.time;
311
+ if (bikeData.slope)
312
+ data.slope = bikeData.slope;
313
+ this.cyclingData = this.getCyclingMode().updateData(data);
314
+ return this.cyclingData;
315
+ }
316
+ transformData() {
317
+ if (this.cyclingData === undefined)
318
+ return;
319
+ let distance = 0;
320
+ if (this.distanceInternal !== undefined && this.cyclingData.distanceInternal !== undefined) {
321
+ distance = this.cyclingData.distanceInternal - this.distanceInternal;
322
+ }
323
+ if (this.cyclingData.distanceInternal !== undefined)
324
+ this.distanceInternal = this.cyclingData.distanceInternal;
325
+ let data = {
326
+ speed: this.cyclingData.speed,
327
+ slope: this.cyclingData.slope,
328
+ power: (0, utils_1.intVal)(this.cyclingData.power),
329
+ cadence: (0, utils_1.intVal)(this.cyclingData.pedalRpm),
330
+ heartrate: (0, utils_1.intVal)(this.cyclingData.heartrate),
331
+ distance,
332
+ timestamp: Date.now(),
333
+ deviceTime: this.cyclingData.time,
334
+ deviceDistanceCounter: this.cyclingData.distanceInternal
335
+ };
336
+ if (this.ignoreHrm)
337
+ delete data.heartrate;
338
+ if (this.ignorePower) {
339
+ delete data.power;
340
+ delete data.cadence;
341
+ }
342
+ if (this.ignoreBike) {
343
+ data = { heartrate: data.heartrate };
344
+ }
345
+ this.deviceData = data;
346
+ }
347
+ sendRequest(request) {
348
+ return __awaiter(this, void 0, void 0, function* () {
349
+ this.requestBusy = true;
350
+ try {
351
+ this.logEvent({ message: 'sendRequest', request });
352
+ const bike = this.getBike();
353
+ const isReset = (!request || request.reset || Object.keys(request).length === 0);
354
+ if (isReset) {
355
+ this.requestBusy = false;
356
+ return {};
357
+ }
358
+ if (request.slope !== undefined) {
359
+ yield bike.setSlope(request.slope);
360
+ }
361
+ if (request.targetPower !== undefined) {
362
+ yield bike.setPower(request.targetPower);
363
+ }
364
+ this.requestBusy = false;
365
+ return request;
366
+ }
367
+ catch (err) {
368
+ this.requestBusy = false;
369
+ this.logEvent({ message: 'sendRequest error', error: err.message || err });
370
+ return;
371
+ }
372
+ });
373
+ }
374
+ refreshRequests() {
375
+ if (!this.cyclingData.isPedalling || this.cyclingData.pedalRpm === 0)
376
+ return;
377
+ let bikeRequest = this.getCyclingMode().sendBikeUpdate({ refresh: true }) || {};
378
+ const prev = this.requests[this.requests.length - 1] || {};
379
+ if (bikeRequest.targetPower !== undefined && bikeRequest.targetPower !== prev.targetPower) {
380
+ this.logEvent({ message: 'add request', request: bikeRequest });
381
+ this.requests.push(bikeRequest);
382
+ }
383
+ }
384
+ processClientRequest(request) {
385
+ if (request.slope !== undefined) {
386
+ this.cyclingData.slope = request.slope;
387
+ }
388
+ return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
389
+ let bikeRequest = this.getCyclingMode().sendBikeUpdate(request);
390
+ this.logEvent({ message: 'add request', request: bikeRequest });
391
+ this.requests.push(bikeRequest);
392
+ resolve(bikeRequest);
393
+ }));
394
+ }
395
+ }
396
+ exports.default = DaumAdapterBase;