incyclist-devices 1.4.40 → 1.4.43
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-device.js +14 -9
- package/lib/ble/ble-interface.d.ts +1 -0
- package/lib/ble/ble-interface.js +42 -10
- package/lib/ble/fm.js +4 -4
- package/lib/ble/incyclist-protocol.js +1 -1
- package/package.json +1 -1
package/lib/ble/ble-device.js
CHANGED
|
@@ -93,7 +93,7 @@ class BleDevice extends ble_1.BleDeviceClass {
|
|
|
93
93
|
const connectPeripheral = (peripheral) => __awaiter(this, void 0, void 0, function* () {
|
|
94
94
|
this.connectState.isConnecting = true;
|
|
95
95
|
const connected = this.ble.findConnected(peripheral);
|
|
96
|
-
if (!connected) {
|
|
96
|
+
if (!connected && peripheral.state !== 'connected') {
|
|
97
97
|
try {
|
|
98
98
|
yield peripheral.connectAsync();
|
|
99
99
|
}
|
|
@@ -102,15 +102,20 @@ class BleDevice extends ble_1.BleDeviceClass {
|
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
try {
|
|
105
|
-
this.
|
|
105
|
+
if (!this.characteristics)
|
|
106
|
+
this.characteristics = [];
|
|
106
107
|
if (!connected) {
|
|
107
|
-
this.
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
108
|
+
if (!this.characteristics || this.characteristics.length === 0) {
|
|
109
|
+
this.logEvent({ message: 'connect: discover characteristics start' });
|
|
110
|
+
const res = yield peripheral.discoverSomeServicesAndCharacteristicsAsync([], []);
|
|
111
|
+
const { characteristics } = res;
|
|
112
|
+
this.logEvent({ message: 'connect: discover characteristics result',
|
|
113
|
+
result: characteristics.map(c => ({ uuid: ble_1.uuid(c.uuid), properties: c.properties.join(','), service: ble_1.uuid(c._serviceUuid) }))
|
|
114
|
+
});
|
|
115
|
+
this.characteristics = characteristics;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
}
|
|
114
119
|
}
|
|
115
120
|
else {
|
|
116
121
|
this.characteristics = connected.characteristics;
|
|
@@ -29,6 +29,7 @@ export default class BleInterface extends BleInterfaceClass {
|
|
|
29
29
|
devices: BleDeviceInfo[];
|
|
30
30
|
logger: EventLogger;
|
|
31
31
|
deviceCache: any[];
|
|
32
|
+
peripheralCache: any[];
|
|
32
33
|
static deviceClasses: BleDeviceClassInfo[];
|
|
33
34
|
static _instance: BleInterface;
|
|
34
35
|
static getInstance(props?: {
|
package/lib/ble/ble-interface.js
CHANGED
|
@@ -20,6 +20,7 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
20
20
|
this.connectState = { isConnecting: false, isConnected: false, isInitSuccess: false };
|
|
21
21
|
this.devices = [];
|
|
22
22
|
this.deviceCache = [];
|
|
23
|
+
this.peripheralCache = [];
|
|
23
24
|
if (props.logger)
|
|
24
25
|
this.logger = props.logger;
|
|
25
26
|
else if (props.log) {
|
|
@@ -356,12 +357,12 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
356
357
|
if (!this.isConnected()) {
|
|
357
358
|
yield this.connect();
|
|
358
359
|
}
|
|
360
|
+
const peripheralsProcessed = [];
|
|
359
361
|
this.logEvent({ message: 'scan()', props, scanState: this.scanState, cache: this.deviceCache.map(p => ({ name: p.advertisement ? p.advertisement.localName : '', address: p.address })) });
|
|
360
362
|
if (!props.isBackgroundScan && this.scanState.isBackgroundScan) {
|
|
361
363
|
yield this.stopScan();
|
|
362
364
|
this.scanState.isBackgroundScan = false;
|
|
363
365
|
}
|
|
364
|
-
const detectedPeripherals = {};
|
|
365
366
|
let opStr;
|
|
366
367
|
if (scanForDevice) {
|
|
367
368
|
opStr = 'search device';
|
|
@@ -374,6 +375,7 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
374
375
|
}
|
|
375
376
|
if (this.scanState.isScanning) {
|
|
376
377
|
try {
|
|
378
|
+
this.logEvent({ message: `${opStr}: waiting for previous scan to finish` });
|
|
377
379
|
yield this.waitForScanFinished(timeout);
|
|
378
380
|
}
|
|
379
381
|
catch (err) {
|
|
@@ -383,6 +385,8 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
383
385
|
}
|
|
384
386
|
return new Promise((resolve, reject) => {
|
|
385
387
|
this.scanState.isScanning = true;
|
|
388
|
+
if (props.isBackgroundScan)
|
|
389
|
+
this.scanState.isBackgroundScan = true;
|
|
386
390
|
if (scanForDevice && device instanceof ble_1.BleDeviceClass) {
|
|
387
391
|
if (this.devices && this.devices.length > 0) {
|
|
388
392
|
const connectedDevices = this.devices.map(i => ({ name: i.device.name, address: i.device.address, isConnected: i.isConnected, connectState: i.device.getConnectState() }));
|
|
@@ -432,16 +436,46 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
432
436
|
}
|
|
433
437
|
}
|
|
434
438
|
}
|
|
435
|
-
const onPeripheralFound = (peripheral, fromCache = false) => {
|
|
439
|
+
const onPeripheralFound = (peripheral, fromCache = false) => __awaiter(this, void 0, void 0, function* () {
|
|
436
440
|
if (fromCache)
|
|
437
441
|
this.logEvent({ message: 'adding from Cache', peripheral: peripheral.address });
|
|
438
442
|
if (!peripheral || !peripheral.advertisement)
|
|
439
443
|
return;
|
|
440
|
-
|
|
444
|
+
let existingPeripheral = this.peripheralCache.find(i => i.address === peripheral.address);
|
|
445
|
+
if (existingPeripheral && Date.now() - existingPeripheral.ts > 600000) {
|
|
446
|
+
existingPeripheral.ts = Date.now();
|
|
447
|
+
}
|
|
448
|
+
if (!existingPeripheral) {
|
|
449
|
+
this.peripheralCache.push({ address: peripheral.address, ts: Date.now(), peripheral });
|
|
450
|
+
existingPeripheral = this.peripheralCache.find(i => i.address === peripheral.address);
|
|
451
|
+
}
|
|
452
|
+
let shouldAddDevice = peripheralsProcessed.find(p => p === peripheral.address) === undefined;
|
|
453
|
+
if (shouldAddDevice) {
|
|
441
454
|
if (process.env.BLE_DEBUG)
|
|
442
|
-
console.log('discovered', peripheral);
|
|
443
|
-
|
|
444
|
-
|
|
455
|
+
console.log('discovered', peripheral.id, peripheral.address, peripheral.advertisement.localName);
|
|
456
|
+
peripheralsProcessed.push(peripheral.address);
|
|
457
|
+
let characteristics;
|
|
458
|
+
if (!existingPeripheral.characteristics) {
|
|
459
|
+
try {
|
|
460
|
+
if (existingPeripheral.peripheral && existingPeripheral.peripheral.state !== 'connected')
|
|
461
|
+
yield peripheral.connectAsync();
|
|
462
|
+
const res = yield peripheral.discoverSomeServicesAndCharacteristicsAsync([], []);
|
|
463
|
+
this.logEvent({ message: 'characteristic info (+):', info: res.characteristics.map(c => `${peripheral.address} ${c.uuid} ${c.properties}`) });
|
|
464
|
+
if (peripheral.disconnect && typeof (peripheral.disconnect) === 'function')
|
|
465
|
+
peripheral.disconnect(() => { });
|
|
466
|
+
existingPeripheral.characteristics = res.characteristics;
|
|
467
|
+
characteristics = res.characteristics;
|
|
468
|
+
}
|
|
469
|
+
catch (err) {
|
|
470
|
+
console.log(err);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
characteristics = existingPeripheral.characteristics;
|
|
475
|
+
this.logEvent({ message: 'characteristic info (+):', info: characteristics.map(c => `${peripheral.address} ${c.uuid} ${c.properties}`) });
|
|
476
|
+
}
|
|
477
|
+
if (!fromCache)
|
|
478
|
+
this.addPeripheralToCache(peripheral);
|
|
445
479
|
let DeviceClasses;
|
|
446
480
|
if (scanForDevice && (!deviceTypes || deviceTypes.length === 0)) {
|
|
447
481
|
const classes = BleInterface.deviceClasses.map(c => c.Class);
|
|
@@ -461,6 +495,7 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
461
495
|
if (device && device.getProfile && device.getProfile() !== d.getProfile())
|
|
462
496
|
return;
|
|
463
497
|
d.setInterface(this);
|
|
498
|
+
d.characteristics = characteristics;
|
|
464
499
|
if (scanForDevice) {
|
|
465
500
|
if ((device.id && device.id !== '' && d.id === device.id) ||
|
|
466
501
|
(device.address && device.address !== '' && d.address === device.address) ||
|
|
@@ -496,9 +531,7 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
496
531
|
}
|
|
497
532
|
});
|
|
498
533
|
}
|
|
499
|
-
|
|
500
|
-
}
|
|
501
|
-
};
|
|
534
|
+
});
|
|
502
535
|
this.logEvent({ message: `${opStr}: start scanning`, requested: scanForDevice ? { name: device.name, address: device.address } : undefined, timeout });
|
|
503
536
|
this.deviceCache.forEach(peripheral => {
|
|
504
537
|
onPeripheralFound(peripheral, true);
|
|
@@ -510,7 +543,6 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
510
543
|
return reject(err);
|
|
511
544
|
}
|
|
512
545
|
bleBinding.on('discover', (p) => {
|
|
513
|
-
console.log('~~~ discovered:', p.address, p.advertisement ? p.advertisement.localName : '');
|
|
514
546
|
onPeripheralFound(p);
|
|
515
547
|
});
|
|
516
548
|
});
|
package/lib/ble/fm.js
CHANGED
|
@@ -53,7 +53,7 @@ class BleFitnessMachineDevice extends ble_device_1.BleDevice {
|
|
|
53
53
|
offset += 2;
|
|
54
54
|
}
|
|
55
55
|
if (flags & IndoorBikeDataFlag.AverageSpeedPresent) {
|
|
56
|
-
this.data.averageSpeed = data.readUInt16LE(offset);
|
|
56
|
+
this.data.averageSpeed = data.readUInt16LE(offset) / 100;
|
|
57
57
|
offset += 2;
|
|
58
58
|
}
|
|
59
59
|
if (flags & IndoorBikeDataFlag.InstantaneousCadence) {
|
|
@@ -61,7 +61,7 @@ class BleFitnessMachineDevice extends ble_device_1.BleDevice {
|
|
|
61
61
|
offset += 2;
|
|
62
62
|
}
|
|
63
63
|
if (flags & IndoorBikeDataFlag.AverageCadencePresent) {
|
|
64
|
-
this.data.averageCadence = data.readUInt16LE(offset);
|
|
64
|
+
this.data.averageCadence = data.readUInt16LE(offset) / 2;
|
|
65
65
|
offset += 2;
|
|
66
66
|
}
|
|
67
67
|
if (flags & IndoorBikeDataFlag.TotalDistancePresent) {
|
|
@@ -89,7 +89,7 @@ class BleFitnessMachineDevice extends ble_device_1.BleDevice {
|
|
|
89
89
|
offset += 2;
|
|
90
90
|
}
|
|
91
91
|
if (flags & IndoorBikeDataFlag.MetabolicEquivalentPresent) {
|
|
92
|
-
this.data.metabolicEquivalent = data.readUInt16LE(offset);
|
|
92
|
+
this.data.metabolicEquivalent = data.readUInt16LE(offset) / 10;
|
|
93
93
|
offset += 2;
|
|
94
94
|
}
|
|
95
95
|
if (flags & IndoorBikeDataFlag.ElapsedTimePresent) {
|
|
@@ -140,7 +140,7 @@ class FmAdapter extends Device_1.default {
|
|
|
140
140
|
isHrm() { return false; }
|
|
141
141
|
isPower() { return true; }
|
|
142
142
|
getProfile() {
|
|
143
|
-
return '
|
|
143
|
+
return 'Smart Trainer';
|
|
144
144
|
}
|
|
145
145
|
getName() {
|
|
146
146
|
return `${this.device.name}`;
|
|
@@ -88,7 +88,7 @@ class BleProtocol extends DeviceProtocol_1.default {
|
|
|
88
88
|
}
|
|
89
89
|
});
|
|
90
90
|
this.logger.logEvent({ message: 'scan started' });
|
|
91
|
-
yield this.ble.scan({ deviceTypes: supportedDeviceTypes });
|
|
91
|
+
yield this.ble.scan({ deviceTypes: supportedDeviceTypes, timeout: 20000 });
|
|
92
92
|
if (props && props.onScanFinished) {
|
|
93
93
|
props.onScanFinished(props.id);
|
|
94
94
|
}
|