incyclist-devices 2.2.7 → 2.2.9

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.
@@ -4,6 +4,7 @@ import IncyclistDevice from '../../base/adpater';
4
4
  import { AntDeviceProperties, AntDeviceSettings, LegacyProfile, BaseDeviceData, AdapterStartStatus } from '../types';
5
5
  import { IAdapter, IncyclistAdapterData, IncyclistBikeData, IncyclistCapability } from '../../types';
6
6
  import EventEmitter from 'events';
7
+ import ICyclingMode from '../../modes/types';
7
8
  export default class AntAdapter<TDeviceData extends BaseDeviceData> extends IncyclistDevice<AntDeviceProperties> {
8
9
  sensor: ISensor;
9
10
  data: IncyclistAdapterData;
@@ -42,6 +43,7 @@ export default class AntAdapter<TDeviceData extends BaseDeviceData> extends Incy
42
43
  mapData(deviceData: TDeviceData): IncyclistBikeData;
43
44
  transformData(data: IncyclistBikeData, deviceData: TDeviceData): void;
44
45
  mapToAdapterData(deviceData: any): void;
46
+ refreshDeviceData(): void;
45
47
  onDeviceData(deviceData: TDeviceData): void;
46
48
  isWaitingForData(): boolean;
47
49
  _wait(): Promise<boolean>;
@@ -68,4 +70,5 @@ export default class AntAdapter<TDeviceData extends BaseDeviceData> extends Incy
68
70
  stop(): Promise<boolean>;
69
71
  startSensor(): Promise<boolean>;
70
72
  stopSensor(): Promise<void>;
73
+ setCyclingMode(mode: string | ICyclingMode, settings?: any, sendInitCommands?: boolean): void;
71
74
  }
