incyclist-devices 3.0.13 → 3.0.14
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,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BlePeripheral = void 0;
|
|
4
|
-
const utils_js_1 = require("
|
|
4
|
+
const utils_js_1 = require("../../utils/utils.js");
|
|
5
|
+
const utils_js_2 = require("../utils.js");
|
|
5
6
|
const interface_js_1 = require("./interface.js");
|
|
6
7
|
class BlePeripheral {
|
|
7
8
|
announcement;
|
|
@@ -14,6 +15,8 @@ class BlePeripheral {
|
|
|
14
15
|
disconnecting = false;
|
|
15
16
|
disconnectedSignalled = false;
|
|
16
17
|
discoveredServiceUUIds;
|
|
18
|
+
discoverServicesPromise;
|
|
19
|
+
discoverCharacteristicsPromise = {};
|
|
17
20
|
onErrorHandler = this.onPeripheralError.bind(this);
|
|
18
21
|
constructor(announcement) {
|
|
19
22
|
this.announcement = announcement;
|
|
@@ -29,7 +32,7 @@ class BlePeripheral {
|
|
|
29
32
|
return this.ble;
|
|
30
33
|
}
|
|
31
34
|
getAnnouncedServices() {
|
|
32
|
-
return this.announcement.serviceUUIDs.map(s => (0,
|
|
35
|
+
return this.announcement.serviceUUIDs.map(s => (0, utils_js_2.beautifyUUID)(s));
|
|
33
36
|
}
|
|
34
37
|
getDiscoveredServices() {
|
|
35
38
|
return this.discoveredServiceUUIds ?? [];
|
|
@@ -123,7 +126,7 @@ class BlePeripheral {
|
|
|
123
126
|
const serviceData = this.announcement?.serviceData;
|
|
124
127
|
if (!serviceData)
|
|
125
128
|
return;
|
|
126
|
-
const data = serviceData.find(sd => (0,
|
|
129
|
+
const data = serviceData.find(sd => (0, utils_js_2.matches)(sd.uuid, uuid))?.data;
|
|
127
130
|
if (data)
|
|
128
131
|
return Buffer.from(data);
|
|
129
132
|
return data;
|
|
@@ -147,6 +150,13 @@ class BlePeripheral {
|
|
|
147
150
|
this.logEvent({ message: 'peripheral error', address: this.getPeripheral()?.address, error: err.message });
|
|
148
151
|
}
|
|
149
152
|
async discoverServices() {
|
|
153
|
+
this.discoverServicesPromise = this.discoverServicesPromise ?? this._discoverServices();
|
|
154
|
+
const promise = this.discoverServicesPromise;
|
|
155
|
+
const res = await promise;
|
|
156
|
+
(0, utils_js_1.sleep)(0).then(() => { delete this.discoverServicesPromise; });
|
|
157
|
+
return res;
|
|
158
|
+
}
|
|
159
|
+
async _discoverServices() {
|
|
150
160
|
if (!this.getPeripheral())
|
|
151
161
|
return [];
|
|
152
162
|
if (this.getPeripheral().discoverServicesAsync) {
|
|
@@ -157,25 +167,32 @@ class BlePeripheral {
|
|
|
157
167
|
services = await peripheral.discoverServicesAsync([])
|
|
158
168
|
.catch(() => []);
|
|
159
169
|
}
|
|
160
|
-
this.discoveredServiceUUIds = services.map(s => (0,
|
|
170
|
+
this.discoveredServiceUUIds = services.map(s => (0, utils_js_2.beautifyUUID)(s.uuid));
|
|
161
171
|
this.logEvent({ message: 'discover services result', address: this.getPeripheral().address, services: this.discoveredServiceUUIds });
|
|
162
172
|
return services.map(s => s.uuid);
|
|
163
173
|
}
|
|
164
174
|
else {
|
|
165
175
|
this.logEvent({ message: 'discover services and characteristics', address: this.getPeripheral().address });
|
|
166
176
|
const res = await this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([], []);
|
|
167
|
-
this.discoveredServiceUUIds = res.services.map(s => (0,
|
|
177
|
+
this.discoveredServiceUUIds = res.services.map(s => (0, utils_js_2.beautifyUUID)(s.uuid));
|
|
168
178
|
this.logEvent({ message: 'discover services result', address: this.getPeripheral().address, services: this.discoveredServiceUUIds });
|
|
169
179
|
return res.services.map(s => s.uuid);
|
|
170
180
|
}
|
|
171
181
|
}
|
|
172
182
|
async discoverCharacteristics(serviceUUID) {
|
|
183
|
+
this.discoverCharacteristicsPromise[serviceUUID] = this.discoverCharacteristicsPromise[serviceUUID] ?? this._discoverCharacteristics(serviceUUID);
|
|
184
|
+
const promise = this.discoverCharacteristicsPromise[serviceUUID];
|
|
185
|
+
const res = await promise;
|
|
186
|
+
(0, utils_js_1.sleep)(0).then(() => { delete this.discoverCharacteristicsPromise[serviceUUID]; });
|
|
187
|
+
return res;
|
|
188
|
+
}
|
|
189
|
+
async _discoverCharacteristics(serviceUUID) {
|
|
173
190
|
if (!this.getPeripheral())
|
|
174
191
|
return [];
|
|
175
192
|
this.logEvent({ message: 'discover services and characteristics', service: serviceUUID, address: this.getPeripheral().address });
|
|
176
193
|
const res = await this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([serviceUUID], [])
|
|
177
194
|
.catch(() => ({ services: [], characteristics: [] }));
|
|
178
|
-
res.characteristics.forEach(c => this.characteristics[(0,
|
|
195
|
+
res.characteristics.forEach(c => this.characteristics[(0, utils_js_2.beautifyUUID)(c.uuid)] = c);
|
|
179
196
|
return res.characteristics.map(c => {
|
|
180
197
|
const { uuid, properties, name, _serviceUuid } = c;
|
|
181
198
|
return { uuid, properties, name, _serviceUuid };
|
|
@@ -185,7 +202,7 @@ class BlePeripheral {
|
|
|
185
202
|
try {
|
|
186
203
|
if (this.disconnecting || !this.connected)
|
|
187
204
|
return false;
|
|
188
|
-
const uuid = (0,
|
|
205
|
+
const uuid = (0, utils_js_2.beautifyUUID)(characteristicUUID);
|
|
189
206
|
const onData = (data, isNotify) => {
|
|
190
207
|
try {
|
|
191
208
|
callback(characteristicUUID, data, isNotify);
|
|
@@ -238,7 +255,7 @@ class BlePeripheral {
|
|
|
238
255
|
}
|
|
239
256
|
unsubscribe(characteristicUUID) {
|
|
240
257
|
try {
|
|
241
|
-
const uuid = (0,
|
|
258
|
+
const uuid = (0, utils_js_2.beautifyUUID)(characteristicUUID);
|
|
242
259
|
const subscription = this.subscribed.find(s => s.uuid === uuid);
|
|
243
260
|
if (!subscription) {
|
|
244
261
|
return Promise.resolve(true);
|
|
@@ -305,7 +322,7 @@ class BlePeripheral {
|
|
|
305
322
|
const res = await this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([], []);
|
|
306
323
|
const found = [];
|
|
307
324
|
res.characteristics.forEach(c => {
|
|
308
|
-
this.characteristics[(0,
|
|
325
|
+
this.characteristics[(0, utils_js_2.beautifyUUID)(c.uuid)] = c;
|
|
309
326
|
found.push(c.uuid);
|
|
310
327
|
});
|
|
311
328
|
return found;
|
|
@@ -317,11 +334,11 @@ class BlePeripheral {
|
|
|
317
334
|
}
|
|
318
335
|
async discoverSomeCharacteristics(characteristics) {
|
|
319
336
|
try {
|
|
320
|
-
const target = characteristics.map(c => (0,
|
|
337
|
+
const target = characteristics.map(c => (0, utils_js_2.fullUUID)(c));
|
|
321
338
|
const res = await this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([], target);
|
|
322
339
|
const found = [];
|
|
323
340
|
res.characteristics.forEach(c => {
|
|
324
|
-
this.characteristics[(0,
|
|
341
|
+
this.characteristics[(0, utils_js_2.beautifyUUID)(c.uuid)] = c;
|
|
325
342
|
found.push(c.uuid);
|
|
326
343
|
});
|
|
327
344
|
return found;
|
|
@@ -356,10 +373,10 @@ class BlePeripheral {
|
|
|
356
373
|
async read(characteristicUUID) {
|
|
357
374
|
if (this.disconnecting || !this.connected)
|
|
358
375
|
return Buffer.from([]);
|
|
359
|
-
let c = this.characteristics[(0,
|
|
376
|
+
let c = this.characteristics[(0, utils_js_2.beautifyUUID)(characteristicUUID)];
|
|
360
377
|
if (!c) {
|
|
361
378
|
await this.discoverAllCharacteristics();
|
|
362
|
-
c = this.characteristics[(0,
|
|
379
|
+
c = this.characteristics[(0, utils_js_2.beautifyUUID)(characteristicUUID)];
|
|
363
380
|
if (!c) {
|
|
364
381
|
return Promise.reject(new Error('characteristic not found: ' + characteristicUUID));
|
|
365
382
|
}
|
|
@@ -378,11 +395,11 @@ class BlePeripheral {
|
|
|
378
395
|
async write(characteristicUUID, data, options) {
|
|
379
396
|
if (this.disconnecting || !this.connected)
|
|
380
397
|
return Buffer.from([]);
|
|
381
|
-
const uuid = (0,
|
|
398
|
+
const uuid = (0, utils_js_2.beautifyUUID)(characteristicUUID);
|
|
382
399
|
let c = this.characteristics[uuid];
|
|
383
400
|
if (!c) {
|
|
384
401
|
await this.discoverAllCharacteristics();
|
|
385
|
-
c = this.characteristics[(0,
|
|
402
|
+
c = this.characteristics[(0, utils_js_2.beautifyUUID)(characteristicUUID)];
|
|
386
403
|
if (!c) {
|
|
387
404
|
return Promise.reject(new Error('characteristic not found: ' + characteristicUUID));
|
|
388
405
|
}
|
|
@@ -415,22 +432,22 @@ class BlePeripheral {
|
|
|
415
432
|
});
|
|
416
433
|
}
|
|
417
434
|
async queryRawCharacteristic(uuid) {
|
|
418
|
-
const characteristicUUID = (0,
|
|
419
|
-
let c = this.characteristics[(0,
|
|
435
|
+
const characteristicUUID = (0, utils_js_2.beautifyUUID)(uuid);
|
|
436
|
+
let c = this.characteristics[(0, utils_js_2.beautifyUUID)(uuid)];
|
|
420
437
|
if (c)
|
|
421
438
|
return c;
|
|
422
439
|
const res = await this.getPeripheral().discoverSomeServicesAndCharacteristicsAsync([], [characteristicUUID]);
|
|
423
440
|
if (res.characteristics.length === 0) {
|
|
424
441
|
return;
|
|
425
442
|
}
|
|
426
|
-
c = res.characteristics.find(dc => (0,
|
|
443
|
+
c = res.characteristics.find(dc => (0, utils_js_2.beautifyUUID)(dc.uuid) === (0, utils_js_2.beautifyUUID)(characteristicUUID));
|
|
427
444
|
if (c) {
|
|
428
445
|
this.characteristics[characteristicUUID] = c;
|
|
429
446
|
}
|
|
430
447
|
return c;
|
|
431
448
|
}
|
|
432
449
|
getRawCharacteristic(uuid, query = false) {
|
|
433
|
-
return this.characteristics[(0,
|
|
450
|
+
return this.characteristics[(0, utils_js_2.beautifyUUID)(uuid)];
|
|
434
451
|
}
|
|
435
452
|
logEvent(event) {
|
|
436
453
|
event.peripheral = this.announcement?.name;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { sleep } from "../../utils/utils.js";
|
|
1
2
|
import { beautifyUUID, fullUUID, matches } from "../utils.js";
|
|
2
3
|
import { BleInterface } from "./interface.js";
|
|
3
4
|
export class BlePeripheral {
|
|
@@ -11,6 +12,8 @@ export class BlePeripheral {
|
|
|
11
12
|
disconnecting = false;
|
|
12
13
|
disconnectedSignalled = false;
|
|
13
14
|
discoveredServiceUUIds;
|
|
15
|
+
discoverServicesPromise;
|
|
16
|
+
discoverCharacteristicsPromise = {};
|
|
14
17
|
onErrorHandler = this.onPeripheralError.bind(this);
|
|
15
18
|
constructor(announcement) {
|
|
16
19
|
this.announcement = announcement;
|
|
@@ -144,6 +147,13 @@ export class BlePeripheral {
|
|
|
144
147
|
this.logEvent({ message: 'peripheral error', address: this.getPeripheral()?.address, error: err.message });
|
|
145
148
|
}
|
|
146
149
|
async discoverServices() {
|
|
150
|
+
this.discoverServicesPromise = this.discoverServicesPromise ?? this._discoverServices();
|
|
151
|
+
const promise = this.discoverServicesPromise;
|
|
152
|
+
const res = await promise;
|
|
153
|
+
sleep(0).then(() => { delete this.discoverServicesPromise; });
|
|
154
|
+
return res;
|
|
155
|
+
}
|
|
156
|
+
async _discoverServices() {
|
|
147
157
|
if (!this.getPeripheral())
|
|
148
158
|
return [];
|
|
149
159
|
if (this.getPeripheral().discoverServicesAsync) {
|
|
@@ -167,6 +177,13 @@ export class BlePeripheral {
|
|
|
167
177
|
}
|
|
168
178
|
}
|
|
169
179
|
async discoverCharacteristics(serviceUUID) {
|
|
180
|
+
this.discoverCharacteristicsPromise[serviceUUID] = this.discoverCharacteristicsPromise[serviceUUID] ?? this._discoverCharacteristics(serviceUUID);
|
|
181
|
+
const promise = this.discoverCharacteristicsPromise[serviceUUID];
|
|
182
|
+
const res = await promise;
|
|
183
|
+
sleep(0).then(() => { delete this.discoverCharacteristicsPromise[serviceUUID]; });
|
|
184
|
+
return res;
|
|
185
|
+
}
|
|
186
|
+
async _discoverCharacteristics(serviceUUID) {
|
|
170
187
|
if (!this.getPeripheral())
|
|
171
188
|
return [];
|
|
172
189
|
this.logEvent({ message: 'discover services and characteristics', service: serviceUUID, address: this.getPeripheral().address });
|
|
@@ -14,6 +14,8 @@ export declare class BlePeripheral implements IBlePeripheral {
|
|
|
14
14
|
protected disconnecting: boolean;
|
|
15
15
|
protected disconnectedSignalled: boolean;
|
|
16
16
|
protected discoveredServiceUUIds: Array<string> | undefined;
|
|
17
|
+
protected discoverServicesPromise: Promise<string[]> | undefined;
|
|
18
|
+
protected discoverCharacteristicsPromise: Record<string, Promise<BleCharacteristic[]> | undefined>;
|
|
17
19
|
protected onErrorHandler: any;
|
|
18
20
|
constructor(announcement: BlePeripheralAnnouncement);
|
|
19
21
|
get services(): BleService[];
|
|
@@ -32,7 +34,9 @@ export declare class BlePeripheral implements IBlePeripheral {
|
|
|
32
34
|
protected onPeripheralDisconnect(): Promise<void>;
|
|
33
35
|
protected onPeripheralError(err: Error): void;
|
|
34
36
|
discoverServices(): Promise<string[]>;
|
|
37
|
+
protected _discoverServices(): Promise<string[]>;
|
|
35
38
|
discoverCharacteristics(serviceUUID: string): Promise<BleCharacteristic[]>;
|
|
39
|
+
protected _discoverCharacteristics(serviceUUID: string): Promise<BleCharacteristic[]>;
|
|
36
40
|
subscribe(characteristicUUID: string, callback: (characteristicUuid: string, data: Buffer, isNotify?: any) => void): Promise<boolean>;
|
|
37
41
|
unsubscribe(characteristicUUID: string): Promise<boolean>;
|
|
38
42
|
subscribeSelected(characteristics: string[], callback: (characteristicUuid: string, data: Buffer, isNotify?: boolean) => void): Promise<boolean>;
|