incyclist-devices 1.4.33 → 1.4.36
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 +92 -40
- package/lib/ble/ble.d.ts +1 -0
- package/lib/ble/hrm.d.ts +1 -1
- package/lib/ble/hrm.js +9 -9
- package/lib/ble/pwr.d.ts +2 -2
- package/lib/ble/pwr.js +5 -4
- package/lib/modes/power-meter.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,49 @@ 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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
87
|
+
if (binding) {
|
|
88
|
+
const binding_init_original = binding.init.bind(binding);
|
|
89
|
+
const self = this;
|
|
90
|
+
binding.on('error', (err) => { this.getBinding().emit('error', err); });
|
|
91
|
+
binding.init = function () {
|
|
92
|
+
try {
|
|
93
|
+
binding_init_original();
|
|
94
|
+
self.connectState.isInitSuccess = true;
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
self.connectState.isInitSuccess = false;
|
|
98
|
+
self.connectState.isConnected = false;
|
|
99
|
+
self.connectState.isConnecting = false;
|
|
100
|
+
self.logEvent({ message: 'connect result: error', error: err.message });
|
|
101
|
+
return reject(new Error(err.message));
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const state = this.getBinding().state;
|
|
109
|
+
if (state === ble_1.BleState.POWERED_ON) {
|
|
110
|
+
clearTimeout(this.connectState.timeout);
|
|
111
|
+
this.connectState.timeout = null;
|
|
112
|
+
this.getBinding().removeAllListeners('stateChange');
|
|
113
|
+
this.getBinding().on('stateChange', this.onStateChange.bind(this));
|
|
114
|
+
this.connectState.isConnected = true;
|
|
115
|
+
this.connectState.isConnecting = false;
|
|
116
|
+
this.logEvent({ message: 'connect result: success' });
|
|
117
|
+
return resolve(true);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
101
120
|
this.getBinding().once('error', (err) => {
|
|
102
121
|
this.connectState.isConnected = true;
|
|
103
122
|
this.connectState.isConnecting = false;
|
|
@@ -116,17 +135,20 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
116
135
|
this.logEvent({ message: 'connect result: success' });
|
|
117
136
|
return resolve(true);
|
|
118
137
|
}
|
|
138
|
+
else {
|
|
139
|
+
this.logEvent({ message: 'BLE state change', state });
|
|
140
|
+
}
|
|
119
141
|
});
|
|
120
142
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
this.connectState.timeout
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
this.connectState.isConnected = false;
|
|
146
|
+
this.connectState.isConnecting = false;
|
|
147
|
+
if (this.connectState.timeout)
|
|
148
|
+
clearTimeout(this.connectState.timeout);
|
|
149
|
+
this.connectState.timeout = null;
|
|
150
|
+
this.logEvent({ message: 'connect result: error', error: err.message });
|
|
151
|
+
return reject(new Error('bluetooth unavailable, cause: ' + err.message));
|
|
130
152
|
}
|
|
131
153
|
});
|
|
132
154
|
}
|
|
@@ -209,8 +231,33 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
209
231
|
}
|
|
210
232
|
connectDevice(requested, timeout = 2000) {
|
|
211
233
|
return __awaiter(this, void 0, void 0, function* () {
|
|
212
|
-
const
|
|
213
|
-
|
|
234
|
+
const { id, name, address } = requested;
|
|
235
|
+
this.logEvent({ message: 'connectDevice', id, name, address });
|
|
236
|
+
let devices = [];
|
|
237
|
+
let retry = false;
|
|
238
|
+
let retryCount = 0;
|
|
239
|
+
do {
|
|
240
|
+
if (retryCount > 0) {
|
|
241
|
+
this.logEvent({ message: 'retry connect device', retryCount });
|
|
242
|
+
}
|
|
243
|
+
try {
|
|
244
|
+
devices = yield this.scan({ timeout, device: requested });
|
|
245
|
+
if (devices.length === 0) {
|
|
246
|
+
retryCount++;
|
|
247
|
+
retry = retryCount < 5;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
catch (err) {
|
|
251
|
+
if (err.message === 'scanning already in progress') {
|
|
252
|
+
this.logEvent({ message: 'scan busy' });
|
|
253
|
+
yield utils_1.sleep(1000);
|
|
254
|
+
retryCount++;
|
|
255
|
+
retry = retryCount < 5;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
throw err;
|
|
259
|
+
}
|
|
260
|
+
} while (devices.length === 0 && retry);
|
|
214
261
|
if (devices.length === 0)
|
|
215
262
|
throw new Error('device not found');
|
|
216
263
|
if (devices[0]) {
|
|
@@ -237,8 +284,10 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
237
284
|
}
|
|
238
285
|
const detectedPeripherals = {};
|
|
239
286
|
this.devices = [];
|
|
240
|
-
if (scanForDevice)
|
|
241
|
-
|
|
287
|
+
if (scanForDevice) {
|
|
288
|
+
const { id, address, name } = device;
|
|
289
|
+
this.logEvent({ message: 'search device request', device: { id, address, name }, deviceTypes });
|
|
290
|
+
}
|
|
242
291
|
else
|
|
243
292
|
this.logEvent({ message: 'scan start', services });
|
|
244
293
|
return new Promise((resolve, reject) => {
|
|
@@ -295,9 +344,12 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
295
344
|
this.scanState.timeout = null;
|
|
296
345
|
bleBinding.stopScanning(() => {
|
|
297
346
|
this.scanState.isScanning = false;
|
|
347
|
+
resolve([d]);
|
|
298
348
|
});
|
|
299
349
|
}
|
|
300
|
-
|
|
350
|
+
else {
|
|
351
|
+
resolve([d]);
|
|
352
|
+
}
|
|
301
353
|
}
|
|
302
354
|
});
|
|
303
355
|
}
|
|
@@ -306,9 +358,9 @@ class BleInterface extends ble_1.BleInterfaceClass {
|
|
|
306
358
|
this.scanState.timeout = setTimeout(() => {
|
|
307
359
|
this.scanState.timeout = null;
|
|
308
360
|
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}` : '') });
|
|
309
|
-
resolve(this.devices.map(i => i.device));
|
|
310
361
|
bleBinding.stopScanning(() => {
|
|
311
362
|
this.scanState.isScanning = false;
|
|
363
|
+
resolve(this.devices.map(i => i.device));
|
|
312
364
|
});
|
|
313
365
|
}, timeout);
|
|
314
366
|
});
|
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.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export default class BleHrmDevice extends BleDevice {
|
|
|
19
19
|
constructor(props?: any);
|
|
20
20
|
getProfile(): string;
|
|
21
21
|
getServiceUUids(): string[];
|
|
22
|
-
parseHrm(
|
|
22
|
+
parseHrm(_data: Uint8Array): HrmData;
|
|
23
23
|
onData(characteristic: string, data: Buffer): void;
|
|
24
24
|
write(characteristic: any, data: any): Promise<boolean>;
|
|
25
25
|
read(characteristic: any): Promise<Buffer>;
|
package/lib/ble/hrm.js
CHANGED
|
@@ -28,20 +28,20 @@ class BleHrmDevice extends ble_device_1.BleDevice {
|
|
|
28
28
|
getServiceUUids() {
|
|
29
29
|
return BleHrmDevice.services;
|
|
30
30
|
}
|
|
31
|
-
parseHrm(
|
|
31
|
+
parseHrm(_data) {
|
|
32
|
+
const data = Buffer.from(_data);
|
|
32
33
|
try {
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
let offset = 1;
|
|
34
|
+
const flags = data.readUInt8(0);
|
|
35
|
+
let offset = 2;
|
|
36
36
|
if (flags % 1 === 0) {
|
|
37
|
-
this.heartrate =
|
|
37
|
+
this.heartrate = data.readUInt8(1);
|
|
38
38
|
}
|
|
39
39
|
else {
|
|
40
|
-
this.heartrate =
|
|
41
|
-
offset =
|
|
40
|
+
this.heartrate = data.readUInt16LE(1);
|
|
41
|
+
offset = 3;
|
|
42
42
|
}
|
|
43
43
|
if (flags % 0xF) {
|
|
44
|
-
this.rr = (
|
|
44
|
+
this.rr = (data.readUInt16LE(offset)) / 1024;
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
catch (err) {
|
|
@@ -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.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ declare type PowerData = {
|
|
|
14
14
|
accTorque?: number;
|
|
15
15
|
time: number;
|
|
16
16
|
rpm: number;
|
|
17
|
-
raw?:
|
|
17
|
+
raw?: string;
|
|
18
18
|
};
|
|
19
19
|
declare type CrankData = {
|
|
20
20
|
revolutions?: number;
|
|
@@ -39,7 +39,7 @@ export default class BleCyclingPowerDevice extends BleDevice {
|
|
|
39
39
|
rpm: number;
|
|
40
40
|
time: any;
|
|
41
41
|
};
|
|
42
|
-
parsePower(
|
|
42
|
+
parsePower(_data: Uint8Array): PowerData;
|
|
43
43
|
onData(characteristic: string, data: Buffer): void;
|
|
44
44
|
write(characteristic: any, data: any): Promise<boolean>;
|
|
45
45
|
read(characteristic: any): Promise<Buffer>;
|
package/lib/ble/pwr.js
CHANGED
|
@@ -66,7 +66,8 @@ class BleCyclingPowerDevice extends ble_device_1.BleDevice {
|
|
|
66
66
|
this.prevCrankData.cntUpdateMissing = cntUpdateMissing + 1;
|
|
67
67
|
return { rpm, time: this.timeOffset + c.time };
|
|
68
68
|
}
|
|
69
|
-
parsePower(
|
|
69
|
+
parsePower(_data) {
|
|
70
|
+
const data = Buffer.from(_data);
|
|
70
71
|
try {
|
|
71
72
|
let offset = 4;
|
|
72
73
|
const flags = data.readUInt16LE(0);
|
|
@@ -91,7 +92,7 @@ class BleCyclingPowerDevice extends ble_device_1.BleDevice {
|
|
|
91
92
|
catch (err) {
|
|
92
93
|
}
|
|
93
94
|
const { instantaneousPower, balance, accTorque, rpm, time } = this;
|
|
94
|
-
return { instantaneousPower, balance, accTorque, rpm, time, raw: data };
|
|
95
|
+
return { instantaneousPower, balance, accTorque, rpm, time, raw: data.toString('hex') };
|
|
95
96
|
}
|
|
96
97
|
onData(characteristic, data) {
|
|
97
98
|
if (characteristic.toLocaleLowerCase() === '2a63') {
|
|
@@ -176,7 +177,7 @@ class PwrAdapter extends Device_1.default {
|
|
|
176
177
|
const data = {
|
|
177
178
|
isPedalling: false,
|
|
178
179
|
power: 0,
|
|
179
|
-
pedalRpm:
|
|
180
|
+
pedalRpm: undefined,
|
|
180
181
|
speed: 0,
|
|
181
182
|
heartrate: 0,
|
|
182
183
|
distanceInternal: 0,
|
|
@@ -226,7 +227,7 @@ class PwrAdapter extends Device_1.default {
|
|
|
226
227
|
}
|
|
227
228
|
catch (err) {
|
|
228
229
|
this.logger.logEvent({ message: 'start result: error', error: err.message });
|
|
229
|
-
|
|
230
|
+
throw new Error(`could not start device, reason:${err.message}`);
|
|
230
231
|
}
|
|
231
232
|
});
|
|
232
233
|
}
|
package/lib/modes/power-meter.js
CHANGED
|
@@ -47,7 +47,7 @@ class PowerMeterCyclingMode extends power_base_1.default {
|
|
|
47
47
|
let power = bikeData.power || 0;
|
|
48
48
|
const slope = prevData.slope || 0;
|
|
49
49
|
const distanceInternal = prevData.distanceInternal || 0;
|
|
50
|
-
if (
|
|
50
|
+
if (bikeData.pedalRpm === 0 || bikeData.isPedalling === false) {
|
|
51
51
|
power = 0;
|
|
52
52
|
}
|
|
53
53
|
const m = this.getWeight();
|