incyclist-devices 1.4.33 → 1.4.34

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.
@@ -9,7 +9,7 @@ export interface ConnectState {
9
9
  isConnecting: boolean;
10
10
  isConnected: boolean;
11
11
  timeout?: NodeJS.Timeout;
12
- isOpened: boolean;
12
+ isInitSuccess: boolean;
13
13
  }
14
14
  export interface BleDeviceInfo {
15
15
  device: BleDeviceClass;
@@ -10,12 +10,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const gd_eventlog_1 = require("gd-eventlog");
13
+ const utils_1 = require("../utils");
13
14
  const ble_1 = require("./ble");
14
15
  class BleInterface extends ble_1.BleInterfaceClass {
15
16
  constructor(props = {}) {
16
17
  super(props);
17
18
  this.scanState = { isScanning: false, timeout: undefined };
18
- this.connectState = { isConnecting: false, isConnected: false, isOpened: false };
19
+ this.connectState = { isConnecting: false, isConnected: false, isInitSuccess: false };
19
20
  this.devices = [];
20
21
  if (props.logger)
21
22
  this.logger = props.logger;
@@ -73,31 +74,44 @@ class BleInterface extends ble_1.BleInterfaceClass {
73
74
  this.logEvent({ message: 'connect request' });
74
75
  if (!this.getBinding())
75
76
  return Promise.reject(new Error('no binding defined'));
76
- if (!this.connectState.isOpened) {
77
- this.connectState.timeout = setTimeout(() => {
78
- this.connectState.isConnected = false;
79
- this.connectState.isConnecting = false;
80
- this.connectState.timeout = null;
81
- this.logEvent({ message: 'connect result: timeout' });
82
- reject(new Error('timeout'));
83
- }, timeout);
84
- try {
77
+ this.connectState.timeout = setTimeout(() => {
78
+ this.connectState.isConnected = false;
79
+ this.connectState.isConnecting = false;
80
+ this.connectState.timeout = null;
81
+ this.logEvent({ message: 'connect result: timeout' });
82
+ reject(new Error('timeout'));
83
+ }, timeout);
84
+ try {
85
+ if (!this.connectState.isInitSuccess) {
85
86
  const binding = this.getBinding()._bindings;
86
87
  const binding_init_original = binding.init.bind(binding);
87
88
  const self = this;
88
89
  binding.init = function () {
89
90
  try {
90
91
  binding_init_original();
91
- self.connectState.isOpened = true;
92
+ self.connectState.isInitSuccess = true;
92
93
  }
93
94
  catch (err) {
94
- self.connectState.isOpened = false;
95
+ self.connectState.isInitSuccess = false;
95
96
  self.connectState.isConnected = false;
96
97
  self.connectState.isConnecting = false;
97
98
  self.logEvent({ message: 'connect result: error', error: err.message });
98
99
  return reject(new Error(err.message));
99
100
  }
100
101
  };
102
+ }
103
+ const state = this.getBinding().state;
104
+ if (state === ble_1.BleState.POWERED_ON) {
105
+ clearTimeout(this.connectState.timeout);
106
+ this.connectState.timeout = null;
107
+ this.getBinding().removeAllListeners('stateChange');
108
+ this.getBinding().on('stateChange', this.onStateChange.bind(this));
109
+ this.connectState.isConnected = true;
110
+ this.connectState.isConnecting = false;
111
+ this.logEvent({ message: 'connect result: success' });
112
+ return resolve(true);
113
+ }
114
+ else {
101
115
  this.getBinding().once('error', (err) => {
102
116
  this.connectState.isConnected = true;
103
117
  this.connectState.isConnecting = false;
@@ -116,17 +130,20 @@ class BleInterface extends ble_1.BleInterfaceClass {
116
130
  this.logEvent({ message: 'connect result: success' });
117
131
  return resolve(true);
118
132
  }
133
+ else {
134
+ this.logEvent({ message: 'BLE state change', state });
135
+ }
119
136
  });
120
137
  }
121
- catch (err) {
122
- this.connectState.isConnected = false;
123
- this.connectState.isConnecting = false;
124
- if (this.connectState.timeout)
125
- clearTimeout(this.connectState.timeout);
126
- this.connectState.timeout = null;
127
- this.logEvent({ message: 'connect result: error', error: err.message });
128
- return reject(new Error('bluetooth unavailable, cause: ' + err.message));
129
- }
138
+ }
139
+ catch (err) {
140
+ this.connectState.isConnected = false;
141
+ this.connectState.isConnecting = false;
142
+ if (this.connectState.timeout)
143
+ clearTimeout(this.connectState.timeout);
144
+ this.connectState.timeout = null;
145
+ this.logEvent({ message: 'connect result: error', error: err.message });
146
+ return reject(new Error('bluetooth unavailable, cause: ' + err.message));
130
147
  }
131
148
  });
132
149
  }
@@ -209,8 +226,33 @@ class BleInterface extends ble_1.BleInterfaceClass {
209
226
  }
210
227
  connectDevice(requested, timeout = 2000) {
211
228
  return __awaiter(this, void 0, void 0, function* () {
212
- const devices = yield this.scan({ timeout, device: requested });
213
- const { id, address, name } = requested;
229
+ const { id, name, address } = requested;
230
+ this.logEvent({ message: 'connectDevice', id, name, address });
231
+ let devices = [];
232
+ let retry = false;
233
+ let retryCount = 0;
234
+ do {
235
+ if (retryCount > 0) {
236
+ this.logEvent({ message: 'retry connect device', retryCount });
237
+ }
238
+ try {
239
+ devices = yield this.scan({ timeout, device: requested });
240
+ if (devices.length === 0) {
241
+ retryCount++;
242
+ retry = retryCount < 5;
243
+ }
244
+ }
245
+ catch (err) {
246
+ if (err.message === 'scanning already in progress') {
247
+ this.logEvent({ message: 'scan busy' });
248
+ yield utils_1.sleep(1000);
249
+ retryCount++;
250
+ retry = retryCount < 5;
251
+ continue;
252
+ }
253
+ throw err;
254
+ }
255
+ } while (devices.length === 0 && retry);
214
256
  if (devices.length === 0)
215
257
  throw new Error('device not found');
216
258
  if (devices[0]) {
@@ -295,9 +337,12 @@ class BleInterface extends ble_1.BleInterfaceClass {
295
337
  this.scanState.timeout = null;
296
338
  bleBinding.stopScanning(() => {
297
339
  this.scanState.isScanning = false;
340
+ resolve([d]);
298
341
  });
299
342
  }
300
- resolve([d]);
343
+ else {
344
+ resolve([d]);
345
+ }
301
346
  }
302
347
  });
303
348
  }
@@ -306,9 +351,9 @@ class BleInterface extends ble_1.BleInterfaceClass {
306
351
  this.scanState.timeout = setTimeout(() => {
307
352
  this.scanState.timeout = null;
308
353
  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}` : '') });
309
- resolve(this.devices.map(i => i.device));
310
354
  bleBinding.stopScanning(() => {
311
355
  this.scanState.isScanning = false;
356
+ resolve(this.devices.map(i => i.device));
312
357
  });
313
358
  }, timeout);
314
359
  });
package/lib/ble/ble.d.ts CHANGED
@@ -23,6 +23,7 @@ export interface BleBinding extends EventEmitter {
23
23
  startScanning(serviceUUIDs?: string[], allowDuplicates?: boolean, callback?: (error?: Error) => void): void;
24
24
  stopScanning(callback?: () => void): void;
25
25
  _bindings: any;
26
+ state: string;
26
27
  }
27
28
  export declare type ScanProps = {
28
29
  timeout?: number;
package/lib/ble/hrm.js CHANGED
@@ -113,7 +113,7 @@ class HrmAdapter extends Device_1.default {
113
113
  }
114
114
  catch (err) {
115
115
  this.logger.logEvent({ message: 'start result: error', error: err.message });
116
- return false;
116
+ throw new Error(`could not start device, reason:${err.message}`);
117
117
  }
118
118
  });
119
119
  }
package/lib/ble/pwr.js CHANGED
@@ -226,7 +226,7 @@ class PwrAdapter extends Device_1.default {
226
226
  }
227
227
  catch (err) {
228
228
  this.logger.logEvent({ message: 'start result: error', error: err.message });
229
- return false;
229
+ throw new Error(`could not start device, reason:${err.message}`);
230
230
  }
231
231
  });
232
232
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-devices",
3
- "version": "1.4.33",
3
+ "version": "1.4.34",
4
4
  "dependencies": {
5
5
  "@serialport/parser-byte-length": "^9.0.1",
6
6
  "@serialport/parser-delimiter": "^9.0.1",