incyclist-devices 2.1.21 → 2.1.22
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,10 +1,15 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import EventEmitter from "events";
|
|
3
3
|
import { EventLogger } from "gd-eventlog";
|
|
4
|
-
import { IAntDevice, IChannel, ISensor } from "incyclist-ant-plus";
|
|
4
|
+
import { Channel, IAntDevice, IChannel, ISensor } from "incyclist-ant-plus";
|
|
5
5
|
import { AntDeviceSettings, AntScanProps, AntInterfaceProps } from "../types";
|
|
6
6
|
import { IncyclistInterface } from "../../types";
|
|
7
7
|
import AntDeviceBinding from "./binding";
|
|
8
|
+
type ChannelUsage = 'scan' | 'sensor';
|
|
9
|
+
interface ChannelInfo {
|
|
10
|
+
channel: Channel;
|
|
11
|
+
usage: ChannelUsage;
|
|
12
|
+
}
|
|
8
13
|
export default class AntInterface extends EventEmitter implements IncyclistInterface {
|
|
9
14
|
static _instance: AntInterface;
|
|
10
15
|
static INTERFACE_NAME: string;
|
|
@@ -22,6 +27,7 @@ export default class AntInterface extends EventEmitter implements IncyclistInter
|
|
|
22
27
|
};
|
|
23
28
|
protected props: AntInterfaceProps;
|
|
24
29
|
protected logEnabled: boolean;
|
|
30
|
+
protected channelsInUse: Array<ChannelInfo>;
|
|
25
31
|
constructor(props: AntInterfaceProps);
|
|
26
32
|
getName(): string;
|
|
27
33
|
getBinding(): typeof AntDeviceBinding;
|
|
@@ -42,5 +48,8 @@ export default class AntInterface extends EventEmitter implements IncyclistInter
|
|
|
42
48
|
isScanning(): boolean;
|
|
43
49
|
stopScan(): Promise<boolean>;
|
|
44
50
|
startSensor(sensor: ISensor, onDeviceData: (data: any) => void): Promise<boolean>;
|
|
51
|
+
private blockChannel;
|
|
52
|
+
private unblockChannel;
|
|
45
53
|
stopSensor(sensor: ISensor): Promise<boolean>;
|
|
46
54
|
}
|
|
55
|
+
export {};
|
|
@@ -31,6 +31,7 @@ class AntInterface extends events_1.default {
|
|
|
31
31
|
this.device = undefined;
|
|
32
32
|
this.connected = false;
|
|
33
33
|
this.connectPromise = null;
|
|
34
|
+
this.channelsInUse = [];
|
|
34
35
|
this.logEnabled = props.log || true;
|
|
35
36
|
const { binding, logger } = props;
|
|
36
37
|
this.setLogger(logger || new gd_eventlog_1.EventLogger('Ant+'));
|
|
@@ -106,16 +107,32 @@ class AntInterface extends events_1.default {
|
|
|
106
107
|
return __awaiter(this, void 0, void 0, function* () {
|
|
107
108
|
this.logEvent({ message: 'ANT+ disconnecting ...' });
|
|
108
109
|
let closed = false;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
110
|
+
try {
|
|
111
|
+
let promises = [];
|
|
112
|
+
if (this.channelsInUse.length > 0) {
|
|
113
|
+
this.channelsInUse.forEach(c => {
|
|
114
|
+
if (c.usage === 'scan')
|
|
115
|
+
promises.push(this.stopScan());
|
|
116
|
+
else
|
|
117
|
+
promises.push(c.channel.stopAllSensors());
|
|
118
|
+
});
|
|
119
|
+
yield Promise.allSettled(promises);
|
|
120
|
+
yield (0, utils_1.sleep)(200);
|
|
121
|
+
}
|
|
122
|
+
if (this.device) {
|
|
123
|
+
try {
|
|
124
|
+
closed = yield this.device.close();
|
|
125
|
+
}
|
|
126
|
+
catch (_a) {
|
|
127
|
+
closed = false;
|
|
128
|
+
}
|
|
112
129
|
}
|
|
113
|
-
|
|
114
|
-
closed =
|
|
130
|
+
else {
|
|
131
|
+
closed = true;
|
|
115
132
|
}
|
|
116
133
|
}
|
|
117
|
-
|
|
118
|
-
|
|
134
|
+
catch (err) {
|
|
135
|
+
this.logEvent({ message: 'Error', fn: '', error: err.message, stack: err.stack });
|
|
119
136
|
}
|
|
120
137
|
this.logEvent({ message: 'ANT+ disconnected' });
|
|
121
138
|
this.connectPromise = null;
|
|
@@ -205,10 +222,12 @@ class AntInterface extends events_1.default {
|
|
|
205
222
|
addListeners(channel);
|
|
206
223
|
try {
|
|
207
224
|
const success = yield channel.startScanner();
|
|
225
|
+
this.blockChannel(channel, 'scan');
|
|
208
226
|
this.logEvent({ message: 'scan started', success });
|
|
209
227
|
}
|
|
210
228
|
catch (err) {
|
|
211
229
|
this.logEvent({ message: 'scan could not be started', error: err.message, stack: err.stack });
|
|
230
|
+
this.unblockChannel(channel);
|
|
212
231
|
removeListeners(channel);
|
|
213
232
|
return done(detected);
|
|
214
233
|
}
|
|
@@ -261,10 +280,12 @@ class AntInterface extends events_1.default {
|
|
|
261
280
|
this.logEvent({ message: 'stopping scan done ..' });
|
|
262
281
|
return true;
|
|
263
282
|
}
|
|
283
|
+
const channel = this.activeScan.channel;
|
|
264
284
|
return new Promise(done => {
|
|
265
285
|
this.activeScan.emitter.emit('stop');
|
|
266
286
|
this.once('scan stopped', (res) => {
|
|
267
287
|
this.logEvent({ message: 'stopping scan done ..' });
|
|
288
|
+
this.unblockChannel(channel);
|
|
268
289
|
done(res);
|
|
269
290
|
});
|
|
270
291
|
});
|
|
@@ -281,6 +302,7 @@ class AntInterface extends events_1.default {
|
|
|
281
302
|
channel = this.device.getChannel();
|
|
282
303
|
if (!channel)
|
|
283
304
|
return false;
|
|
305
|
+
this.blockChannel(channel, 'sensor');
|
|
284
306
|
channel.setProps({ logger: this.logger });
|
|
285
307
|
const onData = (profile, deviceID, data, tag) => {
|
|
286
308
|
if (profile === sensor.getProfile() && deviceID === sensor.getDeviceID())
|
|
@@ -304,10 +326,19 @@ class AntInterface extends events_1.default {
|
|
|
304
326
|
yield channel.stopSensor(sensor);
|
|
305
327
|
}
|
|
306
328
|
catch (_a) { }
|
|
329
|
+
this.unblockChannel(channel);
|
|
307
330
|
return false;
|
|
308
331
|
}
|
|
309
332
|
});
|
|
310
333
|
}
|
|
334
|
+
blockChannel(channel, usage) {
|
|
335
|
+
this.channelsInUse.push({ channel: channel, usage });
|
|
336
|
+
}
|
|
337
|
+
unblockChannel(channel) {
|
|
338
|
+
const idx = this.channelsInUse.findIndex(c => { var _a; return ((_a = c.channel) === null || _a === void 0 ? void 0 : _a.getChannelNo()) === channel.getChannelNo(); });
|
|
339
|
+
if (idx !== -1)
|
|
340
|
+
this.channelsInUse.splice(idx, 1);
|
|
341
|
+
}
|
|
311
342
|
stopSensor(sensor) {
|
|
312
343
|
return __awaiter(this, void 0, void 0, function* () {
|
|
313
344
|
if (!this.isConnected()) {
|
|
@@ -328,6 +359,7 @@ class AntInterface extends events_1.default {
|
|
|
328
359
|
channel.flush();
|
|
329
360
|
channel.removeAllListeners('data');
|
|
330
361
|
const stopped = yield channel.stopSensor(sensor);
|
|
362
|
+
this.unblockChannel(channel);
|
|
331
363
|
return stopped;
|
|
332
364
|
}
|
|
333
365
|
catch (err) {
|