incyclist-devices 1.5.11 → 1.5.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.
Files changed (136) hide show
  1. package/LICENSE +0 -0
  2. package/lib/DeviceSupport.d.ts +36 -36
  3. package/lib/DeviceSupport.js +82 -82
  4. package/lib/ant/AntAdapter.d.ts +50 -50
  5. package/lib/ant/AntAdapter.js +109 -109
  6. package/lib/ant/AntScanner.d.ts +60 -60
  7. package/lib/ant/AntScanner.js +651 -651
  8. package/lib/ant/antfe/AntFEAdapter.d.ts +83 -83
  9. package/lib/ant/antfe/AntFEAdapter.js +652 -652
  10. package/lib/ant/antfe/ant-fe-adv-st-mode.d.ts +9 -9
  11. package/lib/ant/antfe/ant-fe-adv-st-mode.js +51 -51
  12. package/lib/ant/antfe/ant-fe-erg-mode.d.ts +6 -6
  13. package/lib/ant/antfe/ant-fe-erg-mode.js +14 -14
  14. package/lib/ant/antfe/ant-fe-st-mode.d.ts +5 -5
  15. package/lib/ant/antfe/ant-fe-st-mode.js +13 -13
  16. package/lib/ant/anthrm/AntHrmAdapter.d.ts +16 -16
  17. package/lib/ant/anthrm/AntHrmAdapter.js +130 -130
  18. package/lib/ant/antpwr/pwr-adapter.d.ts +49 -49
  19. package/lib/ant/antpwr/pwr-adapter.js +251 -251
  20. package/lib/ant/utils.d.ts +1 -1
  21. package/lib/ant/utils.js +23 -23
  22. package/lib/antv2/AntAdapter.d.ts +48 -0
  23. package/lib/antv2/AntAdapter.js +104 -0
  24. package/lib/antv2/adapter-factory.d.ts +11 -11
  25. package/lib/antv2/adapter-factory.js +40 -40
  26. package/lib/antv2/ant-binding.d.ts +13 -13
  27. package/lib/antv2/ant-binding.js +27 -27
  28. package/lib/antv2/ant-device.d.ts +51 -51
  29. package/lib/antv2/ant-device.js +115 -115
  30. package/lib/antv2/ant-interface.d.ts +37 -37
  31. package/lib/antv2/ant-interface.js +255 -255
  32. package/lib/antv2/fe.d.ts +29 -29
  33. package/lib/antv2/fe.js +262 -262
  34. package/lib/antv2/hr.d.ts +18 -18
  35. package/lib/antv2/hr.js +93 -93
  36. package/lib/antv2/incyclist-protocol.d.ts +37 -37
  37. package/lib/antv2/incyclist-protocol.js +126 -126
  38. package/lib/antv2/pwr.d.ts +28 -28
  39. package/lib/antv2/pwr.js +163 -163
  40. package/lib/antv2/sensor-factory.d.ts +5 -5
  41. package/lib/antv2/sensor-factory.js +20 -20
  42. package/lib/ble/ble-device.d.ts +63 -63
  43. package/lib/ble/ble-device.js +444 -444
  44. package/lib/ble/ble-erg-mode.d.ts +18 -18
  45. package/lib/ble/ble-erg-mode.js +132 -132
  46. package/lib/ble/ble-interface.d.ts +100 -100
  47. package/lib/ble/ble-interface.js +721 -721
  48. package/lib/ble/ble-peripheral.d.ts +36 -36
  49. package/lib/ble/ble-peripheral.js +200 -200
  50. package/lib/ble/ble-st-mode.d.ts +15 -15
  51. package/lib/ble/ble-st-mode.js +95 -95
  52. package/lib/ble/ble.d.ts +129 -129
  53. package/lib/ble/ble.js +86 -86
  54. package/lib/ble/consts.d.ts +14 -14
  55. package/lib/ble/consts.js +17 -17
  56. package/lib/ble/elite.d.ts +90 -90
  57. package/lib/ble/elite.js +322 -322
  58. package/lib/ble/fm.d.ts +125 -125
  59. package/lib/ble/fm.js +745 -745
  60. package/lib/ble/hrm.d.ts +48 -48
  61. package/lib/ble/hrm.js +134 -134
  62. package/lib/ble/incyclist-protocol.d.ts +31 -31
  63. package/lib/ble/incyclist-protocol.js +153 -153
  64. package/lib/ble/pwr.d.ts +89 -89
  65. package/lib/ble/pwr.js +321 -321
  66. package/lib/ble/tacx.d.ts +92 -90
  67. package/lib/ble/tacx.js +763 -731
  68. package/lib/ble/wahoo-kickr.d.ts +98 -98
  69. package/lib/ble/wahoo-kickr.js +496 -496
  70. package/lib/calculations.d.ts +13 -13
  71. package/lib/calculations.js +150 -150
  72. package/lib/cycling-mode.d.ts +76 -76
  73. package/lib/cycling-mode.js +79 -79
  74. package/lib/daum/DaumAdapter.d.ts +66 -66
  75. package/lib/daum/DaumAdapter.js +396 -396
  76. package/lib/daum/DaumPowerMeterCyclingMode.d.ts +8 -8
  77. package/lib/daum/DaumPowerMeterCyclingMode.js +21 -21
  78. package/lib/daum/ERGCyclingMode.d.ts +26 -26
  79. package/lib/daum/ERGCyclingMode.js +201 -201
  80. package/lib/daum/SmartTrainerCyclingMode.d.ts +41 -41
  81. package/lib/daum/SmartTrainerCyclingMode.js +344 -344
  82. package/lib/daum/classic/DaumClassicAdapter.d.ts +22 -22
  83. package/lib/daum/classic/DaumClassicAdapter.js +183 -183
  84. package/lib/daum/classic/DaumClassicCyclingMode.d.ts +13 -13
  85. package/lib/daum/classic/DaumClassicCyclingMode.js +97 -97
  86. package/lib/daum/classic/DaumClassicProtocol.d.ts +27 -27
  87. package/lib/daum/classic/DaumClassicProtocol.js +185 -185
  88. package/lib/daum/classic/bike.d.ts +68 -68
  89. package/lib/daum/classic/bike.js +467 -467
  90. package/lib/daum/classic/utils.d.ts +13 -13
  91. package/lib/daum/classic/utils.js +143 -143
  92. package/lib/daum/constants.d.ts +19 -19
  93. package/lib/daum/constants.js +22 -22
  94. package/lib/daum/premium/DaumClassicCyclingMode.d.ts +14 -14
  95. package/lib/daum/premium/DaumClassicCyclingMode.js +86 -86
  96. package/lib/daum/premium/DaumPremiumAdapter.d.ts +16 -16
  97. package/lib/daum/premium/DaumPremiumAdapter.js +163 -163
  98. package/lib/daum/premium/DaumPremiumProtocol.d.ts +32 -32
  99. package/lib/daum/premium/DaumPremiumProtocol.js +207 -207
  100. package/lib/daum/premium/bike.d.ts +127 -127
  101. package/lib/daum/premium/bike.js +904 -904
  102. package/lib/daum/premium/tcpserial.d.ts +33 -33
  103. package/lib/daum/premium/tcpserial.js +123 -123
  104. package/lib/daum/premium/utils.d.ts +62 -62
  105. package/lib/daum/premium/utils.js +376 -376
  106. package/lib/device.d.ts +92 -92
  107. package/lib/device.js +71 -71
  108. package/lib/kettler/comms.d.ts +59 -59
  109. package/lib/kettler/comms.js +242 -242
  110. package/lib/kettler/ergo-racer/ERGCyclingMode.d.ts +25 -25
  111. package/lib/kettler/ergo-racer/ERGCyclingMode.js +144 -144
  112. package/lib/kettler/ergo-racer/adapter.d.ts +101 -101
  113. package/lib/kettler/ergo-racer/adapter.js +639 -639
  114. package/lib/kettler/ergo-racer/protocol.d.ts +41 -41
  115. package/lib/kettler/ergo-racer/protocol.js +203 -203
  116. package/lib/modes/power-base.d.ts +20 -20
  117. package/lib/modes/power-base.js +70 -70
  118. package/lib/modes/power-meter.d.ts +20 -20
  119. package/lib/modes/power-meter.js +78 -78
  120. package/lib/modes/simulator.d.ts +29 -29
  121. package/lib/modes/simulator.js +140 -140
  122. package/lib/protocol.d.ts +74 -74
  123. package/lib/protocol.js +41 -41
  124. package/lib/registry.d.ts +8 -8
  125. package/lib/registry.js +33 -33
  126. package/lib/simulator/Simulator.d.ts +69 -69
  127. package/lib/simulator/Simulator.js +288 -288
  128. package/lib/types/command.d.ts +8 -8
  129. package/lib/types/command.js +2 -2
  130. package/lib/types/route.d.ts +24 -24
  131. package/lib/types/route.js +9 -9
  132. package/lib/types/user.d.ts +11 -11
  133. package/lib/types/user.js +9 -9
  134. package/lib/utils.d.ts +14 -14
  135. package/lib/utils.js +114 -114
  136. package/package.json +47 -47
@@ -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;