incyclist-devices 2.3.9 → 2.3.10

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.
@@ -27,6 +27,7 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
27
27
  protected expectedServices: string[];
28
28
  protected matching: string[];
29
29
  protected connectTask: InteruptableTask<TaskState, boolean>;
30
+ protected disconnectTask: InteruptableTask<TaskState, boolean>;
30
31
  protected scanTask: InteruptableTask<TaskState, void>;
31
32
  protected discoverTask: InteruptableTask<TaskState, void>;
32
33
  protected onDiscovered: (peripheral: BleRawPeripheral) => void;
@@ -35,8 +36,9 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
35
36
  id: string;
36
37
  peripheral: IBlePeripheral;
37
38
  }[];
38
- protected connectAttemptCnt: number;
39
39
  protected emitted: BlePeripheralAnnouncement[];
40
+ protected confirmedBleState: BleInterfaceState;
41
+ protected currentBleState: BleInterfaceState;
40
42
  static getInstance(props?: InterfaceProps): BleInterface;
41
43
  protected constructor(props: InterfaceProps);
42
44
  setProps(props: InterfaceProps): void;
@@ -52,6 +54,7 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
52
54
  registerConnected(peripheral: IBlePeripheral, id: string): void;
53
55
  unregisterConnected(id: string): void;
54
56
  protected isConnecting(): boolean;
57
+ protected isDisconnecting(): boolean;
55
58
  scan(props: BleScanProps): Promise<DeviceSettings[]>;
56
59
  stopScan(): Promise<boolean>;
57
60
  onScanDone(): DeviceSettings[];
@@ -94,9 +97,9 @@ export declare class BleInterface extends EventEmitter implements IBleInterface<
94
97
  protected connectBle(): Promise<boolean>;
95
98
  protected waitForBleConnected(): Promise<boolean>;
96
99
  protected onError(err: Error): void;
97
- protected onConnected(): void;
100
+ protected onConnected(): Promise<void>;
98
101
  protected onDisconnected(): Promise<void>;
99
- protected onBleStateChange(state: BleInterfaceState): void;
102
+ protected onBleStateChange(state: BleInterfaceState): Promise<void>;
100
103
  protected getAdapterFactory(): BleAdapterFactory<TBleSensor>;
101
104
  protected getConnectTimeout(): number;
102
105
  protected getExpectedServices(): string[];
@@ -50,7 +50,6 @@ class BleInterface extends events_1.default {
50
50
  this.expectedServices = ['180d', '1818', '1826', '6e40fec1'];
51
51
  this.matching = [];
52
52
  this.connectedPeripherals = [];
53
- this.connectAttemptCnt = 0;
54
53
  this.emitted = [];
55
54
  this.instanceId = ++instanceCount;
56
55
  this.props = props;
@@ -111,41 +110,59 @@ class BleInterface extends events_1.default {
111
110
  log: this.logEvent.bind(this),
112
111
  });
113
112
  const success = yield this.connectTask.run().catch(() => false);
114
- if (success) {
115
- this.startPeripheralScan();
116
- }
117
113
  return success;
118
114
  });
119
115
  }
