incyclist-devices 2.0.1 → 2.0.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.
@@ -70,7 +70,7 @@ class AntAdapter extends adpater_1.default {
70
70
  const as = this.settings;
71
71
  if (as.interface !== settings.interface)
72
72
  return false;
73
- if (as.deviceID !== settings.deviceID || as.profile !== settings.profile)
73
+ if (Number(as.deviceID) !== Number(settings.deviceID) || as.profile !== settings.profile)
74
74
  return false;
75
75
  return true;
76
76
  }
@@ -235,14 +235,19 @@ class AntAdapter extends adpater_1.default {
235
235
  }
236
236
  stop() {
237
237
  return __awaiter(this, void 0, void 0, function* () {
238
- this.stopDataTimeoutCheck();
239
- let stopped = yield this.ant.stopSensor(this.sensor);
240
- if (!stopped)
241
- return false;
238
+ let stopped;
239
+ try {
240
+ this.stopDataTimeoutCheck();
241
+ stopped = yield this.ant.stopSensor(this.sensor);
242
+ }
243
+ catch (err) {
244
+ this.logEvent({ message: 'stop sensor failed', reason: err.message });
245
+ }
242
246
  this.started = false;
243
247
  this.stopped = true;
248
+ this.paused = false;
244
249
  this.removeAllListeners();
245
- return true;
250
+ return stopped;
246
251
  });
247
252
  }
248
253
  }
@@ -203,7 +203,7 @@ class AntInterface extends events_1.default {
203
203
  return stopped;
204
204
  }
205
205
  catch (err) {
206
- console.log('ERROR', err);
206
+ this.logEvent({ message: 'error', fn: 'stopScan()', error: err.message, stack: err.stack });
207
207
  return false;
208
208
  }
209
209
  });
@@ -241,8 +241,9 @@ class AntInterface extends events_1.default {
241
241
  }
