incyclist-devices 1.4.31 → 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.
@@ -89,7 +89,7 @@ class BleDevice extends ble_1.BleDeviceClass {
89
89
  characteristics.forEach(c => {
90
90
  if (c.properties.find(p => p === 'notify')) {
91
91
  c.on('data', (data, _isNotification) => {
92
- this.onData(c.uuid, data);
92
+ this.onData(ble_1.uuid(c.uuid), data);
93
93
  });
94
94
  c.subscribe((err) => {
95
95
  if (err)
@@ -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
  }
@@ -173,10 +190,10 @@ class BleInterface extends ble_1.BleInterfaceClass {
173
190
  });
174
191
  };
175
192
  if (typeof services === 'string') {
176
- return get(deviceTypes, (s) => s === services);
193
+ return get(deviceTypes, (s) => s === ble_1.uuid(services));
177
194
  }
178
195
  if (Array.isArray(services)) {
179
- return get(deviceTypes, s => services.includes(s));
196
+ return get(deviceTypes, s => services.map(ble_1.uuid).includes(s));
180
197
  }
181
198
  return [];
182
199
  }
@@ -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]) {
@@ -236,6 +278,7 @@ class BleInterface extends ble_1.BleInterfaceClass {
236
278
  yield this.connect();
237
279
  }
238
280
  const detectedPeripherals = {};
281
+ this.devices = [];
239
282
  if (scanForDevice)
240
283
  this.logEvent({ message: 'search device request', device, deviceTypes });
241
284
  else
@@ -275,13 +318,16 @@ class BleInterface extends ble_1.BleInterfaceClass {
275
318
  const d = new C({ peripheral });
276
319
  d.setInterface(this);
277
320
  if (scanForDevice) {
278
- if ((device.id && d.id === device.id) || (device.address && d.address === device.address) || (device.name && d.name === device.name))
321
+ if ((device.id && device.id !== '' && d.id === device.id) ||
322
+ (device.address && device.address !== '' && d.address === device.address) ||
323
+ (device.name && device.name !== '' && d.name === device.name))
279
324
  cntFound++;
280
325
  }
281
326
  else
282
327
  cntFound++;
283
- if (cntFound > 0) {
284
- this.logEvent({ message: 'scan: device found', device: d.name, services: d.services.join(',') });
328
+ const existing = this.devices.find(i => i.device.id === d.id);
329
+ if (cntFound > 0 && !existing) {
330
+ this.logEvent({ message: 'scan: device found', device: d.name, address: d.address, services: d.services.join(',') });
285
331
  this.devices.push({ device: d, isConnected: false });
286
332
  this.emit('device', d);
287
333
  }
@@ -291,9 +337,12 @@ class BleInterface extends ble_1.BleInterfaceClass {
291
337
  this.scanState.timeout = null;
292
338
  bleBinding.stopScanning(() => {
293
339
  this.scanState.isScanning = false;
340
+ resolve([d]);
294
341
  });
295
342
  }
296
- resolve([d]);
343
+ else {
344
+ resolve([d]);
345
+ }
297
346
  }
298
347
  });
299
348
  }
@@ -301,10 +350,10 @@ class BleInterface extends ble_1.BleInterfaceClass {
301
350
  });
302
351
  this.scanState.timeout = setTimeout(() => {
303
352
  this.scanState.timeout = null;
304
- this.logEvent({ message: 'scan result: devices found', devices: this.devices.map(i => i.device.name) });
305
- resolve(this.devices.map(i => i.device));
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}` : '') });
306
354
  bleBinding.stopScanning(() => {
307
355
  this.scanState.isScanning = false;
356
+ resolve(this.devices.map(i => i.device));
308
357
  });
309
358
  }, timeout);
310
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;
@@ -79,3 +80,4 @@ export declare enum BleState {
79
80
  POWERED_OFF = "poweredOff",
80
81
  POWERED_ON = "poweredOn"
81
82
  }
83
+ export declare const uuid: (s: any) => any;
package/lib/ble/ble.js CHANGED
@@ -49,3 +49,13 @@ var BleState;
49
49
  BleState["POWERED_OFF"] = "poweredOff";
50
50
  BleState["POWERED_ON"] = "poweredOn";
51
51
  })(BleState = exports.BleState || (exports.BleState = {}));
52
+ exports.uuid = (s) => {
53
+ if (s) {
54
+ if (s.includes('-')) {
55
+ const parts = s.split('-');
56
+ const uuidNo = parseInt('0x' + parts[0]);
57
+ return uuidNo.toString(16);
58
+ }
59
+ return s;
60
+ }
61
+ };
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.31",
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",