@@ -119,6 +119,21 @@ class AntAdapter extends adpater_1.default {
119
119
  mapToAdapterData(deviceData) {
120
120
  throw new Error('message not implemented');
121
121
  }
122
+ refreshDeviceData() {
123
+ if (this.isStopped() || this.isPaused())
124
+ return;
125
+ const logData = this.getLogData(this.deviceData, ['PairedDevices', 'RawData', '_RawData']);
126
+ this.logEvent({ message: 'refreshDeviceData', data: logData, paused: this.paused, started: this.started, canEmit: this.canEmitData() });
127
+ if (this.isControllable()) {
128
+ let incyclistData = this.mapData(this.deviceData);
129
+ incyclistData = this.getCyclingMode().updateData(incyclistData);
130
+ this.transformData(incyclistData, this.deviceData);
131
+ }
132
+ else {
133
+ this.mapToAdapterData(this.deviceData);
134
+ }
135
+ this.emitData(this.data);
136
+ }
122
137
  onDeviceData(deviceData) {
123
138
  this.dataMsgCount++;
124
139
  this.lastDataTS = Date.now();
@@ -423,8 +438,10 @@ class AntAdapter extends adpater_1.default {
423
438
  stop() {
424
439
  return __awaiter(this, void 0, void 0, function* () {
425
440
  let stopped;
426
- this.logger.logEvent({ message: 'stopping device', device: this.getName() });
427
441
  this.internalEmitter.emit('stop');
442
+ if (this.stopped)
443
+ return;
444
+ this.logger.logEvent({ message: 'stopping device', device: this.getName() });
428
445
  this.promiseWaitForData = null;
429
446
  if (this.startStatus) {
430
447
  this.startStatus.interrupted = true;
@@ -461,5 +478,9 @@ class AntAdapter extends adpater_1.default {
461
478
  catch (_a) { }
462
479
  });
463
480
  }
481
+ setCyclingMode(mode, settings, sendInitCommands) {
482
+ super.setCyclingMode(mode, settings, sendInitCommands);
483
+ this.refreshDeviceData();
484
+ }
464
485
  }
465
486
  exports.default = AntAdapter;
@@ -45,6 +45,8 @@ export default class AntInterface extends EventEmitter implements IncyclistInter
45
45
  scannerWaitForConnection(): Promise<void>;
46
46
  scan(props?: AntScanProps): Promise<AntDeviceSettings[]>;
47
47
  isScanning(): boolean;
48
+ protected stopAllSensors(sensors: Array<ISensor>): Promise<void>;
49
+ protected stopDevices(detected: AntDeviceSettings[]): Promise<void>;
48
50
  stopScan(): Promise<boolean>;
49
51
  startSensor(sensor: ISensor, onDeviceData: (data: any) => void): Promise<boolean>;
50
52
  private blockChannel;
@@ -16,6 +16,7 @@ const events_1 = __importDefault(require("events"));
16
16
  const gd_eventlog_1 = require("gd-eventlog");
17
17
  const sensor_factory_1 = __importDefault(require("../factories/sensor-factory"));
18
18
  const utils_1 = require("../../utils/utils");
19
+ const incyclist_devices_1 = require("incyclist-devices");
19
20
  class AntInterface extends events_1.default {
20
21
  static getInstance(props = {}) {
21
22
  if (AntInterface._instance === undefined)
@@ -237,6 +238,8 @@ class AntInterface extends events_1.default {
237
238
  this.activeScan.emitter.on('stop', () => __awaiter(this, void 0, void 0, function* () {
238
239
  this.activeScan.emitter.removeAllListeners();
239
240
  this.emit('stop-scan');
241
+ yield this.stopDevices(detected);
242
+ yield this.stopAllSensors(sensors);
240
243
  const stopped = yield this.activeScan.channel.stopScanner();
241
244
  this.logEvent({ message: 'scan stopped' });
242
245
  removeListeners(channel);
@@ -276,6 +279,38 @@ class AntInterface extends events_1.default {
276
279
  isScanning() {
277
280
  return this.scanPromise !== undefined && this.scanPromise !== null;
278
281
  }
282
+ stopAllSensors(sensors) {
283
+ return __awaiter(this, void 0, void 0, function* () {
284
+ this.logger.logEvent({ message: 'stopping all sensors ' });
285
+ let promises = [];
286
+ sensors.forEach((sensor) => {
287
+ promises.push(this.stopSensor(sensor).catch(err => {
288
+ var _a;
289
+ this.logger.logEvent({ message: 'could not stop sensor', error: err.message, channel: (_a = sensor.getChannel()) === null || _a === void 0 ? void 0 : _a.getChannelNo(), stack: err.stack });
290
+ }));
291
+ });
292
+ if (promises.length > 0) {
293
+ yield Promise.allSettled(promises);
294
+ }
295
+ this.logger.logEvent({ message: 'sensors stopped' });
296
+ });
297
+ }
298
+ stopDevices(detected) {
299
+ return __awaiter(this, void 0, void 0, function* () {
300
+ this.logger.logEvent({ message: 'stopping devices' });
301
+ let promises = [];
302
+ detected.forEach((settings) => {
303
+ const adapter = incyclist_devices_1.AdapterFactory.create(settings);
304
+ promises.push(adapter.stop().catch(err => {
305
+ this.logger.logEvent({ message: 'could not stop device', error: err.message, deviceID: settings.deviceID, stack: err.stack });
306
+ }));
307
+ });
308
+ if (promises.length > 0) {
309
+ yield Promise.allSettled(promises);
310
+ }
311
+ this.logger.logEvent({ message: 'devices stopped' });
312
+ });
313
+ }
279
314
  stopScan() {
280
315
  return __awaiter(this, void 0, void 0, function* () {
281
316
  this.logEvent({ message: 'stopping scan ..' });
@@ -348,7 +383,7 @@ class AntInterface extends events_1.default {
348
383
  return true;
349
384
  }
350
385
  const channel = sensor.getChannel();
351
- if (channel) {
386
+ if (channel !== undefined) {
352
387
  try {
353
388
  if (!channel.flush) {
354
389
  this.logEvent({ message: 'old version of ant-channel detected' });
@@ -371,8 +406,7 @@ class AntInterface extends events_1.default {
371
406
  }
372
407
  }
373
408
  else {
374
- this.logEvent({ message: 'could not stop sensor', deviceID: sensor.getDeviceID(), error: 'no channel attached' });
375
- return false;
409
+ return true;
376
410
  }
377
411
  });
378
412
  }
@@ -43,7 +43,14 @@ class AntFEAdapter extends adapter_1.default {
43
43
  if (this.promiseStop)
44
44
  return;
45
45
  if (this.promiseSendUpdate) {
46
- this.logEvent({ message: 'send bike update skipped', device: this.getName(), request, reason: 'busy' });
46
+ let stack;
47
+ try {
48
+ throw new Error();
49
+ }
50
+ catch (err) {
51
+ stack = err.stack;
52
+ }
53
+ this.logEvent({ message: 'send bike update skipped', device: this.getName(), request, reason: 'busy', stack });
47
54
  return;
48
55
  }
49
56
  let isReset = request.reset && Object.keys(request).length === 1;
@@ -5,6 +5,7 @@ import { BleDeviceProperties, BleDeviceSettings, BleStartProperties } from "../t
5
5
  import { IAdapter, IncyclistBikeData, IncyclistAdapterData, DeviceProperties } from "../../types";
6
6
  import { BleDeviceData } from "./types";
7
7
  import { LegacyProfile } from "../../antv2/types";
8
+ import ICyclingMode from "../../modes/types";
8
9
  export default class BleAdapter<TDeviceData extends BleDeviceData, TDevice extends BleComms> extends IncyclistDevice<BleDeviceProperties> {
9
10
  protected ble: BleInterface;
10
11
  protected deviceData: TDeviceData;
@@ -27,6 +28,7 @@ export default class BleAdapter<TDeviceData extends BleDeviceData, TDevice exten
27
28
  getProtocolName(): string;
28
29
  getID(): string;
29
30
  getName(): string;
31
+ refreshDeviceData(): void;
30
32
  onDeviceData(deviceData: TDeviceData): void;
31
33
  mapData(deviceData: TDeviceData): IncyclistAdapterData | IncyclistBikeData;
32
34
  transformData(data: IncyclistBikeData): IncyclistAdapterData;
@@ -39,4 +41,5 @@ export default class BleAdapter<TDeviceData extends BleDeviceData, TDevice exten
39
41
  pause(): Promise<boolean>;
40
42
  resume(): Promise<boolean>;
41
43
  update(): void;
44
+ setCyclingMode(mode: string | ICyclingMode, settings?: any, sendInitCommands?: boolean): void;
42
45
  }
@@ -108,6 +108,25 @@ class BleAdapter extends adpater_1.default {
108
108
  const settings = this.settings;
109
109
  return settings.name || settings.id || settings.address;
110
110
  }
111
+ refreshDeviceData() {
112
+ if (this.isStopped() || this.isPaused())
113
+ return;
114
+ try {
115
+ this.logEvent({ message: 'refreshDeviceData', data: this.deviceData, isControllable: this.isControllable() });
116
+ if (this.isControllable()) {
117
+ const mappedData = this.mapData(this.deviceData);
118
+ const incyclistData = this.getCyclingMode().updateData(mappedData);
119
+ this.data = this.transformData(incyclistData);
120
+ }
121
+ else {
122
+ this.data = this.mapData(this.deviceData);
123
+ }
124
+ this.emitData(this.data);
125
+ }
126
+ catch (err) {
127
+ this.logEvent({ message: 'error', fn: 'refreshDeviceData', error: err.message, stack: err.stack });
128
+ }
129
+ }
111
130
  onDeviceData(deviceData) {
112
131
  try {
113
132
  this.dataMsgCount++;
@@ -231,5 +250,9 @@ class BleAdapter extends adpater_1.default {
231
250
  }
232
251
  update() {
233
252
  }
253
+ setCyclingMode(mode, settings, sendInitCommands) {
254
+ super.setCyclingMode(mode, settings, sendInitCommands);
255
+ this.refreshDeviceData();
256
+ }
234
257
  }
235
258
  exports.default = BleAdapter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "2.2.7",
3
+ "version": "2.2.9",
4
4
  "dependencies": {
5
5
  "@serialport/bindings-interface": "^1.2.2",
6
6
  "@serialport/parser-byte-length": "^9.0.1",