incyclist-devices 1.4.32 → 1.4.35
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.
- package/lib/ble/ble-interface.d.ts +1 -1
- package/lib/ble/ble-interface.js +79 -29
- package/lib/ble/ble.d.ts +1 -0
- package/lib/ble/hrm.js +1 -1
- package/lib/ble/pwr.js +1 -1
- package/package.json +1 -1
package/lib/ble/ble-interface.js
CHANGED
|
@@ -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,
|
|
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,45 @@ 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
|
-
|
|
77
|
-
this.connectState.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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;
|
|
89
|
+
binding.on('error', (err) => { this.getBinding().emit('error', err); });
|
|
88
90
|
binding.init = function () {
|
|
89
91
|
try {
|
|
90
92
|
binding_init_original();
|
|
91
|
-
self.connectState.
|
|
93
|
+
self.connectState.isInitSuccess = true;
|
|
92
94
|
}
|
|
93
95
|
catch (err) {
|
|
94
|
-
self.connectState.
|
|
96
|
+
self.connectState.isInitSuccess = false;
|
|
95
97
|
self.connectState.isConnected = false;
|
|
96
98
|
self.connectState.isConnecting = false;
|
|
97
99
|
self.logEvent({ message: 'connect result: error', error: err.message });
|
|
98
100
|
return reject(new Error(err.message));
|
|
99
101
|
}
|
|
100
102
|
};
|
|
103
|
+
}
|
|
104
|
+
const state = this.getBinding().state;
|
|
105
|
+
if (state === ble_1.BleState.POWERED_ON) {
|
|
106
|
+
clearTimeout(this.connectState.timeout);
|
|
107
|
+
this.connectState.timeout = null;
|
|
108
|
+
this.getBinding().removeAllListeners('stateChange');
|
|
109
|
+
this.getBinding().on('stateChange', this.onStateChange.bind(this));
|
|
110
|
+
this.connectState.isConnected = true;
|
|
111
|
+
this.connectState.isConnecting = false;
|
|
112
|
+
this.logEvent({ message: 'connect result: success' });
|
|
113
|
+
return resolve(true);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
101
116
|
this.getBinding().once('error', (err) => {
|
|
102
117
|
this.connectState.isConnected = true;
|
|
103
118
|
this.connectState.isConnecting = false;
|
|
@@ -116,17 +131,20 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
116
131
|
this.logEvent({ message: 'connect result: success' });
|
|
117
132
|
return resolve(true);
|
|
118
133
|
}
|
|
134
|
+
else {
|
|
135
|
+
this.logEvent({ message: 'BLE state change', state });
|
|
136
|
+
}
|
|
119
137
|
});
|
|
120
138
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
this.connectState.timeout
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
this.connectState.isConnected = false;
|
|
142
|
+
this.connectState.isConnecting = false;
|
|
143
|
+
if (this.connectState.timeout)
|
|
144
|
+
clearTimeout(this.connectState.timeout);
|
|
145
|
+
this.connectState.timeout = null;
|
|
146
|
+
this.logEvent({ message: 'connect result: error', error: err.message });
|
|
147
|
+
return reject(new Error('bluetooth unavailable, cause: ' + err.message));
|
|
130
148
|
}
|
|
131
149
|
});
|
|
132
150
|
}
|
|
@@ -209,8 +227,33 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
209
227
|
}
|
|
210
228
|
connectDevice(requested, timeout = 2000) {
|
|
211
229
|
return __awaiter(this, void 0, void 0, function* () {
|
|
212
|
-
const
|
|
213
|
-
|
|
230
|
+
const { id, name, address } = requested;
|
|
231
|
+
this.logEvent({ message: 'connectDevice', id, name, address });
|
|
232
|
+
let devices = [];
|
|
233
|
+
let retry = false;
|
|
234
|
+
let retryCount = 0;
|
|
235
|
+
do {
|
|
236
|
+
if (retryCount > 0) {
|
|
237
|
+
this.logEvent({ message: 'retry connect device', retryCount });
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
devices = yield this.scan({ timeout, device: requested });
|
|
241
|
+
if (devices.length === 0) {
|
|
242
|
+
retryCount++;
|
|
243
|
+
retry = retryCount < 5;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
catch (err) {
|
|
247
|
+
if (err.message === 'scanning already in progress') {
|
|
248
|
+
this.logEvent({ message: 'scan busy' });
|
|
249
|
+
yield utils_1.sleep(1000);
|
|
250
|
+
retryCount++;
|
|
251
|
+
retry = retryCount < 5;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
throw err;
|
|
255
|
+
}
|
|
256
|
+
} while (devices.length === 0 && retry);
|
|
214
257
|
if (devices.length === 0)
|
|
215
258
|
throw new Error('device not found');
|
|
216
259
|
if (devices[0]) {
|
|
@@ -236,6 +279,7 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
236
279
|
yield this.connect();
|
|
237
280
|
}
|
|
238
281
|
const detectedPeripherals = {};
|
|
282
|
+
this.devices = [];
|
|
239
283
|
if (scanForDevice)
|
|
240
284
|
this.logEvent({ message: 'search device request', device, deviceTypes });
|
|
241
285
|
else
|
|
@@ -275,13 +319,16 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
275
319
|
const d = new C({ peripheral });
|
|
276
320
|
d.setInterface(this);
|
|
277
321
|
if (scanForDevice) {
|
|
278
|
-
if ((device.id &&
|
|
322
|
+
if ((device.id && device.id !== '' && d.id === device.id) ||
|
|
323
|
+
(device.address && device.address !== '' && d.address === device.address) ||
|
|
324
|
+
(device.name && device.name !== '' && d.name === device.name))
|
|
279
325
|
cntFound++;
|
|
280
326
|
}
|
|
281
327
|
else
|
|
282
328
|
cntFound++;
|
|
283
|
-
|
|
284
|
-
|
|
329
|
+
const existing = this.devices.find(i => i.device.id === d.id);
|
|
330
|
+
if (cntFound > 0 && !existing) {
|
|
331
|
+
this.logEvent({ message: 'scan: device found', device: d.name, address: d.address, services: d.services.join(',') });
|
|
285
332
|
this.devices.push({ device: d, isConnected: false });
|
|
286
333
|
this.emit('device', d);
|
|
287
334
|
}
|
|
@@ -291,9 +338,12 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
291
338
|
this.scanState.timeout = null;
|
|
292
339
|
bleBinding.stopScanning(() => {
|
|
293
340
|
this.scanState.isScanning = false;
|
|
341
|
+
resolve([d]);
|
|
294
342
|
});
|
|
295
343
|
}
|
|
296
|
-
|
|
344
|
+
else {
|
|
345
|
+
resolve([d]);
|
|
346
|
+
}
|
|
297
347
|
}
|
|
298
348
|
});
|
|
299
349
|
}
|
|
@@ -301,10 +351,10 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
301
351
|
});
|
|
302
352
|
this.scanState.timeout = setTimeout(() => {
|
|
303
353
|
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));
|
|
354
|
+
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
355
|
bleBinding.stopScanning(() => {
|
|
307
356
|
this.scanState.isScanning = false;
|
|
357
|
+
resolve(this.devices.map(i => i.device));
|
|
308
358
|
});
|
|
309
359
|
}, timeout);
|
|
310
360
|
});
|
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
|
-
|
|
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
|
-
|
|
229
|
+
throw new Error(`could not start device, reason:${err.message}`);
|
|
230
230
|
}
|
|
231
231
|
});
|
|
232
232
|
}
|