incyclist-devices 2.0.3 → 2.0.5

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.
@@ -1,10 +1,11 @@
1
1
  import { Profile } from "incyclist-ant-plus";
2
- import AntAdapter from "./adapter";
2
+ import { DeviceData } from "../types/data";
3
+ import AntAdapter, { BaseDeviceData } from "./adapter";
3
4
  import { AntDeviceProperties, AntDeviceSettings, LegacyProfile } from "./types";
4
5
  export type AntAdapterInfo = {
5
6
  antProfile: Profile;
6
7
  incyclistProfile: LegacyProfile;
7
- Adapter: typeof AntAdapter;
8
+ Adapter: typeof AntAdapter<BaseDeviceData, DeviceData>;
8
9
  };
9
10
  export type AdapterQuery = {
10
11
  antProfile?: Profile;
@@ -15,7 +16,7 @@ export default class AntAdapterFactory {
15
16
  adapters: AntAdapterInfo[];
16
17
  static getInstance(): AntAdapterFactory;
17
18
  constructor();
18
- register(antProfile: Profile, incyclistProfile: LegacyProfile, Adapter: typeof AntAdapter): void;
19
+ register<TDeviceData extends BaseDeviceData, TData>(antProfile: Profile, incyclistProfile: LegacyProfile, Adapter: typeof AntAdapter<TDeviceData, TData>): void;
19
20
  getAdapter(query?: AdapterQuery): any;
20
21
  createInstance(settings: AntDeviceSettings, props?: AntDeviceProperties): any;
21
22
  createFromDetected(profile: Profile, deviceID: number, props?: AntDeviceProperties): any;
@@ -4,15 +4,19 @@ import AntInterface from './ant-interface';
4
4
  import IncyclistDevice from '../base/adpater';
5
5
  import { AntDeviceProperties, AntDeviceSettings } from './types';
6
6
  import { DeviceProperties } from '../types/device';
7
- import { Bike, IncyclistDeviceAdapter, OnDeviceDataCallback } from '../types/adapter';
7
+ import { Bike, IncyclistDeviceAdapter } from '../types/adapter';
8
8
  import CyclingMode from '../modes/cycling-mode';
9
9
  import { User } from '../types/user';
10
10
  export declare const DEFAULT_UPDATE_FREQUENCY = 1000;
11
- export default class AntAdapter extends IncyclistDevice {
11
+ export type BaseDeviceData = {
12
+ DeviceID: number;
13
+ ManId?: number;
14
+ };
15
+ export default class AntAdapter<TDeviceData extends BaseDeviceData, TData> extends IncyclistDevice {
12
16
  sensor: ISensor;
13
17
  lastUpdate?: number;
14
- data: any;
15
- deviceData: any;
18
+ data: TData;
19
+ deviceData: TDeviceData;
16
20
  updateFrequency: number;
17
21
  channel: IChannel;
18
22
  ant: AntInterface;
@@ -22,7 +26,7 @@ export default class AntAdapter extends IncyclistDevice {
22
26
  bikeSettings: {
23
27
  weight?: number;
24
28
  };
25
- onDataFn: OnDeviceDataCallback;
29
+ onDataFn: (data: TData) => void;
26
30
  startupRetryPause: number;
27
31
  protected ivDataTimeout: NodeJS.Timer;
28
32
  protected lastDataTS: number;
@@ -50,7 +54,7 @@ export default class AntAdapter extends IncyclistDevice {
50
54
  start(props?: AntDeviceProperties): Promise<boolean>;
51
55
  stop(): Promise<boolean>;
52
56
  }
53
- export declare class ControllableAntAdapter extends AntAdapter implements Bike {
57
+ export declare class ControllableAntAdapter<TDeviceData extends BaseDeviceData, TData> extends AntAdapter<TDeviceData, TData> implements Bike {
54
58
  cyclingMode: CyclingMode;
55
59
  user?: User;
56
60
  constructor(settings: AntDeviceSettings, props?: AntDeviceProperties);
@@ -61,5 +65,6 @@ export declare class ControllableAntAdapter extends AntAdapter implements Bike {
61
65
  getSupportedCyclingModes(): any[];
62
66
  getDefaultCyclingMode(): CyclingMode;
63
67
  setCyclingMode(mode: CyclingMode | string, settings?: any): void;
68
+ sendInitCommands(): Promise<boolean>;
64
69
  getCyclingMode(): CyclingMode;
65
70
  }
@@ -306,6 +306,11 @@ class ControllableAntAdapter extends AntAdapter {
306
306
  this.cyclingMode = selectedMode;
307
307
  this.cyclingMode.setSettings(settings);
308
308
  }
309
+ sendInitCommands() {
310
+ return __awaiter(this, void 0, void 0, function* () {
311
+ return true;
312
+ });
313
+ }
309
314
  getCyclingMode() {
310
315
  if (!this.cyclingMode)
311
316
  this.setCyclingMode(this.getDefaultCyclingMode());
@@ -57,6 +57,7 @@ class AntInterface extends events_1.default {
57
57
  logEvent(event) {
58
58
  if (this.logger)
59
59
  this.logger.logEvent(event);
60
+ console.log('~~~ ANT', event);
60
61
  }
61
62
  isConnected() {
62
63
  return this.connectState.connected;
@@ -2,12 +2,22 @@ import { FitnessEquipmentSensorState, ISensor, Profile } from "incyclist-ant-plu
2
2
  import { ControllableAntAdapter } from "../adapter";
3
3
  import CyclingMode, { IncyclistBikeData, UpdateRequest } from '../../modes/cycling-mode';
4
4
  import { AntDeviceProperties, AntDeviceSettings, LegacyProfile } from "../types";
5
- export default class AntFEAdapter extends ControllableAntAdapter {
5
+ type FitnessEquipmentSensorData = {
6
+ speed: number;
7
+ slope: number;
8
+ power: number;
9
+ cadence: number;
10
+ heartrate: number;
11
+ distance: number;
12
+ timestamp: number;
13
+ };
14
+ export default class AntFEAdapter extends ControllableAntAdapter<FitnessEquipmentSensorState, FitnessEquipmentSensorData> {
6
15
  static INCYCLIST_PROFILE_NAME: LegacyProfile;
7
16
  static ANT_PROFILE_NAME: Profile;
8
17
  protected distanceInternal?: number;
9
18
  protected startProps: AntDeviceProperties;
10
19
  protected isReconnecting: boolean;
20
+ protected sensorConnected: boolean;
11
21
  constructor(settings: AntDeviceSettings, props?: AntDeviceProperties);
12
22
  createSensor(settings: AntDeviceSettings): ISensor;
13
23
  getName(): string;
@@ -20,9 +30,11 @@ export default class AntFEAdapter extends ControllableAntAdapter {
20
30
  onDeviceData(deviceData: FitnessEquipmentSensorState): void;
21
31
  canSendUpdate(): boolean;
22
32
  mapToCycleModeData(deviceData: FitnessEquipmentSensorState): IncyclistBikeData;
23
- transformData(bikeData: IncyclistBikeData): any;
33
+ transformData(bikeData: IncyclistBikeData): FitnessEquipmentSensorData;
24
34
  start(props?: any): Promise<any>;
25
35
  setFEDefaultTimeout(): void;
36
+ stop(): Promise<boolean>;
26
37
  reconnect(): Promise<boolean>;
27
- setCyclingMode(mode: string | CyclingMode, settings?: any): void;
38
+ sendInitCommands(): Promise<boolean>;
28
39
  }
40
+ export {};
@@ -38,6 +38,7 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
38
38
  this.logger = new gd_eventlog_1.EventLogger('Ant+FE');
39
39
  this.isReconnecting = false;
40
40
  this.startProps = {};
41
+ this.sensorConnected = false;
41
42
  this.capabilities = [
42
43
  capabilities_1.IncyclistCapability.Power, capabilities_1.IncyclistCapability.Speed, capabilities_1.IncyclistCapability.Cadence,
43
44
  capabilities_1.IncyclistCapability.Control
@@ -166,7 +167,7 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
166
167
  }
167
168
  if (bikeData.distanceInternal !== undefined)
168
169
  this.distanceInternal = bikeData.distanceInternal;
169
- let data = {
170
+ const data = {
170
171
  speed: bikeData.speed,
171
172
  slope: bikeData.slope,
172
173
  power: bikeData.power !== undefined ? Math.round(bikeData.power) : undefined,
@@ -180,6 +181,7 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
180
181
  start(props) {
181
182
  return __awaiter(this, void 0, void 0, function* () {
182
183
  const wasPaused = this.paused;
184
+ this.startProps = props || {};
183
185
  if (wasPaused)
184
186
  this.resume();
185
187
  if (this.started && !wasPaused) {
@@ -188,7 +190,6 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
188
190
  const connected = yield this.connect();
189
191
  if (!connected)
190
192
  throw new Error(`could not start device, reason:could not connect`);
191
- this.startProps = props;
192
193
  this.logEvent({ message: 'starting device', props, isStarted: this.started, isReconnecting: this.isReconnecting });
193
194
  const opts = props || {};
194
195
  const { args = {}, user = {} } = opts;
@@ -203,41 +204,42 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
203
204
  }
204
205
  };
205
206
  to = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
206
- yield this.stop();
207
207
  reject(new Error(`could not start device, reason:timeout`));
208
+ this.started = false;
208
209
  to = null;
209
210
  }), totalTimeout);
210
211
  this.setFEDefaultTimeout();
211
212
  let success = false;
212
213
  let status = { userSent: false, slopeSent: false };
213
214
  let retry = 0;
214
- let startSuccess = 0;
215
+ let hasData = false;
215
216
  while (!success && retry < MAX_RETRIES) {
216
217
  retry++;
217
- if (!this.started) {
218
+ if (!this.sensorConnected) {
218
219
  this.logEvent({ message: 'start sensor', props });
219
- this.started = yield this.ant.startSensor(this.sensor, this.onDeviceData.bind(this));
220
- if (this.started) {
220
+ this.sensorConnected = yield this.ant.startSensor(this.sensor, this.onDeviceData.bind(this));
221
+ if (this.sensorConnected) {
221
222
  this.logEvent({ message: 'sensor started', props });
222
- startSuccess++;
223
223
  }
224
- if (this.started && startSuccess === 1) {
224
+ }
225
+ if (this.sensorConnected && !hasData) {
226
+ try {
227
+ yield this.waitForData(timeout);
228
+ hasData = true;
229
+ }
230
+ catch (err) {
231
+ stopTimeoutCheck();
225
232
  try {
226
- yield this.waitForData(timeout);
227
- }
228
- catch (err) {
229
- stopTimeoutCheck();
230
- try {
231
- yield yield this.ant.stopSensor(this.sensor);
232
- }
233
- catch (_a) { }
234
- this.started = false;
235
- return reject(new Error('could not start device, reason:timeout'));
233
+ yield yield this.ant.stopSensor(this.sensor);
234
+ this.sensorConnected = false;
236
235
  }
236
+ catch (_a) { }
237
+ this.started = false;
238
+ return reject(new Error('could not start device, reason: no data received'));
237
239
  }
238
- status = { userSent: false, slopeSent: false };
239
240
  }
240
- if (!this.started) {
241
+ status = { userSent: false, slopeSent: false };
242
+ if (!hasData) {
241
243
  yield (0, utils_2.sleep)(2000);
242
244
  continue;
243
245
  }
@@ -270,10 +272,6 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
270
272
  }
271
273
  catch (err) {
272
274
  this.logger.logEvent({ message: 'sending FE message error', error: err.message });
273
- try {
274
- yield this.ant.stopSensor(this.sensor);
275
- }
276
- catch (_b) { }
277
275
  this.started = false;
278
276
  }
279
277
  success = status.userSent && status.slopeSent;
@@ -282,22 +280,24 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
282
280
  success = true;
283
281
  }
284
282
  }
285
- while (success && this.dataMsgCount === 0) {
286
- yield (0, utils_2.sleep)(500);
287
- }
288
283
  if (success) {
289
284
  this.logEvent({ message: 'start success' });
285
+ this.started = true;
290
286
  stopTimeoutCheck();
291
287
  resolve(true);
292
288
  }
293
289
  else {
294
290
  this.logEvent({ message: 'start failed' });
295
291
  stopTimeoutCheck();
296
- if (this.started) {
292
+ if (!hasData) {
293
+ reject(new Error('could not start device, reason: no data received'));
294
+ }
295
+ else if (this.sensorConnected) {
297
296
  reject(new Error('could not start device, reason: could not send FE commands'));
298
297
  }
299
- else
298
+ else {
300
299
  reject(new Error('could not start device, reason: could not connect'));
300
+ }
301
301
  this.started = false;
302
302
  }
303
303
  }));
@@ -307,6 +307,11 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
307
307
  const fe = this.sensor;
308
308
  fe.setSendTimeout(5000);
309
309
  }
310
+ stop() {
311
+ const stopped = super.stop();
312
+ this.sensorConnected = false;
313
+ return stopped;
314
+ }
310
315
  reconnect() {
311
316
  return __awaiter(this, void 0, void 0, function* () {
312
317
  this.logger.logEvent({ message: 'reconnect to device' });
@@ -326,16 +331,25 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
326
331
  }
327
332
  });
328
333
  }
329
- setCyclingMode(mode, settings) {
330
- const modeChange = this.cyclingMode.getName() !== mode;
331
- super.setCyclingMode(mode, settings);
332
- if (modeChange && this.started && !this.stopped) {
333
- if (this.getCyclingMode() instanceof ant_fe_erg_mode_1.default) {
334
- const power = this.data.power;
335
- const request = power ? { targetPower: power } : this.getCyclingMode().getBikeInitRequest();
336
- this.sendUpdate(request, true);
334
+ sendInitCommands() {
335
+ return __awaiter(this, void 0, void 0, function* () {
336
+ if (this.started && !this.stopped) {
337
+ try {
338
+ if (this.getCyclingMode() instanceof ant_fe_erg_mode_1.default) {
339
+ const power = this.data.power;
340
+ const request = power ? { targetPower: power } : this.getCyclingMode().getBikeInitRequest();
341
+ yield this.sendUpdate(request, true);
342
+ return true;
343
+ }
344
+ }
345
+ catch (_a) {
346
+ return false;
347
+ }
337
348
  }
338
- }
349
+ else {
350
+ return false;
351
+ }
352
+ });
339
353
  }
340
354
  }
341
355
  exports.default = AntFEAdapter;
@@ -1,7 +1,10 @@
1
1
  import { HeartRateSensorState, ISensor, Profile } from "incyclist-ant-plus";
2
2
  import AntAdapter from "../adapter";
3
3
  import { AntDeviceProperties, AntDeviceSettings, LegacyProfile } from "../types";
4
- export default class AntHrAdapter extends AntAdapter {
4
+ type HeartRateSensorData = {
5
+ heartrate: number;
6
+ };
7
+ export default class AntHrAdapter extends AntAdapter<HeartRateSensorState, HeartRateSensorData> {
5
8
  static INCYCLIST_PROFILE_NAME: LegacyProfile;
6
9
  static ANT_PROFILE_NAME: Profile;
7
10
  constructor(settings: AntDeviceSettings, props?: AntDeviceProperties);
@@ -13,3 +16,4 @@ export default class AntHrAdapter extends AntAdapter {
13
16
  mapData(deviceData: HeartRateSensorState): void;
14
17
  hasData(): boolean;
15
18
  }
19
+ export {};
@@ -1,9 +1,16 @@
1
- import { ISensor, Profile } from "incyclist-ant-plus";
1
+ import { BicyclePowerSensorState, ISensor, Profile } from "incyclist-ant-plus";
2
2
  import { ControllableAntAdapter } from "../adapter";
3
3
  import CyclingMode, { IncyclistBikeData } from '../../modes/cycling-mode';
4
4
  import { AntDeviceProperties, AntDeviceSettings, LegacyProfile } from "../types";
5
- import { DeviceData } from "../../types/data";
6
- export default class AntPwrAdapter extends ControllableAntAdapter {
5
+ type PowerSensorData = {
6
+ speed: number;
7
+ slope: number;
8
+ power: number;
9
+ cadence: number;
10
+ distance: number;
11
+ timestamp: number;
12
+ };
13
+ export default class AntPwrAdapter extends ControllableAntAdapter<BicyclePowerSensorState, PowerSensorData> {
7
14
  static INCYCLIST_PROFILE_NAME: LegacyProfile;
8
15
  static ANT_PROFILE_NAME: Profile;
9
16
  protected distanceInternal?: number;
@@ -19,6 +26,7 @@ export default class AntPwrAdapter extends ControllableAntAdapter {
19
26
  canSendUpdate(): boolean;
20
27
  sendUpdate(request: any): void;
21
28
  mapData(deviceData: any): IncyclistBikeData;
22
- transformData(bikeData: IncyclistBikeData): DeviceData;
29
+ transformData(bikeData: IncyclistBikeData): PowerSensorData;
23
30
  hasData(): boolean;
24
31
  }
32
+ export {};
@@ -119,7 +119,7 @@ class AntPwrAdapter extends adapter_1.ControllableAntAdapter {
119
119
  }
120
120
  if (bikeData.distanceInternal !== undefined)
121
121
  this.distanceInternal = bikeData.distanceInternal;
122
- let data = {
122
+ const data = {
123
123
  speed: bikeData.speed,
124
124
  slope: bikeData.slope,
125
125
  power: bikeData.power,
@@ -64,5 +64,6 @@ export declare class ControllableDevice extends IncyclistDevice implements Bike
64
64
  getSupportedCyclingModes(): any[];
65
65
  getDefaultCyclingMode(): CyclingMode;
66
66
  setCyclingMode(mode: CyclingMode | string, settings?: any): void;
67
+ sendInitCommands(): Promise<boolean>;
67
68
  getCyclingMode(): CyclingMode;
68
69
  }
@@ -65,6 +65,8 @@ class IncyclistDevice extends events_1.default {
65
65
  if (!this.logger || this.paused)
66
66
  return;
67
67
  this.logger.logEvent(event);
68
+ const logText = '~~~' + this.getInterface().toUpperCase();
69
+ console.log(logText, event);
68
70
  }
69
71
  getMaxUpdateFrequency() {
70
72
  return this.updateFrequency;
@@ -174,6 +176,11 @@ class ControllableDevice extends IncyclistDevice {
174
176
  this.cyclingMode = selectedMode;
175
177
  this.cyclingMode.setSettings(settings);
176
178
  }
179
+ sendInitCommands() {
180
+ return __awaiter(this, void 0, void 0, function* () {
181
+ return true;
182
+ });
183
+ }
177
184
  getCyclingMode() {
178
185
  if (!this.cyclingMode)
179
186
  this.setCyclingMode(this.getDefaultCyclingMode());
@@ -49,5 +49,6 @@ export declare class BleControllableAdapter extends BleAdapter implements Bike {
49
49
  getSupportedCyclingModes(): any[];
50
50
  getDefaultCyclingMode(): CyclingMode;
51
51
  setCyclingMode(mode: CyclingMode | string, settings?: any): void;
52
+ sendInitCommands(): Promise<boolean>;
52
53
  getCyclingMode(): CyclingMode;
53
54
  }
@@ -281,6 +281,11 @@ class BleControllableAdapter extends BleAdapter {
281
281
  this.cyclingMode = selectedMode;
282
282
  this.cyclingMode.setSettings(settings);
283
283
  }
284
+ sendInitCommands() {
285
+ return __awaiter(this, void 0, void 0, function* () {
286
+ return true;
287
+ });
288
+ }
284
289
  getCyclingMode() {
285
290
  if (!this.cyclingMode)
286
291
  this.setCyclingMode(this.getDefaultCyclingMode());
@@ -88,9 +88,7 @@ class BleComms extends events_1.default {
88
88
  if (this.logger) {
89
89
  this.logger.logEvent(event);
90
90
  }
91
- if (process.env.BLE_DEBUG) {
92
- console.log('~~~BLE:', event);
93
- }
91
+ console.log('~~~BLE:', event);
94
92
  }
95
93
  setLogger(logger) {
96
94
  this.logger = logger;
@@ -80,9 +80,7 @@ class BleInterface extends events_1.default {
80
80
  if (this.logger) {
81
81
  this.logger.logEvent(event);
82
82
  }
83
- if (process.env.BLE_DEBUG) {
84
- console.log('~~BLE:', event);
85
- }
83
+ console.log('~~BLE:', event);
86
84
  }
87
85
  onStateChange(state) {
88
86
  if (state !== 'poweredOn') {
@@ -32,9 +32,7 @@ class BlePeripheralConnector {
32
32
  if (this.logger) {
33
33
  this.logger.logEvent(event);
34
34
  }
35
- if (process.env.BLE_DEBUG) {
36
- console.log('~~~BLE:', event);
37
- }
35
+ console.log('~~~BLE:', event);
38
36
  }
39
37
  connect() {
40
38
  return __awaiter(this, void 0, void 0, function* () {
@@ -17,6 +17,6 @@ export default class BleFmAdapter extends BleControllableAdapter {
17
17
  mapData(deviceData: IndoorBikeData): IncyclistBikeData;
18
18
  transformData(bikeData: IncyclistBikeData): DeviceData;
19
19
  start(props?: BleStartProperties): Promise<any>;
20
- sendUpdate(request: any): Promise<void>;
21
- setCyclingMode(mode: string | CyclingMode, settings?: any): void;
20
+ sendUpdate(request: any, enforced?: boolean): Promise<void>;
21
+ sendInitCommands(): Promise<boolean>;
22
22
  }
@@ -113,10 +113,11 @@ class BleFmAdapter extends adapter_1.BleControllableAdapter {
113
113
  start(props = {}) {
114
114
  return __awaiter(this, void 0, void 0, function* () {
115
115
  const wasPaused = this.paused;
116
- this.resume();
116
+ if (wasPaused)
117
+ this.resume();
117
118
  if (this.started && !wasPaused)
118
119
  return true;
119
- this.logEvent(Object.assign(Object.assign({ message: 'start requested' }, this.getSettings()), { protocol: this.getProtocolName(), props }));
120
+ this.logEvent(Object.assign(Object.assign({ message: 'starting device' }, this.getSettings()), { protocol: this.getProtocolName(), props, isStarted: this.started }));
120
121
  const { restart = wasPaused } = props;
121
122
  if (!restart && this.ble.isScanning() && !this.getComms().isConnected()) {
122
123
  }
@@ -172,7 +173,7 @@ class BleFmAdapter extends adapter_1.BleControllableAdapter {
172
173
  if (!hasControl)
173
174
  throw new Error('could not establish control');
174
175
  const startRequest = this.getCyclingMode().getBikeInitRequest();
175
- yield this.sendUpdate(startRequest);
176
+ yield this.sendUpdate(startRequest, true);
176
177
  }
177
178
  if (!this.started && !wasPaused) {
178
179
  comms.on('data', (data) => {
@@ -207,9 +208,9 @@ class BleFmAdapter extends adapter_1.BleControllableAdapter {
207
208
  }
208
209
  });
209
210
  }
210
- sendUpdate(request) {
211
+ sendUpdate(request, enforced = false) {
211
212
  return __awaiter(this, void 0, void 0, function* () {
212
- if (this.paused || !this.device)
213
+ if (!enforced && (this.paused || !this.device))
213
214
  return;
214
215
  try {
215
216
  const update = this.getCyclingMode().sendBikeUpdate(request);
@@ -227,23 +228,25 @@ class BleFmAdapter extends adapter_1.BleControllableAdapter {
227
228
  }
228
229
  });
229
230
  }
230
- setCyclingMode(mode, settings) {
231
- const modeChange = this.cyclingMode.getName() !== mode;
232
- const prevMode = this.getCyclingMode();
233
- super.setCyclingMode(mode, settings);
234
- const isPaused = this.isPaused();
235
- if (modeChange && this.started && !this.stopped) {
236
- if (prevMode instanceof ble_st_mode_1.default) {
237
- const power = this.data.power;
238
- const request = power ? { targetPower: power } : this.getCyclingMode().getBikeInitRequest();
239
- if (isPaused)
240
- this.resume();
241
- this.sendUpdate(request).then().catch().finally(() => {
242
- if (isPaused)
243
- this.pause();
244
- });
231
+ sendInitCommands() {
232
+ return __awaiter(this, void 0, void 0, function* () {
233
+ if (this.started && !this.stopped) {
234
+ try {
235
+ if (this.getCyclingMode() instanceof ble_erg_mode_1.default) {
236
+ const power = this.data.power;
237
+ const request = power ? { targetPower: power } : this.getCyclingMode().getBikeInitRequest();
238
+ yield this.sendUpdate(request, true);
239
+ return true;
240
+ }
241
+ }
242
+ catch (_a) {
243
+ return false;
244
+ }
245
245
  }
246
- }
246
+ else {
247
+ return false;
248
+ }
249
+ });
247
250
  }
248
251
  }
249
252
  exports.default = BleFmAdapter;
@@ -34,6 +34,7 @@ class BleERGCyclingMode extends power_base_1.default {
34
34
  }
35
35
  getBikeInitRequest() {
36
36
  const startPower = Number(this.getSetting('startPower'));
37
+ this.prevRequest = { targetPower: startPower };
37
38
  return { targetPower: startPower };
38
39
  }
39
40
  sendBikeUpdate(request) {
@@ -32,6 +32,7 @@ class FtmsCyclingMode extends power_base_1.default {
32
32
  return config.properties.find(p => p.name === name);
33
33
  }
34
34
  getBikeInitRequest() {
35
+ this.prevRequest = { slope: 0 };
35
36
  return { slope: 0 };
36
37
  }
37
38
  sendBikeUpdate(request) {
@@ -228,9 +228,14 @@ class TCPPortBinding {
228
228
  }
229
229
  this.writeOperation = new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
230
230
  yield resolveNextTick();
231
- this.socket.write(buffer, () => {
232
- resolve();
233
- });
231
+ try {
232
+ this.socket.write(buffer, () => {
233
+ resolve();
234
+ });
235
+ }
236
+ catch (err) {
237
+ this.onError(err);
238
+ }
234
239
  }));
235
240
  return this.writeOperation;
236
241
  }
@@ -27,6 +27,7 @@ export default class DaumAdapterBase extends SerialIncyclistDevice implements Da
27
27
  updateBusy: boolean;
28
28
  constructor(settings: SerialDeviceSettings, props?: DeviceProperties);
29
29
  setCyclingMode(mode: CyclingMode | string, settings?: any): void;
30
+ sendInitCommands(): Promise<boolean>;
30
31
  getSupportedCyclingModes(): Array<any>;
31
32
  getCyclingMode(): CyclingMode;
32
33
  getDefaultCyclingMode(): CyclingMode;
@@ -61,6 +61,26 @@ class DaumAdapterBase extends adapter_1.SerialIncyclistDevice {
61
61
  this.cyclingMode = selectedMode;
62
62
  this.cyclingMode.setSettings(settings);
63
63
  }
64
+ sendInitCommands() {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ if (this.started && !this.stopped) {
67
+ try {
68
+ if (this.getCyclingMode() instanceof ERGCyclingMode_1.default) {
69
+ const power = this.deviceData.power;
70
+ const request = power ? { targetPower: power } : this.getCyclingMode().getBikeInitRequest();
71
+ yield this.sendUpdate(request);
72
+ return true;
73
+ }
74
+ }
75
+ catch (_a) {
76
+ return false;
77
+ }
78
+ }
79
+ else {
80
+ return false;
81
+ }
82
+ });
83
+ }
64
84
  getSupportedCyclingModes() {
65
85
  return [ERGCyclingMode_1.default, SmartTrainerCyclingMode_1.default, DaumPowerMeterCyclingMode_1.default];
66
86
  }
@@ -215,7 +235,6 @@ class DaumAdapterBase extends adapter_1.SerialIncyclistDevice {
215
235
  this.updateBusy = true;
216
236
  this.getCurrentBikeData()
217
237
  .then(bikeData => {
218
- console.log('~~~ bike data', bikeData);
219
238
  this.updateData(this.cyclingData, bikeData);
220
239
  const data = this.transformData();
221
240
  this.updateBusy = false;
@@ -68,7 +68,7 @@ export declare class DaumClassicSimulator {
68
68
  simulateTimeout(ms: number): void;
69
69
  simulateNoResponse(cnt?: number): void;
70
70
  simulateIllegalResponse(cnt?: number): void;
71
- isPedalling(): 0 | 1;
71
+ isPedalling(): 1 | 0;
72
72
  }
73
73
  export declare class DaumClassicMockBinding extends MockPortBinding {
74
74
  prevCommand: Buffer;
@@ -2,7 +2,7 @@ export declare const DEFAULT_AGE = 30;
2
2
  export declare const DEFAULT_USER_WEIGHT = 75;
3
3
  export declare const DEFAULT_BIKE_WEIGHT = 10;
4
4
  export declare function getCockpit(c: any): "Cardio" | "Fitness" | "Vita De Luxe" | "8008" | "8008 TRS" | "8080" | "Therapie" | "8008 TRS Pro" | "8008 TRS3" | "ergo_lyps Cardio Pro" | "Unknown";
5
- export declare function getBikeType(type: any): 0 | 1;
5
+ export declare function getBikeType(type: any): 1 | 0;
6
6
  export declare function getGender(sex: any): 1 | 2;
7
7
  export declare function getLength(length: any): number;
8
8
  export declare function getWeight(weight?: any): number;
@@ -25,7 +25,6 @@ const DAUM_PREMIUM_DEFAULT_PORT = 51955;
25
25
  const START_RETRY_TIMEOUT = 1500;
26
26
  const DEFAULT_GEAR = 10;
27
27
  const getBikeProps = (props) => {
28
- console.log('~~~getBikeProps', props);
29
28
  const { host, port = DAUM_PREMIUM_DEFAULT_PORT, interface: ifaceName } = props;
30
29
  let serial;
31
30
  if (ifaceName && typeof ifaceName === 'string') {
@@ -47,7 +46,6 @@ const getBikeProps = (props) => {
47
46
  };
48
47
  class DaumPremiumAdapter extends DaumAdapter_1.default {
49
48
  constructor(settings, props) {
50
- console.log('~~~ new premium adapter');
51
49
  const logger = new gd_eventlog_1.EventLogger('DaumPremium');
52
50
  const commProps = Object.assign(Object.assign({}, getBikeProps(settings)), { logger });
53
51
  const bike = new comms_1.default(commProps);
@@ -42,7 +42,6 @@ class SinglePathScanner {
42
42
  }
43
43
  scan() {
44
44
  return __awaiter(this, void 0, void 0, function* () {
45
- console.log('~~~ start SERIAL Scan', this.isScanning);
46
45
  if (this.isScanning)
47
46
  return;
48
47
  this.isScanning = true;
@@ -53,7 +52,6 @@ class SinglePathScanner {
53
52
  let found = false;
54
53
  while (!found && this.isScanning) {
55
54
  try {
56
- console.log('~~~ adapter check attempt');
57
55
  const { protocol } = this.props;
58
56
  let host, port;
59
57
  if (this.serial.getName() === exports.SerialInterfaceType.TCPIP) {
@@ -72,7 +70,6 @@ class SinglePathScanner {
72
70
  yield (0, utils_1.sleep)(100);
73
71
  }
74
72
  catch (err) {
75
- console.log('~~~ERROR', err);
76
73
  this.logger.logEvent({ message: 'error', fn: 'scan()', error: err.message || err, stack: err.stack });
77
74
  yield (0, utils_1.sleep)(100);
78
75
  }
@@ -85,13 +82,11 @@ exports.SinglePathScanner = SinglePathScanner;
85
82
  class SerialInterface extends events_1.default {
86
83
  static getInstance(props) {
87
84
  const { ifaceName, binding, logger } = props;
88
- console.log('~~~ new instance #1', ifaceName, serialport_1.default.getInstance().getBinding(ifaceName));
89
85
  let instance = SerialInterface._instances.find(i => i.ifaceName === ifaceName);
90
86
  if (!instance) {
91
87
  if (binding)
92
88
  instance = new SerialInterface(props);
93
89
  else {
94
- console.log('~~~ new instance', ifaceName, serialport_1.default.getInstance().getBinding(ifaceName));
95
90
  instance = new SerialInterface({ ifaceName, binding: serialport_1.default.getInstance().getBinding(ifaceName), logger });
96
91
  if (instance)
97
92
  SerialInterface._instances.push(instance);
@@ -115,6 +110,7 @@ class SerialInterface extends events_1.default {
115
110
  this.isScanning = false;
116
111
  this.isStopScanRequested = false;
117
112
  this.scanEvents = new events_1.default();
113
+ this.scanEvents.setMaxListeners(100);
118
114
  this.logger = props.logger || new gd_eventlog_1.EventLogger(`Serial:${ifaceName}`);
119
115
  this.connected = false;
120
116
  this.logger.logEvent({ message: 'new serial interface', ifaceName });
@@ -160,7 +156,6 @@ class SerialInterface extends events_1.default {
160
156
  }
161
157
  openPort(path) {
162
158
  return __awaiter(this, void 0, void 0, function* () {
163
- console.log('~~~ SerialPort.openPort', this.ifaceName, path);
164
159
  this.logger.logEvent({ message: 'opening port', path });
165
160
  const port = serialport_1.default.getInstance().getSerialPort(this.ifaceName, { path });
166
161
  if (!port) {
@@ -193,7 +188,6 @@ class SerialInterface extends events_1.default {
193
188
  }
194
189
  closePort(path) {
195
190
  return __awaiter(this, void 0, void 0, function* () {
196
- console.log('~~~ SerialPort.closePort', this.ifaceName, path);
197
191
  const existing = this.ports.findIndex(p => p.path === path);
198
192
  if (existing === -1)
199
193
  return true;
@@ -217,7 +211,6 @@ class SerialInterface extends events_1.default {
217
211
  }
218
212
  scan(props) {
219
213
  return __awaiter(this, void 0, void 0, function* () {
220
- console.log('~~serial scan', this.isScanning, this.isConnected());
221
214
  if (this.isScanning)
222
215
  return [];
223
216
  if (!this.isConnected())
@@ -41,6 +41,7 @@ export declare class Simulator extends ControllableDevice {
41
41
  getDefaultCyclingMode(): CyclingMode;
42
42
  getCyclingMode(): CyclingMode;
43
43
  setCyclingMode(mode: CyclingMode | string, settings?: any): void;
44
+ sendInitCommands(): Promise<boolean>;
44
45
  start(props?: SimulatorProperties): Promise<boolean>;
45
46
  stop(): Promise<boolean>;
46
47
  pause(): Promise<boolean>;
@@ -88,6 +88,11 @@ class Simulator extends adpater_1.ControllableDevice {
88
88
  this.cyclingMode = selectedMode;
89
89
  this.cyclingMode.setSettings(settings);
90
90
  }
91
+ sendInitCommands() {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ return true;
94
+ });
95
+ }
91
96
  start(props) {
92
97
  return __awaiter(this, void 0, void 0, function* () {
93
98
  this.startProps = props;
@@ -30,7 +30,7 @@ export interface IncyclistDeviceAdapter extends EventEmitter {
30
30
  onData(callback: OnDeviceDataCallback): any;
31
31
  }
32
32
  export interface Bike {
33
- setCyclingMode(mode: CyclingMode | string, settings?: any): void;
33
+ setCyclingMode(mode: CyclingMode | string, settings?: any, sendInitCommands?: boolean): void;
34
34
  getSupportedCyclingModes(): Array<any>;
35
35
  getCyclingMode(): CyclingMode;
36
36
  getDefaultCyclingMode(): CyclingMode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "dependencies": {
5
5
  "@serialport/bindings-interface": "^1.2.2",
6
6
  "@serialport/parser-byte-length": "^9.0.1",