242
242
  stopSensor(sensor) {
243
243
  return __awaiter(this, void 0, void 0, function* () {
244
- if (!this.isConnected() || !this.device)
244
+ if (!this.isConnected() || !this.device) {
245
245
  return true;
246
+ }
246
247
  const channel = sensor.getChannel();
247
248
  if (channel) {
248
249
  try {
@@ -257,7 +258,8 @@ class AntInterface extends events_1.default {
257
258
  }
258
259
  channel.flush();
259
260
  channel.removeAllListeners('data');
260
- return yield channel.stopSensor(sensor);
261
+ const stopped = yield channel.stopSensor(sensor);
262
+ return stopped;
261
263
  }
262
264
  catch (err) {
263
265
  this.logEvent({ message: 'could not stop sensor', error: err.message || err, stack: err.stack });
@@ -180,7 +180,8 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
180
180
  start(props) {
181
181
  return __awaiter(this, void 0, void 0, function* () {
182
182
  const wasPaused = this.paused;
183
- this.paused = false;
183
+ if (wasPaused)
184
+ this.resume();
184
185
  if (this.started && !wasPaused) {
185
186
  return true;
186
187
  }
@@ -214,10 +215,10 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
214
215
  while (!success && retry < MAX_RETRIES) {
215
216
  retry++;
216
217
  if (!this.started) {
217
- this.logger.logEvent({ message: 'start sensor', props });
218
+ this.logEvent({ message: 'start sensor', props });
218
219
  this.started = yield this.ant.startSensor(this.sensor, this.onDeviceData.bind(this));
219
220
  if (this.started) {
220
- this.logger.logEvent({ message: 'sensor started', props });
221
+ this.logEvent({ message: 'sensor started', props });
221
222
  startSuccess++;
222
223
  }
223
224
  if (this.started && startSuccess === 1) {
@@ -258,8 +259,9 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
258
259
  else if (startRequest.slope !== undefined && startRequest.slope !== null) {
259
260
  status.slopeSent = yield fe.sendTrackResistance(startRequest.slope);
260
261
  }
261
- else
262
+ else {
262
263
  status.slopeSent = true;
264
+ }
263
265
  }
264
266
  else {
265
267
  status.slopeSent = yield fe.sendTrackResistance(0.0);
@@ -284,17 +286,19 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
284
286
  yield (0, utils_2.sleep)(500);
285
287
  }
286
288
  if (success) {
287
- this.logger.logEvent({ message: 'start success' });
289
+ this.logEvent({ message: 'start success' });
288
290
  stopTimeoutCheck();
289
291
  resolve(true);
290
292
  }
291
293
  else {
292
- this.logger.logEvent({ message: 'start failed' });
294
+ this.logEvent({ message: 'start failed' });
293
295
  stopTimeoutCheck();
294
- if (this.started)
296
+ if (this.started) {
295
297
  reject(new Error('could not start device, reason: could not send FE commands'));
298
+ }
296
299
  else
297
300
  reject(new Error('could not start device, reason: could not connect'));
301
+ this.started = false;
298
302
  }
299
303
  }));
300
304
  });
@@ -325,7 +329,6 @@ class AntFEAdapter extends adapter_1.ControllableAntAdapter {
325
329
  setCyclingMode(mode, settings) {
326
330
  const modeChange = this.cyclingMode.getName() !== mode;
327
331
  super.setCyclingMode(mode, settings);
328
- console.log('~~~ setCyclingMode', mode, modeChange, this.started, this.stopped, this.paused, this.isReconnecting, this.data);
329
332
  if (modeChange && this.started && !this.stopped) {
330
333
  if (this.getCyclingMode() instanceof ant_fe_erg_mode_1.default) {
331
334
  const power = this.data.power;
@@ -41,6 +41,10 @@ export default class BleInterface extends EventEmitter implements IncyclistInter
41
41
  binding: BleBinding;
42
42
  connectedDevices: BleAdapter[];
43
43
  sensorIsConnecting: boolean;
44
+ emittingAdapters: {
45
+ comms: BleComms;
46
+ cb: (data: any) => void;
47
+ }[];
44
48
  static _instance: BleInterface;
45
49
  static getInstance(props?: {
46
50
  binding?: BleBinding;
@@ -42,6 +42,7 @@ class BleInterface extends events_1.default {
42
42
  super();
43
43
  this.scanState = { isScanning: false, isConnecting: false, timeout: undefined, isBackgroundScan: false };
44
44
  this.connectState = { isConnecting: false, isConnected: false, isInitSuccess: false };
45
+ this.emittingAdapters = [];
45
46
  this.props = props;
46
47
  if (props.binding)
47
48
  this.setBinding(props.binding);
@@ -309,7 +310,7 @@ class BleInterface extends events_1.default {
309
310
  return callback(null);
310
311
  const { id, name, address } = (0, utils_1.getPeripheralInfo)(peripheral);
311
312
  const settings = { protocol: DeviceClass.protocol, interface: 'ble', id, name: peripheral.name || name, address: peripheral.address || address };
312
- callback(settings, characteristics);
313
+ callback(settings, characteristics, peripheral);
313
314
  }
314
315
  });
315
316
  }
@@ -448,6 +449,11 @@ class BleInterface extends events_1.default {
448
449
  this.scanState.isScanning = false;
449
450
  resolve(detected);
450
451
  });
452
+ this.emittingAdapters.forEach(a => {
453
+ a.comms.off('data', a.cb);
454
+ a.comms.unsubscribeAll();
455
+ });
456
+ this.emittingAdapters = [];
451
457
  };
452
458
  this.logEvent({ message: `${opStr}: start scanning`, requested, timeout });
453
459
  this.scanState.timeout = setTimeout(onTimeout, adjustedScanTimeout);
@@ -458,14 +464,22 @@ class BleInterface extends events_1.default {
458
464
  return reject(err);
459
465
  }
460
466
  ble.on('discover', (p) => {
461
- this.onPeripheralFound(p, (deviceSettings, characteristics) => {
467
+ this.onPeripheralFound(p, (deviceSettings, characteristics, peripheral) => __awaiter(this, void 0, void 0, function* () {
462
468
  if (deviceSettings) {
463
469
  detected.push(deviceSettings);
464
470
  const device = this.getAdapterFactory().createInstance(deviceSettings);
465
471
  device.getComms().characteristics = characteristics;
472
+ device.getComms().peripheral = peripheral;
473
+ try {
474
+ yield device.getComms().subscribeAll();
475
+ const cb = (deviceData) => { this.emit('data', deviceSettings, deviceData); };
476
+ device.getComms().on('data', cb);
477
+ this.emittingAdapters.push({ comms: device.getComms(), cb });
478
+ }
479
+ catch (_a) { }
466
480
  this.emit('device', deviceSettings);
467
481
  }
468
- }, { protocolFilter });
482
+ }), { protocolFilter });
469
483
  });
470
484
  const cachedItems = this.peripheralCache.filter(protocolFilter ? services : []);
471
485
  if (cachedItems && cachedItems.length > 0) {
@@ -491,6 +505,11 @@ class BleInterface extends events_1.default {
491
505
  ble.removeAllListeners('discover');
492
506
  this.peripheralCache.handleStopScan();
493
507
  ble.stopScanning();
508
+ this.emittingAdapters.forEach(a => {
509
+ a.comms.unsubscribeAll();
510
+ a.comms.off('data', a.cb);
511
+ });
512
+ this.emittingAdapters = [];
494
513
  this.scanState.isScanning = false;
495
514
  this.logEvent({ message: 'scan stop result: success' });
496
515
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "dependencies": {
5
5
  "@serialport/bindings-interface": "^1.2.2",
6
6
  "@serialport/parser-byte-length": "^9.0.1",