incyclist-devices 2.3.0-beta.9 → 2.3.1
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/README.MD +55 -0
- package/lib/antv2/base/adapter.d.ts +2 -2
- package/lib/base/adpater.d.ts +3 -0
- package/lib/base/adpater.js +13 -2
- package/lib/ble/adapter-factory.d.ts +24 -20
- package/lib/ble/adapter-factory.js +36 -13
- package/lib/ble/base/adapter.d.ts +4 -2
- package/lib/ble/base/adapter.js +43 -24
- package/lib/ble/base/comms.d.ts +74 -2
- package/lib/ble/base/comms.js +596 -3
- package/lib/ble/base/interface.d.ts +9 -5
- package/lib/ble/base/interface.js +77 -40
- package/lib/ble/base/peripheral.d.ts +7 -3
- package/lib/ble/base/peripheral.js +76 -22
- package/lib/ble/base/sensor.d.ts +1 -1
- package/lib/ble/base/sensor.js +17 -3
- package/lib/ble/ble-interface.d.ts +4 -7
- package/lib/ble/ble-interface.js +2 -16
- package/lib/ble/ble-peripheral.d.ts +0 -1
- package/lib/ble/ble-peripheral.js +11 -7
- package/lib/ble/characteristics/csc/features.d.ts +10 -0
- package/lib/ble/characteristics/csc/features.js +19 -0
- package/lib/ble/characteristics/csc/measurement.d.ts +33 -0
- package/lib/ble/characteristics/csc/measurement.js +109 -0
- package/lib/ble/characteristics/types.d.ts +6 -0
- package/lib/ble/characteristics/types.js +2 -0
- package/lib/ble/consts.d.ts +1 -0
- package/lib/ble/consts.js +2 -1
- package/lib/ble/cp/comm.d.ts +1 -1
- package/lib/ble/cp/comm.js +2 -2
- package/lib/ble/csc/adapter.d.ts +17 -0
- package/lib/ble/csc/adapter.js +66 -0
- package/lib/ble/csc/index.d.ts +3 -0
- package/lib/ble/csc/index.js +19 -0
- package/lib/ble/csc/sensor.d.ts +21 -0
- package/lib/ble/csc/sensor.js +64 -0
- package/lib/ble/csc/types.d.ts +6 -0
- package/lib/ble/csc/types.js +2 -0
- package/lib/ble/elite/comms.d.ts +1 -1
- package/lib/ble/elite/comms.js +2 -2
- package/lib/ble/fm/adapter.d.ts +1 -1
- package/lib/ble/fm/adapter.js +32 -23
- package/lib/ble/fm/comms.d.ts +1 -1
- package/lib/ble/fm/comms.js +3 -3
- package/lib/ble/fm/sensor.d.ts +1 -1
- package/lib/ble/fm/sensor.js +6 -5
- package/lib/ble/hr/comm.d.ts +1 -1
- package/lib/ble/hr/comm.js +2 -2
- package/lib/ble/index.js +2 -0
- package/lib/ble/tacx/adapter.d.ts +1 -1
- package/lib/ble/tacx/adapter.js +12 -10
- package/lib/ble/tacx/comms.d.ts +1 -1
- package/lib/ble/tacx/comms.js +2 -2
- package/lib/ble/tacx/sensor.js +9 -3
- package/lib/ble/types.d.ts +8 -2
- package/lib/ble/utils.d.ts +1 -0
- package/lib/ble/utils.js +5 -1
- package/lib/ble/wahoo/adapter.d.ts +1 -0
- package/lib/ble/wahoo/adapter.js +14 -0
- package/lib/ble/wahoo/comms.d.ts +1 -1
- package/lib/ble/wahoo/comms.js +2 -2
- package/lib/ble/wahoo/sensor.js +3 -6
- package/lib/direct-connect/base/interface.d.ts +1 -0
- package/lib/direct-connect/base/interface.js +8 -3
- package/lib/direct-connect/base/peripheral.d.ts +3 -3
- package/lib/direct-connect/base/peripheral.js +11 -7
- package/lib/modes/ant-fe-adv-st-mode.d.ts +7 -1
- package/lib/modes/ant-fe-adv-st-mode.js +4 -3
- package/lib/types/adapter.d.ts +2 -0
- package/lib/types/interface.d.ts +1 -0
- package/package.json +1 -1
package/README.MD
CHANGED
|
@@ -19,6 +19,11 @@ __BLE__
|
|
|
19
19
|
- Wahoo Smart Trainers (Wahoo specific service)
|
|
20
20
|
- Tacx FE-C over BLE
|
|
21
21
|
|
|
22
|
+
__Direct Connect (Wifi)__
|
|
23
|
+
- Smart Trainers (FTMS)
|
|
24
|
+
- Power Meters (CP)
|
|
25
|
+
- Heartrate Monitors (HR)
|
|
26
|
+
|
|
22
27
|
__Serial__
|
|
23
28
|
- Daum Classic Ergo Bikes
|
|
24
29
|
- Daum Premium Ergo Bikes (also over TCP/IP)
|
|
@@ -41,6 +46,7 @@ As this library supports various OS( Linux, Windows, Mac) and Incyclist is based
|
|
|
41
46
|
- Ant: specified by the [incyclist-ant-plus](https://github.com/incyclist/ant-plus) library
|
|
42
47
|
- Serial: specified by the [serialport](https://serialport.io/) library
|
|
43
48
|
- BLE: specified by the [noble](https://github.com/noble/noble) library
|
|
49
|
+
- Wifi: a combination of Multicast DNS as provided by the [Bonjour](https://github.com/onlxltd/bonjour-service) library and createSocket() wich creates a Socket class from NodeJS net module
|
|
44
50
|
|
|
45
51
|
__Ant Example__
|
|
46
52
|
|
|
@@ -76,6 +82,55 @@ const logger = new EventLogger('BLESample')
|
|
|
76
82
|
const ble = InterfaceFactory.create('ble',{logger, log:true, binding:noble})
|
|
77
83
|
```
|
|
78
84
|
|
|
85
|
+
__Direct Connect Example__
|
|
86
|
+
```
|
|
87
|
+
const { Bonjour } = require('bonjour-service')
|
|
88
|
+
const net = require('net');
|
|
89
|
+
|
|
90
|
+
const createBinding = ()=>{
|
|
91
|
+
return {
|
|
92
|
+
mdns: new MDNSBinding(),
|
|
93
|
+
net: {
|
|
94
|
+
createSocket: ()=>new net.Socket()
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
class MDNSBinding {
|
|
100
|
+
|
|
101
|
+
connect() {
|
|
102
|
+
this.bonjour = new Bonjour()
|
|
103
|
+
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
disconnect() {
|
|
107
|
+
if (this.bonjour) {
|
|
108
|
+
this.bonjour.destroy()
|
|
109
|
+
this.bonjour = null
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
find(opts , onUp) {
|
|
114
|
+
this.bonjour.find(opts, (s)=>{
|
|
115
|
+
this.handleAnnouncement(s,onUp)
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
handleAnnouncement(service,callback) {
|
|
120
|
+
const {name,txt,port,referer,protocol} = service
|
|
121
|
+
const announcement = {
|
|
122
|
+
name,address:referer?.address,protocol,port,
|
|
123
|
+
serialNo:txt?.['serial-number'],
|
|
124
|
+
serviceUUIDs:txt?.['ble-service-uuids']?.split(',')
|
|
125
|
+
}
|
|
126
|
+
if (callback)
|
|
127
|
+
callback(announcement)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
|
|
79
134
|
### Check availability of interface
|
|
80
135
|
|
|
81
136
|
For some interfaces (ANT and BLE) it cannot be guaranteed that the underlying hardware supports the interface ( e.g. a USB stick might be required). Therefore this library offers a `connect` method, that allows to check if the interface is availabe
|
|
@@ -56,8 +56,8 @@ export default class AntAdapter<TDeviceData extends BaseDeviceData> extends Incy
|
|
|
56
56
|
getProfile(): Profile;
|
|
57
57
|
getLogData(data: any, excludeList: any): any;
|
|
58
58
|
check(): Promise<boolean>;
|
|
59
|
-
checkCapabilities(): Promise<void>;
|
|
60
|
-
initControl(): Promise<void>;
|
|
59
|
+
protected checkCapabilities(): Promise<void>;
|
|
60
|
+
protected initControl(): Promise<void>;
|
|
61
61
|
getDefaultStartupTimeout(): number;
|
|
62
62
|
startPreChecks(props: AntDeviceProperties): Promise<'done' | 'connected' | 'connection-failed'>;
|
|
63
63
|
resetStartStatus(): void;
|
package/lib/base/adpater.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export default class IncyclistDevice<P extends DeviceProperties> extends EventEm
|
|
|
11
11
|
started: boolean;
|
|
12
12
|
stopped: boolean;
|
|
13
13
|
paused: boolean;
|
|
14
|
+
scanning: boolean;
|
|
14
15
|
protected props: P;
|
|
15
16
|
protected cyclingMode: ICyclingMode;
|
|
16
17
|
protected logger: EventLogger;
|
|
@@ -66,5 +67,7 @@ export default class IncyclistDevice<P extends DeviceProperties> extends EventEm
|
|
|
66
67
|
getStartProps(startProps?: P): P;
|
|
67
68
|
hasDataListeners(): boolean | OnDeviceDataCallback;
|
|
68
69
|
onData(callback: OnDeviceDataCallback): void;
|
|
70
|
+
onScanStart(): void;
|
|
71
|
+
onScanStop(): void;
|
|
69
72
|
}
|
|
70
73
|
export type IncyclistDeviceAdapter = IncyclistDevice<DeviceProperties>;
|
package/lib/base/adpater.js
CHANGED
|
@@ -26,6 +26,7 @@ class IncyclistDevice extends events_1.default {
|
|
|
26
26
|
this.started = false;
|
|
27
27
|
this.stopped = false;
|
|
28
28
|
this.paused = false;
|
|
29
|
+
this.scanning = false;
|
|
29
30
|
this.user = {};
|
|
30
31
|
this.data = {};
|
|
31
32
|
this.cyclingMode = this.getDefaultCyclingMode();
|
|
@@ -226,11 +227,15 @@ class IncyclistDevice extends events_1.default {
|
|
|
226
227
|
const updateFrequency = this.getMaxUpdateFrequency();
|
|
227
228
|
if (updateFrequency === -1 || updateFrequency === undefined)
|
|
228
229
|
return true;
|
|
229
|
-
|
|
230
|
+
const ok = (!this.lastUpdate || (Date.now() - this.lastUpdate) > updateFrequency);
|
|
231
|
+
return ok;
|
|
230
232
|
}
|
|
231
233
|
canEmitData() {
|
|
232
|
-
if (this.
|
|
234
|
+
if (this.scanning)
|
|
235
|
+
return this.isUpdateWithinFrequency();
|
|
236
|
+
if (this.paused || this.stopped) {
|
|
233
237
|
return false;
|
|
238
|
+
}
|
|
234
239
|
return this.isUpdateWithinFrequency();
|
|
235
240
|
}
|
|
236
241
|
emitData(data) {
|
|
@@ -264,6 +269,12 @@ class IncyclistDevice extends events_1.default {
|
|
|
264
269
|
onData(callback) {
|
|
265
270
|
this.onDataFn = callback;
|
|
266
271
|
}
|
|
272
|
+
onScanStart() {
|
|
273
|
+
this.scanning = true;
|
|
274
|
+
}
|
|
275
|
+
onScanStop() {
|
|
276
|
+
this.scanning = false;
|
|
277
|
+
}
|
|
267
278
|
}
|
|
268
279
|
IncyclistDevice.controllers = {};
|
|
269
280
|
exports.default = IncyclistDevice;
|
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
import BleAdapter from "./base/adapter";
|
|
2
|
-
import { BleDeviceSettings, BleProtocol
|
|
2
|
+
import { BleDeviceSettings, BleProtocol } from "./types";
|
|
3
3
|
import { DeviceProperties } from "../types";
|
|
4
|
+
import { BleComms } from "./base/comms";
|
|
4
5
|
import { BleDeviceData } from "./base/types";
|
|
5
|
-
export interface BleAdapterInfo
|
|
6
|
+
export interface BleAdapterInfo {
|
|
6
7
|
protocol: BleProtocol;
|
|
7
|
-
Adapter: typeof BleAdapter<BleDeviceData,
|
|
8
|
-
Comm: typeof
|
|
8
|
+
Adapter: typeof BleAdapter<BleDeviceData, BleComms>;
|
|
9
|
+
Comm: typeof BleComms;
|
|
9
10
|
}
|
|
10
|
-
export default class BleAdapterFactory
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
createInstance(settings: BleDeviceSettings, props?: DeviceProperties): BleAdapter<BleDeviceData, T>;
|
|
11
|
+
export default class BleAdapterFactory {
|
|
12
|
+
static _instance: BleAdapterFactory;
|
|
13
|
+
implementations: BleAdapterInfo[];
|
|
14
|
+
instances: Array<BleAdapter<BleDeviceData, BleComms>>;
|
|
15
|
+
static getInstance(): BleAdapterFactory;
|
|
16
|
+
constructor();
|
|
17
|
+
getAdapterInfo(protocol: BleProtocol): BleAdapterInfo;
|
|
18
|
+
getAll(): BleAdapterInfo[];
|
|
19
|
+
createInstance(settings: BleDeviceSettings, props?: DeviceProperties): BleAdapter<BleDeviceData, BleComms>;
|
|
20
20
|
removeInstance(query: {
|
|
21
21
|
settings?: BleDeviceSettings;
|
|
22
|
-
adapter?: BleAdapter<BleDeviceData,
|
|
22
|
+
adapter?: BleAdapter<BleDeviceData, BleComms>;
|
|
23
23
|
}): void;
|
|
24
|
-
find(settings?: BleDeviceSettings): BleAdapter<BleDeviceData,
|
|
25
|
-
register(protocol: BleProtocol, Adapter: typeof BleAdapter<BleDeviceData,
|
|
26
|
-
getAllInstances(): Array<BleAdapter<BleDeviceData,
|
|
27
|
-
getAllSupportedComms(): (typeof
|
|
28
|
-
getAllSupportedAdapters(): Array<(typeof BleAdapter<BleDeviceData,
|
|
24
|
+
find(settings?: BleDeviceSettings): BleAdapter<BleDeviceData, BleComms>;
|
|
25
|
+
register(protocol: BleProtocol, Adapter: typeof BleAdapter<BleDeviceData, BleComms>, Comm: typeof BleComms): void;
|
|
26
|
+
getAllInstances(): Array<BleAdapter<BleDeviceData, BleComms>>;
|
|
27
|
+
getAllSupportedComms(): (typeof BleComms)[];
|
|
28
|
+
getAllSupportedAdapters(): Array<(typeof BleAdapter<BleDeviceData, BleComms>)>;
|
|
29
29
|
getAllSupportedServices(): string[];
|
|
30
|
+
getDeviceClasses(peripheral: any, props?: {
|
|
31
|
+
protocol?: BleProtocol;
|
|
32
|
+
services?: string[];
|
|
33
|
+
}): (typeof BleComms)[];
|
|
30
34
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const comms_utils_1 = require("./base/comms-utils");
|
|
3
4
|
const utils_1 = require("./utils");
|
|
4
5
|
class BleAdapterFactory {
|
|
5
|
-
static getInstance(
|
|
6
|
-
if (!BleAdapterFactory.
|
|
7
|
-
BleAdapterFactory.
|
|
8
|
-
return BleAdapterFactory.
|
|
6
|
+
static getInstance() {
|
|
7
|
+
if (!BleAdapterFactory._instance)
|
|
8
|
+
BleAdapterFactory._instance = new BleAdapterFactory();
|
|
9
|
+
return BleAdapterFactory._instance;
|
|
9
10
|
}
|
|
10
|
-
constructor(
|
|
11
|
-
this.transport = transport;
|
|
11
|
+
constructor() {
|
|
12
12
|
this.implementations = [];
|
|
13
13
|
this.instances = [];
|
|
14
14
|
}
|
|
@@ -37,8 +37,7 @@ class BleAdapterFactory {
|
|
|
37
37
|
return existing;
|
|
38
38
|
}
|
|
39
39
|
const info = this.getAdapterInfo(protocol);
|
|
40
|
-
|
|
41
|
-
if (!(info === null || info === void 0 ? void 0 : info.Adapter))
|
|
40
|
+
if (!info || !info.Adapter)
|
|
42
41
|
return;
|
|
43
42
|
const adapter = new info.Adapter(adapterSettings, props);
|
|
44
43
|
this.instances.push(adapter);
|
|
@@ -70,18 +69,42 @@ class BleAdapterFactory {
|
|
|
70
69
|
return this.instances;
|
|
71
70
|
}
|
|
72
71
|
getAllSupportedComms() {
|
|
73
|
-
const supported =
|
|
72
|
+
const supported = BleAdapterFactory.getInstance().getAll();
|
|
74
73
|
return supported.map(info => info.Comm);
|
|
75
74
|
}
|
|
76
75
|
getAllSupportedAdapters() {
|
|
77
|
-
const supported =
|
|
76
|
+
const supported = BleAdapterFactory.getInstance().getAll();
|
|
78
77
|
return supported.map(info => info.Adapter);
|
|
79
78
|
}
|
|
80
79
|
getAllSupportedServices() {
|
|
81
|
-
const supported =
|
|
82
|
-
const res = [
|
|
80
|
+
const supported = BleAdapterFactory.getInstance().getAll();
|
|
81
|
+
const res = [];
|
|
82
|
+
if (supported && supported.length > 0) {
|
|
83
|
+
supported.forEach(info => {
|
|
84
|
+
if (info && info.Comm && info.Comm.services) {
|
|
85
|
+
info.Comm.services.forEach(s => {
|
|
86
|
+
if (!res.includes(s))
|
|
87
|
+
res.push(s);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
83
92
|
return res;
|
|
84
93
|
}
|
|
94
|
+
getDeviceClasses(peripheral, props = {}) {
|
|
95
|
+
let DeviceClasses;
|
|
96
|
+
const { protocol, services = peripheral.advertisement.serviceUuids } = props;
|
|
97
|
+
const classes = this.getAllSupportedComms();
|
|
98
|
+
DeviceClasses = (0, comms_utils_1.getDevicesFromServices)(classes, services);
|
|
99
|
+
if (protocol && DeviceClasses && DeviceClasses.length > 0) {
|
|
100
|
+
DeviceClasses = DeviceClasses.filter((C) => {
|
|
101
|
+
const device = new C({ peripheral });
|
|
102
|
+
if (device.getProtocol() !== protocol)
|
|
103
|
+
return false;
|
|
104
|
+
return true;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return DeviceClasses;
|
|
108
|
+
}
|
|
85
109
|
}
|
|
86
|
-
BleAdapterFactory._instances = {};
|
|
87
110
|
exports.default = BleAdapterFactory;
|
|
@@ -21,7 +21,7 @@ export default class BleAdapter<TDeviceData extends BleDeviceData, TDevice exten
|
|
|
21
21
|
waitForPeripheral(): Promise<void>;
|
|
22
22
|
updateSensor(peripheral: IBlePeripheral): void;
|
|
23
23
|
close(): Promise<boolean>;
|
|
24
|
-
|
|
24
|
+
getSensor(): TDevice;
|
|
25
25
|
isEqual(settings: BleDeviceSettings): boolean;
|
|
26
26
|
isSame(adapter: IAdapter): boolean;
|
|
27
27
|
isConnected(): boolean;
|
|
@@ -44,14 +44,16 @@ export default class BleAdapter<TDeviceData extends BleDeviceData, TDevice exten
|
|
|
44
44
|
protected isStarting(): boolean;
|
|
45
45
|
protected hasData(): boolean;
|
|
46
46
|
protected waitForInitialData(startupTimeout: any): Promise<void>;
|
|
47
|
-
protected checkCapabilities(): void
|
|
47
|
+
protected checkCapabilities(): Promise<void>;
|
|
48
48
|
protected initControl(_props?: BleStartProperties): Promise<void>;
|
|
49
49
|
protected startAdapter(startProps?: BleStartProperties): Promise<boolean>;
|
|
50
50
|
startSensor(): Promise<boolean>;
|
|
51
|
+
protected onDisconnectDone(): Promise<void>;
|
|
51
52
|
stop(): Promise<boolean>;
|
|
52
53
|
pause(): Promise<boolean>;
|
|
53
54
|
resume(): Promise<boolean>;
|
|
54
55
|
protected getBle(): IBleInterface<any>;
|
|
55
56
|
update(): void;
|
|
56
57
|
setCyclingMode(mode: string | ICyclingMode, settings?: any, sendInitCommands?: boolean): void;
|
|
58
|
+
onScanStart(): void;
|
|
57
59
|
}
|
package/lib/ble/base/adapter.js
CHANGED
|
@@ -63,7 +63,7 @@ class BleAdapter extends adpater_1.default {
|
|
|
63
63
|
return true;
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
getSensor() {
|
|
67
67
|
return this.device;
|
|
68
68
|
}
|
|
69
69
|
isEqual(settings) {
|
|
@@ -142,8 +142,9 @@ class BleAdapter extends adpater_1.default {
|
|
|
142
142
|
this.dataMsgCount++;
|
|
143
143
|
this.lastDataTS = Date.now();
|
|
144
144
|
this.deviceData = Object.assign({}, deviceData);
|
|
145
|
-
if (!this.canEmitData())
|
|
145
|
+
if (!this.canEmitData()) {
|
|
146
146
|
return;
|
|
147
|
+
}
|
|
147
148
|
this.logEvent({ message: 'onDeviceData', device: this.getName(), interface: this.getInterface(), data: deviceData, isControllable: this.isControllable() });
|
|
148
149
|
if (this.isControllable()) {
|
|
149
150
|
const mappedData = this.mapData(deviceData);
|
|
@@ -198,6 +199,8 @@ class BleAdapter extends adpater_1.default {
|
|
|
198
199
|
if (this.isStarting()) {
|
|
199
200
|
yield this.stop();
|
|
200
201
|
}
|
|
202
|
+
const ble = this.getBle();
|
|
203
|
+
ble.once('disconnect-done', this.onDisconnectDone.bind(this));
|
|
201
204
|
this.startTask = new task_1.InteruptableTask(this.startAdapter(startProps), {
|
|
202
205
|
timeout: startProps === null || startProps === void 0 ? void 0 : startProps.timeout,
|
|
203
206
|
name: 'start',
|
|
@@ -246,6 +249,8 @@ class BleAdapter extends adpater_1.default {
|
|
|
246
249
|
});
|
|
247
250
|
}
|
|
248
251
|
checkCapabilities() {
|
|
252
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
253
|
+
});
|
|
249
254
|
}
|
|
250
255
|
initControl(_props) {
|
|
251
256
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -281,7 +286,7 @@ class BleAdapter extends adpater_1.default {
|
|
|
281
286
|
return false;
|
|
282
287
|
}
|
|
283
288
|
yield this.waitForInitialData(timeout);
|
|
284
|
-
this.checkCapabilities();
|
|
289
|
+
yield this.checkCapabilities();
|
|
285
290
|
if (this.hasCapability(types_1.IncyclistCapability.Control))
|
|
286
291
|
yield this.initControl(startProps);
|
|
287
292
|
this.stopped = false;
|
|
@@ -307,52 +312,62 @@ class BleAdapter extends adpater_1.default {
|
|
|
307
312
|
startSensor() {
|
|
308
313
|
return __awaiter(this, void 0, void 0, function* () {
|
|
309
314
|
var _a;
|
|
310
|
-
if (!((_a = this.
|
|
315
|
+
if (!((_a = this.getSensor()) === null || _a === void 0 ? void 0 : _a.hasPeripheral())) {
|
|
311
316
|
yield this.waitForPeripheral();
|
|
312
317
|
}
|
|
313
|
-
if (!this.
|
|
318
|
+
if (!this.getSensor()) {
|
|
314
319
|
return false;
|
|
315
320
|
}
|
|
316
|
-
const
|
|
321
|
+
const sensor = this.getSensor();
|
|
322
|
+
const connected = yield sensor.startSensor();
|
|
323
|
+
yield sensor.subscribe();
|
|
317
324
|
if (connected) {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
325
|
+
sensor.on('data', this.onDeviceDataHandler);
|
|
326
|
+
sensor.on('disconnected', this.emit.bind(this));
|
|
327
|
+
sensor.on('error', console.log);
|
|
321
328
|
}
|
|
322
329
|
return connected;
|
|
323
330
|
});
|
|
324
331
|
}
|
|
325
|
-
|
|
332
|
+
onDisconnectDone() {
|
|
326
333
|
return __awaiter(this, void 0, void 0, function* () {
|
|
327
|
-
this.logEvent({ message: '
|
|
334
|
+
this.logEvent({ message: 'disconnecting device', device: this.getName(), interface: this.getInterface() });
|
|
328
335
|
if (this.isStarting()) {
|
|
329
336
|
yield this.startTask.stop();
|
|
330
337
|
}
|
|
331
|
-
this.started = false;
|
|
332
|
-
this.resetData();
|
|
333
338
|
let reason = 'unknown';
|
|
334
339
|
let stopped = false;
|
|
335
|
-
|
|
336
|
-
this.logEvent({ message: 'device stopped - not started yet', device: this.getName(), interface: this.getInterface() });
|
|
337
|
-
return true;
|
|
338
|
-
}
|
|
339
|
-
this.getComms().reset();
|
|
340
|
+
const sensor = this.getSensor();
|
|
340
341
|
try {
|
|
341
|
-
stopped = yield
|
|
342
|
+
stopped = yield sensor.stopSensor();
|
|
342
343
|
}
|
|
343
344
|
catch (err) {
|
|
344
345
|
reason = err.message;
|
|
345
346
|
}
|
|
346
|
-
if (stopped) {
|
|
347
|
-
this.logEvent({ message: 'device
|
|
347
|
+
if (!stopped) {
|
|
348
|
+
this.logEvent({ message: 'disconnecting device failed', device: this.getName(), interface: this.getInterface(), reason });
|
|
348
349
|
}
|
|
349
|
-
|
|
350
|
-
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
stop() {
|
|
353
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
354
|
+
this.logEvent({ message: 'stopping device', device: this.getName(), interface: this.getInterface() });
|
|
355
|
+
if (this.isStarting()) {
|
|
356
|
+
yield this.startTask.stop();
|
|
351
357
|
}
|
|
358
|
+
this.started = false;
|
|
359
|
+
this.resetData();
|
|
360
|
+
if (!this.getSensor()) {
|
|
361
|
+
this.logEvent({ message: 'device stopped - not started yet', device: this.getName(), interface: this.getInterface() });
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
const sensor = this.getSensor();
|
|
365
|
+
sensor.reset();
|
|
352
366
|
this.resetData();
|
|
353
367
|
this.stopped = true;
|
|
354
368
|
this.started = false;
|
|
355
|
-
|
|
369
|
+
this.logEvent({ message: 'device stopped', device: this.getName(), interface: this.getInterface() });
|
|
370
|
+
return this.stopped;
|
|
356
371
|
});
|
|
357
372
|
}
|
|
358
373
|
pause() {
|
|
@@ -386,5 +401,9 @@ class BleAdapter extends adpater_1.default {
|
|
|
386
401
|
super.setCyclingMode(mode, settings, sendInitCommands);
|
|
387
402
|
this.refreshDeviceData();
|
|
388
403
|
}
|
|
404
|
+
onScanStart() {
|
|
405
|
+
if (!this.isStarted())
|
|
406
|
+
this.start();
|
|
407
|
+
}
|
|
389
408
|
}
|
|
390
409
|
exports.default = BleAdapter;
|
package/lib/ble/base/comms.d.ts
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import EventEmitter from "events";
|
|
2
|
+
import { EventLogger } from "gd-eventlog";
|
|
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;
|
|
3
18
|
}
|
|
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 {};
|