incyclist-devices 2.1.1 → 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.
@@ -13,22 +13,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const adapter_1 = __importDefault(require("../base/adapter"));
16
+ const types_1 = require("../../types");
16
17
  const ant_fe_adv_st_mode_1 = __importDefault(require("../../modes/ant-fe-adv-st-mode"));
17
- const utils_1 = require("../../utils/utils");
18
- const capabilities_1 = require("../../types/capabilities");
19
18
  const consts_1 = require("../../base/consts");
20
19
  const antble_erg_1 = __importDefault(require("../../modes/antble-erg"));
21
20
  const antble_smarttrainer_1 = __importDefault(require("../../modes/antble-smarttrainer"));
22
21
  const DEFAULT_BIKE_WEIGHT_MOUNTAIN = 14.5;
23
- const MAX_RETRIES = 3;
24
22
  class AntFEAdapter extends adapter_1.default {
25
23
  constructor(settings, props) {
26
24
  super(settings, props);
27
25
  this.startProps = {};
28
- this.sensorConnected = false;
29
26
  this.capabilities = [
30
- capabilities_1.IncyclistCapability.Power, capabilities_1.IncyclistCapability.Speed, capabilities_1.IncyclistCapability.Cadence,
31
- capabilities_1.IncyclistCapability.Control
27
+ types_1.IncyclistCapability.Power, types_1.IncyclistCapability.Speed, types_1.IncyclistCapability.Cadence,
28
+ types_1.IncyclistCapability.Control
32
29
  ];
33
30
  }
34
31
  getDisplayName() {
@@ -36,9 +33,6 @@ class AntFEAdapter extends adapter_1.default {
36
33
  const pwrStr = InstantaneousPower ? ` (${InstantaneousPower})` : '';
37
34
  return `${this.getUniqueName()}${pwrStr}`;
38
35
  }
39
- getDefaultReconnectDelay() {
40
- return 2000;
41
- }
42
36
  isReconnecting() {
43
37
  return this.promiseReconnect !== null && this.promiseReconnect !== undefined;
44
38
  }
@@ -71,8 +65,8 @@ class AntFEAdapter extends adapter_1.default {
71
65
  }
72
66
  onDeviceData(deviceData) {
73
67
  super.onDeviceData(deviceData);
74
- if (deviceData.HeartRate && !this.hasCapability(capabilities_1.IncyclistCapability.HeartRate)) {
75
- this.capabilities.push(capabilities_1.IncyclistCapability.HeartRate);
68
+ if (deviceData.HeartRate && !this.hasCapability(types_1.IncyclistCapability.HeartRate)) {
69
+ this.capabilities.push(types_1.IncyclistCapability.HeartRate);
76
70
  this.emit('device-info', this.getSettings(), { capabilities: this.capabilities });
77
71
  }
78
72
  }
@@ -118,134 +112,45 @@ class AntFEAdapter extends adapter_1.default {
118
112
  data.deviceTime = adapterData.time;
119
113
  this.data = data;
120
114
  }
121
- start(props) {
122
- return __awaiter(this, void 0, void 0, function* () {
123
- const isReconnect = (props === null || props === void 0 ? void 0 : props.reconnect) || false;
124
- const startProps = Object.assign({}, props || {});
125
- delete startProps.reconnect;
126
- return yield this.performStart(props, isReconnect);
115
+ start(props = {}) {
116
+ const _super = Object.create(null, {
117
+ start: { get: () => super.start }
127
118
  });
128
- }
129
- performStart(props, isReconnect) {
130
119
  return __awaiter(this, void 0, void 0, function* () {
131
- const wasPaused = this.paused;
132
- const wasStopped = this.stopped;
133
- this.startProps = props;
134
- if (wasPaused)
135
- this.resume();
136
- this.stopped = false;
137
- if (this.started && !wasPaused && !wasStopped) {
138
- return true;
139
- }
140
- const connected = yield this.connect();
141
- if (!connected)
142
- throw new Error(`could not start device, reason:could not connect`);
143
- this.logEvent({ message: 'starting device', props, isStarted: this.started, isReconnecting: isReconnect });
144
- const { startupTimeout = this.getDefaultStartupTimeout(), reconnectTimeout = this.getDefaultReconnectDelay() } = props || {};
145
- const totalTimeout = Math.min(startupTimeout + 10000, startupTimeout * 2);
146
- let status = { timeout: false, sensorStarted: false, hasData: false, userSent: false, slopeSent: false };
147
- const doStart = () => __awaiter(this, void 0, void 0, function* () {
148
- this.setFEDefaultTimeout();
149
- let success = false;
150
- let retry = 0;
151
- if (isReconnect) {
152
- status.userSent = true;
153
- status.slopeSent = true;
154
- }
155
- while (!success && retry < MAX_RETRIES && !status.timeout) {
156
- if (retry !== 0) {
157
- console.log('~~~ RETRY', status);
158
- }
159
- retry++;
160
- yield this.initSensor(status, props);
161
- yield this.waitForInitialData(status, startupTimeout);
162
- yield this.sendInititalUserMessage(status, props);
163
- yield this.sendInitialRequest(status, props);
164
- if (!status.hasData) {
165
- yield this.stopSensor();
166
- yield (0, utils_1.sleep)(reconnectTimeout);
167
- continue;
168
- }
169
- success = status.sensorStarted && status.hasData && status.userSent && status.slopeSent;
170
- }
171
- if (success) {
172
- this.logEvent({ message: 'ANT FE start success' });
173
- this.started = true;
174
- this.paused = false;
175
- return true;
176
- }
177
- else {
178
- this.started = false;
179
- if (!status.sensorStarted) {
180
- this.logEvent({ message: 'ANT FE start failed', reason: 'could not connect' });
181
- throw new Error('could not start device, reason:could not connect');
182
- }
183
- else if (!status.hasData) {
184
- this.logEvent({ message: 'ANT FE start failed', reason: 'no data received' });
185
- throw new Error('could not start device, reason:no data received');
186
- }
187
- else {
188
- this.logEvent({ message: 'ANT FE start failed', reason: 'could not send FE commands' });
189
- throw new Error('could not start device, reason:could not send FE commands');
190
- }
191
- }
192
- });
193
- try {
194
- yield (0, utils_1.runWithTimeout)(doStart(), totalTimeout);
195
- }
196
- catch (err) {
197
- if (err.message === 'Timeout') {
198
- this.started = false;
199
- status.timeout = true;
200
- throw new Error(`could not start device, reason:timeout`);
201
- }
202
- throw err;
203
- }
204
- return true;
120
+ return yield _super.start.call(this, props);
205
121
  });
206
122
  }
207
- waitForInitialData(status, startupTimeout) {
208
- return __awaiter(this, void 0, void 0, function* () {
209
- if ((status.sensorStarted && status.hasData) || !status.sensorStarted || status.timeout)
210
- return;
211
- this.logEvent({ message: 'wait for sensor data', });
212
- status.hasData = yield this.waitForData(startupTimeout);
213
- if (status.hasData)
214
- this.logEvent({ message: 'sensor data received', });
215
- });
123
+ resetStartStatus() {
124
+ const props = this.startProps;
125
+ const isReconnect = props.reconnect || false;
126
+ super.resetStartStatus();
127
+ if (isReconnect) {
128
+ delete props.reconnect;
129
+ this.startStatus.userInitialized = true;
130
+ this.startStatus.controlInitialized = true;
131
+ }
216
132
  }
217
- stopSensor() {
133
+ startPreChecks(props) {
134
+ const _super = Object.create(null, {
135
+ startPreChecks: { get: () => super.startPreChecks }
136
+ });
218
137
  return __awaiter(this, void 0, void 0, function* () {
219
- if (!this.sensorConnected)
220
- return;
221
- try {
222
- yield yield this.ant.stopSensor(this.sensor);
223
- this.sensorConnected = false;
224
- }
225
- catch (_a) { }
138
+ this.startProps = props;
139
+ this.setFEDefaultTimeout();
140
+ return yield _super.startPreChecks.call(this, props);
226
141
  });
227
142
  }
228
- initSensor(status, props) {
143
+ initControl() {
229
144
  return __awaiter(this, void 0, void 0, function* () {
230
- status.sensorStarted = this.sensorConnected;
231
- if (status.sensorStarted || status.timeout)
232
- return;
233
- this.logEvent({ message: 'start sensor', props });
234
- try {
235
- this.sensorConnected = yield this.startSensor();
236
- if (this.sensorConnected) {
237
- this.logEvent({ message: 'sensor started', props });
238
- status.sensorStarted = true;
239
- }
240
- }
241
- catch (err) {
242
- this.logEvent({ message: 'start sensor failed', reason: err.message, props });
243
- }
145
+ yield this.sendInititalUserMessage();
146
+ yield this.sendInitialRequest();
244
147
  });
245
148
  }
246
- sendInititalUserMessage(status, props) {
149
+ sendInititalUserMessage() {
247
150
  return __awaiter(this, void 0, void 0, function* () {
248
- if (!status.sensorStarted || !status.hasData || status.userSent || status.timeout)
151
+ const props = this.startProps;
152
+ const { sensorStarted, hasData, userInitialized, timeout } = this.startStatus;
153
+ if (!sensorStarted || !hasData || userInitialized || timeout)
249
154
  return;
250
155
  const opts = props || {};
251
156
  const { args = {}, user = {} } = opts;
@@ -256,39 +161,40 @@ class AntFEAdapter extends adapter_1.default {
256
161
  const defaultBikeWeight = bikeType === 'mountain' ? DEFAULT_BIKE_WEIGHT_MOUNTAIN : consts_1.DEFAULT_BIKE_WEIGHT;
257
162
  const userWeight = args.userWeight || user.weight || consts_1.DEFAULT_USER_WEIGHT;
258
163
  const bikeWeight = args.bikeWeight || defaultBikeWeight;
259
- status.userSent = status.userSent || (yield fe.sendUserConfiguration(userWeight, bikeWeight, args.wheelDiameter, args.gearRatio));
164
+ this.startStatus.userInitialized = yield fe.sendUserConfiguration(userWeight, bikeWeight, args.wheelDiameter, args.gearRatio);
260
165
  }
261
166
  catch (err) {
262
167
  this.logEvent({ message: 'sending FE message error', error: err.message });
263
- status.userSent = false;
168
+ this.startStatus.userInitialized = false;
264
169
  }
265
170
  });
266
171
  }
267
- sendInitialRequest(status, props) {
172
+ sendInitialRequest() {
268
173
  return __awaiter(this, void 0, void 0, function* () {
269
- if (!status.sensorStarted || !status.hasData || status.slopeSent || status.timeout)
174
+ const { sensorStarted, hasData, controlInitialized, timeout } = this.startStatus;
175
+ if (!sensorStarted || !hasData || controlInitialized || timeout)
270
176
  return;
271
177
  const fe = this.sensor;
272
178
  try {
273
179
  const startRequest = this.getCyclingMode().getBikeInitRequest();
274
180
  if (startRequest) {
275
181
  if (startRequest.targetPower !== undefined && startRequest.targetPower !== null) {
276
- status.slopeSent = yield fe.sendTargetPower(startRequest.targetPower);
182
+ this.startStatus.controlInitialized = yield fe.sendTargetPower(startRequest.targetPower);
277
183
  }
278
184
  else if (startRequest.slope !== undefined && startRequest.slope !== null) {
279
- status.slopeSent = yield fe.sendTrackResistance(startRequest.slope);
185
+ this.startStatus.controlInitialized = yield fe.sendTrackResistance(startRequest.slope);
280
186
  }
281
187
  else {
282
- status.slopeSent = true;
188
+ this.startStatus.controlInitialized = true;
283
189
  }
284
190
  }
285
191
  else {
286
- status.slopeSent = yield fe.sendTrackResistance(0.0);
192
+ this.startStatus.controlInitialized = yield fe.sendTrackResistance(0.0);
287
193
  }
288
194
  }
289
195
  catch (err) {
290
196
  this.logEvent({ message: 'sending FE message error', error: err.message });
291
- status.slopeSent = false;
197
+ this.startStatus.controlInitialized = false;
292
198
  }
293
199
  });
294
200
  }
@@ -310,7 +216,7 @@ class AntFEAdapter extends adapter_1.default {
310
216
  const doReconnect = () => __awaiter(this, void 0, void 0, function* () {
311
217
  try {
312
218
  yield this.stop();
313
- yield this.performStart(this.startProps, true);
219
+ yield this.start(Object.assign(Object.assign({}, this.startProps), { reconnect: true }));
314
220
  this.started = true;
315
221
  this.logEvent({ message: 'reconnect success' });
316
222
  return true;
@@ -338,7 +244,6 @@ class AntFEAdapter extends adapter_1.default {
338
244
  }
339
245
  }
340
246
  catch (err) {
341
- console.log(err);
342
247
  return false;
343
248
  }
344
249
  }
@@ -2,7 +2,7 @@ import AntPwrAdapter from "./pwr";
2
2
  import AntFEAdapter from "./fe";
3
3
  import AntHrAdapter from "./hr";
4
4
  import AntAdapterFactory from "./factories/adapter-factory";
5
- import AntInterface from "./base/ant-interface";
5
+ import AntInterface from "./base/interface";
6
6
  import { AntInterfaceProps } from "./types";
7
7
  export { AntDeviceSettings, AntDeviceProperties, AntScanProps } from "./types";
8
8
  export { AntAdapterFactory, AntFEAdapter, AntHrAdapter, AntPwrAdapter, AntInterface, AntInterfaceProps };
@@ -13,8 +13,8 @@ exports.AntHrAdapter = hr_1.default;
13
13
  const cad_1 = __importDefault(require("./cad"));
14
14
  const adapter_factory_1 = __importDefault(require("./factories/adapter-factory"));
15
15
  exports.AntAdapterFactory = adapter_factory_1.default;
16
- const ant_interface_1 = __importDefault(require("./base/ant-interface"));
17
- exports.AntInterface = ant_interface_1.default;
16
+ const interface_1 = __importDefault(require("./base/interface"));
17
+ exports.AntInterface = interface_1.default;
18
18
  const af = adapter_factory_1.default.getInstance();
19
19
  af.register('PWR', 'Power Meter', pwr_1.default);
20
20
  af.register('HR', 'Heartrate Monitor', hr_1.default);
@@ -4,6 +4,7 @@ import AntAdapter from './base/adapter';
4
4
  export interface AntDeviceSettings extends DeviceSettings {
5
5
  deviceID?: string;
6
6
  profile: Profile | LegacyProfile;
7
+ interface: string;
7
8
  protocol?: string;
8
9
  }
9
10
  export type LegacyProfile = 'Heartrate Monitor' | 'Power Meter' | 'Smart Trainer' | 'Speed Sensor' | 'Cadence Sensor' | 'Speed + Cadence Sensor';
@@ -40,3 +41,11 @@ export interface ConnectState {
40
41
  connected: boolean;
41
42
  connecting: boolean;
42
43
  }
44
+ export interface AdapterStartStatus {
45
+ timeout: boolean;
46
+ sensorStarted: boolean;
47
+ hasData: boolean;
48
+ userInitialized?: boolean;
49
+ controlInitialized?: boolean;
50
+ interrupted?: boolean;
51
+ }
@@ -1,6 +1,6 @@
1
1
  import { InterfaceProps } from "../types/interface";
2
2
  import { SerialInterface } from "../serial";
3
- import AntInterface from "../antv2/base/ant-interface";
3
+ import AntInterface from "../antv2/base/interface";
4
4
  import { BleInterface } from "../ble";
5
5
  export default class InterfaceFactory {
6
6
  static create(ifaceName: string, props?: InterfaceProps): AntInterface | BleInterface | SerialInterface;
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const serial_1 = require("../serial");
7
- const ant_interface_1 = __importDefault(require("../antv2/base/ant-interface"));
7
+ const interface_1 = __importDefault(require("../antv2/base/interface"));
8
8
  const device_1 = require("../types/device");
9
9
  const ble_1 = require("../ble");
10
10
  class InterfaceFactory {
@@ -17,7 +17,7 @@ class InterfaceFactory {
17
17
  return serial_1.SerialInterface.getInstance(serialProps);
18
18
  case device_1.INTERFACE.ANT:
19
19
  const antProps = props;
20
- return ant_interface_1.default.getInstance(antProps);
20
+ return interface_1.default.getInstance(antProps);
21
21
  case device_1.INTERFACE.BLE:
22
22
  const bleProps = props;
23
23
  return ble_1.BleInterface.getInstance(bleProps);
@@ -65,7 +65,7 @@ class SerialInterface extends events_1.default {
65
65
  this.logger.logEvent(event);
66
66
  }
67
67
  const w = global.window;
68
- if ((w === null || w === void 0 ? void 0 : w.DEVICE_DEBUG) || process.env.BLE_DEBUG) {
68
+ if ((w === null || w === void 0 ? void 0 : w.DEVICE_DEBUG) || process.env.SERIAL_DEBUG || process.env.ANT_DEBUG || process.env.BLE_DEBUG) {
69
69
  console.log('~~~ Serial', event);
70
70
  }
71
71
  }
@@ -32,7 +32,7 @@ class SinglePathScanner {
32
32
  this.logger.logEvent(event);
33
33
  }
34
34
  const w = global.window;
35
- if ((w === null || w === void 0 ? void 0 : w.DEVICE_DEBUG) || process.env.BLE_DEBUG) {
35
+ if ((w === null || w === void 0 ? void 0 : w.DEVICE_DEBUG) || process.env.BLE_DEBUG || process.env.ANT_DEBUG || process.env.SERIAL_DEBUG) {
36
36
  console.log('~~~ SerialScanner', event);
37
37
  }
38
38
  }
@@ -67,7 +67,7 @@ class SinglePathScanner {
67
67
  }
68
68
  const adapterSettings = { interface: this.serial.getName(), host, port, protocol };
69
69
  const adapter = adapter_factory_1.default.getInstance().createInstance(adapterSettings);
70
- found = yield adapter.check();
70
+ found = yield (adapter === null || adapter === void 0 ? void 0 : adapter.check());
71
71
  if (found) {
72
72
  this.isFound = true;
73
73
  const name = adapter.getName();
@@ -65,7 +65,7 @@ class DaumClassicMockImpl {
65
65
  }
66
66
  list() {
67
67
  return __awaiter(this, void 0, void 0, function* () {
68
- return binding_mock_1.MockBinding.list();
68
+ return yield binding_mock_1.MockBinding.list();
69
69
  });
70
70
  }
71
71
  open(options) {
@@ -4,6 +4,7 @@ export declare function runWithRetries(fn: any, maxRetries: any, timeBetween: an
4
4
  export declare function floatVal(d?: number | string): number;
5
5
  export declare function intVal(d?: number | string): number;
6
6
  export declare function hexstr(arr: any, start?: any, len?: any): string;
7
+ export declare function isTrue(value: any): boolean;
7
8
  export declare class Queue<T> {
8
9
  protected data: Array<T>;
9
10
  constructor(values?: Array<T>);
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.waitWithTimeout = exports.runWithTimeout = exports.Queue = exports.hexstr = exports.intVal = exports.floatVal = exports.runWithRetries = exports.resolveNextTick = exports.sleep = void 0;
12
+ exports.waitWithTimeout = exports.runWithTimeout = exports.Queue = exports.isTrue = exports.hexstr = exports.intVal = exports.floatVal = exports.runWithRetries = exports.resolveNextTick = exports.sleep = void 0;
13
13
  const sleep = (ms) => {
14
14
  return new Promise(resolve => setTimeout(resolve, ms));
15
15
  };
@@ -92,6 +92,23 @@ function hexstr(arr, start, len) {
92
92
  return str;
93
93
  }
94
94
  exports.hexstr = hexstr;
95
+ function isTrue(value) {
96
+ if (typeof (value) === 'string') {
97
+ value = value.trim().toLowerCase();
98
+ }
99
+ switch (value) {
100
+ case true:
101
+ case "true":
102
+ case 1:
103
+ case "1":
104
+ case "on":
105
+ case "yes":
106
+ return true;
107
+ default:
108
+ return false;
109
+ }
110
+ }
111
+ exports.isTrue = isTrue;
95
112
  class Queue {
96
113
  constructor(values) {
97
114
  this.data = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "dependencies": {
5
5
  "@serialport/bindings-interface": "^1.2.2",
6
6
  "@serialport/parser-byte-length": "^9.0.1",