incyclist-devices 2.2.10-beta.0 → 2.3.0-beta.10
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/antv2/base/interface.js +2 -2
- package/lib/antv2/factories/index.d.ts +1 -0
- package/lib/antv2/factories/index.js +17 -0
- package/lib/antv2/fe/adapter.js +5 -4
- package/lib/antv2/index.d.ts +1 -0
- package/lib/antv2/index.js +12 -8
- package/lib/base/adpater.d.ts +4 -0
- package/lib/base/adpater.js +17 -3
- package/lib/ble/adapter-factory.d.ts +20 -24
- package/lib/ble/adapter-factory.js +13 -36
- package/lib/ble/base/adapter.d.ts +19 -5
- package/lib/ble/base/adapter.js +211 -62
- package/lib/ble/base/comms.d.ts +2 -74
- package/lib/ble/base/comms.js +3 -596
- package/lib/ble/base/interface.d.ts +102 -0
- package/lib/ble/base/interface.js +591 -0
- package/lib/ble/base/peripheral.d.ts +35 -0
- package/lib/ble/base/peripheral.js +311 -0
- package/lib/ble/base/sensor.d.ts +32 -0
- package/lib/ble/base/sensor.js +135 -0
- package/lib/ble/base/types.d.ts +7 -0
- package/lib/ble/base/types.js +7 -0
- package/lib/ble/bindings/mock.js +6 -0
- package/lib/ble/bindings/types.d.ts +3 -4
- package/lib/ble/ble-interface.d.ts +7 -4
- package/lib/ble/ble-interface.js +16 -2
- package/lib/ble/ble-peripheral.d.ts +1 -0
- package/lib/ble/ble-peripheral.js +7 -11
- package/lib/ble/consts.d.ts +0 -6
- package/lib/ble/consts.js +1 -7
- package/lib/ble/cp/adapter.d.ts +3 -3
- package/lib/ble/cp/adapter.js +12 -13
- package/lib/ble/cp/comm.d.ts +1 -1
- package/lib/ble/cp/comm.js +2 -2
- package/lib/ble/cp/index.d.ts +1 -1
- package/lib/ble/cp/index.js +2 -2
- package/lib/ble/cp/sensor.d.ts +27 -0
- package/lib/ble/cp/sensor.js +107 -0
- package/lib/ble/elite/comms.d.ts +1 -1
- package/lib/ble/elite/comms.js +2 -2
- package/lib/ble/factories/adapter-factory.d.ts +34 -0
- package/lib/ble/factories/adapter-factory.js +137 -0
- package/lib/ble/factories/index.d.ts +2 -0
- package/lib/ble/factories/index.js +18 -0
- package/lib/ble/factories/interface-factory.d.ts +7 -0
- package/lib/ble/factories/interface-factory.js +18 -0
- package/lib/ble/factories/types.d.ts +18 -0
- package/lib/ble/factories/types.js +2 -0
- package/lib/ble/fm/adapter.d.ts +12 -6
- package/lib/ble/fm/adapter.js +100 -108
- package/lib/ble/fm/comms.d.ts +1 -1
- package/lib/ble/fm/comms.js +3 -3
- package/lib/ble/fm/consts.d.ts +93 -0
- package/lib/ble/fm/consts.js +55 -1
- package/lib/ble/fm/index.d.ts +1 -1
- package/lib/ble/fm/index.js +2 -2
- package/lib/ble/fm/sensor.d.ts +44 -0
- package/lib/ble/fm/sensor.js +384 -0
- package/lib/ble/hr/adapter.d.ts +3 -3
- package/lib/ble/hr/adapter.js +5 -8
- package/lib/ble/hr/comm.d.ts +1 -1
- package/lib/ble/hr/comm.js +2 -2
- package/lib/ble/hr/index.d.ts +1 -1
- package/lib/ble/hr/index.js +2 -2
- package/lib/ble/hr/sensor.d.ts +17 -0
- package/lib/ble/hr/sensor.js +52 -0
- package/lib/ble/index.d.ts +4 -3
- package/lib/ble/index.js +29 -13
- package/lib/ble/tacx/adapter.d.ts +4 -2
- package/lib/ble/tacx/adapter.js +26 -66
- package/lib/ble/tacx/comms.d.ts +1 -1
- package/lib/ble/tacx/comms.js +2 -2
- package/lib/ble/tacx/consts.d.ts +23 -0
- package/lib/ble/tacx/consts.js +27 -0
- package/lib/ble/tacx/index.d.ts +1 -1
- package/lib/ble/tacx/index.js +2 -2
- package/lib/ble/tacx/sensor.d.ts +63 -0
- package/lib/ble/tacx/sensor.js +596 -0
- package/lib/ble/types.d.ts +87 -52
- package/lib/ble/utils.d.ts +7 -2
- package/lib/ble/utils.js +93 -9
- package/lib/ble/wahoo/adapter.d.ts +2 -2
- package/lib/ble/wahoo/adapter.js +4 -72
- package/lib/ble/wahoo/comms.d.ts +1 -1
- package/lib/ble/wahoo/comms.js +2 -2
- package/lib/ble/wahoo/consts.d.ts +16 -0
- package/lib/ble/wahoo/consts.js +7 -0
- package/lib/ble/wahoo/index.d.ts +1 -1
- package/lib/ble/wahoo/index.js +2 -2
- package/lib/ble/wahoo/sensor.d.ts +48 -0
- package/lib/ble/wahoo/sensor.js +356 -0
- package/lib/direct-connect/base/comms.d.ts +3 -0
- package/lib/direct-connect/base/comms.js +7 -0
- package/lib/direct-connect/base/interface.d.ts +75 -0
- package/lib/direct-connect/base/interface.js +311 -0
- package/lib/direct-connect/base/peripheral.d.ts +50 -0
- package/lib/direct-connect/base/peripheral.js +522 -0
- package/lib/direct-connect/base/sensor.d.ts +3 -0
- package/lib/direct-connect/base/sensor.js +7 -0
- package/lib/direct-connect/bindings/index.d.ts +1 -0
- package/lib/direct-connect/bindings/index.js +17 -0
- package/lib/direct-connect/bindings/types.d.ts +37 -0
- package/lib/direct-connect/bindings/types.js +2 -0
- package/lib/direct-connect/consts.d.ts +17 -0
- package/lib/direct-connect/consts.js +20 -0
- package/lib/direct-connect/index.d.ts +3 -0
- package/lib/direct-connect/index.js +22 -0
- package/lib/direct-connect/messages/CharacteristicNotification.d.ts +12 -0
- package/lib/direct-connect/messages/CharacteristicNotification.js +23 -0
- package/lib/direct-connect/messages/DiscoverCharacteristics.d.ts +22 -0
- package/lib/direct-connect/messages/DiscoverCharacteristics.js +43 -0
- package/lib/direct-connect/messages/DiscoverServices.d.ts +15 -0
- package/lib/direct-connect/messages/DiscoverServices.js +33 -0
- package/lib/direct-connect/messages/EnableCharacteristicNotifications.d.ts +18 -0
- package/lib/direct-connect/messages/EnableCharacteristicNotifications.js +35 -0
- package/lib/direct-connect/messages/ReadCharacteristic.d.ts +18 -0
- package/lib/direct-connect/messages/ReadCharacteristic.js +34 -0
- package/lib/direct-connect/messages/WriteCharacteristic.d.ts +18 -0
- package/lib/direct-connect/messages/WriteCharacteristic.js +36 -0
- package/lib/direct-connect/messages/error.d.ts +5 -0
- package/lib/direct-connect/messages/error.js +18 -0
- package/lib/direct-connect/messages/factory.d.ts +6 -0
- package/lib/direct-connect/messages/factory.js +44 -0
- package/lib/direct-connect/messages/index.d.ts +9 -0
- package/lib/direct-connect/messages/index.js +25 -0
- package/lib/direct-connect/messages/message.d.ts +21 -0
- package/lib/direct-connect/messages/message.js +105 -0
- package/lib/direct-connect/types.d.ts +24 -0
- package/lib/direct-connect/types.js +9 -0
- package/lib/direct-connect/utils.d.ts +5 -0
- package/lib/direct-connect/utils.js +73 -0
- package/lib/factories/adapters.js +14 -4
- package/lib/factories/interfaces.d.ts +2 -1
- package/lib/factories/interfaces.js +10 -7
- package/lib/index.d.ts +5 -4
- package/lib/index.js +6 -2
- package/lib/modes/antble-smarttrainer.d.ts +2 -16
- package/lib/modes/antble-smarttrainer.js +1 -98
- package/lib/modes/types.d.ts +1 -5
- package/lib/serial/bindings/tcp.d.ts +5 -4
- package/lib/serial/bindings/tcp.js +16 -23
- package/lib/types/adapter.d.ts +3 -0
- package/lib/types/device.d.ts +2 -1
- package/lib/types/device.js +1 -0
- package/lib/utils/calculations.d.ts +0 -1
- package/lib/utils/calculations.js +6 -22
- package/lib/utils/operation.d.ts +17 -0
- package/lib/utils/operation.js +20 -0
- package/lib/utils/task.d.ts +47 -0
- package/lib/utils/task.js +139 -0
- package/package.json +3 -1
package/lib/ble/base/adapter.js
CHANGED
|
@@ -13,24 +13,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const adpater_1 = __importDefault(require("../../base/adpater"));
|
|
16
|
-
const
|
|
17
|
-
const
|
|
16
|
+
const types_1 = require("../../types");
|
|
17
|
+
const interface_factory_1 = require("../factories/interface-factory");
|
|
18
|
+
const task_1 = require("../../utils/task");
|
|
19
|
+
const utils_1 = require("../../utils/utils");
|
|
18
20
|
class BleAdapter extends adpater_1.default {
|
|
19
21
|
constructor(settings, props) {
|
|
20
22
|
super(settings, props);
|
|
21
23
|
this.onDeviceDataHandler = this.onDeviceData.bind(this);
|
|
22
|
-
if (this.settings.interface !== INTERFACE_NAME)
|
|
23
|
-
throw new Error('Incorrect interface');
|
|
24
24
|
this.deviceData = {};
|
|
25
25
|
this.data = {};
|
|
26
26
|
this.dataMsgCount = 0;
|
|
27
27
|
this.updateFrequency = 1000;
|
|
28
|
-
this.ble = ble_interface_1.default.getInstance();
|
|
29
28
|
}
|
|
30
29
|
getUniqueName() {
|
|
31
30
|
var _a;
|
|
32
31
|
const settings = this.settings;
|
|
33
|
-
if (((_a = settings.name) === null || _a === void 0 ? void 0 : _a.match(/
|
|
32
|
+
if (((_a = settings.name) === null || _a === void 0 ? void 0 : _a.match(/\d/g)) || settings.address === undefined)
|
|
34
33
|
return this.getName();
|
|
35
34
|
else {
|
|
36
35
|
const addressHash = settings.address.substring(0, 2) + settings.address.slice(-2);
|
|
@@ -39,26 +38,29 @@ class BleAdapter extends adpater_1.default {
|
|
|
39
38
|
}
|
|
40
39
|
connect() {
|
|
41
40
|
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
41
|
+
const ble = this.getBle();
|
|
42
|
+
return yield ble.connect();
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
getPeripheral() {
|
|
46
|
+
const ble = this.getBle();
|
|
47
|
+
const p = ble === null || ble === void 0 ? void 0 : ble.createPeripheralFromSettings(this.settings);
|
|
48
|
+
return p;
|
|
49
|
+
}
|
|
50
|
+
waitForPeripheral() {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
this.logEvent({ message: 'waiting for sensor ...', device: this.getName(), interface: this.getInterface() });
|
|
53
|
+
const ble = this.getBle();
|
|
54
|
+
const peripheral = yield ble.waitForPeripheral(this.settings);
|
|
55
|
+
this.updateSensor(peripheral);
|
|
55
56
|
});
|
|
56
57
|
}
|
|
58
|
+
updateSensor(peripheral) {
|
|
59
|
+
throw new Error('method not implemented');
|
|
60
|
+
}
|
|
57
61
|
close() {
|
|
58
62
|
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
-
|
|
60
|
-
return true;
|
|
61
|
-
return yield this.device.disconnect();
|
|
63
|
+
return true;
|
|
62
64
|
});
|
|
63
65
|
}
|
|
64
66
|
getComms() {
|
|
@@ -81,16 +83,24 @@ class BleAdapter extends adpater_1.default {
|
|
|
81
83
|
return this.isEqual(adapter.getSettings());
|
|
82
84
|
}
|
|
83
85
|
isConnected() {
|
|
84
|
-
|
|
86
|
+
var _a;
|
|
87
|
+
return (_a = this.device) === null || _a === void 0 ? void 0 : _a.isConnected();
|
|
85
88
|
}
|
|
86
89
|
resetData() {
|
|
90
|
+
super.resetData();
|
|
87
91
|
this.dataMsgCount = 0;
|
|
88
92
|
this.deviceData = {};
|
|
89
93
|
this.data = {};
|
|
90
94
|
this.lastDataTS = undefined;
|
|
91
95
|
}
|
|
92
96
|
getInterface() {
|
|
93
|
-
|
|
97
|
+
const iface = this.settings.interface;
|
|
98
|
+
if (typeof iface === 'string')
|
|
99
|
+
return this.settings.interface;
|
|
100
|
+
else {
|
|
101
|
+
const i = this.settings.interface;
|
|
102
|
+
return i.getName();
|
|
103
|
+
}
|
|
94
104
|
}
|
|
95
105
|
getProfile() {
|
|
96
106
|
const C = this.constructor;
|
|
@@ -112,7 +122,7 @@ class BleAdapter extends adpater_1.default {
|
|
|
112
122
|
if (this.isStopped() || this.isPaused())
|
|
113
123
|
return;
|
|
114
124
|
try {
|
|
115
|
-
this.logEvent({ message: 'refreshDeviceData', data: this.deviceData, isControllable: this.isControllable() });
|
|
125
|
+
this.logEvent({ message: 'refreshDeviceData', device: this.getName(), interface: this.getInterface(), data: this.deviceData, isControllable: this.isControllable() });
|
|
116
126
|
if (this.isControllable()) {
|
|
117
127
|
const mappedData = this.mapData(this.deviceData);
|
|
118
128
|
const incyclistData = this.getCyclingMode().updateData(mappedData);
|
|
@@ -124,7 +134,7 @@ class BleAdapter extends adpater_1.default {
|
|
|
124
134
|
this.emitData(this.data);
|
|
125
135
|
}
|
|
126
136
|
catch (err) {
|
|
127
|
-
this.logEvent({ message: 'error', fn: 'refreshDeviceData', error: err.message, stack: err.stack });
|
|
137
|
+
this.logEvent({ message: 'error', fn: 'refreshDeviceData', device: this.getName(), interface: this.getInterface(), error: err.message, stack: err.stack });
|
|
128
138
|
}
|
|
129
139
|
}
|
|
130
140
|
onDeviceData(deviceData) {
|
|
@@ -132,9 +142,10 @@ class BleAdapter extends adpater_1.default {
|
|
|
132
142
|
this.dataMsgCount++;
|
|
133
143
|
this.lastDataTS = Date.now();
|
|
134
144
|
this.deviceData = Object.assign({}, deviceData);
|
|
135
|
-
if (!this.canEmitData())
|
|
145
|
+
if (!this.canEmitData()) {
|
|
136
146
|
return;
|
|
137
|
-
|
|
147
|
+
}
|
|
148
|
+
this.logEvent({ message: 'onDeviceData', device: this.getName(), interface: this.getInterface(), data: deviceData, isControllable: this.isControllable() });
|
|
138
149
|
if (this.isControllable()) {
|
|
139
150
|
const mappedData = this.mapData(deviceData);
|
|
140
151
|
const incyclistData = this.getCyclingMode().updateData(mappedData);
|
|
@@ -146,7 +157,7 @@ class BleAdapter extends adpater_1.default {
|
|
|
146
157
|
this.emitData(this.data);
|
|
147
158
|
}
|
|
148
159
|
catch (err) {
|
|
149
|
-
this.logEvent({ message: 'Error', fn: 'onDeviceData', error: err.message, stack: err.stack });
|
|
160
|
+
this.logEvent({ message: 'Error', fn: 'onDeviceData', device: this.getName(), interface: this.getInterface(), error: err.message, stack: err.stack });
|
|
150
161
|
}
|
|
151
162
|
}
|
|
152
163
|
mapData(deviceData) {
|
|
@@ -164,6 +175,9 @@ class BleAdapter extends adpater_1.default {
|
|
|
164
175
|
check() {
|
|
165
176
|
return this.start({ scanOnly: true });
|
|
166
177
|
}
|
|
178
|
+
getDefaultStartupTimeout() {
|
|
179
|
+
return 30000;
|
|
180
|
+
}
|
|
167
181
|
startPreChecks(props) {
|
|
168
182
|
return __awaiter(this, void 0, void 0, function* () {
|
|
169
183
|
const wasPaused = this.paused;
|
|
@@ -171,10 +185,7 @@ class BleAdapter extends adpater_1.default {
|
|
|
171
185
|
this.stopped = false;
|
|
172
186
|
if (wasPaused)
|
|
173
187
|
this.resume();
|
|
174
|
-
if (this.started && !
|
|
175
|
-
return 'done';
|
|
176
|
-
}
|
|
177
|
-
if (this.started && wasPaused) {
|
|
188
|
+
if (this.started && !wasStopped) {
|
|
178
189
|
return 'done';
|
|
179
190
|
}
|
|
180
191
|
const connected = yield this.connect();
|
|
@@ -185,45 +196,176 @@ class BleAdapter extends adpater_1.default {
|
|
|
185
196
|
}
|
|
186
197
|
start(startProps) {
|
|
187
198
|
return __awaiter(this, void 0, void 0, function* () {
|
|
188
|
-
|
|
199
|
+
if (this.isStarting()) {
|
|
200
|
+
yield this.stop();
|
|
201
|
+
}
|
|
202
|
+
const ble = this.getBle();
|
|
203
|
+
ble.once('disconnect-done', this.onDisconnectDone.bind(this));
|
|
204
|
+
this.startTask = new task_1.InteruptableTask(this.startAdapter(startProps), {
|
|
205
|
+
timeout: startProps === null || startProps === void 0 ? void 0 : startProps.timeout,
|
|
206
|
+
name: 'start',
|
|
207
|
+
errorOnTimeout: false,
|
|
208
|
+
log: this.logEvent.bind(this)
|
|
209
|
+
});
|
|
210
|
+
const res = yield this.startTask.run();
|
|
211
|
+
return res;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
isStarting() {
|
|
215
|
+
var _a;
|
|
216
|
+
return (_a = this.startTask) === null || _a === void 0 ? void 0 : _a.isRunning();
|
|
217
|
+
}
|
|
218
|
+
hasData() {
|
|
219
|
+
return this.dataMsgCount > 0;
|
|
220
|
+
}
|
|
221
|
+
waitForInitialData(startupTimeout) {
|
|
222
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
223
|
+
let waitTask;
|
|
224
|
+
let iv;
|
|
225
|
+
const wait = () => {
|
|
226
|
+
const res = new Promise((resolve) => {
|
|
227
|
+
iv = setInterval(() => {
|
|
228
|
+
if (this.hasData()) {
|
|
229
|
+
clearInterval(iv);
|
|
230
|
+
resolve(true);
|
|
231
|
+
}
|
|
232
|
+
else if (!this.isStarting() || !(waitTask === null || waitTask === void 0 ? void 0 : waitTask.isRunning)) {
|
|
233
|
+
resolve(false);
|
|
234
|
+
clearInterval(iv);
|
|
235
|
+
}
|
|
236
|
+
}, 10);
|
|
237
|
+
});
|
|
238
|
+
return res;
|
|
239
|
+
};
|
|
240
|
+
waitTask = new task_1.InteruptableTask(wait(), {
|
|
241
|
+
errorOnTimeout: false,
|
|
242
|
+
timeout: startupTimeout
|
|
243
|
+
});
|
|
244
|
+
this.logEvent({ message: 'wait for sensor data', device: this.getName(), interface: this.getInterface() });
|
|
245
|
+
const hasData = yield waitTask.run();
|
|
246
|
+
clearInterval(iv);
|
|
247
|
+
if (hasData)
|
|
248
|
+
this.logEvent({ message: 'sensor data received', device: this.getName(), interface: this.getInterface() });
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
checkCapabilities() {
|
|
252
|
+
}
|
|
253
|
+
initControl(_props) {
|
|
254
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
startAdapter(startProps) {
|
|
258
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
189
259
|
const props = this.getStartProps(startProps);
|
|
260
|
+
const { timeout = this.getDefaultStartupTimeout() } = startProps !== null && startProps !== void 0 ? startProps : {};
|
|
261
|
+
const wasPaused = this.paused;
|
|
190
262
|
const preCheckResult = yield this.startPreChecks(props);
|
|
191
|
-
if (preCheckResult === 'done')
|
|
263
|
+
if (preCheckResult === 'done') {
|
|
264
|
+
yield (0, utils_1.resolveNextTick)();
|
|
265
|
+
this.logEvent({ message: `start result: ${this.started ? 'success' : 'failed'}`, preCheckResult, device: this.getName(), interface: this.getInterface(), protocol: this.getProtocolName() });
|
|
192
266
|
return this.started;
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
267
|
+
}
|
|
268
|
+
if (preCheckResult === 'connection-failed') {
|
|
269
|
+
this.logEvent({ message: 'start result: error', error: 'could not start device, reason:could not connect', device: this.getName(), interface: this.getInterface(), protocol: this.getProtocolName() });
|
|
270
|
+
yield (0, utils_1.resolveNextTick)();
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
this.logEvent({ message: 'starting device', device: this.getName(), interface: this.getInterface(), props, isStarted: this.started });
|
|
196
274
|
try {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
this.
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
275
|
+
this.resetData();
|
|
276
|
+
this.stopped = false;
|
|
277
|
+
const connected = yield this.startSensor();
|
|
278
|
+
if (connected) {
|
|
279
|
+
this.logEvent({ message: 'peripheral connected', device: this.getName(), interface: this.getInterface(), props });
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
this.logEvent({ message: 'peripheral connection failed', device: this.getName(), interface: this.getInterface(), reason: 'unknown', props });
|
|
283
|
+
this.stopped = true;
|
|
284
|
+
return false;
|
|
205
285
|
}
|
|
286
|
+
yield this.waitForInitialData(timeout);
|
|
287
|
+
this.checkCapabilities();
|
|
288
|
+
if (this.hasCapability(types_1.IncyclistCapability.Control))
|
|
289
|
+
yield this.initControl(startProps);
|
|
290
|
+
this.stopped = false;
|
|
291
|
+
this.started = true;
|
|
292
|
+
if (wasPaused)
|
|
293
|
+
this.resume();
|
|
294
|
+
if (!this.isStarting()) {
|
|
295
|
+
this.started = false;
|
|
296
|
+
this.stopped = true;
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
this.logEvent({ message: 'start result: success', device: this.getName(), interface: this.getInterface(), protocol: this.getProtocolName() });
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
catch (err) {
|
|
303
|
+
this.logEvent({ message: 'start result: error', error: err.message, device: this.getName(), interface: this.getInterface(), protocol: this.getProtocolName() });
|
|
304
|
+
this.started = false;
|
|
305
|
+
this.stopped = true;
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
startSensor() {
|
|
311
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
312
|
+
var _a;
|
|
313
|
+
if (!((_a = this.getComms()) === null || _a === void 0 ? void 0 : _a.hasPeripheral())) {
|
|
314
|
+
yield this.waitForPeripheral();
|
|
315
|
+
}
|
|
316
|
+
if (!this.getComms()) {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
const sensor = this.getComms();
|
|
320
|
+
const connected = yield sensor.startSensor();
|
|
321
|
+
yield sensor.subscribe();
|
|
322
|
+
if (connected) {
|
|
323
|
+
sensor.on('data', this.onDeviceDataHandler);
|
|
324
|
+
sensor.on('disconnected', this.emit.bind(this));
|
|
325
|
+
sensor.on('error', console.log);
|
|
326
|
+
}
|
|
327
|
+
return connected;
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
onDisconnectDone() {
|
|
331
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
332
|
+
this.logEvent({ message: 'disconnecting device', device: this.getName(), interface: this.getInterface() });
|
|
333
|
+
if (this.isStarting()) {
|
|
334
|
+
yield this.startTask.stop();
|
|
335
|
+
}
|
|
336
|
+
let reason = 'unknown';
|
|
337
|
+
let stopped = false;
|
|
338
|
+
const sensor = this.getComms();
|
|
339
|
+
try {
|
|
340
|
+
stopped = yield sensor.stopSensor();
|
|
206
341
|
}
|
|
207
342
|
catch (err) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
343
|
+
reason = err.message;
|
|
344
|
+
}
|
|
345
|
+
if (!stopped) {
|
|
346
|
+
this.logEvent({ message: 'disconnecting device failed', device: this.getName(), interface: this.getInterface(), reason });
|
|
211
347
|
}
|
|
212
348
|
});
|
|
213
349
|
}
|
|
214
350
|
stop() {
|
|
215
351
|
return __awaiter(this, void 0, void 0, function* () {
|
|
216
|
-
this.
|
|
217
|
-
this.
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
this.
|
|
352
|
+
this.logEvent({ message: 'stopping device', device: this.getName(), interface: this.getInterface() });
|
|
353
|
+
if (this.isStarting()) {
|
|
354
|
+
yield this.startTask.stop();
|
|
355
|
+
}
|
|
356
|
+
this.started = false;
|
|
357
|
+
this.resetData();
|
|
358
|
+
if (!this.getComms()) {
|
|
359
|
+
this.logEvent({ message: 'device stopped - not started yet', device: this.getName(), interface: this.getInterface() });
|
|
224
360
|
return true;
|
|
225
361
|
}
|
|
226
|
-
|
|
362
|
+
const sensor = this.getComms();
|
|
363
|
+
sensor.reset();
|
|
364
|
+
this.resetData();
|
|
365
|
+
this.stopped = true;
|
|
366
|
+
this.started = false;
|
|
367
|
+
this.logEvent({ message: 'device stopped', device: this.getName(), interface: this.getInterface() });
|
|
368
|
+
return this.stopped;
|
|
227
369
|
});
|
|
228
370
|
}
|
|
229
371
|
pause() {
|
|
@@ -231,9 +373,9 @@ class BleAdapter extends adpater_1.default {
|
|
|
231
373
|
pause: { get: () => super.pause }
|
|
232
374
|
});
|
|
233
375
|
return __awaiter(this, void 0, void 0, function* () {
|
|
234
|
-
var _a;
|
|
235
376
|
const res = yield _super.pause.call(this);
|
|
236
|
-
|
|
377
|
+
const ble = this.getBle();
|
|
378
|
+
ble.pauseLogging();
|
|
237
379
|
return res;
|
|
238
380
|
});
|
|
239
381
|
}
|
|
@@ -242,17 +384,24 @@ class BleAdapter extends adpater_1.default {
|
|
|
242
384
|
resume: { get: () => super.resume }
|
|
243
385
|
});
|
|
244
386
|
return __awaiter(this, void 0, void 0, function* () {
|
|
245
|
-
|
|
387
|
+
const ble = this.getBle();
|
|
388
|
+
ble.resumeLogging();
|
|
246
389
|
const res = yield _super.resume.call(this);
|
|
247
|
-
(_a = this.getComms()) === null || _a === void 0 ? void 0 : _a.resume();
|
|
248
390
|
return res;
|
|
249
391
|
});
|
|
250
392
|
}
|
|
393
|
+
getBle() {
|
|
394
|
+
return interface_factory_1.BleMultiTransportInterfaceFactory.createInstance(this.getInterface());
|
|
395
|
+
}
|
|
251
396
|
update() {
|
|
252
397
|
}
|
|
253
398
|
setCyclingMode(mode, settings, sendInitCommands) {
|
|
254
399
|
super.setCyclingMode(mode, settings, sendInitCommands);
|
|
255
400
|
this.refreshDeviceData();
|
|
256
401
|
}
|
|
402
|
+
onScanStart() {
|
|
403
|
+
if (!this.isStarted())
|
|
404
|
+
this.start();
|
|
405
|
+
}
|
|
257
406
|
}
|
|
258
407
|
exports.default = BleAdapter;
|
package/lib/ble/base/comms.d.ts
CHANGED
|
@@ -1,75 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { LegacyProfile } from "../../antv2/types";
|
|
4
|
-
import BleInterface from "../ble-interface";
|
|
5
|
-
import BlePeripheralConnector from "../ble-peripheral";
|
|
6
|
-
import { BleCharacteristic, BleCommsConnectProps, BleDeviceConstructProps, BleDeviceInfo, BleDeviceSettings, BlePeripheral, BleProtocol, BleWriteProps, ConnectState, IBlePeripheralConnector } from "../types";
|
|
7
|
-
type CommandQueueItem = {
|
|
8
|
-
uuid: string;
|
|
9
|
-
data: Buffer;
|
|
10
|
-
timeout: number;
|
|
11
|
-
resolve: any;
|
|
12
|
-
reject: any;
|
|
13
|
-
};
|
|
14
|
-
export interface MessageLog {
|
|
15
|
-
uuid: string;
|
|
16
|
-
timestamp: any;
|
|
17
|
-
data: string;
|
|
1
|
+
import { IBleSensor } from "../types";
|
|
2
|
+
export declare class BleSensor implements IBleSensor {
|
|
18
3
|
}
|
|
19
|
-
export declare class BleComms extends EventEmitter {
|
|
20
|
-
static services: string[];
|
|
21
|
-
static protocol: BleProtocol;
|
|
22
|
-
id: string;
|
|
23
|
-
paused: boolean;
|
|
24
|
-
address: string;
|
|
25
|
-
name: string;
|
|
26
|
-
services: string[];
|
|
27
|
-
ble: BleInterface;
|
|
28
|
-
peripheral?: BlePeripheral;
|
|
29
|
-
characteristics: BleCharacteristic[];
|
|
30
|
-
state?: string;
|
|
31
|
-
logger?: EventLogger;
|
|
32
|
-
deviceInfo: BleDeviceInfo;
|
|
33
|
-
isInitialized: boolean;
|
|
34
|
-
subscribedCharacteristics: string[];
|
|
35
|
-
writeQueue: CommandQueueItem[];
|
|
36
|
-
workerIv: NodeJS.Timeout;
|
|
37
|
-
prevMessages: MessageLog[];
|
|
38
|
-
connectState: ConnectState;
|
|
39
|
-
constructor(props?: BleDeviceConstructProps);
|
|
40
|
-
getConnectState(): ConnectState;
|
|
41
|
-
isConnected(): boolean;
|
|
42
|
-
pause(): void;
|
|
43
|
-
resume(): void;
|
|
44
|
-
getServiceUUids(): string[];
|
|
45
|
-
getProfile(): LegacyProfile;
|
|
46
|
-
getProtocol(): BleProtocol;
|
|
47
|
-
getSettings(): BleDeviceSettings;
|
|
48
|
-
getServices(): string[];
|
|
49
|
-
logEvent(event: any): void;
|
|
50
|
-
setLogger(logger: EventLogger): void;
|
|
51
|
-
setInterface(ble: BleInterface): void;
|
|
52
|
-
static isMatching(characteristics: string[]): boolean;
|
|
53
|
-
reset(): void;
|
|
54
|
-
cleanupListeners(): void;
|
|
55
|
-
onDisconnect(): Promise<void>;
|
|
56
|
-
waitForConnectFinished(timeout: any): Promise<unknown>;
|
|
57
|
-
hasService(serviceUuid: any): boolean;
|
|
58
|
-
init(): Promise<boolean>;
|
|
59
|
-
initDevice(): Promise<boolean>;
|
|
60
|
-
connectPeripheral(peripheral: BlePeripheral): Promise<boolean>;
|
|
61
|
-
subscribeMultiple(characteristics: string[], conn?: IBlePeripheralConnector): Promise<void>;
|
|
62
|
-
subscribeAll(conn?: BlePeripheralConnector): Promise<void>;
|
|
63
|
-
unsubscribeAll(conn?: BlePeripheralConnector): void;
|
|
64
|
-
connect(props?: BleCommsConnectProps): Promise<boolean>;
|
|
65
|
-
disconnect(): Promise<boolean>;
|
|
66
|
-
checkForDuplicate(characteristic: string, data: Buffer): boolean;
|
|
67
|
-
onData(characteristic: string, _data: Buffer): boolean;
|
|
68
|
-
timeoutCheck(): void;
|
|
69
|
-
startWorker(): void;
|
|
70
|
-
stopWorker(): void;
|
|
71
|
-
write(characteristicUuid: string, data: Buffer, props?: BleWriteProps): Promise<ArrayBuffer>;
|
|
72
|
-
read(characteristicUuid: string): Promise<Uint8Array>;
|
|
73
|
-
getDeviceInfo(): Promise<BleDeviceInfo>;
|
|
74
|
-
}
|
|
75
|
-
export {};
|