incyclist-devices 1.4.36 → 1.4.37

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.
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { EventLogger } from "gd-eventlog";
3
- import { BleInterfaceClass, BleDeviceClass, BlePeripheral, BleCharacteristic, BleDeviceProps, ConnectProps } from "./ble";
3
+ import { BleInterfaceClass, BleDeviceClass, BlePeripheral, BleDeviceProps, ConnectProps } from "./ble";
4
4
  interface ConnectState {
5
5
  isConnecting: boolean;
6
6
  isConnected: boolean;
@@ -17,7 +17,7 @@ export declare abstract class BleDevice extends BleDeviceClass {
17
17
  services: string[];
18
18
  ble: BleInterfaceClass;
19
19
  peripheral?: BlePeripheral;
20
- characteristics: BleCharacteristic[];
20
+ characteristics: any[];
21
21
  state?: string;
22
22
  connectState: ConnectState;
23
23
  logger?: EventLogger;
@@ -71,11 +71,14 @@ class BleDevice extends ble_1.BleDeviceClass {
71
71
  return __awaiter(this, void 0, void 0, function* () {
72
72
  const connectPeripheral = (peripheral) => __awaiter(this, void 0, void 0, function* () {
73
73
  this.connectState.isConnecting = true;
74
- try {
75
- yield peripheral.connectAsync();
76
- }
77
- catch (err) {
78
- this.logEvent({ message: 'cannot connect', error: err.message || err });
74
+ const connected = this.ble.findConnected(peripheral);
75
+ if (!connected) {
76
+ try {
77
+ yield peripheral.connectAsync();
78
+ }
79
+ catch (err) {
80
+ this.logEvent({ message: 'cannot connect', error: err.message || err });
81
+ }
79
82
  }
80
83
  this.connectState.isConnecting = false;
81
84
  this.connectState.isConnected = true;
@@ -85,16 +88,24 @@ class BleDevice extends ble_1.BleDeviceClass {
85
88
  this.ble.addConnectedDevice(this);
86
89
  this.peripheral.once('disconnect', () => { this.onDisconnect(); });
87
90
  try {
88
- const { characteristics } = yield peripheral.discoverSomeServicesAndCharacteristicsAsync(this.services || [], []);
89
- characteristics.forEach(c => {
91
+ if (!connected) {
92
+ const { characteristics } = yield peripheral.discoverSomeServicesAndCharacteristicsAsync(this.services || [], []);
93
+ this.characteristics = characteristics;
94
+ }
95
+ else {
96
+ this.characteristics = connected.characteristics;
97
+ }
98
+ this.characteristics.forEach(c => {
90
99
  if (c.properties.find(p => p === 'notify')) {
91
100
  c.on('data', (data, _isNotification) => {
92
101
  this.onData(ble_1.uuid(c.uuid), data);
93
102
  });
94
- c.subscribe((err) => {
95
- if (err)
96
- this.logEvent({ message: 'cannot subscribe', error: err.message || err });
97
- });
103
+ if (!connected) {
104
+ c.subscribe((err) => {
105
+ if (err)
106
+ this.logEvent({ message: 'cannot subscribe', error: err.message || err });
107
+ });
108
+ }
98
109
  }
99
110
  });
100
111
  }
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { EventLogger } from 'gd-eventlog';
3
- import { BleInterfaceClass, ConnectProps, ScanProps, BleDeviceClass, BleBinding } from './ble';
3
+ import { BleInterfaceClass, ConnectProps, ScanProps, BleDeviceClass, BlePeripheral, BleBinding } from './ble';
4
4
  export interface ScanState {
5
5
  isScanning: boolean;
6
6
  timeout?: NodeJS.Timeout;
@@ -53,5 +53,6 @@ export default class BleInterface extends BleInterfaceClass {
53
53
  stopScan(): Promise<boolean>;
54
54
  isScanning(): boolean;
55
55
  addConnectedDevice(device: BleDeviceClass): void;
56
+ findConnected(device: BleDeviceClass | BlePeripheral): BleDeviceClass;
56
57
  removeConnectedDevice(device: BleDeviceClass): void;
57
58
  }
@@ -283,7 +283,6 @@ class BleInterface extends ble_1.BleInterfaceClass {
283
283
  yield this.connect();
284
284
  }
285
285
  const detectedPeripherals = {};
286
- this.devices = [];
287
286
  if (scanForDevice) {
288
287
  const { id, address, name } = device;
289
288
  this.logEvent({ message: 'search device request', device: { id, address, name }, deviceTypes });
@@ -296,7 +295,15 @@ class BleInterface extends ble_1.BleInterfaceClass {
296
295
  return reject(new Error('scanning already in progress'));
297
296
  }
298
297
  this.scanState.isScanning = true;
299
- bleBinding.startScanning(services, true, (err) => {
298
+ if (scanForDevice && device instanceof ble_1.BleDeviceClass) {
299
+ const existing = this.devices.find(i => i.device.address === device.address && i.isConnected);
300
+ if (existing) {
301
+ device.peripheral = existing.device.peripheral;
302
+ this.logEvent({ message: 'scan: device already connected', device: device.name, address: device.address });
303
+ return resolve([device]);
304
+ }
305
+ }
306
+ bleBinding.startScanning([], true, (err) => {
300
307
  if (err) {
301
308
  this.logEvent({ message: 'scan result: error', error: err.message });
302
309
  this.scanState.isScanning = false;
@@ -306,6 +313,8 @@ class BleInterface extends ble_1.BleInterfaceClass {
306
313
  if (!peripheral || !peripheral.advertisement)
307
314
  return;
308
315
  if (!detectedPeripherals[peripheral.id]) {
316
+ if (process.env.BLE_DEBUG)
317
+ console.log('discovered', peripheral);
309
318
  detectedPeripherals[peripheral.id] = peripheral;
310
319
  let DeviceClasses;
311
320
  if (scanForDevice && (!deviceTypes || deviceTypes.length === 0)) {
@@ -323,6 +332,8 @@ class BleInterface extends ble_1.BleInterfaceClass {
323
332
  return;
324
333
  const C = DeviceClass;
325
334
  const d = new C({ peripheral });
335
+ if (device.getProfile && device.getProfile() !== d.getProfile())
336
+ return;
326
337
  d.setInterface(this);
327
338
  if (scanForDevice) {
328
339
  if ((device.id && device.id !== '' && d.id === device.id) ||
@@ -332,7 +343,7 @@ class BleInterface extends ble_1.BleInterfaceClass {
332
343
  }
333
344
  else
334
345
  cntFound++;
335
- const existing = this.devices.find(i => i.device.id === d.id);
346
+ const existing = this.devices.find(i => i.device.id === d.id && i.device.getProfile() === d.getProfile());
336
347
  if (cntFound > 0 && !existing) {
337
348
  this.logEvent({ message: 'scan: device found', device: d.name, address: d.address, services: d.services.join(',') });
338
349
  this.devices.push({ device: d, isConnected: false });
@@ -343,6 +354,7 @@ class BleInterface extends ble_1.BleInterfaceClass {
343
354
  clearTimeout(this.scanState.timeout);
344
355
  this.scanState.timeout = null;
345
356
  bleBinding.stopScanning(() => {
357
+ this.getBinding().removeAllListeners('discover');
346
358
  this.scanState.isScanning = false;
347
359
  resolve([d]);
348
360
  });
@@ -353,11 +365,14 @@ class BleInterface extends ble_1.BleInterfaceClass {
353
365
  }
354
366
  });
355
367
  }
368
+ else {
369
+ }
356
370
  });
357
371
  });
358
372
  this.scanState.timeout = setTimeout(() => {
359
373
  this.scanState.timeout = null;
360
374
  this.logEvent({ message: 'scan result: devices found', devices: this.devices.map(i => i.device.name + (!i.device.name || i.device.name === '') ? `addr=${i.device.address}` : '') });
375
+ this.getBinding().removeAllListeners('discover');
361
376
  bleBinding.stopScanning(() => {
362
377
  this.scanState.isScanning = false;
363
378
  resolve(this.devices.map(i => i.device));
@@ -372,6 +387,7 @@ class BleInterface extends ble_1.BleInterfaceClass {
372
387
  }
373
388
  if (!this.getBinding())
374
389
  return Promise.reject(new Error('no binding defined'));
390
+ this.getBinding().removeAllListeners('discover');
375
391
  this.logEvent({ message: 'scan stop request' });
376
392
  return new Promise(resolve => {
377
393
  this.getBinding().stopScanning(() => {
@@ -392,6 +408,12 @@ class BleInterface extends ble_1.BleInterfaceClass {
392
408
  }
393
409
  this.devices.push({ device, isConnected: true });
394
410
  }
411
+ findConnected(device) {
412
+ const connected = this.devices.find(i => i.device.id === device.id && i.isConnected);
413
+ if (connected)
414
+ return connected.device;
415
+ return undefined;
416
+ }
395
417
  removeConnectedDevice(device) {
396
418
  const existigDevice = this.devices.find(i => i.device.id === device.id);
397
419
  if (existigDevice) {
package/lib/ble/ble.d.ts CHANGED
@@ -51,6 +51,7 @@ export declare abstract class BleInterfaceClass extends EventEmitter {
51
51
  abstract isScanning(): boolean;
52
52
  abstract addConnectedDevice(device: BleDeviceClass): void;
53
53
  abstract removeConnectedDevice(device: BleDeviceClass): void;
54
+ abstract findConnected(device: BleDeviceClass | BlePeripheral): BleDeviceClass;
54
55
  getBinding(): BleBinding;
55
56
  setBinding(binding: BleBinding): void;
56
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "1.4.36",
3
+ "version": "1.4.37",
4
4
  "dependencies": {
5
5
  "@serialport/parser-byte-length": "^9.0.1",
6
6
  "@serialport/parser-delimiter": "^9.0.1",