@robotical/raftjs 1.3.5 → 1.4.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/dist/react-native/RaftAttributeHandler.d.ts +1 -1
- package/dist/react-native/RaftAttributeHandler.js +18 -10
- package/dist/react-native/RaftAttributeHandler.js.map +1 -1
- package/dist/react-native/RaftChannelBLE.native.d.ts +11 -11
- package/dist/react-native/RaftChannelBLE.native.js +61 -41
- package/dist/react-native/RaftChannelBLE.native.js.map +1 -1
- package/dist/react-native/RaftChannelBLE.web.d.ts +3 -4
- package/dist/react-native/RaftChannelBLE.web.js +7 -8
- package/dist/react-native/RaftChannelBLE.web.js.map +1 -1
- package/dist/react-native/RaftChannelBLEScanner.native.d.ts +6 -6
- package/dist/react-native/RaftChannelBLEScanner.native.js +38 -46
- package/dist/react-native/RaftChannelBLEScanner.native.js.map +1 -1
- package/dist/react-native/RaftConnector.d.ts +3 -3
- package/dist/react-native/RaftConnector.js +4 -4
- package/dist/react-native/RaftConnector.js.map +1 -1
- package/dist/react-native/RaftCustomAttrHandler.d.ts +1 -1
- package/dist/react-native/RaftCustomAttrHandler.js +4 -4
- package/dist/react-native/RaftCustomAttrHandler.js.map +1 -1
- package/dist/react-native/RaftDeviceInfo.d.ts +3 -0
- package/dist/react-native/RaftDeviceManager.d.ts +16 -11
- package/dist/react-native/RaftDeviceManager.js +196 -50
- package/dist/react-native/RaftDeviceManager.js.map +1 -1
- package/dist/react-native/RaftDeviceMgrIF.d.ts +9 -6
- package/dist/react-native/RaftDeviceStates.d.ts +5 -1
- package/dist/react-native/RaftDeviceStates.js +3 -3
- package/dist/react-native/RaftDeviceStates.js.map +1 -1
- package/dist/react-native/RaftMsgHandler.d.ts +1 -1
- package/dist/react-native/RaftMsgHandler.js +1 -2
- package/dist/react-native/RaftMsgHandler.js.map +1 -1
- package/dist/react-native/RaftStruct.d.ts +3 -0
- package/dist/react-native/RaftStruct.js +208 -0
- package/dist/react-native/RaftStruct.js.map +1 -0
- package/dist/react-native/RaftSysTypeManager.d.ts +14 -0
- package/dist/react-native/RaftSysTypeManager.js +53 -0
- package/dist/react-native/RaftSysTypeManager.js.map +1 -0
- package/dist/react-native/RaftSystemType.d.ts +3 -0
- package/dist/react-native/RaftTypes.d.ts +14 -15
- package/dist/react-native/RaftTypes.js +13 -50
- package/dist/react-native/RaftTypes.js.map +1 -1
- package/dist/react-native/RaftUpdateManager.js +6 -3
- package/dist/react-native/RaftUpdateManager.js.map +1 -1
- package/dist/react-native/RaftUtils.d.ts +1 -0
- package/dist/react-native/RaftUtils.js +17 -4
- package/dist/react-native/RaftUtils.js.map +1 -1
- package/dist/react-native/main.d.ts +2 -1
- package/dist/react-native/main.js +4 -4
- package/dist/react-native/main.js.map +1 -1
- package/dist/web/RaftAttributeHandler.d.ts +1 -1
- package/dist/web/RaftAttributeHandler.js +18 -10
- package/dist/web/RaftAttributeHandler.js.map +1 -1
- package/dist/web/RaftChannelBLE.web.d.ts +3 -4
- package/dist/web/RaftChannelBLE.web.js +7 -8
- package/dist/web/RaftChannelBLE.web.js.map +1 -1
- package/dist/web/RaftConnector.d.ts +3 -3
- package/dist/web/RaftConnector.js +4 -4
- package/dist/web/RaftConnector.js.map +1 -1
- package/dist/web/RaftCustomAttrHandler.d.ts +1 -1
- package/dist/web/RaftCustomAttrHandler.js +4 -4
- package/dist/web/RaftCustomAttrHandler.js.map +1 -1
- package/dist/web/RaftDeviceInfo.d.ts +3 -0
- package/dist/web/RaftDeviceManager.d.ts +16 -11
- package/dist/web/RaftDeviceManager.js +196 -50
- package/dist/web/RaftDeviceManager.js.map +1 -1
- package/dist/web/RaftDeviceMgrIF.d.ts +9 -6
- package/dist/web/RaftDeviceStates.d.ts +5 -1
- package/dist/web/RaftDeviceStates.js +3 -3
- package/dist/web/RaftDeviceStates.js.map +1 -1
- package/dist/web/RaftMsgHandler.d.ts +1 -1
- package/dist/web/RaftMsgHandler.js +1 -2
- package/dist/web/RaftMsgHandler.js.map +1 -1
- package/dist/web/RaftStruct.d.ts +3 -0
- package/dist/web/RaftStruct.js +208 -0
- package/dist/web/RaftStruct.js.map +1 -0
- package/dist/web/RaftSysTypeManager.d.ts +14 -0
- package/dist/web/RaftSysTypeManager.js +53 -0
- package/dist/web/RaftSysTypeManager.js.map +1 -0
- package/dist/web/RaftSystemType.d.ts +3 -0
- package/dist/web/RaftTypes.d.ts +14 -15
- package/dist/web/RaftTypes.js +13 -50
- package/dist/web/RaftTypes.js.map +1 -1
- package/dist/web/RaftUpdateManager.js +6 -3
- package/dist/web/RaftUpdateManager.js.map +1 -1
- package/dist/web/RaftUtils.d.ts +1 -0
- package/dist/web/RaftUtils.js +17 -4
- package/dist/web/RaftUtils.js.map +1 -1
- package/dist/web/main.d.ts +2 -1
- package/dist/web/main.js +4 -4
- package/dist/web/main.js.map +1 -1
- package/examples/dashboard/package.json +8 -15
- package/examples/dashboard/src/ConnManager.ts +18 -14
- package/examples/dashboard/src/DeviceActionsForm.tsx +49 -49
- package/examples/dashboard/src/DeviceLineChart.tsx +44 -20
- package/examples/dashboard/src/DevicePanel.tsx +33 -2
- package/examples/dashboard/src/DevicesPanel.tsx +5 -4
- package/examples/dashboard/src/LatencyTest.ts +130 -0
- package/examples/dashboard/src/LatencyTestPanel.tsx +92 -0
- package/examples/dashboard/src/Main.tsx +191 -73
- package/examples/dashboard/src/SettingsManager.ts +67 -0
- package/examples/dashboard/src/SettingsScreen.tsx +174 -0
- package/examples/dashboard/src/SystemTypeCog/CogStateInfo.ts +14 -8
- package/examples/dashboard/src/SystemTypeCog/SystemTypeCog.ts +12 -5
- package/examples/dashboard/src/SystemTypeGeneric/StateInfoGeneric.ts +30 -0
- package/examples/dashboard/src/SystemTypeGeneric/SystemTypeGeneric.ts +91 -0
- package/examples/dashboard/src/SystemTypeMarty/RICStateInfo.ts +23 -4
- package/examples/dashboard/src/SystemTypeMarty/RICSystemUtils.ts +1 -1
- package/examples/dashboard/src/SystemTypeMarty/SystemTypeMarty.ts +5 -2
- package/examples/dashboard/src/index.html +2 -2
- package/examples/dashboard/src/index.tsx +0 -2
- package/examples/dashboard/src/styles.css +14 -0
- package/package.json +7 -15
- package/src/RaftAttributeHandler.ts +23 -14
- package/src/RaftChannelBLE.native.ts +77 -53
- package/src/RaftChannelBLE.web.ts +8 -8
- package/src/RaftChannelBLEScanner.native.ts +38 -44
- package/src/RaftConnector.ts +5 -5
- package/src/RaftCustomAttrHandler.ts +5 -5
- package/src/RaftDeviceInfo.ts +3 -0
- package/src/RaftDeviceManager.ts +236 -65
- package/src/RaftDeviceMgrIF.ts +11 -6
- package/src/RaftDeviceStates.ts +7 -3
- package/src/RaftMsgHandler.ts +1 -2
- package/src/RaftStruct.ts +209 -0
- package/src/RaftSysTypeManager.ts +60 -0
- package/src/RaftSystemType.ts +3 -0
- package/src/RaftTypes.ts +30 -33
- package/src/RaftUpdateManager.ts +6 -3
- package/src/RaftUtils.ts +14 -4
- package/src/main.ts +2 -2
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
import CustomAttrHandler from "./RaftCustomAttrHandler";
|
|
11
11
|
import { DeviceTypeAttribute, DeviceTypePollRespMetadata, decodeAttrUnitsEncoding, isAttrTypeSigned } from "./RaftDeviceInfo";
|
|
12
12
|
import { DeviceAttributesState, DeviceTimeline } from "./RaftDeviceStates";
|
|
13
|
-
import
|
|
13
|
+
import { structSizeOf, structUnpack } from "./RaftStruct";
|
|
14
14
|
|
|
15
15
|
export default class AttributeHandler {
|
|
16
16
|
|
|
@@ -22,17 +22,17 @@ export default class AttributeHandler {
|
|
|
22
22
|
private POLL_RESULT_WRAP_VALUE = this.POLL_RESULT_TIMESTAMP_SIZE === 2 ? 65536 : 4294967296;
|
|
23
23
|
private POLL_RESULT_RESOLUTION_US = 1000;
|
|
24
24
|
|
|
25
|
-
public processMsgAttrGroup(msgBuffer:
|
|
25
|
+
public processMsgAttrGroup(msgBuffer: Uint8Array, msgBufIdx: number, deviceTimeline: DeviceTimeline, pollRespMetadata: DeviceTypePollRespMetadata,
|
|
26
26
|
devAttrsState: DeviceAttributesState, maxDataPoints: number): number {
|
|
27
27
|
|
|
28
28
|
// console.log(`processMsgAttrGroup msg ${msgHexStr} timestamp ${timestamp} origTimestamp ${origTimestamp} msgBufIdx ${msgBufIdx}`)
|
|
29
29
|
|
|
30
30
|
// Extract msg timestamp
|
|
31
|
-
|
|
31
|
+
const { newBufIdx, timestampUs } = this.extractTimestampAndAdvanceIdx(msgBuffer, msgBufIdx, deviceTimeline);
|
|
32
32
|
if (newBufIdx < 0)
|
|
33
33
|
return -1;
|
|
34
34
|
msgBufIdx = newBufIdx;
|
|
35
|
-
|
|
35
|
+
|
|
36
36
|
// Start of message data
|
|
37
37
|
const msgDataStartIdx = msgBufIdx;
|
|
38
38
|
|
|
@@ -69,7 +69,10 @@ export default class AttributeHandler {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
// Number of bytes in group
|
|
72
|
-
|
|
72
|
+
let pollRespSizeBytes = msgBufIdx - msgDataStartIdx;
|
|
73
|
+
if (pollRespSizeBytes < pollRespMetadata.b) {
|
|
74
|
+
pollRespSizeBytes = pollRespMetadata.b;
|
|
75
|
+
}
|
|
73
76
|
|
|
74
77
|
// Check if any attributes were added (in addition to timestamp)
|
|
75
78
|
if (newAttrValues.length === 0) {
|
|
@@ -78,7 +81,7 @@ export default class AttributeHandler {
|
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
// All attributes must have the same number of new values
|
|
81
|
-
|
|
84
|
+
const numNewDataPoints = newAttrValues[0].length;
|
|
82
85
|
for (let i = 1; i < newAttrValues.length; i++) {
|
|
83
86
|
if (newAttrValues[i].length !== numNewDataPoints) {
|
|
84
87
|
console.warn(`DeviceManager msg attrGroup ${pollRespMetadata} attrName ${pollRespMetadata.a[i].n} newAttrValues ${newAttrValues} do not have the same length`);
|
|
@@ -100,7 +103,8 @@ export default class AttributeHandler {
|
|
|
100
103
|
devAttrsState[attrDef.n] = {
|
|
101
104
|
name: attrDef.n,
|
|
102
105
|
newAttribute: true,
|
|
103
|
-
newData:
|
|
106
|
+
newData: false,
|
|
107
|
+
numNewValues: 0,
|
|
104
108
|
values: [],
|
|
105
109
|
units: decodeAttrUnitsEncoding(attrDef.u || ""),
|
|
106
110
|
range: attrDef.r || [0, 0],
|
|
@@ -119,11 +123,12 @@ export default class AttributeHandler {
|
|
|
119
123
|
// Add the new values
|
|
120
124
|
devAttrsState[attrDef.n].values.push(...newAttrValues[attrIdx]);
|
|
121
125
|
devAttrsState[attrDef.n].newData = newAttrValues[attrIdx].length > 0;
|
|
126
|
+
devAttrsState[attrDef.n].numNewValues = newAttrValues[attrIdx].length;
|
|
122
127
|
}
|
|
123
128
|
|
|
124
129
|
// Handle the timestamps with increments if specified
|
|
125
130
|
const timeIncUs: number = pollRespMetadata.us ? pollRespMetadata.us : 1000;
|
|
126
|
-
|
|
131
|
+
const timestampsUs = Array(numNewDataPoints).fill(0);
|
|
127
132
|
for (let i = 0; i < numNewDataPoints; i++) {
|
|
128
133
|
timestampsUs[i] = timestampUs + i * timeIncUs;
|
|
129
134
|
}
|
|
@@ -141,7 +146,7 @@ export default class AttributeHandler {
|
|
|
141
146
|
return msgDataStartIdx+pollRespSizeBytes;
|
|
142
147
|
}
|
|
143
148
|
|
|
144
|
-
private processMsgAttribute(attrDef: DeviceTypeAttribute, msgBuffer:
|
|
149
|
+
private processMsgAttribute(attrDef: DeviceTypeAttribute, msgBuffer: Uint8Array, msgBufIdx: number, msgDataStartIdx: number): { values: number[], newMsgBufIdx: number} {
|
|
145
150
|
|
|
146
151
|
// Current field message string index
|
|
147
152
|
let curFieldBufIdx = msgBufIdx;
|
|
@@ -169,11 +174,11 @@ export default class AttributeHandler {
|
|
|
169
174
|
const maskOnSignedValue = "m" in attrDef && isAttrTypeSigned(attrTypesOnly);
|
|
170
175
|
|
|
171
176
|
// Extract the value using python-struct
|
|
172
|
-
|
|
177
|
+
const unpackValues = structUnpack(maskOnSignedValue ? attrTypesOnly.toUpperCase() : attrTypesOnly, attrBuf);
|
|
173
178
|
let attrValues = unpackValues as number[];
|
|
174
179
|
|
|
175
180
|
// Get number of bytes consumed
|
|
176
|
-
const numBytesConsumed =
|
|
181
|
+
const numBytesConsumed = structSizeOf(attrTypesOnly);
|
|
177
182
|
|
|
178
183
|
// // Check if sign extendable mask specified on signed value
|
|
179
184
|
// if (mmSpecifiedOnSignedValue) {
|
|
@@ -236,6 +241,10 @@ export default class AttributeHandler {
|
|
|
236
241
|
// Move buffer position if using relative positioning
|
|
237
242
|
msgBufIdx += attrUsesAbsPos ? 0 : numBytesConsumed;
|
|
238
243
|
|
|
244
|
+
// if (attrDef.n === "amb0") {
|
|
245
|
+
// console.log(`${new Date().toISOString()} ${attrDef.n} ${attrValues}`);
|
|
246
|
+
// }
|
|
247
|
+
|
|
239
248
|
// Return the value
|
|
240
249
|
return { values: attrValues, newMsgBufIdx: msgBufIdx };
|
|
241
250
|
}
|
|
@@ -252,7 +261,7 @@ export default class AttributeHandler {
|
|
|
252
261
|
return value;
|
|
253
262
|
}
|
|
254
263
|
|
|
255
|
-
private extractTimestampAndAdvanceIdx(msgBuffer:
|
|
264
|
+
private extractTimestampAndAdvanceIdx(msgBuffer: Uint8Array, msgBufIdx: number, timestampWrapHandler: DeviceTimeline):
|
|
256
265
|
{ newBufIdx: number, timestampUs: number } {
|
|
257
266
|
|
|
258
267
|
// Check there are enough characters for the timestamp
|
|
@@ -264,9 +273,9 @@ export default class AttributeHandler {
|
|
|
264
273
|
const tsBuffer = msgBuffer.slice(msgBufIdx, msgBufIdx + this.POLL_RESULT_TIMESTAMP_SIZE);
|
|
265
274
|
let timestampUs: number;
|
|
266
275
|
if (this.POLL_RESULT_TIMESTAMP_SIZE === 2) {
|
|
267
|
-
timestampUs =
|
|
276
|
+
timestampUs = structUnpack(">H", tsBuffer)[0] as number * this.POLL_RESULT_RESOLUTION_US;
|
|
268
277
|
} else {
|
|
269
|
-
timestampUs =
|
|
278
|
+
timestampUs = structUnpack(">I", tsBuffer)[0] as number * this.POLL_RESULT_RESOLUTION_US;
|
|
270
279
|
}
|
|
271
280
|
|
|
272
281
|
// Check if time is before lastReportTimeMs - in which case a wrap around occurred to add on the max value
|
|
@@ -14,29 +14,26 @@ import { RaftConnEvent, RaftConnEventFn } from "./RaftConnEvents";
|
|
|
14
14
|
import RaftLog from "./RaftLog";
|
|
15
15
|
import RaftMsgHandler from "./RaftMsgHandler";
|
|
16
16
|
import RaftUtils from "./RaftUtils";
|
|
17
|
-
import
|
|
18
|
-
import {
|
|
17
|
+
import RaftChannelBLEScanner from "./RaftChannelBLEScanner.native";
|
|
18
|
+
import { DiscoveredDevice } from "./RaftTypes";
|
|
19
19
|
|
|
20
20
|
const _bleManager = new BleManager();
|
|
21
21
|
|
|
22
22
|
export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
23
|
-
// BLE UUIDS
|
|
24
|
-
public static ServiceUUID = "aa76677e-9cfd-4626-a510-0d305be57c8d";
|
|
25
|
-
public static CogServiceUUID = "da903f65-d5c2-4f4d-a065-d1aade7af874";
|
|
26
|
-
public static CmdUUID = "aa76677e-9cfd-4626-a510-0d305be57c8e";
|
|
27
|
-
public static RespUUID = "aa76677e-9cfd-4626-a510-0d305be57c8f";
|
|
28
|
-
|
|
29
|
-
// BleManager
|
|
30
|
-
|
|
31
23
|
|
|
32
24
|
// Conn event fn
|
|
33
25
|
private _onConnEvent: RaftConnEventFn | null = null;
|
|
34
26
|
|
|
27
|
+
// Default command and response UUIDs
|
|
28
|
+
_cmdUUID = 'aa76677e-9cfd-4626-a510-0d305be57c8e';
|
|
29
|
+
_respUUID = 'aa76677e-9cfd-4626-a510-0d305be57c8f';
|
|
30
|
+
_serviceUUIDs = ['aa76677e-9cfd-4626-a510-0d305be57c8d', 'da903f65-d5c2-4f4d-a065-d1aade7af874'];
|
|
31
|
+
|
|
35
32
|
// BLE Scanner
|
|
36
|
-
_bleScanner:
|
|
33
|
+
_bleScanner: RaftChannelBLEScanner;
|
|
37
34
|
|
|
38
|
-
//
|
|
39
|
-
|
|
35
|
+
// Device to connect to using BLE
|
|
36
|
+
_deviceToConnectTo: DiscoveredDevice | null = null;
|
|
40
37
|
|
|
41
38
|
// Handle BLE disconnection through retry
|
|
42
39
|
// May be set after connection checking (e.g. using LEDs)
|
|
@@ -49,7 +46,6 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
49
46
|
_bleSubscrOnStateChange: Subscription | null = null;
|
|
50
47
|
_bleDevice: Device | null = null;
|
|
51
48
|
|
|
52
|
-
|
|
53
49
|
// MTU (Maximum Transmission Unit) size to request
|
|
54
50
|
MTU_SIZE_TO_REQUEST = 512;
|
|
55
51
|
|
|
@@ -61,23 +57,22 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
61
57
|
private _msgTxMinTimeBetweenMs = 1;
|
|
62
58
|
private readonly maxRetries = 1;
|
|
63
59
|
|
|
64
|
-
// Connected flag and retries
|
|
65
|
-
private _isConnected = false;
|
|
66
|
-
// private readonly _maxConnRetries = 3;
|
|
67
|
-
|
|
68
60
|
// File Handler parameters
|
|
69
61
|
private _requestedBatchAckSize = 10;
|
|
70
62
|
private _requestedFileBlockSize = 500;
|
|
71
63
|
|
|
64
|
+
// Connected device UUID
|
|
65
|
+
private _connectedDeviceServiceUUID?: string;
|
|
66
|
+
|
|
72
67
|
constructor() {
|
|
73
68
|
RaftLog.debug('BLEChannel constructor');
|
|
74
69
|
|
|
75
70
|
// _bleManager.setLogLevel(LogLevel.Verbose);
|
|
76
71
|
|
|
77
72
|
// Scanner
|
|
78
|
-
this._bleScanner = new
|
|
73
|
+
this._bleScanner = new RaftChannelBLEScanner(
|
|
79
74
|
_bleManager,
|
|
80
|
-
|
|
75
|
+
this._serviceUUIDs,
|
|
81
76
|
this.scanningEvent.bind(this),
|
|
82
77
|
);
|
|
83
78
|
|
|
@@ -87,6 +82,10 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
87
82
|
}, true);
|
|
88
83
|
}
|
|
89
84
|
|
|
85
|
+
setConnectedDeviceUUID(serviceUUID: string) {
|
|
86
|
+
this._connectedDeviceServiceUUID = serviceUUID;
|
|
87
|
+
}
|
|
88
|
+
|
|
90
89
|
getBleManager(): BleManager {
|
|
91
90
|
return _bleManager;
|
|
92
91
|
}
|
|
@@ -99,7 +98,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
99
98
|
|
|
100
99
|
// isConnected
|
|
101
100
|
isConnected(): boolean {
|
|
102
|
-
return this._bleDevice !== null
|
|
101
|
+
return this._bleDevice !== null;
|
|
103
102
|
}
|
|
104
103
|
|
|
105
104
|
setOnConnEvent(connEventFn: RaftConnEventFn): void {
|
|
@@ -124,12 +123,23 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
124
123
|
this._retryConnectionIfLost = retry;
|
|
125
124
|
}
|
|
126
125
|
|
|
127
|
-
async discoveryStart(
|
|
126
|
+
async discoveryStart(uuids: string[], tries = 10): Promise<boolean> {
|
|
127
|
+
if (tries <= 0) {
|
|
128
|
+
RaftLog.debug(`BLEChannel discoveryStart failed after 5 tries`);
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
128
131
|
// Disconnect any existing connection
|
|
129
132
|
// await this.disconnect();
|
|
130
133
|
|
|
134
|
+
// wait until the bt is powered on
|
|
135
|
+
const state = await _bleManager.state();
|
|
136
|
+
if (state !== State.PoweredOn) {
|
|
137
|
+
RaftLog.debug(`BLEChannel discoveryStart waiting for BLE powered on`);
|
|
138
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
139
|
+
return this.discoveryStart(uuids, tries - 1);
|
|
140
|
+
}
|
|
131
141
|
// Start scanning
|
|
132
|
-
await this._bleScanner.scanningStart(
|
|
142
|
+
await this._bleScanner.scanningStart(uuids);
|
|
133
143
|
|
|
134
144
|
// Event
|
|
135
145
|
RaftLog.debug(`BLEChannel discoveryStart emitting BLE_SCANNING_STARTED`);
|
|
@@ -200,16 +210,18 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
200
210
|
* @returns boolean (true if connected)
|
|
201
211
|
*
|
|
202
212
|
*/
|
|
203
|
-
async connect(
|
|
204
|
-
RaftLog.debug('BLEChannel requested connection');
|
|
213
|
+
async connect(discoveredDevice: DiscoveredDevice): Promise<boolean> {
|
|
214
|
+
RaftLog.debug('BLEChannel requested connection ' + JSON.stringify(discoveredDevice));
|
|
215
|
+
this._connectedDeviceServiceUUID = discoveredDevice.serviceUUIDs ? discoveredDevice.serviceUUIDs[0] : undefined;
|
|
216
|
+
|
|
205
217
|
this._retryConnectionIfLost = false;
|
|
206
218
|
this._bleScanner.scanningStop();
|
|
207
219
|
|
|
208
220
|
// Now connecting
|
|
209
|
-
this.emit(RaftConnEvent.CONN_CONNECTING, { deviceId:
|
|
221
|
+
this.emit(RaftConnEvent.CONN_CONNECTING, { deviceId: discoveredDevice.id });
|
|
210
222
|
|
|
211
223
|
// Connect
|
|
212
|
-
this.
|
|
224
|
+
this._deviceToConnectTo = discoveredDevice;
|
|
213
225
|
const connOk = await this._configDeviceConnection();
|
|
214
226
|
|
|
215
227
|
// Check if ok
|
|
@@ -221,8 +233,8 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
221
233
|
|
|
222
234
|
// Emit success
|
|
223
235
|
this.emit(RaftConnEvent.CONN_CONNECTED, {
|
|
224
|
-
deviceId: this.
|
|
225
|
-
name: this.
|
|
236
|
+
deviceId: this._deviceToConnectTo.id,
|
|
237
|
+
name: this._deviceToConnectTo.name,
|
|
226
238
|
});
|
|
227
239
|
return true;
|
|
228
240
|
}
|
|
@@ -236,9 +248,9 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
236
248
|
async disconnect(): Promise<void> {
|
|
237
249
|
RaftLog.debug('BLEChannel disconnect requested');
|
|
238
250
|
this._retryConnectionIfLost = false;
|
|
239
|
-
RaftLog.debug(`this._ricToConnectTo ${this.
|
|
240
|
-
const connectedRIC = this.
|
|
241
|
-
this.
|
|
251
|
+
RaftLog.debug(`this._ricToConnectTo ${this._deviceToConnectTo}`);
|
|
252
|
+
const connectedRIC = this._deviceToConnectTo;
|
|
253
|
+
this._deviceToConnectTo = null;
|
|
242
254
|
|
|
243
255
|
// this._invalidateConnectionInfo(); // //
|
|
244
256
|
|
|
@@ -248,9 +260,9 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
248
260
|
this._bleSubscrOnDisconnect = null;
|
|
249
261
|
}
|
|
250
262
|
// Disconnect from the connected device
|
|
251
|
-
const connMarties = await _bleManager.connectedDevices(
|
|
252
|
-
|
|
253
|
-
|
|
263
|
+
const connMarties = await _bleManager.connectedDevices(
|
|
264
|
+
this._serviceUUIDs
|
|
265
|
+
);
|
|
254
266
|
if (connMarties.length == 0) {
|
|
255
267
|
RaftLog.debug('BLEChannel disconnect - no devices connected');
|
|
256
268
|
} else {
|
|
@@ -277,7 +289,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
277
289
|
*/
|
|
278
290
|
async _configDeviceConnection(): Promise<boolean> {
|
|
279
291
|
// Check there is a RIC to connect to
|
|
280
|
-
if (this.
|
|
292
|
+
if (this._deviceToConnectTo === null) {
|
|
281
293
|
return false;
|
|
282
294
|
}
|
|
283
295
|
|
|
@@ -285,7 +297,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
285
297
|
for (let connRetry = 0; connRetry < 5; connRetry++) {
|
|
286
298
|
try {
|
|
287
299
|
deviceConnected = await _bleManager.connectToDevice(
|
|
288
|
-
this.
|
|
300
|
+
this._deviceToConnectTo.id,
|
|
289
301
|
{
|
|
290
302
|
timeout: 3000,
|
|
291
303
|
},
|
|
@@ -322,7 +334,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
322
334
|
// Request high-priority connection
|
|
323
335
|
try {
|
|
324
336
|
await _bleManager.requestConnectionPriorityForDevice(
|
|
325
|
-
this.
|
|
337
|
+
this._deviceToConnectTo.id,
|
|
326
338
|
ConnectionPriority.High,
|
|
327
339
|
);
|
|
328
340
|
RaftLog.debug(
|
|
@@ -350,9 +362,13 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
350
362
|
// Monitor the inbound characteristic
|
|
351
363
|
try {
|
|
352
364
|
if (this._bleDevice) {
|
|
365
|
+
if (!this._connectedDeviceServiceUUID) {
|
|
366
|
+
RaftLog.error('BLEChannel _configDeviceConnection - no connected device service UUID');
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
353
369
|
this._bleSubscrOnRx = this._bleDevice.monitorCharacteristicForService(
|
|
354
|
-
|
|
355
|
-
|
|
370
|
+
this._connectedDeviceServiceUUID,
|
|
371
|
+
this._respUUID,
|
|
356
372
|
(error: BleError | null, characteristic: Characteristic | null) => {
|
|
357
373
|
this._onMsgRx(error, characteristic);
|
|
358
374
|
},
|
|
@@ -422,15 +438,15 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
422
438
|
// Check if scan in progress - and stop reconn attempts if so
|
|
423
439
|
const scanInProgress = this._bleScanner.isScanInProgress();
|
|
424
440
|
RaftLog.debug(
|
|
425
|
-
`onDeviceDisconnected considering reconnection RICToConnectTo ${this.
|
|
441
|
+
`onDeviceDisconnected considering reconnection RICToConnectTo ${this._deviceToConnectTo?.name} scanInProgress ${scanInProgress} retryConnectionIfLost ${this._retryConnectionIfLost}`,
|
|
426
442
|
);
|
|
427
443
|
if (
|
|
428
444
|
!this._retryConnectionIfLost ||
|
|
429
445
|
scanInProgress ||
|
|
430
|
-
!this.
|
|
446
|
+
!this._deviceToConnectTo
|
|
431
447
|
) {
|
|
432
448
|
RaftLog.debug(
|
|
433
|
-
`onDeviceDisconnected DISCONNECTED_RIC RICToConnectTo ${this.
|
|
449
|
+
`onDeviceDisconnected DISCONNECTED_RIC RICToConnectTo ${this._deviceToConnectTo?.name} scanInProgress ${scanInProgress} retryConnectionIfLost ${this._retryConnectionIfLost}`,
|
|
434
450
|
);
|
|
435
451
|
if (this._retryConnectionIfLost) {
|
|
436
452
|
// this.emit(RaftConnEvent.BLE_DISCONNECTED_RIC);
|
|
@@ -441,7 +457,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
441
457
|
}
|
|
442
458
|
if (await this._configDeviceConnection()) {
|
|
443
459
|
RaftLog.debug(
|
|
444
|
-
`onDeviceDisconnected successful reconn RICToConnectTo ${this.
|
|
460
|
+
`onDeviceDisconnected successful reconn RICToConnectTo ${this._deviceToConnectTo?.name}`,
|
|
445
461
|
);
|
|
446
462
|
|
|
447
463
|
// Indicate connection issue resolved
|
|
@@ -451,7 +467,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
451
467
|
return;
|
|
452
468
|
}
|
|
453
469
|
RaftLog.debug(
|
|
454
|
-
`onDeviceDisconnected retrying reconn RICToConnectTo ${this.
|
|
470
|
+
`onDeviceDisconnected retrying reconn RICToConnectTo ${this._deviceToConnectTo?.name}`,
|
|
455
471
|
);
|
|
456
472
|
}
|
|
457
473
|
},
|
|
@@ -482,7 +498,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
482
498
|
const rxFrame = RaftUtils.atob(msgFrameBase64!);
|
|
483
499
|
|
|
484
500
|
// Debug
|
|
485
|
-
// RaftLog.debug('_onMsgRx from BLE ' +
|
|
501
|
+
// RaftLog.debug('_onMsgRx from BLE ' + RaftUtils.bufferToHex(rxFrame));
|
|
486
502
|
|
|
487
503
|
// Send
|
|
488
504
|
if (rxFrame !== null && this._raftMsgHandler) {
|
|
@@ -517,13 +533,17 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
517
533
|
const msgFrameBase64 = RaftUtils.btoa(msg);
|
|
518
534
|
|
|
519
535
|
try {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
536
|
+
if (!this._connectedDeviceServiceUUID) {
|
|
537
|
+
RaftLog.error('BLEChannel sendTxMsg - no connected device service UUID');
|
|
538
|
+
return false;
|
|
539
|
+
}
|
|
540
|
+
await this._bleDevice!.writeCharacteristicWithoutResponseForService(
|
|
541
|
+
this._connectedDeviceServiceUUID,
|
|
542
|
+
this._cmdUUID,
|
|
523
543
|
msgFrameBase64!,
|
|
524
544
|
);
|
|
525
545
|
return true;
|
|
526
|
-
} catch
|
|
546
|
+
} catch {
|
|
527
547
|
if (retryIdx === this.maxRetries - 1) {
|
|
528
548
|
RaftLog.debug(`sendTxMsg failed after ${this.maxRetries} attempts`);
|
|
529
549
|
return false;
|
|
@@ -556,13 +576,17 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
556
576
|
const msgFrameBase64 = RaftUtils.btoa(msg);
|
|
557
577
|
|
|
558
578
|
try {
|
|
579
|
+
if (!this._connectedDeviceServiceUUID) {
|
|
580
|
+
RaftLog.error('BLEChannel sendTxMsgNoAwait - no connected device service UUID');
|
|
581
|
+
return false;
|
|
582
|
+
}
|
|
559
583
|
this._bleDevice!.writeCharacteristicWithoutResponseForService(
|
|
560
|
-
|
|
561
|
-
|
|
584
|
+
this._connectedDeviceServiceUUID,
|
|
585
|
+
this._cmdUUID,
|
|
562
586
|
msgFrameBase64!,
|
|
563
587
|
);
|
|
564
588
|
return true;
|
|
565
|
-
} catch
|
|
589
|
+
} catch {
|
|
566
590
|
if (retryIdx === this.maxRetries - 1) {
|
|
567
591
|
RaftLog.debug(`sendTxMsgNoAwait failed after ${this.maxRetries} attempts`);
|
|
568
592
|
return false;
|
|
@@ -16,12 +16,12 @@ import { ConnectorOptions } from "./RaftSystemType";
|
|
|
16
16
|
import RaftUtils from "./RaftUtils";
|
|
17
17
|
|
|
18
18
|
export default class RaftChannelBLE implements RaftChannel {
|
|
19
|
-
// BLE UUIDS
|
|
20
|
-
public static RICServiceUUID = "aa76677e-9cfd-4626-a510-0d305be57c8d";
|
|
21
|
-
public static CogServiceUUID = "da903f65-d5c2-4f4d-a065-d1aade7af874";
|
|
22
|
-
public static CmdUUID = "aa76677e-9cfd-4626-a510-0d305be57c8e";
|
|
23
|
-
public static RespUUID = "aa76677e-9cfd-4626-a510-0d305be57c8f";
|
|
24
19
|
|
|
20
|
+
// Default command and response UUIDs
|
|
21
|
+
_cmdUUID = 'aa76677e-9cfd-4626-a510-0d305be57c8e';
|
|
22
|
+
_respUUID = 'aa76677e-9cfd-4626-a510-0d305be57c8f';
|
|
23
|
+
_serviceUUIDs = ['aa76677e-9cfd-4626-a510-0d305be57c8d', 'da903f65-d5c2-4f4d-a065-d1aade7af874'];
|
|
24
|
+
|
|
25
25
|
// Device and characteristics
|
|
26
26
|
private _bleDevice: BluetoothDevice | null = null;
|
|
27
27
|
private _characteristicTx: BluetoothRemoteGATTCharacteristic | null = null;
|
|
@@ -143,7 +143,7 @@ export default class RaftChannelBLE implements RaftChannel {
|
|
|
143
143
|
try {
|
|
144
144
|
let service: BluetoothRemoteGATTService | null = null;
|
|
145
145
|
// iterate over known services
|
|
146
|
-
for (const serviceUUID of
|
|
146
|
+
for (const serviceUUID of this._serviceUUIDs) {
|
|
147
147
|
try {
|
|
148
148
|
service = await this._bleDevice.gatt.getPrimaryService(serviceUUID);
|
|
149
149
|
if (service) {
|
|
@@ -169,13 +169,13 @@ export default class RaftChannelBLE implements RaftChannel {
|
|
|
169
169
|
try {
|
|
170
170
|
// Get Tx and Rx characteristics
|
|
171
171
|
this._characteristicTx = await service.getCharacteristic(
|
|
172
|
-
|
|
172
|
+
this._cmdUUID
|
|
173
173
|
);
|
|
174
174
|
RaftLog.debug(
|
|
175
175
|
`RaftChannelBLE.connect - found char ${this._characteristicTx.uuid}`
|
|
176
176
|
);
|
|
177
177
|
this._characteristicRx = await service.getCharacteristic(
|
|
178
|
-
|
|
178
|
+
this._respUUID
|
|
179
179
|
);
|
|
180
180
|
RaftLog.debug(
|
|
181
181
|
`RaftChannelBLE.connect - found char ${this._characteristicRx.uuid}`
|