120
116
  disconnect(connectionLost) {
121
117
  return __awaiter(this, void 0, void 0, function* () {
122
- var _a;
123
- if (!this.getBinding()) {
124
- return false;
125
- }
126
- if (!this.isConnected() && !connectionLost)
118
+ if (!this.isConnected())
127
119
  return true;
128
- if (!connectionLost)
129
- this.logEvent({ message: 'disconnect request' });
130
- this.emit('disconnect-request');
131
- yield this.stopPeripheralScan();
132
- if (connectionLost) {
133
- this.emitDisconnectAllPeripherals();
134
- }
135
- else {
136
- yield this.disconnectAllPeripherals();
120
+ const performDisconnect = () => __awaiter(this, void 0, void 0, function* () {
121
+ var _a;
122
+ if (!this.getBinding()) {
123
+ this.confirmedBleState = 'poweredOff';
124
+ return false;
125
+ }
126
+ this.getBinding().removeAllListeners('error');
127
+ if (!this.isConnected() && !connectionLost) {
128
+ this.confirmedBleState = 'poweredOff';
129
+ return true;
130
+ }
131
+ if (!connectionLost)
132
+ this.logEvent({ message: 'disconnect request' });
133
+ this.emit('disconnect-request');
134
+ yield this.stopPeripheralScan();
135
+ if (connectionLost) {
136
+ this.emitDisconnectAllPeripherals();
137
+ }
138
+ else {
139
+ yield this.disconnectAllPeripherals();
140
+ }
141
+ if (this.isConnecting())
142
+ yield ((_a = this.connectTask) === null || _a === void 0 ? void 0 : _a.stop());
143
+ this.emit('disconnect-done');
144
+ this.getBinding().removeAllListeners('stateChange');
145
+ this.getBinding().on('stateChange', this.onBleStateChange.bind(this));
146
+ this.getBinding().on('error', this.onError.bind(this));
147
+ this.logEvent({ message: 'interface disconnected', interface: 'BLE' });
148
+ this.confirmedBleState = 'poweredOff';
149
+ return true;
150
+ });
151
+ if (this.isDisconnecting()) {
152
+ return this.disconnectTask.getPromise();
137
153
  }
138
- if (this.isConnecting())
139
- yield ((_a = this.connectTask) === null || _a === void 0 ? void 0 : _a.stop());
140
- this.getBinding().removeAllListeners();
141
- this.connectAttemptCnt = 0;
142
- this.emit('disconnect-done');
143
- return true;
154
+ this.disconnectTask = new task_1.InteruptableTask(performDisconnect(), {
155
+ timeout: this.getConnectTimeout(),
156
+ name: 'BLE connect',
157
+ errorOnTimeout: false,
158
+ log: this.logEvent.bind(this),
159
+ });
160
+ const success = yield this.disconnectTask.run().catch(() => false);
161
+ return success;
144
162
  });
145
163
  }
146
164
  isConnected() {
147
- var _a;
148
- return this.connectAttemptCnt > 0 && ((_a = this.getBinding()) === null || _a === void 0 ? void 0 : _a.state) === 'poweredOn';
165
+ return this.confirmedBleState === 'poweredOn';
149
166
  }
150
167
  registerConnected(peripheral, id) {
151
168
  const p = this.connectedPeripherals.find(p => p.id === id);
@@ -165,6 +182,10 @@ class BleInterface extends events_1.default {
165
182
  var _a;
166
183
  return ((_a = this.connectTask) === null || _a === void 0 ? void 0 : _a.isRunning()) === true;
167
184
  }
185
+ isDisconnecting() {
186
+ var _a;
187
+ return ((_a = this.disconnectTask) === null || _a === void 0 ? void 0 : _a.isRunning()) === true;
188
+ }
168
189
  scan(props) {
169
190
  return __awaiter(this, void 0, void 0, function* () {
170
191
  this.resumeLogging();
@@ -272,12 +293,12 @@ class BleInterface extends events_1.default {
272
293
  }
273
294
  startPeripheralScan() {
274
295
  return __awaiter(this, arguments, void 0, function* (retry = false) {
275
- this.expectedServices = this.getExpectedServices();
276
- if (!retry)
277
- this.logEvent({ message: 'starting peripheral discovery ...' });
278
296
  if (!this.isConnected() || this.isDiscovering()) {
279
297
  return;
280
298
  }
299
+ this.expectedServices = this.getExpectedServices();
300
+ if (!retry)
301
+ this.logEvent({ message: 'starting peripheral discovery ...' });
281
302
  this.discoverTask = new task_1.InteruptableTask(this.discoverPeripherals(), {
282
303
  errorOnTimeout: false,
283
304
  name: 'discover',
@@ -556,10 +577,9 @@ class BleInterface extends events_1.default {
556
577
  }
557
578
  connectBle() {
558
579
  return __awaiter(this, void 0, void 0, function* () {
559
- this.connectAttemptCnt++;
560
- const state = this.getBinding().state;
561
- if (state === 'poweredOn') {
562
- this.logEvent({ message: 'BLE connected' });
580
+ this.currentBleState = this.getBinding().state;
581
+ if (this.currentBleState === 'poweredOn') {
582
+ this.onConnected();
563
583
  return true;
564
584
  }
565
585
  const res = yield this.waitForBleConnected();
@@ -573,13 +593,13 @@ class BleInterface extends events_1.default {
573
593
  return done(false);
574
594
  });
575
595
  this.getBinding().on('stateChange', (state) => {
596
+ if (state === this.confirmedBleState)
597
+ return;
598
+ this.logEvent({ message: 'BLE state change', state });
576
599
  if (state === 'poweredOn') {
577
600
  this.onConnected();
578
601
  return done(true);
579
602
  }
580
- else {
581
- this.logEvent({ message: 'BLE state change', state });
582
- }
583
603
  });
584
604
  });
585
605
  }
@@ -587,26 +607,41 @@ class BleInterface extends events_1.default {
587
607
  this.logError(err, 'BLE connect');
588
608
  }
589
609
  onConnected() {
590
- this.logEvent({ message: 'BLE connected' });
591
- this.getBinding().removeAllListeners('error');
592
- this.getBinding().removeAllListeners('stateChange');
593
- this.getBinding().on('stateChange', this.onBleStateChange.bind(this));
594
- this.getBinding().on('error', this.onError.bind(this));
610
+ return __awaiter(this, void 0, void 0, function* () {
611
+ if (this.isConnected())
612
+ return;
613
+ this.confirmedBleState = 'poweredOn';
614
+ this.getBinding().removeAllListeners('error');
615
+ this.getBinding().removeAllListeners('stateChange');
616
+ this.getBinding().on('stateChange', this.onBleStateChange.bind(this));
617
+ this.getBinding().on('error', this.onError.bind(this));
618
+ this.logEvent({ message: 'BLE connected' });
619
+ this.startPeripheralScan();
620
+ });
595
621
  }
596
622
  onDisconnected() {
597
623
  return __awaiter(this, void 0, void 0, function* () {
624
+ if (this.isDisconnecting() || !this.isConnected())
625
+ return;
598
626
  this.logEvent({ message: 'BLE Disconnected' });
599
627
  yield this.disconnect(true);
600
- this.getBinding().on('stateChange', this.onBleStateChange.bind(this));
601
- this.getBinding().on('error', this.onError.bind(this));
602
628
  });
603
629
  }
604
630
  onBleStateChange(state) {
605
- if (state !== 'poweredOn') {
606
- this.onDisconnected();
607
- }
608
- else
609
- this.onConnected();
631
+ return __awaiter(this, void 0, void 0, function* () {
632
+ if (state === this.currentBleState)
633
+ return;
634
+ this.currentBleState = state;
635
+ if (state === 'poweredOff') {
636
+ this.onDisconnected();
637
+ }
638
+ else {
639
+ if (this.isDisconnecting()) {
640
+ yield this.disconnectTask.getPromise();
641
+ }
642
+ this.onConnected();
643
+ }
644
+ });
610
645
  }
611
646
  getAdapterFactory() {
612
647
  return factories_1.BleAdapterFactory.getInstance('ble');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "2.3.9",
3
+ "version": "2.3.10",
4
4
  "dependencies": {
5
5
  "@serialport/bindings-interface": "^1.2.2",
6
6
  "@serialport/parser-byte-length": "^9.0.1",