incyclist-devices 2.1.0 → 2.1.2

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 (161) hide show
  1. package/lib/adapters.d.ts +1 -1
  2. package/lib/antv2/adapter-factory.d.ts +4 -15
  3. package/lib/antv2/adapter.d.ts +15 -15
  4. package/lib/antv2/adapter.js +67 -21
  5. package/lib/antv2/ant-interface.d.ts +3 -11
  6. package/lib/antv2/ant-interface.js +1 -1
  7. package/lib/antv2/base/adapter.d.ts +69 -0
  8. package/lib/antv2/base/adapter.js +439 -0
  9. package/lib/antv2/base/ant-interface.d.ts +35 -0
  10. package/lib/antv2/base/ant-interface.js +285 -0
  11. package/lib/antv2/base/binding.d.ts +13 -0
  12. package/lib/antv2/base/binding.js +27 -0
  13. package/lib/antv2/base/interface.d.ts +46 -0
  14. package/lib/antv2/base/interface.js +337 -0
  15. package/lib/antv2/cad/adapter.d.ts +10 -0
  16. package/lib/antv2/cad/adapter.js +25 -0
  17. package/lib/antv2/cad/index.d.ts +2 -0
  18. package/lib/antv2/cad/index.js +7 -0
  19. package/lib/antv2/consts.d.ts +2 -0
  20. package/lib/antv2/consts.js +5 -0
  21. package/lib/antv2/factories/adapter-factory.d.ts +14 -0
  22. package/lib/antv2/factories/adapter-factory.js +73 -0
  23. package/lib/antv2/factories/sensor-factory.d.ts +5 -0
  24. package/lib/antv2/factories/sensor-factory.js +22 -0
  25. package/lib/antv2/fe/adapter.d.ts +21 -30
  26. package/lib/antv2/fe/adapter.js +139 -251
  27. package/lib/antv2/hr/adapter.d.ts +6 -15
  28. package/lib/antv2/hr/adapter.js +7 -55
  29. package/lib/antv2/hr copy/adapter.d.ts +11 -0
  30. package/lib/antv2/hr copy/adapter.js +30 -0
  31. package/lib/antv2/hr copy/index.d.ts +2 -0
  32. package/lib/antv2/hr copy/index.js +7 -0
  33. package/lib/antv2/index.d.ts +5 -4
  34. package/lib/antv2/index.js +5 -3
  35. package/lib/antv2/pwr/adapter.d.ts +9 -33
  36. package/lib/antv2/pwr/adapter.js +19 -140
  37. package/lib/antv2/types.d.ts +32 -4
  38. package/lib/base/adpater.d.ts +39 -60
  39. package/lib/base/adpater.js +139 -161
  40. package/lib/base/consts.d.ts +4 -0
  41. package/lib/base/consts.js +9 -0
  42. package/lib/ble/adapter-factory.d.ts +10 -11
  43. package/lib/ble/base/adapter.d.ts +7 -14
  44. package/lib/ble/base/adapter.js +2 -36
  45. package/lib/ble/ble-interface.d.ts +5 -7
  46. package/lib/ble/cp/adapter.d.ts +6 -8
  47. package/lib/ble/cp/adapter.js +8 -27
  48. package/lib/ble/elite/adapter.d.ts +7 -8
  49. package/lib/ble/elite/adapter.js +5 -25
  50. package/lib/ble/fm/adapter.d.ts +6 -7
  51. package/lib/ble/fm/adapter.js +13 -34
  52. package/lib/ble/hr/adapter.d.ts +5 -8
  53. package/lib/ble/hr/adapter.js +2 -2
  54. package/lib/ble/peripheral-cache.d.ts +2 -3
  55. package/lib/ble/tacx/adapter.d.ts +2 -3
  56. package/lib/ble/tacx/adapter.js +8 -8
  57. package/lib/ble/types.d.ts +1 -2
  58. package/lib/ble/wahoo/adapter.d.ts +2 -2
  59. package/lib/ble/wahoo/adapter.js +6 -6
  60. package/lib/ble/wahoo/comms.js +16 -16
  61. package/lib/factories/adapters.d.ts +7 -0
  62. package/lib/factories/adapters.js +49 -0
  63. package/lib/factories/index.d.ts +3 -0
  64. package/lib/factories/index.js +10 -0
  65. package/lib/factories/interfaces.d.ts +7 -0
  66. package/lib/factories/interfaces.js +27 -0
  67. package/lib/index.d.ts +5 -6
  68. package/lib/index.js +4 -9
  69. package/lib/interfaces.d.ts +1 -1
  70. package/lib/modes/ant-fe-adv-st-mode.d.ts +1 -1
  71. package/lib/modes/antble-erg.d.ts +1 -1
  72. package/lib/modes/antble-smarttrainer.d.ts +2 -2
  73. package/lib/modes/base.d.ts +5 -5
  74. package/lib/modes/daum-classic-standard.d.ts +3 -2
  75. package/lib/modes/daum-erg.d.ts +3 -3
  76. package/lib/modes/daum-premium-standard.d.ts +3 -2
  77. package/lib/modes/daum-smarttrainer.d.ts +3 -2
  78. package/lib/modes/kettler-erg.d.ts +3 -3
  79. package/lib/modes/power-base.d.ts +3 -3
  80. package/lib/modes/power-base.js +2 -2
  81. package/lib/modes/power-meter.d.ts +3 -2
  82. package/lib/modes/simulator.d.ts +2 -1
  83. package/lib/modes/types.d.ts +2 -13
  84. package/lib/serial/SinglePathScanner.d.ts +17 -0
  85. package/lib/serial/SinglePathScanner.js +87 -0
  86. package/lib/serial/adapter-factory.d.ts +6 -6
  87. package/lib/serial/adapter.d.ts +6 -11
  88. package/lib/serial/base/adapter.d.ts +17 -0
  89. package/lib/serial/base/adapter.js +67 -0
  90. package/lib/serial/base/comms.d.ts +62 -0
  91. package/lib/serial/base/comms.js +280 -0
  92. package/lib/serial/base/serial-interface.d.ts +36 -0
  93. package/lib/serial/base/serial-interface.js +288 -0
  94. package/lib/serial/base/serial-scanner.d.ts +16 -0
  95. package/lib/serial/base/serial-scanner.js +87 -0
  96. package/lib/serial/base/serialport.d.ts +17 -0
  97. package/lib/serial/base/serialport.js +87 -0
  98. package/lib/serial/comms.d.ts +2 -2
  99. package/lib/serial/daum/DaumAdapter.d.ts +12 -24
  100. package/lib/serial/daum/DaumAdapter.js +40 -59
  101. package/lib/serial/daum/classic/PROTOCOL_NAME.d.ts +2 -0
  102. package/lib/serial/daum/classic/PROTOCOL_NAME.js +5 -0
  103. package/lib/serial/daum/classic/adapter.d.ts +8 -20
  104. package/lib/serial/daum/classic/adapter.js +28 -58
  105. package/lib/serial/daum/classic/comms.d.ts +4 -6
  106. package/lib/serial/daum/classic/comms.js +2 -2
  107. package/lib/serial/daum/classic/consts.d.ts +2 -0
  108. package/lib/serial/daum/classic/consts.js +5 -0
  109. package/lib/serial/daum/classic/mock.js +5 -4
  110. package/lib/serial/daum/classic/types.d.ts +8 -10
  111. package/lib/serial/daum/classic/utils.d.ts +1 -2
  112. package/lib/serial/daum/consts.d.ts +0 -19
  113. package/lib/serial/daum/consts.js +0 -22
  114. package/lib/serial/daum/premium/adapter.d.ts +8 -11
  115. package/lib/serial/daum/premium/adapter.js +27 -53
  116. package/lib/serial/daum/premium/comms.d.ts +4 -7
  117. package/lib/serial/daum/premium/comms.js +23 -22
  118. package/lib/serial/daum/premium/mock.d.ts +1 -1
  119. package/lib/serial/daum/premium/mock.js +2 -2
  120. package/lib/serial/daum/premium/types.d.ts +32 -17
  121. package/lib/serial/daum/premium/types.js +8 -8
  122. package/lib/serial/daum/premium/utils.d.ts +2 -3
  123. package/lib/serial/daum/premium/utils.js +3 -3
  124. package/lib/serial/daum/types.d.ts +4 -1
  125. package/lib/serial/daum/types.js +9 -1
  126. package/lib/serial/factories/adapter-factory.d.ts +14 -0
  127. package/lib/serial/factories/adapter-factory.js +30 -0
  128. package/lib/serial/index.d.ts +8 -5
  129. package/lib/serial/index.js +15 -15
  130. package/lib/serial/kettler/comms.d.ts +1 -1
  131. package/lib/serial/kettler/ergo-racer/adapter.d.ts +11 -18
  132. package/lib/serial/kettler/ergo-racer/adapter.js +9 -21
  133. package/lib/serial/kettler/types.d.ts +8 -0
  134. package/lib/serial/kettler/types.js +2 -0
  135. package/lib/serial/serial-interface.d.ts +2 -32
  136. package/lib/serial/serial-interface.js +3 -76
  137. package/lib/serial/serial-scanner.d.ts +16 -0
  138. package/lib/serial/serial-scanner.js +87 -0
  139. package/lib/serial/serialport.d.ts +1 -13
  140. package/lib/serial/types.d.ts +45 -0
  141. package/lib/serial/types.js +9 -0
  142. package/lib/simulator/Simulator.d.ts +10 -13
  143. package/lib/simulator/Simulator.js +11 -23
  144. package/lib/types/Command.d.ts +8 -0
  145. package/lib/types/Command.js +2 -0
  146. package/lib/types/adapter.d.ts +22 -33
  147. package/lib/types/adapter.js +0 -68
  148. package/lib/types/command.d.ts +0 -8
  149. package/lib/types/command.js +0 -2
  150. package/lib/types/data.d.ts +12 -1
  151. package/lib/types/device.d.ts +8 -23
  152. package/lib/types/device.js +9 -8
  153. package/lib/types/index.d.ts +6 -0
  154. package/lib/types/index.js +22 -0
  155. package/lib/types/route.d.ts +0 -19
  156. package/lib/types/route.js +0 -2
  157. package/lib/types/types.d.ts +8 -0
  158. package/lib/types/types.js +2 -0
  159. package/lib/utils/utils.d.ts +2 -0
  160. package/lib/utils/utils.js +39 -4
  161. package/package.json +1 -1
