@robdobsn/raftjs 1.3.1 → 1.5.0
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 +12 -9
- package/dist/react-native/RaftAttributeHandler.js.map +1 -1
- package/dist/react-native/RaftChannelBLE.native.d.ts +9 -13
- package/dist/react-native/RaftChannelBLE.native.js +33 -43
- package/dist/react-native/RaftChannelBLE.native.js.map +1 -1
- package/dist/react-native/RaftChannelBLE.web.d.ts +3 -3
- package/dist/react-native/RaftChannelBLE.web.js +23 -7
- 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 -39
- 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 +3 -3
- 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/RaftDeviceManager.d.ts +3 -3
- package/dist/react-native/RaftDeviceManager.js +12 -9
- package/dist/react-native/RaftDeviceManager.js.map +1 -1
- package/dist/react-native/RaftDeviceMgrIF.d.ts +2 -3
- package/dist/react-native/RaftDeviceStates.js +1 -1
- package/dist/react-native/RaftDeviceStates.js.map +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 +207 -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 +4 -0
- package/dist/react-native/RaftTypes.d.ts +2 -2
- package/dist/react-native/RaftTypes.js +3 -3
- 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 +1 -0
- package/dist/react-native/main.js +3 -1
- package/dist/react-native/main.js.map +1 -1
- package/dist/web/RaftAttributeHandler.d.ts +1 -1
- package/dist/web/RaftAttributeHandler.js +12 -9
- package/dist/web/RaftAttributeHandler.js.map +1 -1
- package/dist/web/RaftChannelBLE.web.d.ts +3 -3
- package/dist/web/RaftChannelBLE.web.js +23 -7
- package/dist/web/RaftChannelBLE.web.js.map +1 -1
- package/dist/web/RaftConnector.d.ts +3 -3
- package/dist/web/RaftConnector.js +3 -3
- 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/RaftDeviceManager.d.ts +3 -3
- package/dist/web/RaftDeviceManager.js +12 -9
- package/dist/web/RaftDeviceManager.js.map +1 -1
- package/dist/web/RaftDeviceMgrIF.d.ts +2 -3
- package/dist/web/RaftDeviceStates.js +1 -1
- package/dist/web/RaftDeviceStates.js.map +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 +207 -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 +4 -0
- package/dist/web/RaftTypes.d.ts +2 -2
- package/dist/web/RaftTypes.js +3 -3
- 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 +1 -0
- package/dist/web/main.js +3 -1
- package/dist/web/main.js.map +1 -1
- package/examples/dashboard/package.json +2 -12
- package/examples/dashboard/src/ConnManager.ts +13 -21
- package/examples/dashboard/src/Main.tsx +15 -7
- package/examples/dashboard/src/SystemTypeCog/CogStateInfo.ts +5 -10
- package/examples/dashboard/src/SystemTypeCog/SystemTypeCog.ts +3 -0
- package/examples/dashboard/src/SystemTypeGeneric/StateInfoGeneric.ts +24 -0
- package/examples/dashboard/src/SystemTypeGeneric/SystemTypeGeneric.ts +87 -0
- package/examples/dashboard/src/SystemTypeMarty/SystemTypeMarty.ts +3 -0
- package/examples/dashboard/src/index.tsx +0 -2
- package/examples/dashboard/src/styles.css +13 -2
- package/package.json +2 -11
- package/src/RaftAttributeHandler.ts +16 -13
- package/src/RaftChannelBLE.native.ts +43 -54
- package/src/RaftChannelBLE.web.ts +45 -28
- package/src/RaftChannelBLEScanner.native.ts +37 -39
- package/src/RaftConnector.ts +3 -3
- package/src/RaftCustomAttrHandler.ts +5 -5
- package/src/RaftDeviceManager.ts +16 -14
- package/src/RaftDeviceMgrIF.ts +2 -3
- package/src/RaftDeviceStates.ts +1 -1
- package/src/RaftMsgHandler.ts +1 -2
- package/src/RaftStruct.ts +208 -0
- package/src/RaftSysTypeManager.ts +60 -0
- package/src/RaftSystemType.ts +4 -0
- package/src/RaftTypes.ts +2 -2
- package/src/RaftUpdateManager.ts +6 -3
- package/src/RaftUtils.ts +14 -4
- package/src/main.ts +1 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { RaftSubscribeForUpdatesCBType, RaftSystemType } from "../../../../src/RaftSystemType";
|
|
2
|
+
import { RaftEventFn, RaftLog, RaftOKFail, RaftPublishEvent, RaftPublishEventNames, RaftSystemUtils } from "../../../../src/main";
|
|
3
|
+
import { StateInfoGeneric } from "./StateInfoGeneric";
|
|
4
|
+
import { DeviceManager } from "../../../../src/RaftDeviceManager";
|
|
5
|
+
|
|
6
|
+
export default class SystemTypeGeneric implements RaftSystemType {
|
|
7
|
+
nameForDialogs = "Generic System";
|
|
8
|
+
defaultWiFiHostname = "Generic";
|
|
9
|
+
firmwareDestName = "ricfw";
|
|
10
|
+
normalFileDestName = "fs";
|
|
11
|
+
connectorOptions = {wsSuffix: "wsjson", bleConnItvlMs: 50};
|
|
12
|
+
BLEServiceUUIDs = ["aa76677e-9cfd-4626-a510-0d305be57c8d", "da903f65-d5c2-4f4d-a065-d1aade7af874"];
|
|
13
|
+
BLECmdUUID = "aa76677e-9cfd-4626-a510-0d305be57c8e";
|
|
14
|
+
BLERespUUID = "aa76677e-9cfd-4626-a510-0d305be57c8f";
|
|
15
|
+
|
|
16
|
+
// Event handler
|
|
17
|
+
private _onEvent: RaftEventFn | null = null;
|
|
18
|
+
|
|
19
|
+
// Raft system utils
|
|
20
|
+
private _systemUtils: RaftSystemUtils | null = null;
|
|
21
|
+
|
|
22
|
+
// Device manager
|
|
23
|
+
private _deviceManager: DeviceManager = new DeviceManager();
|
|
24
|
+
|
|
25
|
+
// Setup
|
|
26
|
+
setup(systemUtils: RaftSystemUtils, onEvent: RaftEventFn | null): void {
|
|
27
|
+
this._systemUtils = systemUtils;
|
|
28
|
+
this._onEvent = onEvent;
|
|
29
|
+
this._deviceManager.setup(systemUtils);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Latest data from servos, IMU, etc
|
|
33
|
+
private _stateInfo: StateInfoGeneric = new StateInfoGeneric(this._deviceManager);
|
|
34
|
+
getStateInfo(): StateInfoGeneric {
|
|
35
|
+
return this._stateInfo;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Subscribe for updates
|
|
39
|
+
subscribeForUpdates: RaftSubscribeForUpdatesCBType | null = async (systemUtils: RaftSystemUtils, enable: boolean) => {
|
|
40
|
+
// Subscription rate
|
|
41
|
+
const subscribeRateHz = 0.1;
|
|
42
|
+
try {
|
|
43
|
+
const subscribeDisable = '{"cmdName":"subscription","action":"update",' +
|
|
44
|
+
'"pubRecs":[' +
|
|
45
|
+
`{"name":"devjson","rateHz":0,}` +
|
|
46
|
+
']}';
|
|
47
|
+
const subscribeEnable = '{"cmdName":"subscription","action":"update",' +
|
|
48
|
+
'"pubRecs":[' +
|
|
49
|
+
`{"name":"devjson","trigger":"timeorchange","rateHz":${subscribeRateHz.toString()}}` +
|
|
50
|
+
']}';
|
|
51
|
+
|
|
52
|
+
const msgHandler = systemUtils.getMsgHandler();
|
|
53
|
+
const ricResp = await msgHandler.sendRICRESTCmdFrame<RaftOKFail>(
|
|
54
|
+
enable ? subscribeEnable : subscribeDisable
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
// Debug
|
|
58
|
+
RaftLog.debug(`subscribe enable/disable returned ${JSON.stringify(ricResp)}`);
|
|
59
|
+
} catch (error: unknown) {
|
|
60
|
+
RaftLog.warn(`getRICCalibInfo Failed subscribe for updates ${error}`);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Invalidate state
|
|
65
|
+
stateIsInvalid(): void {};
|
|
66
|
+
|
|
67
|
+
// Other message type
|
|
68
|
+
rxOtherMsgType(payload: Uint8Array, frameTimeMs: number) {
|
|
69
|
+
|
|
70
|
+
// RICLog.debug(`rxOtherMsgType payload ${RICUtils.bufferToHex(payload)}`);
|
|
71
|
+
RaftLog.verbose(`rxOtherMsgType payloadLen ${payload.length}`);
|
|
72
|
+
const topicIDs = this._stateInfo.updateFromMsg(payload, frameTimeMs);
|
|
73
|
+
|
|
74
|
+
// Call event handler if registered
|
|
75
|
+
if (this._onEvent) {
|
|
76
|
+
this._onEvent("pub", RaftPublishEvent.PUBLISH_EVENT_DATA, RaftPublishEventNames[RaftPublishEvent.PUBLISH_EVENT_DATA],
|
|
77
|
+
{
|
|
78
|
+
topicIDs: topicIDs,
|
|
79
|
+
payload: payload,
|
|
80
|
+
frameTimeMs: frameTimeMs
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// Get device manager
|
|
86
|
+
deviceMgrIF = this._deviceManager;
|
|
87
|
+
}
|
|
@@ -12,6 +12,9 @@ export default class SystemTypeMarty implements RaftSystemType {
|
|
|
12
12
|
firmwareDestName = "ricfw";
|
|
13
13
|
normalFileDestName = "fs";
|
|
14
14
|
connectorOptions = {wsSuffix: "ws", bleConnItvlMs: 7.5};
|
|
15
|
+
BLEServiceUUIDs = ["aa76677e-9cfd-4626-a510-0d305be57c8d"];
|
|
16
|
+
BLECmdUUID = "aa76677e-9cfd-4626-a510-0d305be57c8e";
|
|
17
|
+
BLERespUUID = "aa76677e-9cfd-4626-a510-0d305be57c8f";
|
|
15
18
|
|
|
16
19
|
// LED Pattern checker
|
|
17
20
|
private _ledPatternChecker: RICLEDPatternChecker = new RICLEDPatternChecker();
|
|
@@ -68,7 +68,13 @@ h1 {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
.action-button {
|
|
71
|
-
|
|
71
|
+
color: #fff;
|
|
72
|
+
cursor: pointer;
|
|
73
|
+
background-color: #007bff;
|
|
74
|
+
border: none;
|
|
75
|
+
border-radius: 4px;
|
|
76
|
+
padding: 10px 20px;
|
|
77
|
+
font-size: 1rem;
|
|
72
78
|
}
|
|
73
79
|
|
|
74
80
|
.info-boxes {
|
|
@@ -98,8 +104,11 @@ h1 {
|
|
|
98
104
|
}
|
|
99
105
|
|
|
100
106
|
.ip-addr-input {
|
|
101
|
-
|
|
107
|
+
border: 1px solid #ccc;
|
|
108
|
+
border-radius: 4px;
|
|
102
109
|
margin-bottom: 10px;
|
|
110
|
+
padding: 10px;
|
|
111
|
+
width: 100%;
|
|
103
112
|
}
|
|
104
113
|
|
|
105
114
|
.command-input {
|
|
@@ -116,6 +125,7 @@ h1 {
|
|
|
116
125
|
background-color: #007bff;
|
|
117
126
|
color: white;
|
|
118
127
|
cursor: pointer;
|
|
128
|
+
font-size: 1rem;
|
|
119
129
|
}
|
|
120
130
|
|
|
121
131
|
.send-command-button:hover {
|
|
@@ -203,6 +213,7 @@ h1 {
|
|
|
203
213
|
color: #fff;
|
|
204
214
|
cursor: pointer;
|
|
205
215
|
margin-left: 10px;
|
|
216
|
+
font-size: 1rem;
|
|
206
217
|
}
|
|
207
218
|
|
|
208
219
|
.example-load-button:hover {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@robdobsn/raftjs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Javascript/TS library for Raft library",
|
|
5
5
|
"main": "dist/web/main.js",
|
|
6
6
|
"types": "dist/web/main.d.ts",
|
|
@@ -25,31 +25,22 @@
|
|
|
25
25
|
"build": "tsc -p tsconfig.json",
|
|
26
26
|
"build:phone": "tsc -p tsconfig.react-native.json",
|
|
27
27
|
"build-all": "npm run clean && npm run build && npm run build:phone",
|
|
28
|
-
"test": "jest",
|
|
29
28
|
"lint": "eslint ./src",
|
|
30
29
|
"clean": "rm -rf dist build package",
|
|
31
30
|
"docs": "typedoc --entryPoints src/main.ts",
|
|
32
31
|
"watch-all": "tsc -p tsconfig.json --watch & tsc -p tsconfig.react-native.json --watch"
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|
|
35
|
-
"@types/jest": "^29.5.12",
|
|
36
34
|
"@types/node": "^20.14.0",
|
|
37
|
-
"@types/python-struct": "^1.0.4",
|
|
38
|
-
"@types/semver": "^7.5.8",
|
|
39
35
|
"@types/web-bluetooth": "^0.0.20",
|
|
40
36
|
"@typescript-eslint/eslint-plugin": "^8.6.0",
|
|
41
37
|
"eslint": "^9.4.0",
|
|
42
|
-
"
|
|
43
|
-
"react-native-ble-plx": "^3.2.1",
|
|
44
|
-
"ts-jest": "^29.1.4",
|
|
38
|
+
"react-native-ble-plx": "^3.2.0",
|
|
45
39
|
"typescript": "^5.4.5"
|
|
46
40
|
},
|
|
47
41
|
"dependencies": {
|
|
48
42
|
"@types/text-encoding": "^0.0.39",
|
|
49
|
-
"axios": "^1.7.2",
|
|
50
43
|
"isomorphic-ws": "^5.0.0",
|
|
51
|
-
"python-struct": "^1.1.3",
|
|
52
|
-
"semver": "^7.6.2",
|
|
53
44
|
"text-encoding": "^0.7.0",
|
|
54
45
|
"tslib": "^2.6.2"
|
|
55
46
|
},
|
|
@@ -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`);
|
|
@@ -123,7 +126,7 @@ export default class AttributeHandler {
|
|
|
123
126
|
|
|
124
127
|
// Handle the timestamps with increments if specified
|
|
125
128
|
const timeIncUs: number = pollRespMetadata.us ? pollRespMetadata.us : 1000;
|
|
126
|
-
|
|
129
|
+
const timestampsUs = Array(numNewDataPoints).fill(0);
|
|
127
130
|
for (let i = 0; i < numNewDataPoints; i++) {
|
|
128
131
|
timestampsUs[i] = timestampUs + i * timeIncUs;
|
|
129
132
|
}
|
|
@@ -141,7 +144,7 @@ export default class AttributeHandler {
|
|
|
141
144
|
return msgDataStartIdx+pollRespSizeBytes;
|
|
142
145
|
}
|
|
143
146
|
|
|
144
|
-
private processMsgAttribute(attrDef: DeviceTypeAttribute, msgBuffer:
|
|
147
|
+
private processMsgAttribute(attrDef: DeviceTypeAttribute, msgBuffer: Uint8Array, msgBufIdx: number, msgDataStartIdx: number): { values: number[], newMsgBufIdx: number} {
|
|
145
148
|
|
|
146
149
|
// Current field message string index
|
|
147
150
|
let curFieldBufIdx = msgBufIdx;
|
|
@@ -169,11 +172,11 @@ export default class AttributeHandler {
|
|
|
169
172
|
const maskOnSignedValue = "m" in attrDef && isAttrTypeSigned(attrTypesOnly);
|
|
170
173
|
|
|
171
174
|
// Extract the value using python-struct
|
|
172
|
-
|
|
175
|
+
const unpackValues = structUnpack(maskOnSignedValue ? attrTypesOnly.toUpperCase() : attrTypesOnly, attrBuf);
|
|
173
176
|
let attrValues = unpackValues as number[];
|
|
174
177
|
|
|
175
178
|
// Get number of bytes consumed
|
|
176
|
-
const numBytesConsumed =
|
|
179
|
+
const numBytesConsumed = structSizeOf(attrTypesOnly);
|
|
177
180
|
|
|
178
181
|
// // Check if sign extendable mask specified on signed value
|
|
179
182
|
// if (mmSpecifiedOnSignedValue) {
|
|
@@ -252,7 +255,7 @@ export default class AttributeHandler {
|
|
|
252
255
|
return value;
|
|
253
256
|
}
|
|
254
257
|
|
|
255
|
-
private extractTimestampAndAdvanceIdx(msgBuffer:
|
|
258
|
+
private extractTimestampAndAdvanceIdx(msgBuffer: Uint8Array, msgBufIdx: number, timestampWrapHandler: DeviceTimeline):
|
|
256
259
|
{ newBufIdx: number, timestampUs: number } {
|
|
257
260
|
|
|
258
261
|
// Check there are enough characters for the timestamp
|
|
@@ -264,9 +267,9 @@ export default class AttributeHandler {
|
|
|
264
267
|
const tsBuffer = msgBuffer.slice(msgBufIdx, msgBufIdx + this.POLL_RESULT_TIMESTAMP_SIZE);
|
|
265
268
|
let timestampUs: number;
|
|
266
269
|
if (this.POLL_RESULT_TIMESTAMP_SIZE === 2) {
|
|
267
|
-
timestampUs =
|
|
270
|
+
timestampUs = structUnpack(">H", tsBuffer)[0] as number * this.POLL_RESULT_RESOLUTION_US;
|
|
268
271
|
} else {
|
|
269
|
-
timestampUs =
|
|
272
|
+
timestampUs = structUnpack(">I", tsBuffer)[0] as number * this.POLL_RESULT_RESOLUTION_US;
|
|
270
273
|
}
|
|
271
274
|
|
|
272
275
|
// Check if time is before lastReportTimeMs - in which case a wrap around occurred to add on the max value
|
|
@@ -14,28 +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 CmdUUID = "aa76677e-9cfd-4626-a510-0d305be57c8e";
|
|
26
|
-
public static RespUUID = "aa76677e-9cfd-4626-a510-0d305be57c8f";
|
|
27
|
-
|
|
28
|
-
// BleManager
|
|
29
|
-
|
|
30
23
|
|
|
31
24
|
// Conn event fn
|
|
32
25
|
private _onConnEvent: RaftConnEventFn | null = null;
|
|
33
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
|
+
|
|
34
32
|
// BLE Scanner
|
|
35
|
-
_bleScanner:
|
|
33
|
+
_bleScanner: RaftChannelBLEScanner;
|
|
36
34
|
|
|
37
|
-
//
|
|
38
|
-
|
|
35
|
+
// Device to connect to using BLE
|
|
36
|
+
_deviceToConnectTo: DiscoveredDevice | null = null;
|
|
39
37
|
|
|
40
38
|
// Handle BLE disconnection through retry
|
|
41
39
|
// May be set after connection checking (e.g. using LEDs)
|
|
@@ -48,11 +46,6 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
48
46
|
_bleSubscrOnStateChange: Subscription | null = null;
|
|
49
47
|
_bleDevice: Device | null = null;
|
|
50
48
|
|
|
51
|
-
// BLE UUIDS
|
|
52
|
-
_RICServiceUUID = 'aa76677e-9cfd-4626-a510-0d305be57c8d';
|
|
53
|
-
_RICCmdUUID = 'aa76677e-9cfd-4626-a510-0d305be57c8e';
|
|
54
|
-
_RICRespUUID = 'aa76677e-9cfd-4626-a510-0d305be57c8f';
|
|
55
|
-
|
|
56
49
|
// MTU (Maximum Transmission Unit) size to request
|
|
57
50
|
MTU_SIZE_TO_REQUEST = 512;
|
|
58
51
|
|
|
@@ -64,10 +57,6 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
64
57
|
private _msgTxMinTimeBetweenMs = 1;
|
|
65
58
|
private readonly maxRetries = 1;
|
|
66
59
|
|
|
67
|
-
// Connected flag and retries
|
|
68
|
-
private _isConnected = false;
|
|
69
|
-
// private readonly _maxConnRetries = 3;
|
|
70
|
-
|
|
71
60
|
// File Handler parameters
|
|
72
61
|
private _requestedBatchAckSize = 10;
|
|
73
62
|
private _requestedFileBlockSize = 500;
|
|
@@ -78,9 +67,9 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
78
67
|
// _bleManager.setLogLevel(LogLevel.Verbose);
|
|
79
68
|
|
|
80
69
|
// Scanner
|
|
81
|
-
this._bleScanner = new
|
|
70
|
+
this._bleScanner = new RaftChannelBLEScanner(
|
|
82
71
|
_bleManager,
|
|
83
|
-
|
|
72
|
+
this._serviceUUIDs,
|
|
84
73
|
this.scanningEvent.bind(this),
|
|
85
74
|
);
|
|
86
75
|
|
|
@@ -102,7 +91,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
102
91
|
|
|
103
92
|
// isConnected
|
|
104
93
|
isConnected(): boolean {
|
|
105
|
-
return this._bleDevice !== null
|
|
94
|
+
return this._bleDevice !== null;
|
|
106
95
|
}
|
|
107
96
|
|
|
108
97
|
setOnConnEvent(connEventFn: RaftConnEventFn): void {
|
|
@@ -127,12 +116,12 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
127
116
|
this._retryConnectionIfLost = retry;
|
|
128
117
|
}
|
|
129
118
|
|
|
130
|
-
async discoveryStart(): Promise<boolean> {
|
|
119
|
+
async discoveryStart(uuids: string[]): Promise<boolean> {
|
|
131
120
|
// Disconnect any existing connection
|
|
132
121
|
// await this.disconnect();
|
|
133
122
|
|
|
134
123
|
// Start scanning
|
|
135
|
-
await this._bleScanner.scanningStart();
|
|
124
|
+
await this._bleScanner.scanningStart(uuids);
|
|
136
125
|
|
|
137
126
|
// Event
|
|
138
127
|
RaftLog.debug(`BLEChannel discoveryStart emitting BLE_SCANNING_STARTED`);
|
|
@@ -203,16 +192,16 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
203
192
|
* @returns boolean (true if connected)
|
|
204
193
|
*
|
|
205
194
|
*/
|
|
206
|
-
async connect(
|
|
195
|
+
async connect(discoveredDevice: DiscoveredDevice): Promise<boolean> {
|
|
207
196
|
RaftLog.debug('BLEChannel requested connection');
|
|
208
197
|
this._retryConnectionIfLost = false;
|
|
209
198
|
this._bleScanner.scanningStop();
|
|
210
199
|
|
|
211
200
|
// Now connecting
|
|
212
|
-
this.emit(RaftConnEvent.CONN_CONNECTING, { deviceId:
|
|
201
|
+
this.emit(RaftConnEvent.CONN_CONNECTING, { deviceId: discoveredDevice.id });
|
|
213
202
|
|
|
214
203
|
// Connect
|
|
215
|
-
this.
|
|
204
|
+
this._deviceToConnectTo = discoveredDevice;
|
|
216
205
|
const connOk = await this._configDeviceConnection();
|
|
217
206
|
|
|
218
207
|
// Check if ok
|
|
@@ -224,8 +213,8 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
224
213
|
|
|
225
214
|
// Emit success
|
|
226
215
|
this.emit(RaftConnEvent.CONN_CONNECTED, {
|
|
227
|
-
deviceId: this.
|
|
228
|
-
name: this.
|
|
216
|
+
deviceId: this._deviceToConnectTo.id,
|
|
217
|
+
name: this._deviceToConnectTo.name,
|
|
229
218
|
});
|
|
230
219
|
return true;
|
|
231
220
|
}
|
|
@@ -239,9 +228,9 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
239
228
|
async disconnect(): Promise<void> {
|
|
240
229
|
RaftLog.debug('BLEChannel disconnect requested');
|
|
241
230
|
this._retryConnectionIfLost = false;
|
|
242
|
-
RaftLog.debug(`this._ricToConnectTo ${this.
|
|
243
|
-
const connectedRIC = this.
|
|
244
|
-
this.
|
|
231
|
+
RaftLog.debug(`this._ricToConnectTo ${this._deviceToConnectTo}`);
|
|
232
|
+
const connectedRIC = this._deviceToConnectTo;
|
|
233
|
+
this._deviceToConnectTo = null;
|
|
245
234
|
|
|
246
235
|
// this._invalidateConnectionInfo(); // //
|
|
247
236
|
|
|
@@ -251,9 +240,9 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
251
240
|
this._bleSubscrOnDisconnect = null;
|
|
252
241
|
}
|
|
253
242
|
// Disconnect from the connected device
|
|
254
|
-
const connMarties = await _bleManager.connectedDevices(
|
|
255
|
-
this.
|
|
256
|
-
|
|
243
|
+
const connMarties = await _bleManager.connectedDevices(
|
|
244
|
+
this._serviceUUIDs
|
|
245
|
+
);
|
|
257
246
|
if (connMarties.length == 0) {
|
|
258
247
|
RaftLog.debug('BLEChannel disconnect - no devices connected');
|
|
259
248
|
} else {
|
|
@@ -280,7 +269,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
280
269
|
*/
|
|
281
270
|
async _configDeviceConnection(): Promise<boolean> {
|
|
282
271
|
// Check there is a RIC to connect to
|
|
283
|
-
if (this.
|
|
272
|
+
if (this._deviceToConnectTo === null) {
|
|
284
273
|
return false;
|
|
285
274
|
}
|
|
286
275
|
|
|
@@ -288,7 +277,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
288
277
|
for (let connRetry = 0; connRetry < 5; connRetry++) {
|
|
289
278
|
try {
|
|
290
279
|
deviceConnected = await _bleManager.connectToDevice(
|
|
291
|
-
this.
|
|
280
|
+
this._deviceToConnectTo.id,
|
|
292
281
|
{
|
|
293
282
|
timeout: 3000,
|
|
294
283
|
},
|
|
@@ -325,7 +314,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
325
314
|
// Request high-priority connection
|
|
326
315
|
try {
|
|
327
316
|
await _bleManager.requestConnectionPriorityForDevice(
|
|
328
|
-
this.
|
|
317
|
+
this._deviceToConnectTo.id,
|
|
329
318
|
ConnectionPriority.High,
|
|
330
319
|
);
|
|
331
320
|
RaftLog.debug(
|
|
@@ -352,10 +341,10 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
352
341
|
}
|
|
353
342
|
// Monitor the inbound characteristic
|
|
354
343
|
try {
|
|
355
|
-
if (this._bleDevice) {
|
|
344
|
+
if (this._bleDevice && this._bleDevice.serviceUUIDs) {
|
|
356
345
|
this._bleSubscrOnRx = this._bleDevice.monitorCharacteristicForService(
|
|
357
|
-
this.
|
|
358
|
-
this.
|
|
346
|
+
this._bleDevice.serviceUUIDs[0],
|
|
347
|
+
this._respUUID,
|
|
359
348
|
(error: BleError | null, characteristic: Characteristic | null) => {
|
|
360
349
|
this._onMsgRx(error, characteristic);
|
|
361
350
|
},
|
|
@@ -425,15 +414,15 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
425
414
|
// Check if scan in progress - and stop reconn attempts if so
|
|
426
415
|
const scanInProgress = this._bleScanner.isScanInProgress();
|
|
427
416
|
RaftLog.debug(
|
|
428
|
-
`onDeviceDisconnected considering reconnection RICToConnectTo ${this.
|
|
417
|
+
`onDeviceDisconnected considering reconnection RICToConnectTo ${this._deviceToConnectTo?.name} scanInProgress ${scanInProgress} retryConnectionIfLost ${this._retryConnectionIfLost}`,
|
|
429
418
|
);
|
|
430
419
|
if (
|
|
431
420
|
!this._retryConnectionIfLost ||
|
|
432
421
|
scanInProgress ||
|
|
433
|
-
!this.
|
|
422
|
+
!this._deviceToConnectTo
|
|
434
423
|
) {
|
|
435
424
|
RaftLog.debug(
|
|
436
|
-
`onDeviceDisconnected DISCONNECTED_RIC RICToConnectTo ${this.
|
|
425
|
+
`onDeviceDisconnected DISCONNECTED_RIC RICToConnectTo ${this._deviceToConnectTo?.name} scanInProgress ${scanInProgress} retryConnectionIfLost ${this._retryConnectionIfLost}`,
|
|
437
426
|
);
|
|
438
427
|
if (this._retryConnectionIfLost) {
|
|
439
428
|
// this.emit(RaftConnEvent.BLE_DISCONNECTED_RIC);
|
|
@@ -444,7 +433,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
444
433
|
}
|
|
445
434
|
if (await this._configDeviceConnection()) {
|
|
446
435
|
RaftLog.debug(
|
|
447
|
-
`onDeviceDisconnected successful reconn RICToConnectTo ${this.
|
|
436
|
+
`onDeviceDisconnected successful reconn RICToConnectTo ${this._deviceToConnectTo?.name}`,
|
|
448
437
|
);
|
|
449
438
|
|
|
450
439
|
// Indicate connection issue resolved
|
|
@@ -454,7 +443,7 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
454
443
|
return;
|
|
455
444
|
}
|
|
456
445
|
RaftLog.debug(
|
|
457
|
-
`onDeviceDisconnected retrying reconn RICToConnectTo ${this.
|
|
446
|
+
`onDeviceDisconnected retrying reconn RICToConnectTo ${this._deviceToConnectTo?.name}`,
|
|
458
447
|
);
|
|
459
448
|
}
|
|
460
449
|
},
|
|
@@ -521,12 +510,12 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
521
510
|
|
|
522
511
|
try {
|
|
523
512
|
await this._bleDevice.writeCharacteristicWithoutResponseForService(
|
|
524
|
-
this.
|
|
525
|
-
this.
|
|
513
|
+
this._bleDevice.serviceUUIDs ? this._bleDevice.serviceUUIDs[0] : this._serviceUUIDs[0],
|
|
514
|
+
this._cmdUUID,
|
|
526
515
|
msgFrameBase64!,
|
|
527
516
|
);
|
|
528
517
|
return true;
|
|
529
|
-
} catch
|
|
518
|
+
} catch {
|
|
530
519
|
if (retryIdx === this.maxRetries - 1) {
|
|
531
520
|
RaftLog.debug(`sendTxMsg failed after ${this.maxRetries} attempts`);
|
|
532
521
|
return false;
|
|
@@ -560,12 +549,12 @@ export default class RaftChannelPhoneBLE implements RaftChannel {
|
|
|
560
549
|
|
|
561
550
|
try {
|
|
562
551
|
this._bleDevice!.writeCharacteristicWithoutResponseForService(
|
|
563
|
-
this.
|
|
564
|
-
this.
|
|
552
|
+
this._bleDevice.serviceUUIDs ? this._bleDevice.serviceUUIDs[0] : this._serviceUUIDs[0],
|
|
553
|
+
this._cmdUUID,
|
|
565
554
|
msgFrameBase64!,
|
|
566
555
|
);
|
|
567
556
|
return true;
|
|
568
|
-
} catch
|
|
557
|
+
} catch {
|
|
569
558
|
if (retryIdx === this.maxRetries - 1) {
|
|
570
559
|
RaftLog.debug(`sendTxMsgNoAwait failed after ${this.maxRetries} attempts`);
|
|
571
560
|
return false;
|