@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.
Files changed (110) hide show
  1. package/dist/react-native/RaftAttributeHandler.d.ts +1 -1
  2. package/dist/react-native/RaftAttributeHandler.js +12 -9
  3. package/dist/react-native/RaftAttributeHandler.js.map +1 -1
  4. package/dist/react-native/RaftChannelBLE.native.d.ts +9 -13
  5. package/dist/react-native/RaftChannelBLE.native.js +33 -43
  6. package/dist/react-native/RaftChannelBLE.native.js.map +1 -1
  7. package/dist/react-native/RaftChannelBLE.web.d.ts +3 -3
  8. package/dist/react-native/RaftChannelBLE.web.js +23 -7
  9. package/dist/react-native/RaftChannelBLE.web.js.map +1 -1
  10. package/dist/react-native/RaftChannelBLEScanner.native.d.ts +6 -6
  11. package/dist/react-native/RaftChannelBLEScanner.native.js +38 -39
  12. package/dist/react-native/RaftChannelBLEScanner.native.js.map +1 -1
  13. package/dist/react-native/RaftConnector.d.ts +3 -3
  14. package/dist/react-native/RaftConnector.js +3 -3
  15. package/dist/react-native/RaftConnector.js.map +1 -1
  16. package/dist/react-native/RaftCustomAttrHandler.d.ts +1 -1
  17. package/dist/react-native/RaftCustomAttrHandler.js +4 -4
  18. package/dist/react-native/RaftCustomAttrHandler.js.map +1 -1
  19. package/dist/react-native/RaftDeviceManager.d.ts +3 -3
  20. package/dist/react-native/RaftDeviceManager.js +12 -9
  21. package/dist/react-native/RaftDeviceManager.js.map +1 -1
  22. package/dist/react-native/RaftDeviceMgrIF.d.ts +2 -3
  23. package/dist/react-native/RaftDeviceStates.js +1 -1
  24. package/dist/react-native/RaftDeviceStates.js.map +1 -1
  25. package/dist/react-native/RaftMsgHandler.js +1 -2
  26. package/dist/react-native/RaftMsgHandler.js.map +1 -1
  27. package/dist/react-native/RaftStruct.d.ts +3 -0
  28. package/dist/react-native/RaftStruct.js +207 -0
  29. package/dist/react-native/RaftStruct.js.map +1 -0
  30. package/dist/react-native/RaftSysTypeManager.d.ts +14 -0
  31. package/dist/react-native/RaftSysTypeManager.js +53 -0
  32. package/dist/react-native/RaftSysTypeManager.js.map +1 -0
  33. package/dist/react-native/RaftSystemType.d.ts +4 -0
  34. package/dist/react-native/RaftTypes.d.ts +2 -2
  35. package/dist/react-native/RaftTypes.js +3 -3
  36. package/dist/react-native/RaftTypes.js.map +1 -1
  37. package/dist/react-native/RaftUpdateManager.js +6 -3
  38. package/dist/react-native/RaftUpdateManager.js.map +1 -1
  39. package/dist/react-native/RaftUtils.d.ts +1 -0
  40. package/dist/react-native/RaftUtils.js +17 -4
  41. package/dist/react-native/RaftUtils.js.map +1 -1
  42. package/dist/react-native/main.d.ts +1 -0
  43. package/dist/react-native/main.js +3 -1
  44. package/dist/react-native/main.js.map +1 -1
  45. package/dist/web/RaftAttributeHandler.d.ts +1 -1
  46. package/dist/web/RaftAttributeHandler.js +12 -9
  47. package/dist/web/RaftAttributeHandler.js.map +1 -1
  48. package/dist/web/RaftChannelBLE.web.d.ts +3 -3
  49. package/dist/web/RaftChannelBLE.web.js +23 -7
  50. package/dist/web/RaftChannelBLE.web.js.map +1 -1
  51. package/dist/web/RaftConnector.d.ts +3 -3
  52. package/dist/web/RaftConnector.js +3 -3
  53. package/dist/web/RaftConnector.js.map +1 -1
  54. package/dist/web/RaftCustomAttrHandler.d.ts +1 -1
  55. package/dist/web/RaftCustomAttrHandler.js +4 -4
  56. package/dist/web/RaftCustomAttrHandler.js.map +1 -1
  57. package/dist/web/RaftDeviceManager.d.ts +3 -3
  58. package/dist/web/RaftDeviceManager.js +12 -9
  59. package/dist/web/RaftDeviceManager.js.map +1 -1
  60. package/dist/web/RaftDeviceMgrIF.d.ts +2 -3
  61. package/dist/web/RaftDeviceStates.js +1 -1
  62. package/dist/web/RaftDeviceStates.js.map +1 -1
  63. package/dist/web/RaftMsgHandler.js +1 -2
  64. package/dist/web/RaftMsgHandler.js.map +1 -1
  65. package/dist/web/RaftStruct.d.ts +3 -0
  66. package/dist/web/RaftStruct.js +207 -0
  67. package/dist/web/RaftStruct.js.map +1 -0
  68. package/dist/web/RaftSysTypeManager.d.ts +14 -0
  69. package/dist/web/RaftSysTypeManager.js +53 -0
  70. package/dist/web/RaftSysTypeManager.js.map +1 -0
  71. package/dist/web/RaftSystemType.d.ts +4 -0
  72. package/dist/web/RaftTypes.d.ts +2 -2
  73. package/dist/web/RaftTypes.js +3 -3
  74. package/dist/web/RaftTypes.js.map +1 -1
  75. package/dist/web/RaftUpdateManager.js +6 -3
  76. package/dist/web/RaftUpdateManager.js.map +1 -1
  77. package/dist/web/RaftUtils.d.ts +1 -0
  78. package/dist/web/RaftUtils.js +17 -4
  79. package/dist/web/RaftUtils.js.map +1 -1
  80. package/dist/web/main.d.ts +1 -0
  81. package/dist/web/main.js +3 -1
  82. package/dist/web/main.js.map +1 -1
  83. package/examples/dashboard/package.json +2 -12
  84. package/examples/dashboard/src/ConnManager.ts +13 -21
  85. package/examples/dashboard/src/Main.tsx +15 -7
  86. package/examples/dashboard/src/SystemTypeCog/CogStateInfo.ts +5 -10
  87. package/examples/dashboard/src/SystemTypeCog/SystemTypeCog.ts +3 -0
  88. package/examples/dashboard/src/SystemTypeGeneric/StateInfoGeneric.ts +24 -0
  89. package/examples/dashboard/src/SystemTypeGeneric/SystemTypeGeneric.ts +87 -0
  90. package/examples/dashboard/src/SystemTypeMarty/SystemTypeMarty.ts +3 -0
  91. package/examples/dashboard/src/index.tsx +0 -2
  92. package/examples/dashboard/src/styles.css +13 -2
  93. package/package.json +2 -11
  94. package/src/RaftAttributeHandler.ts +16 -13
  95. package/src/RaftChannelBLE.native.ts +43 -54
  96. package/src/RaftChannelBLE.web.ts +45 -28
  97. package/src/RaftChannelBLEScanner.native.ts +37 -39
  98. package/src/RaftConnector.ts +3 -3
  99. package/src/RaftCustomAttrHandler.ts +5 -5
  100. package/src/RaftDeviceManager.ts +16 -14
  101. package/src/RaftDeviceMgrIF.ts +2 -3
  102. package/src/RaftDeviceStates.ts +1 -1
  103. package/src/RaftMsgHandler.ts +1 -2
  104. package/src/RaftStruct.ts +208 -0
  105. package/src/RaftSysTypeManager.ts +60 -0
  106. package/src/RaftSystemType.ts +4 -0
  107. package/src/RaftTypes.ts +2 -2
  108. package/src/RaftUpdateManager.ts +6 -3
  109. package/src/RaftUtils.ts +14 -4
  110. 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();
