incyclist-devices 1.4.102 → 1.5.0

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