incyclist-devices 3.0.7 → 3.0.8

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.
@@ -15,6 +15,7 @@ class BleAdapter extends adpater_js_1.default {
15
15
  device;
16
16
  onDeviceDataHandler = this.onDeviceData.bind(this);
17
17
  onDeviceDisconnectHandler = this.emit.bind(this);
18
+ onDisconnectDoneHandler = this.onDisconnectDone.bind(this);
18
19
  startTask;
19
20
  constructor(settings, props) {
20
21
  super(settings, props);
@@ -198,7 +199,7 @@ class BleAdapter extends adpater_js_1.default {
198
199
  await this.stop();
199
200
  }
200
201
  const ble = this.getBle();
201
- ble.once('disconnect-done', this.onDisconnectDone.bind(this));
202
+ ble.once('disconnect-done', this.onDisconnectDoneHandler);
202
203
  this.startTask = new task_js_1.InteruptableTask(this.startAdapter(startProps), {
203
204
  timeout: startProps?.timeout,
204
205
  name: 'start',
@@ -206,6 +207,9 @@ class BleAdapter extends adpater_js_1.default {
206
207
  log: this.logEvent.bind(this)
207
208
  });
208
209
  const res = await this.startTask.run();
210
+ if (!res) {
211
+ ble.removeListener('disconnect-done', this.onDisconnectDoneHandler);
212
+ }
209
213
  return res;
210
214
  }
211
215
  isStarting() {
@@ -303,6 +307,8 @@ class BleAdapter extends adpater_js_1.default {
303
307
  this.logEvent({ message: 'start result: error', error: err.message, device: this.getName(), interface: this.getInterface(), protocol: this.getProtocolName() });
304
308
  this.started = false;
305
309
  this.stopped = true;
310
+ const ble = this.getBle();
311
+ ble.removeListener('disconnect-done', this.onDisconnectDoneHandler);
306
312
  return false;
307
313
  }
308
314
  }
@@ -380,6 +386,8 @@ class BleAdapter extends adpater_js_1.default {
380
386
  sensor.off('data', this.onDeviceDataHandler);
381
387
  sensor.off('disconnected', this.onDeviceDisconnectHandler);
382
388
  sensor.off('error', console.log);
389
+ const ble = this.getBle();
390
+ ble.removeListener('disconnect-done', this.onDisconnectDoneHandler);
383
391
  sensor.reset();
384
392
  this.resetData();
385
393
  this.stopped = true;
@@ -36,6 +36,7 @@ class BleInterface extends node_events_1.EventEmitter {
36
36
  emitted = [];
37
37
  confirmedBleState;
38
38
  currentBleState;
39
+ isAutoStart = false;
39
40
  stateChangeEventHandler = null;
40
41
  static getInstance(props = {}) {
41
42
  if (BleInterface._instance === undefined)
@@ -93,9 +94,16 @@ class BleInterface extends node_events_1.EventEmitter {
93
94
  return this.binding;
94
95
  }
95
96
  async autoConnect() {
96
- await this.connect();
97
+ this.isAutoStart = true;
98
+ await this.connectInternal();
97
99
  }
98
100
  async connect(reconnect) {
101
+ if (this.isAutoStart)
102
+ return this.isConnected();
103
+ else
104
+ return this.connectInternal(reconnect);
105
+ }
106
+ async connectInternal(reconnect) {
99
107
  if (this.isConnected())
100
108
  return true;
101
109
  if (!this.stateChangeEventHandler) {
@@ -161,6 +169,7 @@ class BleInterface extends node_events_1.EventEmitter {
161
169
  this.getBinding()?.on('error', this.onError.bind(this));
162
170
  this.logEvent({ message: 'interface disconnected', interface: 'BLE' });
163
171
  this.confirmedBleState = 'poweredOff';
172
+ this.emit('disconnect');
164
173
  return true;
165
174
  };
166
175
  if (this.isDisconnecting()) {
@@ -301,7 +310,7 @@ class BleInterface extends node_events_1.EventEmitter {
301
310
  }
302
311
  this.logEvent({ message: 'reconnecting interface', stack: error.stack });
303
312
  await this.disconnect();
304
- await this.connect(true);
313
+ await this.connectInternal(true);
305
314
  }
306
315
  async startPeripheralScan(retry = false) {
307
316
  if (!this.isConnected() || this.isDiscovering()) {
@@ -580,8 +589,9 @@ class BleInterface extends node_events_1.EventEmitter {
580
589
  addKnownDevice(_settings) {
581
590
  }
582
591
  async connectBle() {
583
- this.currentBleState = this.getBinding()?.state;
584
- if (this.currentBleState === 'poweredOn') {
592
+ const prev = this.currentBleState;
593
+ this.currentBleState = this.currentBleState ?? this.getBinding()?.state;
594
+ if (this.currentBleState === 'poweredOn' && prev !== 'poweredOn') {
585
595
  this.onConnected();
586
596
  return true;
587
597
  }
@@ -599,13 +609,16 @@ class BleInterface extends node_events_1.EventEmitter {
599
609
  });
600
610
  this.on('ble-state-change', (state) => {
601
611
  try {
612
+ this.currentBleState = state;
602
613
  if (state === 'poweredOn') {
603
614
  this.onConnected();
604
615
  this.removeAllListeners('ble-state-change');
616
+ this.currentBleState = state;
605
617
  return done(true);
606
618
  }
607
619
  if (state === 'unauthorized') {
608
620
  this.removeAllListeners('ble-state-change');
621
+ this.currentBleState = state;
609
622
  return done(false);
610
623
  }
611
624
  }
@@ -656,7 +669,7 @@ class BleInterface extends node_events_1.EventEmitter {
656
669
  if (this.isDisconnecting()) {
657
670
  await this.disconnectTask.getPromise();
658
671
  }
659
- this.connect();
672
+ this.connectInternal();
660
673
  }
661
674
  }
662
675
  getAdapterFactory() {
@@ -147,7 +147,10 @@ class DirectConnectInterface extends node_events_1.EventEmitter {
147
147
  this.getBinding()?.mdns?.disconnect();
148
148
  this.internalEvents.removeAllListeners();
149
149
  this.connected = false;
150
- return !this.isConnected();
150
+ const disconnected = !this.isConnected();
151
+ if (disconnected)
152
+ this.emit('disconnect');
153
+ return disconnected;
151
154
  }
152
155
  isConnected() {
153
156
  return this.connected && this.getBinding()?.mdns !== undefined && this.binding.mdns !== null;
@@ -414,7 +414,7 @@ class SmartTrainerCyclingMode extends power_base_js_1.default {
414
414
  this.tsStart = Date.now();
415
415
  }
416
416
  if (this.gear === undefined && this.tsStart && data.power > 0 && (Date.now() - this.tsStart > 3000)) {
417
- this.gear = Number(this.getSetting('startGear')) ?? 0;
417
+ this.gear = Number(this.getSetting('startGear') ?? 0);
418
418
  data.gearStr = this.getGearString();
419
419
  }
420
420
  return data;
@@ -10,6 +10,7 @@ export default class BleAdapter extends IncyclistDevice {
10
10
  device;
11
11
  onDeviceDataHandler = this.onDeviceData.bind(this);
12
12
  onDeviceDisconnectHandler = this.emit.bind(this);
13
+ onDisconnectDoneHandler = this.onDisconnectDone.bind(this);
13
14
  startTask;
14
15
  constructor(settings, props) {
15
16
  super(settings, props);
@@ -193,7 +194,7 @@ export default class BleAdapter extends IncyclistDevice {
193
194
  await this.stop();
194
195
  }
195
196
  const ble = this.getBle();
196
- ble.once('disconnect-done', this.onDisconnectDone.bind(this));
197
+ ble.once('disconnect-done', this.onDisconnectDoneHandler);
197
198
  this.startTask = new InteruptableTask(this.startAdapter(startProps), {
198
199
  timeout: startProps?.timeout,
199
200
  name: 'start',
@@ -201,6 +202,9 @@ export default class BleAdapter extends IncyclistDevice {
201
202
  log: this.logEvent.bind(this)
202
203
  });
203
204
  const res = await this.startTask.run();
205
+ if (!res) {
206
+ ble.removeListener('disconnect-done', this.onDisconnectDoneHandler);
207
+ }
204
208
  return res;
205
209
  }
206
210
  isStarting() {
@@ -298,6 +302,8 @@ export default class BleAdapter extends IncyclistDevice {
298
302
  this.logEvent({ message: 'start result: error', error: err.message, device: this.getName(), interface: this.getInterface(), protocol: this.getProtocolName() });
299
303
  this.started = false;
300
304
  this.stopped = true;
305
+ const ble = this.getBle();
306
+ ble.removeListener('disconnect-done', this.onDisconnectDoneHandler);
301
307
  return false;
302
308
  }
303
309
  }
@@ -375,6 +381,8 @@ export default class BleAdapter extends IncyclistDevice {
375
381
  sensor.off('data', this.onDeviceDataHandler);
376
382
  sensor.off('disconnected', this.onDeviceDisconnectHandler);
377
383
  sensor.off('error', console.log);
384
+ const ble = this.getBle();
385
+ ble.removeListener('disconnect-done', this.onDisconnectDoneHandler);
378
386
  sensor.reset();
379
387
  this.resetData();
380
388
  this.stopped = true;
@@ -33,6 +33,7 @@ export class BleInterface extends EventEmitter {
33
33
  emitted = [];
34
34
  confirmedBleState;
35
35
  currentBleState;
36
+ isAutoStart = false;
36
37
  stateChangeEventHandler = null;
37
38
  static getInstance(props = {}) {
38
39
  if (BleInterface._instance === undefined)
@@ -90,9 +91,16 @@ export class BleInterface extends EventEmitter {
90
91
  return this.binding;
91
92
  }
92
93
  async autoConnect() {
93
- await this.connect();
94
+ this.isAutoStart = true;
95
+ await this.connectInternal();
94
96
  }
95
97
  async connect(reconnect) {
98
+ if (this.isAutoStart)
99
+ return this.isConnected();
100
+ else
101
+ return this.connectInternal(reconnect);
102
+ }
103
+ async connectInternal(reconnect) {
96
104
  if (this.isConnected())
97
105
  return true;
98
106
  if (!this.stateChangeEventHandler) {
@@ -158,6 +166,7 @@ export class BleInterface extends EventEmitter {
158
166
  this.getBinding()?.on('error', this.onError.bind(this));
159
167
  this.logEvent({ message: 'interface disconnected', interface: 'BLE' });
160
168
  this.confirmedBleState = 'poweredOff';
169
+ this.emit('disconnect');
161
170
  return true;
162
171
  };
163
172
  if (this.isDisconnecting()) {
@@ -298,7 +307,7 @@ export class BleInterface extends EventEmitter {
298
307
  }
299
308
  this.logEvent({ message: 'reconnecting interface', stack: error.stack });
300
309
  await this.disconnect();
301
- await this.connect(true);
310
+ await this.connectInternal(true);
302
311
  }
303
312
  async startPeripheralScan(retry = false) {
304
313
  if (!this.isConnected() || this.isDiscovering()) {
@@ -577,8 +586,9 @@ export class BleInterface extends EventEmitter {
577
586
  addKnownDevice(_settings) {
578
587
  }
579
588
  async connectBle() {
580
- this.currentBleState = this.getBinding()?.state;
581
- if (this.currentBleState === 'poweredOn') {
589
+ const prev = this.currentBleState;
590
+ this.currentBleState = this.currentBleState ?? this.getBinding()?.state;
591
+ if (this.currentBleState === 'poweredOn' && prev !== 'poweredOn') {
582
592
  this.onConnected();
583
593
  return true;
584
594
  }
@@ -596,13 +606,16 @@ export class BleInterface extends EventEmitter {
596
606
  });
597
607
  this.on('ble-state-change', (state) => {
598
608
  try {
609
+ this.currentBleState = state;
599
610
  if (state === 'poweredOn') {
600
611
  this.onConnected();
601
612
  this.removeAllListeners('ble-state-change');
613
+ this.currentBleState = state;
602
614
  return done(true);
603
615
  }
604
616
  if (state === 'unauthorized') {
605
617
  this.removeAllListeners('ble-state-change');
618
+ this.currentBleState = state;
606
619
  return done(false);
607
620
  }
608
621
  }
@@ -653,7 +666,7 @@ export class BleInterface extends EventEmitter {
653
666
  if (this.isDisconnecting()) {
654
667
  await this.disconnectTask.getPromise();
655
668
  }
656
- this.connect();
669
+ this.connectInternal();
657
670
  }
658
671
  }
659
672
  getAdapterFactory() {
@@ -144,7 +144,10 @@ export default class DirectConnectInterface extends EventEmitter {
144
144
  this.getBinding()?.mdns?.disconnect();
145
145
  this.internalEvents.removeAllListeners();
146
146
  this.connected = false;
147
- return !this.isConnected();
147
+ const disconnected = !this.isConnected();
148
+ if (disconnected)
149
+ this.emit('disconnect');
150
+ return disconnected;
148
151
  }
149
152
  isConnected() {
150
153
  return this.connected && this.getBinding()?.mdns !== undefined && this.binding.mdns !== null;
@@ -376,7 +376,7 @@ export default class SmartTrainerCyclingMode extends PowerBasedCyclingModeBase {
376
376
  this.tsStart = Date.now();
377
377
  }
378
378
  if (this.gear === undefined && this.tsStart && data.power > 0 && (Date.now() - this.tsStart > 3000)) {
379
- this.gear = Number(this.getSetting('startGear')) ?? 0;
379
+ this.gear = Number(this.getSetting('startGear') ?? 0);
380
380
  data.gearStr = this.getGearString();
381
381
  }
382
382
  return data;
@@ -13,6 +13,7 @@ export default class BleAdapter<TDeviceData extends BleDeviceData, TDevice exten
13
13
  protected device: TDevice;
14
14
  protected onDeviceDataHandler: any;
15
15
  protected onDeviceDisconnectHandler: any;
16
+ protected onDisconnectDoneHandler: any;
16
17
  protected startTask: InteruptableTask<TaskState, boolean>;
17
18
  constructor(settings: BleDeviceSettings, props?: DeviceProperties);
18
19
  getUniqueName(): string;
@@ -40,6 +40,7 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
40
40
  protected emitted: BlePeripheralAnnouncement[];
41
41
  protected confirmedBleState: BleInterfaceState;
42
42
  protected currentBleState: BleInterfaceState;
43
+ protected isAutoStart: boolean;
43
44
  protected stateChangeEventHandler: TStateChangeHandler | null;
44
45
  static getInstance(props?: InterfaceProps): BleInterface;
45
46
  protected constructor(props: InterfaceProps);
@@ -51,6 +52,7 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
51
52
  getBinding(): BleBinding;
52
53
  protected autoConnect(): Promise<void>;
53
54
  connect(reconnect?: boolean): Promise<boolean>;
55
+ connectInternal(reconnect?: boolean): Promise<boolean>;
54
56
  disconnect(connectionLost?: boolean): Promise<boolean>;
55
57
  isConnected(): boolean;
56
58
  registerConnected(peripheral: IBlePeripheral, id: string): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "3.0.7",
3
+ "version": "3.0.8",
4
4
  "scripts": {
5
5
  "lint": "eslint . --ext .ts",
6
6
  "build": "npm run build:esm && npm run build:cjs",