incyclist-devices 3.0.17 → 3.0.18
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.
|
@@ -50,6 +50,7 @@ class BlePeripheral {
|
|
|
50
50
|
if (this.connectPromise !== undefined) {
|
|
51
51
|
return this.connectPromise.then(() => this.connected);
|
|
52
52
|
}
|
|
53
|
+
let address;
|
|
53
54
|
this.connectPromise = new Promise((done) => {
|
|
54
55
|
const peripheral = this.getPeripheral();
|
|
55
56
|
this.connected = false;
|
|
@@ -60,7 +61,8 @@ class BlePeripheral {
|
|
|
60
61
|
if (!this.ble.isConnected()) {
|
|
61
62
|
return done();
|
|
62
63
|
}
|
|
63
|
-
|
|
64
|
+
address = peripheral.address;
|
|
65
|
+
this.logEvent({ message: 'connect peripheral', address });
|
|
64
66
|
peripheral.connectAsync().then(() => {
|
|
65
67
|
this.ble.registerConnected(this, peripheralId);
|
|
66
68
|
peripheral.once('disconnect', () => { this.onPeripheralDisconnect(); });
|
|
@@ -75,11 +77,12 @@ class BlePeripheral {
|
|
|
75
77
|
});
|
|
76
78
|
await this.connectPromise;
|
|
77
79
|
delete this.connectPromise;
|
|
80
|
+
this.logEvent({ message: 'connect peripheral result', address, connected: this.connected });
|
|
78
81
|
return this.connected;
|
|
79
82
|
}
|
|
80
83
|
async disconnect(connectionLost = false) {
|
|
81
84
|
this.disconnecting = true;
|
|
82
|
-
if (this.isConnected()) {
|
|
85
|
+
if (this.isConnected() || connectionLost) {
|
|
83
86
|
await this.unsubscribeAll(connectionLost);
|
|
84
87
|
}
|
|
85
88
|
Object.keys(this.characteristics).forEach(uuid => {
|
|
@@ -101,13 +104,14 @@ class BlePeripheral {
|
|
|
101
104
|
.catch(() => { });
|
|
102
105
|
}
|
|
103
106
|
peripheral.removeAllListeners();
|
|
107
|
+
this.ble.unregisterConnected(peripheral.id);
|
|
104
108
|
}
|
|
105
109
|
else {
|
|
106
110
|
delete this.onDisconnectHandler;
|
|
107
111
|
}
|
|
108
|
-
this.ble.unregisterConnected(peripheral.id);
|
|
109
112
|
this.connected = false;
|
|
110
113
|
this.disconnecting = false;
|
|
114
|
+
this.logEvent({ message: 'peripheral disconnect completed', address: peripheral.address });
|
|
111
115
|
return !this.connected;
|
|
112
116
|
}
|
|
113
117
|
isConnected() {
|
|
@@ -200,8 +204,12 @@ class BlePeripheral {
|
|
|
200
204
|
}
|
|
201
205
|
async subscribe(characteristicUUID, callback) {
|
|
202
206
|
try {
|
|
203
|
-
if (this.disconnecting || !this.connected)
|
|
207
|
+
if (this.disconnecting || !this.connected) {
|
|
208
|
+
this.logEvent({ message: 'peripheral subscribe failed', uuid: characteristicUUID, reason: 'not connected',
|
|
209
|
+
disconnecting: this.disconnecting, connected: this.connected
|
|
210
|
+
});
|
|
204
211
|
return false;
|
|
212
|
+
}
|
|
205
213
|
const uuid = (0, utils_js_2.beautifyUUID)(characteristicUUID);
|
|
206
214
|
const onData = (data, isNotify) => {
|
|
207
215
|
try {
|
|
@@ -211,6 +219,7 @@ class BlePeripheral {
|
|
|
211
219
|
};
|
|
212
220
|
const subscription = this.subscribed.find(s => s.uuid === uuid);
|
|
213
221
|
if (subscription) {
|
|
222
|
+
this.logEvent({ message: 'peripheral subscribe skipped', uuid: characteristicUUID, reason: 'already subscribed' });
|
|
214
223
|
const c = this.getRawCharacteristic(characteristicUUID);
|
|
215
224
|
if (c) {
|
|
216
225
|
c.off('data', onData);
|
|
@@ -220,11 +229,13 @@ class BlePeripheral {
|
|
|
220
229
|
}
|
|
221
230
|
let c = await this.queryRawCharacteristic(characteristicUUID).catch(() => null);
|
|
222
231
|
if (!c) {
|
|
232
|
+
this.logEvent({ message: 'peripheral subscribe failed', uuid: characteristicUUID, reason: 'not found' });
|
|
223
233
|
return false;
|
|
224
234
|
}
|
|
225
235
|
return new Promise((resolve, reject) => {
|
|
226
236
|
const info = this.subscribed.find(s => s.uuid === characteristicUUID);
|
|
227
237
|
if (info) {
|
|
238
|
+
this.logEvent({ message: 'peripheral subscribe skipped', uuid: characteristicUUID, reason: 'already subscribed' });
|
|
228
239
|
return Promise.resolve(true);
|
|
229
240
|
}
|
|
230
241
|
this.logEvent({ message: 'subscribe request', address: this.getPeripheral().address, characteristic: uuid, success: true });
|
|
@@ -287,6 +298,8 @@ class BlePeripheral {
|
|
|
287
298
|
}
|
|
288
299
|
}
|
|
289
300
|
async subscribeSelected(characteristics, callback) {
|
|
301
|
+
const uuids = characteristics != null ? characteristics.map(c => (0, utils_js_2.beautifyUUID)(c)).join('|') : 'none';
|
|
302
|
+
this.logEvent({ message: 'peripheral subscribe selected', uuids });
|
|
290
303
|
if (!this.discoveredServiceUUIds) {
|
|
291
304
|
try {
|
|
292
305
|
await this.discoverServices();
|
|
@@ -349,6 +362,7 @@ class BlePeripheral {
|
|
|
349
362
|
}
|
|
350
363
|
}
|
|
351
364
|
async subscribeAll(callback) {
|
|
365
|
+
this.logEvent({ message: 'peripheral subscribe all' });
|
|
352
366
|
if (!this.discoveredServiceUUIds) {
|
|
353
367
|
try {
|
|
354
368
|
await this.discoverServices();
|
|
@@ -360,6 +374,7 @@ class BlePeripheral {
|
|
|
360
374
|
return success;
|
|
361
375
|
}
|
|
362
376
|
async unsubscribeAll(connectionLost = false) {
|
|
377
|
+
this.logEvent({ message: 'peripheral unsubscribe all', connectionLost });
|
|
363
378
|
if (connectionLost) {
|
|
364
379
|
this.subscribed = [];
|
|
365
380
|
return;
|
|
@@ -47,6 +47,7 @@ export class BlePeripheral {
|
|
|
47
47
|
if (this.connectPromise !== undefined) {
|
|
48
48
|
return this.connectPromise.then(() => this.connected);
|
|
49
49
|
}
|
|
50
|
+
let address;
|
|
50
51
|
this.connectPromise = new Promise((done) => {
|
|
51
52
|
const peripheral = this.getPeripheral();
|
|
52
53
|
this.connected = false;
|
|
@@ -57,7 +58,8 @@ export class BlePeripheral {
|
|
|
57
58
|
if (!this.ble.isConnected()) {
|
|
58
59
|
return done();
|
|
59
60
|
}
|
|
60
|
-
|
|
61
|
+
address = peripheral.address;
|
|
62
|
+
this.logEvent({ message: 'connect peripheral', address });
|
|
61
63
|
peripheral.connectAsync().then(() => {
|
|
62
64
|
this.ble.registerConnected(this, peripheralId);
|
|
63
65
|
peripheral.once('disconnect', () => { this.onPeripheralDisconnect(); });
|
|
@@ -72,11 +74,12 @@ export class BlePeripheral {
|
|
|
72
74
|
});
|
|
73
75
|
await this.connectPromise;
|
|
74
76
|
delete this.connectPromise;
|
|
77
|
+
this.logEvent({ message: 'connect peripheral result', address, connected: this.connected });
|
|
75
78
|
return this.connected;
|
|
76
79
|
}
|
|
77
80
|
async disconnect(connectionLost = false) {
|
|
78
81
|
this.disconnecting = true;
|
|
79
|
-
if (this.isConnected()) {
|
|
82
|
+
if (this.isConnected() || connectionLost) {
|
|
80
83
|
await this.unsubscribeAll(connectionLost);
|
|
81
84
|
}
|
|
82
85
|
Object.keys(this.characteristics).forEach(uuid => {
|
|
@@ -98,13 +101,14 @@ export class BlePeripheral {
|
|
|
98
101
|
.catch(() => { });
|
|
99
102
|
}
|
|
100
103
|
peripheral.removeAllListeners();
|
|
104
|
+
this.ble.unregisterConnected(peripheral.id);
|
|
101
105
|
}
|
|
102
106
|
else {
|
|
103
107
|
delete this.onDisconnectHandler;
|
|
104
108
|
}
|
|
105
|
-
this.ble.unregisterConnected(peripheral.id);
|
|
106
109
|
this.connected = false;
|
|
107
110
|
this.disconnecting = false;
|
|
111
|
+
this.logEvent({ message: 'peripheral disconnect completed', address: peripheral.address });
|
|
108
112
|
return !this.connected;
|
|
109
113
|
}
|
|
110
114
|
isConnected() {
|
|
@@ -197,8 +201,12 @@ export class BlePeripheral {
|
|
|
197
201
|
}
|
|
198
202
|
async subscribe(characteristicUUID, callback) {
|
|
199
203
|
try {
|
|
200
|
-
if (this.disconnecting || !this.connected)
|
|
204
|
+
if (this.disconnecting || !this.connected) {
|
|
205
|
+
this.logEvent({ message: 'peripheral subscribe failed', uuid: characteristicUUID, reason: 'not connected',
|
|
206
|
+
disconnecting: this.disconnecting, connected: this.connected
|
|
207
|
+
});
|
|
201
208
|
return false;
|
|
209
|
+
}
|
|
202
210
|
const uuid = beautifyUUID(characteristicUUID);
|
|
203
211
|
const onData = (data, isNotify) => {
|
|
204
212
|
try {
|
|
@@ -208,6 +216,7 @@ export class BlePeripheral {
|
|
|
208
216
|
};
|
|
209
217
|
const subscription = this.subscribed.find(s => s.uuid === uuid);
|
|
210
218
|
if (subscription) {
|
|
219
|
+
this.logEvent({ message: 'peripheral subscribe skipped', uuid: characteristicUUID, reason: 'already subscribed' });
|
|
211
220
|
const c = this.getRawCharacteristic(characteristicUUID);
|
|
212
221
|
if (c) {
|
|
213
222
|
c.off('data', onData);
|
|
@@ -217,11 +226,13 @@ export class BlePeripheral {
|
|
|
217
226
|
}
|
|
218
227
|
let c = await this.queryRawCharacteristic(characteristicUUID).catch(() => null);
|
|
219
228
|
if (!c) {
|
|
229
|
+
this.logEvent({ message: 'peripheral subscribe failed', uuid: characteristicUUID, reason: 'not found' });
|
|
220
230
|
return false;
|
|
221
231
|
}
|
|
222
232
|
return new Promise((resolve, reject) => {
|
|
223
233
|
const info = this.subscribed.find(s => s.uuid === characteristicUUID);
|
|
224
234
|
if (info) {
|
|
235
|
+
this.logEvent({ message: 'peripheral subscribe skipped', uuid: characteristicUUID, reason: 'already subscribed' });
|
|
225
236
|
return Promise.resolve(true);
|
|
226
237
|
}
|
|
227
238
|
this.logEvent({ message: 'subscribe request', address: this.getPeripheral().address, characteristic: uuid, success: true });
|
|
@@ -284,6 +295,8 @@ export class BlePeripheral {
|
|
|
284
295
|
}
|
|
285
296
|
}
|
|
286
297
|
async subscribeSelected(characteristics, callback) {
|
|
298
|
+
const uuids = characteristics != null ? characteristics.map(c => beautifyUUID(c)).join('|') : 'none';
|
|
299
|
+
this.logEvent({ message: 'peripheral subscribe selected', uuids });
|
|
287
300
|
if (!this.discoveredServiceUUIds) {
|
|
288
301
|
try {
|
|
289
302
|
await this.discoverServices();
|
|
@@ -346,6 +359,7 @@ export class BlePeripheral {
|
|
|
346
359
|
}
|
|
347
360
|
}
|
|
348
361
|
async subscribeAll(callback) {
|
|
362
|
+
this.logEvent({ message: 'peripheral subscribe all' });
|
|
349
363
|
if (!this.discoveredServiceUUIds) {
|
|
350
364
|
try {
|
|
351
365
|
await this.discoverServices();
|
|
@@ -357,6 +371,7 @@ export class BlePeripheral {
|
|
|
357
371
|
return success;
|
|
358
372
|
}
|
|
359
373
|
async unsubscribeAll(connectionLost = false) {
|
|
374
|
+
this.logEvent({ message: 'peripheral unsubscribe all', connectionLost });
|
|
360
375
|
if (connectionLost) {
|
|
361
376
|
this.subscribed = [];
|
|
362
377
|
return;
|