incyclist-devices 2.1.3 → 2.1.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.
@@ -48,6 +48,9 @@ class AntAdapter extends adpater_1.default {
48
48
  this.updateFrequency = consts_1.DEFAULT_UPDATE_FREQUENCY;
49
49
  this.channel = undefined;
50
50
  this.ant = interface_1.default.getInstance();
51
+ if (this.isDebugEnabled()) {
52
+ this.ant.setLogger(this);
53
+ }
51
54
  }
52
55
  getProfileName() {
53
56
  const C = this.constructor;
@@ -116,12 +119,10 @@ class AntAdapter extends adpater_1.default {
116
119
  if (!ManId && deviceData.ManId) {
117
120
  this.emit('device-info', this.getSettings(), { manufacturer: (0, utils_2.getBrand)(deviceData.ManId) });
118
121
  }
119
- if (!this.started || this.isStopped())
120
- return;
121
- if (!this.canEmitData())
122
- return;
123
122
  const logData = this.getLogData(deviceData, ['PairedDevices', 'RawData']);
124
123
  this.logEvent({ message: 'onDeviceData', data: logData, paused: this.paused });
124
+ if (!this.started || this.isStopped() || !this.canEmitData())
125
+ return;
125
126
  if (this.isControllable()) {
126
127
  let incyclistData = this.mapData(deviceData);
127
128
  incyclistData = this.getCyclingMode().updateData(incyclistData);
@@ -148,6 +149,7 @@ class AntAdapter extends adpater_1.default {
148
149
  resolve(true);
149
150
  }
150
151
  if (!this.promiseWaitForData) {
152
+ resolve(false);
151
153
  clearInterval(iv);
152
154
  }
153
155
  }, 10);
@@ -172,9 +174,9 @@ class AntAdapter extends adpater_1.default {
172
174
  }
173
175
  try {
174
176
  this.promiseWaitForData = (0, utils_1.runWithTimeout)(this._wait(), timeout);
175
- yield this.promiseWaitForData;
177
+ const hasData = yield this.promiseWaitForData;
176
178
  this.promiseWaitForData = null;
177
- return true;
179
+ return hasData;
178
180
  }
179
181
  catch (err) {
180
182
  this.promiseWaitForData = null;
@@ -293,8 +295,10 @@ class AntAdapter extends adpater_1.default {
293
295
  else {
294
296
  this.started = false;
295
297
  const { sensorStarted, hasData, interrupted } = this.startStatus;
296
- if (interrupted)
297
- return;
298
+ if (interrupted) {
299
+ this.logEvent({ message: 'start device interrupted', device: this.getName() });
300
+ return false;
301
+ }
298
302
  if (!sensorStarted) {
299
303
  this.logEvent({ message: 'start device failed', device: this.getName(), reason: 'could not connect' });
300
304
  throw new Error('could not start device, reason:could not connect');
@@ -362,7 +366,7 @@ class AntAdapter extends adpater_1.default {
362
366
  yield this.checkCapabilities();
363
367
  if (this.hasCapability(types_2.IncyclistCapability.Control))
364
368
  yield this.initControl();
365
- if (!this.startStatus.hasData) {
369
+ if (!this.startStatus.hasData && !this.startStatus.interrupted) {
366
370
  yield this.stopSensor();
367
371
  yield (0, utils_1.sleep)(retryDelay);
368
372
  continue;
@@ -388,7 +392,7 @@ class AntAdapter extends adpater_1.default {
388
392
  }
389
393
  throw err;
390
394
  }
391
- return true;
395
+ return this.started;
392
396
  });
393
397
  }
394
398
  stop() {
@@ -396,8 +400,11 @@ class AntAdapter extends adpater_1.default {
396
400
  let stopped;
397
401
  this.logger.logEvent({ message: 'stopping device', device: this.getName() });
398
402
  this.promiseWaitForData = null;
399
- if (this.startStatus)
403
+ if (this.startStatus) {
400
404
  this.startStatus.interrupted = true;
405
+ console.log('~~~ still starting');
406
+ yield (0, utils_1.sleep)(20);
407
+ }
401
408
  try {
402
409
  stopped = yield this.ant.stopSensor(this.sensor);
403
410
  }
@@ -5,6 +5,7 @@ import { AntDeviceProperties, AntDeviceSettings, LegacyProfile } from "../types"
5
5
  import { IncyclistBikeData, ControllerConfig } from "../../types";
6
6
  interface AntFEStartDeviceProperties extends AntDeviceProperties {
7
7
  reconnect?: boolean;
8
+ restart?: boolean;
8
9
  reconnectTimeout?: number;
9
10
  }
10
11
  export default class AntFEAdapter extends AntAdapter<FitnessEquipmentSensorState> {
@@ -123,12 +123,18 @@ class AntFEAdapter extends adapter_1.default {
123
123
  resetStartStatus() {
124
124
  const props = this.startProps;
125
125
  const isReconnect = props.reconnect || false;
126
+ const isRestart = props.restart || false;
126
127
  super.resetStartStatus();
127
128
  if (isReconnect) {
128
129
  delete props.reconnect;
129
130
  this.startStatus.userInitialized = true;
130
131
  this.startStatus.controlInitialized = true;
131
132
  }
133
+ if (isRestart) {
134
+ delete props.restart;
135
+ this.startStatus.sensorStarted = true;
136
+ this.startStatus.hasData = true;
137
+ }
132
138
  }
133
139
  startPreChecks(props) {
134
140
  const _super = Object.create(null, {
@@ -137,6 +143,11 @@ class AntFEAdapter extends adapter_1.default {
137
143
  return __awaiter(this, void 0, void 0, function* () {
138
144
  this.startProps = props;
139
145
  this.setFEDefaultTimeout();
146
+ if (this.started && this.paused) {
147
+ this.resume();
148
+ props.restart = true;
149
+ return 'connected';
150
+ }
140
151
  return yield _super.startPreChecks.call(this, props);
141
152
  });
142
153
  }
@@ -20,6 +20,7 @@ export default class IncyclistDevice<P extends DeviceProperties> extends EventEm
20
20
  protected data: IncyclistAdapterData;
21
21
  constructor(settings: DeviceSettings, props?: P);
22
22
  getLogger(): EventLogger;
23
+ isDebugEnabled(): boolean;
23
24
  logEvent(event: any): void;
24
25
  getName(): string;
25
26
  getID(): string;
@@ -30,12 +30,18 @@ class IncyclistDevice extends events_1.default {
30
30
  this.cyclingMode = this.getDefaultCyclingMode();
31
31
  }
32
32
  getLogger() { return this.logger; }
33
+ isDebugEnabled() {
34
+ const w = global.window;
35
+ if ((w === null || w === void 0 ? void 0 : w.DEVICE_DEBUG) || process.env.BLE_DEBUG || process.env.ANT_DEBUG) {
36
+ return true;
37
+ }
38
+ return false;
39
+ }
33
40
  logEvent(event) {
34
41
  if (!this.logger || this.paused)
35
42
  return;
36
43
  this.logger.logEvent(event);
37
- const w = global.window;
38
- if ((w === null || w === void 0 ? void 0 : w.DEVICE_DEBUG) || process.env.BLE_DEBUG || process.env.ANT_DEBUG) {
44
+ if (this.isDebugEnabled()) {
39
45
  const logText = `~~~ ${this.getInterface()}: ${this.logger.getName()}`;
40
46
  console.log(logText, event);
41
47
  }
@@ -63,14 +69,16 @@ class IncyclistDevice extends events_1.default {
63
69
  stop() { throw new Error("Method not implemented."); }
64
70
  pause() {
65
71
  return __awaiter(this, void 0, void 0, function* () {
66
- this.logEvent({ message: 'pausing device', device: this.getName() });
72
+ if (this.isStarted() && !this.isStopped())
73
+ this.logEvent({ message: 'pausing device', device: this.getName() });
67
74
  this.paused = true;
68
75
  return true;
69
76
  });
70
77
  }
71
78
  resume() {
72
79
  return __awaiter(this, void 0, void 0, function* () {
73
- this.logger.logEvent({ message: 'resuming device', device: this.getName() });
80
+ if (this.isStarted() && !this.isStopped())
81
+ this.logger.logEvent({ message: 'resuming device', device: this.getName() });
74
82
  this.paused = false;
75
83
  return true;
76
84
  });
@@ -141,10 +141,8 @@ class BleAdapter extends adpater_1.default {
141
141
  }
142
142
  start(props = {}) {
143
143
  return __awaiter(this, void 0, void 0, function* () {
144
- const wasPaused = this.paused;
145
144
  const wasStopped = this.stopped;
146
- if (wasPaused)
147
- this.resume();
145
+ this.resume();
148
146
  if (this.started && !wasStopped)
149
147
  return true;
150
148
  this.stopped = false;
@@ -61,10 +61,12 @@ class BleComms extends events_1.default {
61
61
  return this.connectState.isConnected;
62
62
  }
63
63
  pause() {
64
+ this.ble.pauseLogging();
64
65
  this.paused = true;
65
66
  }
66
67
  resume() {
67
68
  this.paused = false;
69
+ this.ble.resumeLogging();
68
70
  }
69
71
  getServiceUUids() {
70
72
  throw new Error("Method not implemented.");
@@ -282,6 +284,7 @@ class BleComms extends events_1.default {
282
284
  }
283
285
  connect(props) {
284
286
  return __awaiter(this, void 0, void 0, function* () {
287
+ this.ble.resumeLogging();
285
288
  if (!this.ble.isConnected()) {
286
289
  try {
287
290
  yield this.ble.connect();
@@ -382,6 +385,7 @@ class BleComms extends events_1.default {
382
385
  return __awaiter(this, void 0, void 0, function* () {
383
386
  const { id, name, address } = this;
384
387
  this.logEvent({ message: 'disconnect requested', device: { id, name, address } });
388
+ this.ble.pauseLogging();
385
389
  this.connectState.isDisconnecting = true;
386
390
  if (this.workerIv) {
387
391
  this.stopWorker();
@@ -9,6 +9,8 @@ export declare class BleLinuxBinding extends EventEmitter implements BleBinding
9
9
  static getInstance(): any;
10
10
  startScanning(serviceUUIDs?: string[] | undefined, allowDuplicates?: boolean | undefined, callback?: ((error?: Error | undefined) => void) | undefined): void;
11
11
  stopScanning(callback?: (() => void) | undefined): void;
12
+ pauseLogging(): void;
13
+ resumeLogging(): void;
12
14
  on(eventName: string | symbol, listener: (...args: any[]) => void): this;
13
15
  }
14
16
  declare const Binding: any;
@@ -23,6 +23,10 @@ class BleLinuxBinding extends events_1.default {
23
23
  stopScanning(callback) {
24
24
  throw new Error('Method not implemented.');
25
25
  }
26
+ pauseLogging() {
27
+ }
28
+ resumeLogging() {
29
+ }
26
30
  on(eventName, listener) {
27
31
  super.addListener(eventName, listener);
28
32
  if (eventName === 'stateChange') {
@@ -29,6 +29,10 @@ class Binding extends events_1.default {
29
29
  return Binding._instance;
30
30
  }
31
31
  init() { }
32
+ pauseLogging() {
33
+ }
34
+ resumeLogging() {
35
+ }
32
36
  addMock(peripheral) {
33
37
  this.peripherals.push(peripheral);
34
38
  }
@@ -43,6 +43,7 @@ export default class BleInterface extends EventEmitter implements IncyclistInter
43
43
  comms: BleComms;
44
44
  cb: (data: any) => void;
45
45
  }[];
46
+ loggingPaused: boolean;
46
47
  static _instance: BleInterface;
47
48
  static getInstance(props?: {
48
49
  binding?: BleBinding;
@@ -57,6 +58,9 @@ export default class BleInterface extends EventEmitter implements IncyclistInter
57
58
  stopConnectSensor(): void;
58
59
  waitForSensorConnectionFinish(): Promise<void>;
59
60
  getAdapterFactory(): BleAdapterFactory;
61
+ pauseLogging(): void;
62
+ resumeLogging(): void;
63
+ isDebugEnabled(): boolean;
60
64
  logEvent(event: any): void;
61
65
  onStateChange(state: BleInterfaceState): void;
62
66
  onError(err: any): void;
@@ -51,6 +51,7 @@ class BleInterface extends events_1.default {
51
51
  this.logger = props.logger;
52
52
  else
53
53
  this.logger = new gd_eventlog_1.EventLogger('BLE');
54
+ this.loggingPaused = false;
54
55
  }
55
56
  getBinding() { return this.binding; }
56
57
  setBinding(binding) { if (binding)
@@ -75,12 +76,38 @@ class BleInterface extends events_1.default {
75
76
  getAdapterFactory() {
76
77
  return adapter_factory_1.default.getInstance();
77
78
  }
78
- logEvent(event) {
79
- if (this.logger) {
80
- this.logger.logEvent(event);
79
+ pauseLogging() {
80
+ this.logEvent({ message: 'pause logging on BLE Interface' });
81
+ this.loggingPaused = true;
82
+ try {
83
+ this.getBinding().pauseLogging();
81
84
  }
85
+ catch (_a) { }
86
+ }
87
+ resumeLogging() {
88
+ const event = { message: 'resume logging on BLE Interface' };
89
+ this.logger.logEvent(event);
90
+ if (this.isDebugEnabled()) {
91
+ console.log('~~~ BLE', event);
92
+ }
93
+ this.loggingPaused = false;
94
+ try {
95
+ this.getBinding().resumeLogging();
96
+ }
97
+ catch (_a) { }
98
+ }
99
+ isDebugEnabled() {
82
100
  const w = global.window;
83
101
  if ((w === null || w === void 0 ? void 0 : w.DEVICE_DEBUG) || process.env.BLE_DEBUG) {
102
+ return true;
103
+ }
104
+ return false;
105
+ }
106
+ logEvent(event) {
107
+ if (this.logger && !this.loggingPaused) {
108
+ this.logger.logEvent(event);
109
+ }
110
+ if (this.isDebugEnabled()) {
84
111
  console.log('~~~ BLE', event);
85
112
  }
86
113
  }
@@ -98,6 +125,7 @@ class BleInterface extends events_1.default {
98
125
  this.logEvent({ message: 'error', error: err.message, stack: err.stack });
99
126
  }
100
127
  connect(to) {
128
+ this.resumeLogging();
101
129
  const timeout = this.props.timeout || to || 2000;
102
130
  return new Promise((resolve, reject) => {
103
131
  if (this.connectState.isConnected) {
@@ -186,6 +214,7 @@ class BleInterface extends events_1.default {
186
214
  this.connectState.timeout = null;
187
215
  }
188
216
  this.logEvent({ message: 'disconnect result: success' });
217
+ this.pauseLogging();
189
218
  return true;
190
219
  });
191
220
  }
@@ -8,6 +8,8 @@ export type BleInterfaceState = 'unknown' | 'resetting' | 'unsupported' | 'unaut
8
8
  export interface BleBinding extends EventEmitter {
9
9
  startScanning(serviceUUIDs?: string[], allowDuplicates?: boolean, callback?: (error?: Error) => void): void;
10
10
  stopScanning(callback?: () => void): void;
11
+ pauseLogging(): any;
12
+ resumeLogging(): any;
11
13
  _bindings: any;
12
14
  state: BleInterfaceState;
13
15
  on(eventName: string | symbol, listener: (...args: any[]) => void): this;
@@ -36,7 +36,7 @@ class Simulator extends adpater_1.default {
36
36
  this.setCyclingMode(name, modeSettings);
37
37
  this.capabilities = [
38
38
  types_1.IncyclistCapability.Power, types_1.IncyclistCapability.Speed, types_1.IncyclistCapability.Cadence, types_1.IncyclistCapability.Gear,
39
- types_1.IncyclistCapability.Control
39
+ types_1.IncyclistCapability.Control, types_1.IncyclistCapability.HeartRate
40
40
  ];
41
41
  }
42
42
  isEqual(settings) {
@@ -53,12 +53,13 @@ class Simulator extends adpater_1.default {
53
53
  start(props) {
54
54
  return __awaiter(this, void 0, void 0, function* () {
55
55
  this.startProps = props;
56
+ this.stopped = false;
56
57
  this.paused = false;
57
58
  if (props)
58
59
  this.setBikeProps(props);
59
60
  return new Promise((resolve) => {
60
61
  if (!this.isBot)
61
- this.logger.logEvent({ message: 'start', iv: this.iv });
62
+ this.logEvent({ message: 'starting device', device: this.getName(), props });
62
63
  if (this.started) {
63
64
  return resolve(true);
64
65
  }
@@ -94,32 +95,31 @@ class Simulator extends adpater_1.default {
94
95
  });
95
96
  }
96
97
  pause() {
97
- return new Promise((resolve, reject) => {
98
- if (!this.started)
99
- return reject(new Error('illegal state - pause() has been called before start()'));
100
- if (!this.isBot)
101
- this.logger.logEvent({ message: 'pause', iv: this.iv });
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ if (!this.isBot && this.isStarted())
100
+ this.logEvent({ message: 'pausing device', device: this.getName() });
102
101
  this.paused = true;
103
- resolve(true);
102
+ return true;
104
103
  });
105
104
  }
106
105
  resume() {
107
- return new Promise((resolve, reject) => {
108
- if (!this.started)
109
- reject(new Error('illegal state - resume() has been called before start()'));
110
- if (!this.isBot)
111
- this.logger.logEvent({ message: 'resume', iv: this.iv });
106
+ return __awaiter(this, void 0, void 0, function* () {
107
+ if (!this.isBot && this.isStarted())
108
+ this.logger.logEvent({ message: 'resuming device', device: this.getName() });
112
109
  this.paused = false;
113
- resolve(true);
110
+ return true;
114
111
  });
115
112
  }
116
113
  toggle() {
117
- if (this.started) {
118
- return this.stop();
119
- }
120
- else {
121
- return this.start().then(() => { return true; });
122
- }
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ if (this.started) {
116
+ return yield this.stop();
117
+ }
118
+ else {
119
+ yield this.start();
120
+ return true;
121
+ }
122
+ });
123
123
  }
124
124
  faster() {
125
125
  if (this.speed < 15)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "2.1.3",
3
+ "version": "2.1.5",
4
4
  "dependencies": {
5
5
  "@serialport/bindings-interface": "^1.2.2",
6
6
  "@serialport/parser-byte-length": "^9.0.1",