@@ -0,0 +1,439 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const interface_1 = __importDefault(require("./interface"));
16
+ const adpater_1 = __importDefault(require("../../base/adpater"));
17
+ const types_1 = require("../types");
18
+ const types_2 = require("../../types");
19
+ const utils_1 = require("../../utils/utils");
20
+ const utils_2 = require("../utils");
21
+ const consts_1 = require("../consts");
22
+ const sensor_factory_1 = __importDefault(require("../factories/sensor-factory"));
23
+ const gd_eventlog_1 = require("gd-eventlog");
24
+ const INTERFACE_NAME = 'ant';
25
+ const MAX_RETRIES = 3;
26
+ class AntAdapter extends adpater_1.default {
27
+ constructor(settings, props) {
28
+ super(settings, props);
29
+ this.startupRetryPause = 1000;
30
+ const profile = this.getProfileName();
31
+ if (settings.protocol && settings.profile !== this.getLegacyProfileName())
32
+ throw new Error('Incorrect Profile');
33
+ if (!settings.protocol && settings.profile !== this.getProfileName())
34
+ throw new Error('Incorrect Profile');
35
+ const antSettings = this.settings;
36
+ if ((0, types_1.isLegacyProfile)(antSettings.profile))
37
+ antSettings.profile = (0, utils_2.mapLegacyProfile)(antSettings.profile);
38
+ if (this.settings.interface !== 'ant')
39
+ throw new Error('Incorrect interface');
40
+ this.sensor = this.createSensor(settings);
41
+ this.sensorConnected = false;
42
+ this.deviceData = {
43
+ DeviceID: Number(settings.deviceID)
44
+ };
45
+ this.data = {};
46
+ this.dataMsgCount = 0;
47
+ this.logger = new gd_eventlog_1.EventLogger(`Ant+${profile}`);
48
+ this.updateFrequency = consts_1.DEFAULT_UPDATE_FREQUENCY;
49
+ this.channel = undefined;
50
+ this.ant = interface_1.default.getInstance();
51
+ }
52
+ getProfileName() {
53
+ const C = this.constructor;
54
+ return C['ANT_PROFILE_NAME'];
55
+ }
56
+ getLegacyProfileName() {
57
+ const C = this.constructor;
58
+ return C['INCYCLIST_PROFILE_NAME'];
59
+ }
60
+ createSensor(settings) {
61
+ return sensor_factory_1.default.create(this.getProfileName(), Number(settings.deviceID));
62
+ }
63
+ isEqual(settings) {
64
+ const as = this.settings;
65
+ if (as.interface !== settings.interface)
66
+ return false;
67
+ if (Number(as.deviceID) !== Number(settings.deviceID) || as.profile !== settings.profile)
68
+ return false;
69
+ return true;
70
+ }
71
+ getDefaultReconnectDelay() {
72
+ return this.startupRetryPause;
73
+ }
74
+ connect() {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ const connected = yield interface_1.default.getInstance().connect();
77
+ return connected;
78
+ });
79
+ }
80
+ close() {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ return true;
83
+ });
84
+ }
85
+ resetData() {
86
+ this.dataMsgCount = 0;
87
+ const { DeviceID } = this.deviceData;
88
+ this.deviceData = { DeviceID };
89
+ this.data = {};
90
+ this.lastDataTS = undefined;
91
+ }
92
+ isSame(device) {
93
+ if (!(device instanceof AntAdapter))
94
+ return false;
95
+ const adapter = device;
96
+ return (adapter.getID() === this.getID() && adapter.getProfile() === this.getProfile());
97
+ }
98
+ hasData() {
99
+ return this.dataMsgCount > 0;
100
+ }
101
+ mapData(deviceData) {
102
+ throw new Error('message not implemented');
103
+ }
104
+ transformData(data, deviceData) {
105
+ throw new Error('message not implemented');
106
+ }
107
+ mapToAdapterData(deviceData) {
108
+ throw new Error('message not implemented');
109
+ }
110
+ onDeviceData(deviceData) {
111
+ this.dataMsgCount++;
112
+ this.lastDataTS = Date.now();
113
+ try {
114
+ const { ManId } = this.deviceData;
115
+ this.deviceData = Object.assign({}, deviceData);
116
+ if (!ManId && deviceData.ManId) {
117
+ this.emit('device-info', this.getSettings(), { manufacturer: (0, utils_2.getBrand)(deviceData.ManId) });
118
+ }
119
+ if (!this.started || this.isStopped())
120
+ return;
121
+ if (!this.canEmitData())
122
+ return;
123
+ const logData = this.getLogData(deviceData, ['PairedDevices', 'RawData']);
124
+ this.logEvent({ message: 'onDeviceData', data: logData, paused: this.paused });
125
+ if (this.isControllable()) {
126
+ let incyclistData = this.mapData(deviceData);
127
+ incyclistData = this.getCyclingMode().updateData(incyclistData);
128
+ this.transformData(incyclistData, deviceData);
129
+ }
130
+ else {
131
+ this.mapToAdapterData(deviceData);
132
+ }
133
+ this.emitData(this.data);
134
+ }
135
+ catch (err) {
136
+ this.logEvent({ message: 'error', fn: 'onDeviceData()', error: err.message || err, stack: err.stack });
137
+ }
138
+ }
139
+ isWaitingForData() {
140
+ return this.promiseWaitForData !== undefined && this.promiseWaitForData !== null;
141
+ }
142
+ _wait() {
143
+ return __awaiter(this, void 0, void 0, function* () {
144
+ const res = new Promise((resolve) => {
145
+ const iv = setInterval(() => {
146
+ if (this.hasData()) {
147
+ clearInterval(iv);
148
+ resolve(true);
149
+ }
150
+ if (!this.promiseWaitForData) {
151
+ clearInterval(iv);
152
+ }
153
+ }, 10);
154
+ });
155
+ return res;
156
+ });
157
+ }
158
+ waitForData(timeout) {
159
+ return __awaiter(this, void 0, void 0, function* () {
160
+ if (this.hasData()) {
161
+ return true;
162
+ }
163
+ const tsStart = Date.now();
164
+ if (this.promiseWaitForData) {
165
+ let hasData = false;
166
+ try {
167
+ hasData = yield this.promiseWaitForData;
168
+ }
169
+ catch (_a) { }
170
+ if (hasData || Date.now() > tsStart + timeout)
171
+ return hasData;
172
+ }
173
+ try {
174
+ this.promiseWaitForData = (0, utils_1.runWithTimeout)(this._wait(), timeout);
175
+ yield this.promiseWaitForData;
176
+ this.promiseWaitForData = null;
177
+ return true;
178
+ }
179
+ catch (err) {
180
+ this.promiseWaitForData = null;
181
+ return false;
182
+ }
183
+ });
184
+ }
185
+ getID() {
186
+ var _a;
187
+ const id = this.deviceData.DeviceID || ((_a = this.sensor) === null || _a === void 0 ? void 0 : _a.getDeviceID());
188
+ return id.toString();
189
+ }
190
+ getName() {
191
+ if (this.settings.name)
192
+ return this.settings.name;
193
+ const deviceID = this.sensor.getDeviceID();
194
+ const profile = this.sensor.getProfile();
195
+ return `Ant+${profile} ${deviceID}`;
196
+ }
197
+ getUniqueName() {
198
+ if (this.settings.name)
199
+ return this.settings.name;
200
+ const { ManId } = this.deviceData;
201
+ const profile = this.getProfileName();
202
+ const brand = (0, utils_2.getBrand)(ManId);
203
+ const id = this.getID();
204
+ if (brand)
205
+ return `${brand} ${profile} ${id}`;
206
+ else
207
+ return `${this.getName()}`;
208
+ }
209
+ getDisplayName() {
210
+ return this.getUniqueName();
211
+ }
212
+ getInterface() {
213
+ return INTERFACE_NAME;
214
+ }
215
+ getProfile() {
216
+ const settings = this.settings;
217
+ if (settings.protocol === undefined)
218
+ return settings.profile;
219
+ else {
220
+ return (0, utils_2.mapLegacyProfile)(settings.profile);
221
+ }
222
+ }
223
+ getLogData(data, excludeList) {
224
+ const logData = JSON.parse(JSON.stringify(data));
225
+ excludeList.forEach((key) => {
226
+ delete logData[key];
227
+ });
228
+ return logData;
229
+ }
230
+ check() {
231
+ return __awaiter(this, void 0, void 0, function* () {
232
+ try {
233
+ return yield this.start();
234
+ }
235
+ catch (error) {
236
+ return false;
237
+ }
238
+ });
239
+ }
240
+ checkCapabilities() {
241
+ return __awaiter(this, void 0, void 0, function* () {
242
+ return;
243
+ });
244
+ }
245
+ initControl() {
246
+ return __awaiter(this, void 0, void 0, function* () {
247
+ return;
248
+ });
249
+ }
250
+ getDefaultStartupTimeout() {
251
+ return 20000;
252
+ }
253
+ sendUpdate(request) {
254
+ if (!this.isControllable())
255
+ return;
256
+ if (this.isPaused() || this.isStopped())
257
+ return;
258
+ if (!this.hasCapability(types_2.IncyclistCapability.Control))
259
+ this.getCyclingMode().sendBikeUpdate(request);
260
+ else
261
+ throw new Error('method not implemented');
262
+ }
263
+ startPreChecks(props) {
264
+ return __awaiter(this, void 0, void 0, function* () {
265
+ const wasPaused = this.paused;
266
+ const wasStopped = this.stopped;
267
+ this.stopped = false;
268
+ if (wasPaused)
269
+ this.resume();
270
+ if (this.started && !wasPaused && !wasStopped) {
271
+ return 'done';
272
+ }
273
+ if (this.started && wasPaused) {
274
+ return 'done';
275
+ }
276
+ const connected = yield this.connect();
277
+ if (!connected)
278
+ return 'connection-failed';
279
+ return 'connected';
280
+ });
281
+ }
282
+ resetStartStatus() {
283
+ this.startStatus = { timeout: false, hasData: false, sensorStarted: false };
284
+ }
285
+ isStartSuccess() {
286
+ const { timeout, hasData, sensorStarted, controlInitialized, userInitialized, interrupted } = this.startStatus;
287
+ if (interrupted)
288
+ return false;
289
+ if (this.hasCapability(types_2.IncyclistCapability.Control))
290
+ return sensorStarted && hasData && userInitialized && controlInitialized && !timeout;
291
+ else
292
+ return sensorStarted && hasData && !timeout;
293
+ }
294
+ reportStartStatus() {
295
+ const success = this.isStartSuccess();
296
+ if (success) {
297
+ this.logEvent({ message: 'start device success' });
298
+ this.started = true;
299
+ this.paused = false;
300
+ return true;
301
+ }
302
+ else {
303
+ this.started = false;
304
+ const { sensorStarted, hasData, interrupted } = this.startStatus;
305
+ if (interrupted)
306
+ return;
307
+ if (!sensorStarted) {
308
+ this.logEvent({ message: 'start device failed', reason: 'could not connect' });
309
+ throw new Error('could not start device, reason:could not connect');
310
+ }
311
+ else if (!hasData) {
312
+ this.logEvent({ message: 'start device failed', reason: 'no data received' });
313
+ throw new Error('could not start device, reason:no data received');
314
+ }
315
+ else {
316
+ this.logEvent({ message: 'start device failed', reason: 'could not send FE commands' });
317
+ throw new Error('could not start device, reason:could not send FE commands');
318
+ }
319
+ }
320
+ }
321
+ waitForInitialData(startupTimeout) {
322
+ return __awaiter(this, void 0, void 0, function* () {
323
+ const { sensorStarted, hasData, timeout } = this.startStatus;
324
+ if ((sensorStarted && hasData) || !sensorStarted || timeout)
325
+ return;
326
+ this.logEvent({ message: 'wait for sensor data', });
327
+ this.startStatus.hasData = yield this.waitForData(startupTimeout);
328
+ if (this.startStatus.hasData)
329
+ this.logEvent({ message: 'sensor data received', });
330
+ });
331
+ }
332
+ initSensor(props) {
333
+ return __awaiter(this, void 0, void 0, function* () {
334
+ this.startStatus.sensorStarted = this.sensorConnected;
335
+ if (this.startStatus.sensorStarted || this.startStatus.sensorStarted)
336
+ return;
337
+ this.logEvent({ message: 'start sensor', props });
338
+ try {
339
+ this.sensorConnected = yield this.startSensor();
340
+ if (this.sensorConnected) {
341
+ this.logEvent({ message: 'sensor started', props });
342
+ this.startStatus.sensorStarted = true;
343
+ }
344
+ }
345
+ catch (err) {
346
+ this.logEvent({ message: 'start sensor failed', reason: err.message, props });
347
+ }
348
+ });
349
+ }
350
+ start(props = {}) {
351
+ return __awaiter(this, void 0, void 0, function* () {
352
+ const preCheckResult = yield this.startPreChecks(props);
353
+ if (preCheckResult === 'done')
354
+ return this.started;
355
+ if (preCheckResult === 'connection-failed')
356
+ throw new Error(`could not start device, reason:could not connect`);
357
+ this.logEvent({ message: 'starting device', props, isStarted: this.started });
358
+ this.resetStartStatus();
359
+ this.resetData();
360
+ const { startupTimeout = this.getDefaultStartupTimeout() } = props || {};
361
+ const retryDelay = this.getDefaultReconnectDelay();
362
+ const totalTimeout = Math.min(startupTimeout + 10000, (startupTimeout + retryDelay) * MAX_RETRIES);
363
+ const doStart = () => __awaiter(this, void 0, void 0, function* () {
364
+ let success = false;
365
+ let retry = 0;
366
+ while (!success && retry < MAX_RETRIES && !this.startStatus.timeout && !this.startStatus.interrupted) {
367
+ try {
368
+ retry++;
369
+ yield this.initSensor(props);
370
+ yield this.waitForInitialData(startupTimeout);
371
+ yield this.checkCapabilities();
372
+ if (this.hasCapability(types_2.IncyclistCapability.Control))
373
+ yield this.initControl();
374
+ if (!this.startStatus.hasData) {
375
+ yield this.stopSensor();
376
+ yield (0, utils_1.sleep)(retryDelay);
377
+ continue;
378
+ }
379
+ success = this.isStartSuccess();
380
+ }
381
+ catch (err) {
382
+ this.logEvent({ message: 'error', fn: 'start#doStart', error: err.message, stack: err.stack });
383
+ }
384
+ }
385
+ this.reportStartStatus();
386
+ return this.started;
387
+ });
388
+ try {
389
+ yield (0, utils_1.runWithTimeout)(doStart(), totalTimeout);
390
+ }
391
+ catch (err) {
392
+ if (err.message === 'Timeout') {
393
+ this.started = false;
394
+ this.startStatus.timeout = true;
395
+ throw new Error(`could not start device, reason:timeout`);
396
+ }
397
+ throw err;
398
+ }
399
+ return true;
400
+ });
401
+ }
402
+ stop() {
403
+ return __awaiter(this, void 0, void 0, function* () {
404
+ let stopped;
405
+ this.promiseWaitForData = null;
406
+ if (this.startStatus)
407
+ this.startStatus.interrupted = true;
408
+ try {
409
+ stopped = yield this.ant.stopSensor(this.sensor);
410
+ }
411
+ catch (err) {
412
+ this.logEvent({ message: 'stop sensor failed', reason: err.message });
413
+ }
414
+ this.sensorConnected = false;
415
+ this.started = false;
416
+ this.stopped = true;
417
+ this.paused = false;
418
+ this.removeAllListeners();
419
+ return stopped;
420
+ });
421
+ }
422
+ startSensor() {
423
+ return __awaiter(this, void 0, void 0, function* () {
424
+ return this.ant.startSensor(this.sensor, this.onDeviceData.bind(this));
425
+ });
426
+ }
427
+ stopSensor() {
428
+ return __awaiter(this, void 0, void 0, function* () {
429
+ if (!this.sensorConnected)
430
+ return;
431
+ try {
432
+ yield yield this.ant.stopSensor(this.sensor);
433
+ this.sensorConnected = false;
434
+ }
435
+ catch (_a) { }
436
+ });
437
+ }
438
+ }
439
+ exports.default = AntAdapter;
@@ -0,0 +1,35 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from "events";
3
+ import { EventLogger } from "gd-eventlog";
4
+ import { IAntDevice, IChannel, ISensor } from "incyclist-ant-plus";
5
+ import { AntDeviceSettings, AntScanProps, AntInterfaceProps, ConnectState } from "../types";
6
+ import { IncyclistInterface } from "../../types";
7
+ import AntDeviceBinding from "./binding";
8
+ export default class AntInterface extends EventEmitter implements IncyclistInterface {
9
+ static _instance: AntInterface;
10
+ static INTERFACE_NAME: string;
11
+ static getInstance(props?: AntInterfaceProps): AntInterface;
12
+ static hasInstance(): boolean;
13
+ protected logger: EventLogger;
14
+ protected device: IAntDevice;
15
+ protected Binding: typeof AntDeviceBinding;
16
+ protected connectState: ConnectState;
17
+ protected props: AntInterfaceProps;
18
+ protected activeScan: IChannel;
19
+ constructor(props: AntInterfaceProps);
20
+ getName(): string;
21
+ getBinding(): typeof AntDeviceBinding;
22
+ setBinding(binding: typeof AntDeviceBinding): void;
23
+ setLogger(logger: EventLogger): void;
24
+ logEvent(event: any): void;
25
+ isConnected(): boolean;
26
+ connect(): Promise<boolean>;
27
+ disconnect(): Promise<boolean>;
28
+ onError(profile: any, error: any): void;
29
+ onData(profile: any, id: any, data: any, tag: any): void;
30
+ scan(props?: AntScanProps): Promise<AntDeviceSettings[]>;
31
+ stopScan(): Promise<boolean>;
32
+ startSensor(sensor: ISensor, onDeviceData: (data: any) => void): Promise<boolean>;
33
+ stopSensor(sensor: ISensor): Promise<boolean>;
34
+ isScanning(): boolean;
35
+ }