@@ -2,8 +2,6 @@ import React from 'react';
2
2
  import { createRoot } from 'react-dom/client';
3
3
  import Main from './Main';
4
4
  import './styles.css';
5
- import 'process';
6
- import 'buffer';
7
5
 
8
6
  // Find the root element
9
7
  const rootElement = document.getElementById('root');
@@ -68,7 +68,13 @@ h1 {
68
68
  }
69
69
 
70
70
  .action-button {
71
- margin-left: 10px;
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
- width: 100%;
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.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
- "jest": "^29.7.0",
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 struct from 'python-struct';
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: Buffer, msgBufIdx: number, deviceTimeline: DeviceTimeline, pollRespMetadata: DeviceTypePollRespMetadata,
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
- let { newBufIdx, timestampUs } = this.extractTimestampAndAdvanceIdx(msgBuffer, msgBufIdx, deviceTimeline);
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
- const pollRespSizeBytes = pollRespMetadata.b;
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
- let numNewDataPoints = newAttrValues[0].length;
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
- let timestampsUs = Array(numNewDataPoints).fill(0);
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: Buffer, msgBufIdx: number, msgDataStartIdx: number): { values: number[], newMsgBufIdx: number} {
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
- let unpackValues = struct.unpack(maskOnSignedValue ? attrTypesOnly.toUpperCase() : attrTypesOnly, attrBuf);
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 = struct.sizeOf(attrTypesOnly);
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: Buffer, msgBufIdx: number, timestampWrapHandler: DeviceTimeline):
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 = struct.unpack(">H", tsBuffer)[0] as number * this.POLL_RESULT_RESOLUTION_US;
270
+ timestampUs = structUnpack(">H", tsBuffer)[0] as number * this.POLL_RESULT_RESOLUTION_US;
268
271
  } else {
269
- timestampUs = struct.unpack(">I", tsBuffer)[0] as number * this.POLL_RESULT_RESOLUTION_US;
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 RICBLEScanner from "./RaftChannelBLEScanner.native";
18
- import { DiscoveredRIC } from "./RaftTypes";
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: RICBLEScanner;
33
+ _bleScanner: RaftChannelBLEScanner;
36
34
 
37
- // RIC to connect to using BLE
38
- _ricToConnectTo: DiscoveredRIC | null = null;
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 RICBLEScanner(
70
+ this._bleScanner = new RaftChannelBLEScanner(
82
71
  _bleManager,
83
- [this._RICServiceUUID],
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 && this._isConnected;
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(discoveredRIC: DiscoveredRIC): Promise<boolean> {
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: discoveredRIC.id });
201
+ this.emit(RaftConnEvent.CONN_CONNECTING, { deviceId: discoveredDevice.id });
213
202
 
214
203
  // Connect
215
- this._ricToConnectTo = discoveredRIC;
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._ricToConnectTo.id,
228
- name: this._ricToConnectTo.name,
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._ricToConnectTo}`);
243
- const connectedRIC = this._ricToConnectTo;
244
- this._ricToConnectTo = null;
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._RICServiceUUID,
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._ricToConnectTo === null) {
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._ricToConnectTo.id,
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._ricToConnectTo.id,
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._RICServiceUUID,
358
- this._RICRespUUID,
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._ricToConnectTo?.name} scanInProgress ${scanInProgress} retryConnectionIfLost ${this._retryConnectionIfLost}`,
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._ricToConnectTo
422
+ !this._deviceToConnectTo
434
423
  ) {
435
424
  RaftLog.debug(
436
- `onDeviceDisconnected DISCONNECTED_RIC RICToConnectTo ${this._ricToConnectTo?.name} scanInProgress ${scanInProgress} retryConnectionIfLost ${this._retryConnectionIfLost}`,
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._ricToConnectTo?.name}`,
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._ricToConnectTo?.name}`,
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._RICServiceUUID,
525
- this._RICCmdUUID,
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 (error) {
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._RICServiceUUID,
564
- this._RICCmdUUID,
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 (error) {
557
+ } catch {
569
558
  if (retryIdx === this.maxRetries - 1) {
570
559
  RaftLog.debug(`sendTxMsgNoAwait failed after ${this.maxRetries} attempts`);
571
560
  return false;