@robdobsn/raftjs 1.7.2 → 1.7.3

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 (188) hide show
  1. package/dist/web/RaftAttributeHandler.js +5 -5
  2. package/dist/web/RaftAttributeHandler.js.map +1 -1
  3. package/package.json +5 -2
  4. package/.editorconfig +0 -14
  5. package/.gitattributes +0 -11
  6. package/.nvmrc +0 -1
  7. package/TODO.md +0 -1
  8. package/dist/react-native/RaftAttributeHandler.d.ts +0 -12
  9. package/dist/react-native/RaftAttributeHandler.js +0 -249
  10. package/dist/react-native/RaftAttributeHandler.js.map +0 -1
  11. package/dist/react-native/RaftChannel.d.ts +0 -20
  12. package/dist/react-native/RaftChannel.js +0 -12
  13. package/dist/react-native/RaftChannel.js.map +0 -1
  14. package/dist/react-native/RaftChannelBLE.native.d.ts +0 -95
  15. package/dist/react-native/RaftChannelBLE.native.js +0 -482
  16. package/dist/react-native/RaftChannelBLE.native.js.map +0 -1
  17. package/dist/react-native/RaftChannelBLE.web.d.ts +0 -40
  18. package/dist/react-native/RaftChannelBLE.web.js +0 -300
  19. package/dist/react-native/RaftChannelBLE.web.js.map +0 -1
  20. package/dist/react-native/RaftChannelBLEFactory.d.ts +0 -10
  21. package/dist/react-native/RaftChannelBLEFactory.js +0 -17
  22. package/dist/react-native/RaftChannelBLEFactory.js.map +0 -1
  23. package/dist/react-native/RaftChannelBLEScanner.native.d.ts +0 -18
  24. package/dist/react-native/RaftChannelBLEScanner.native.js +0 -138
  25. package/dist/react-native/RaftChannelBLEScanner.native.js.map +0 -1
  26. package/dist/react-native/RaftChannelSimulated.d.ts +0 -32
  27. package/dist/react-native/RaftChannelSimulated.js +0 -418
  28. package/dist/react-native/RaftChannelSimulated.js.map +0 -1
  29. package/dist/react-native/RaftChannelWebSerial.d.ts +0 -39
  30. package/dist/react-native/RaftChannelWebSerial.js +0 -329
  31. package/dist/react-native/RaftChannelWebSerial.js.map +0 -1
  32. package/dist/react-native/RaftChannelWebSocket.d.ts +0 -30
  33. package/dist/react-native/RaftChannelWebSocket.js +0 -207
  34. package/dist/react-native/RaftChannelWebSocket.js.map +0 -1
  35. package/dist/react-native/RaftCommsStats.d.ts +0 -39
  36. package/dist/react-native/RaftCommsStats.js +0 -128
  37. package/dist/react-native/RaftCommsStats.js.map +0 -1
  38. package/dist/react-native/RaftConnEvents.d.ts +0 -39
  39. package/dist/react-native/RaftConnEvents.js +0 -54
  40. package/dist/react-native/RaftConnEvents.js.map +0 -1
  41. package/dist/react-native/RaftConnector.d.ts +0 -245
  42. package/dist/react-native/RaftConnector.js +0 -621
  43. package/dist/react-native/RaftConnector.js.map +0 -1
  44. package/dist/react-native/RaftCustomAttrHandler.d.ts +0 -4
  45. package/dist/react-native/RaftCustomAttrHandler.js +0 -50
  46. package/dist/react-native/RaftCustomAttrHandler.js.map +0 -1
  47. package/dist/react-native/RaftDeviceInfo.d.ts +0 -63
  48. package/dist/react-native/RaftDeviceInfo.js +0 -36
  49. package/dist/react-native/RaftDeviceInfo.js.map +0 -1
  50. package/dist/react-native/RaftDeviceManager.d.ts +0 -40
  51. package/dist/react-native/RaftDeviceManager.js +0 -499
  52. package/dist/react-native/RaftDeviceManager.js.map +0 -1
  53. package/dist/react-native/RaftDeviceMgrIF.d.ts +0 -15
  54. package/dist/react-native/RaftDeviceMgrIF.js +0 -11
  55. package/dist/react-native/RaftDeviceMgrIF.js.map +0 -1
  56. package/dist/react-native/RaftDeviceMsg.d.ts +0 -9
  57. package/dist/react-native/RaftDeviceMsg.js +0 -11
  58. package/dist/react-native/RaftDeviceMsg.js.map +0 -1
  59. package/dist/react-native/RaftDeviceStates.d.ts +0 -37
  60. package/dist/react-native/RaftDeviceStates.js +0 -60
  61. package/dist/react-native/RaftDeviceStates.js.map +0 -1
  62. package/dist/react-native/RaftFileHandler.d.ts +0 -52
  63. package/dist/react-native/RaftFileHandler.js +0 -502
  64. package/dist/react-native/RaftFileHandler.js.map +0 -1
  65. package/dist/react-native/RaftLog.d.ts +0 -22
  66. package/dist/react-native/RaftLog.js +0 -63
  67. package/dist/react-native/RaftLog.js.map +0 -1
  68. package/dist/react-native/RaftMiniHDLC.d.ts +0 -18
  69. package/dist/react-native/RaftMiniHDLC.js +0 -383
  70. package/dist/react-native/RaftMiniHDLC.js.map +0 -1
  71. package/dist/react-native/RaftMsgHandler.d.ts +0 -62
  72. package/dist/react-native/RaftMsgHandler.js +0 -511
  73. package/dist/react-native/RaftMsgHandler.js.map +0 -1
  74. package/dist/react-native/RaftMsgTrackInfo.d.ts +0 -17
  75. package/dist/react-native/RaftMsgTrackInfo.js +0 -42
  76. package/dist/react-native/RaftMsgTrackInfo.js.map +0 -1
  77. package/dist/react-native/RaftProtocolDefs.d.ts +0 -30
  78. package/dist/react-native/RaftProtocolDefs.js +0 -48
  79. package/dist/react-native/RaftProtocolDefs.js.map +0 -1
  80. package/dist/react-native/RaftStreamHandler.d.ts +0 -38
  81. package/dist/react-native/RaftStreamHandler.js +0 -257
  82. package/dist/react-native/RaftStreamHandler.js.map +0 -1
  83. package/dist/react-native/RaftStruct.d.ts +0 -3
  84. package/dist/react-native/RaftStruct.js +0 -208
  85. package/dist/react-native/RaftStruct.js.map +0 -1
  86. package/dist/react-native/RaftSysTypeManager.d.ts +0 -14
  87. package/dist/react-native/RaftSysTypeManager.js +0 -53
  88. package/dist/react-native/RaftSysTypeManager.js.map +0 -1
  89. package/dist/react-native/RaftSystemType.d.ts +0 -28
  90. package/dist/react-native/RaftSystemType.js +0 -3
  91. package/dist/react-native/RaftSystemType.js.map +0 -1
  92. package/dist/react-native/RaftSystemUtils.d.ts +0 -136
  93. package/dist/react-native/RaftSystemUtils.js +0 -410
  94. package/dist/react-native/RaftSystemUtils.js.map +0 -1
  95. package/dist/react-native/RaftTypes.d.ts +0 -194
  96. package/dist/react-native/RaftTypes.js +0 -153
  97. package/dist/react-native/RaftTypes.js.map +0 -1
  98. package/dist/react-native/RaftUpdateEvents.d.ts +0 -33
  99. package/dist/react-native/RaftUpdateEvents.js +0 -46
  100. package/dist/react-native/RaftUpdateEvents.js.map +0 -1
  101. package/dist/react-native/RaftUpdateManager.d.ts +0 -61
  102. package/dist/react-native/RaftUpdateManager.js +0 -621
  103. package/dist/react-native/RaftUpdateManager.js.map +0 -1
  104. package/dist/react-native/RaftUtils.d.ts +0 -126
  105. package/dist/react-native/RaftUtils.js +0 -467
  106. package/dist/react-native/RaftUtils.js.map +0 -1
  107. package/dist/react-native/RaftWifiTypes.d.ts +0 -23
  108. package/dist/react-native/RaftWifiTypes.js +0 -43
  109. package/dist/react-native/RaftWifiTypes.js.map +0 -1
  110. package/dist/react-native/main.d.ts +0 -25
  111. package/dist/react-native/main.js +0 -51
  112. package/dist/react-native/main.js.map +0 -1
  113. package/eslint.config.mjs +0 -33
  114. package/examples/dashboard/package.json +0 -36
  115. package/examples/dashboard/src/CommandPanel.tsx +0 -147
  116. package/examples/dashboard/src/ConnManager.ts +0 -164
  117. package/examples/dashboard/src/DeviceActionsForm.tsx +0 -133
  118. package/examples/dashboard/src/DeviceAttrsForm.tsx +0 -49
  119. package/examples/dashboard/src/DeviceLineChart.tsx +0 -163
  120. package/examples/dashboard/src/DevicePanel.tsx +0 -166
  121. package/examples/dashboard/src/DevicesPanel.tsx +0 -58
  122. package/examples/dashboard/src/DispLedGrid.tsx +0 -110
  123. package/examples/dashboard/src/DispOneLed.tsx +0 -20
  124. package/examples/dashboard/src/LatencyTest.ts +0 -130
  125. package/examples/dashboard/src/LatencyTestPanel.tsx +0 -92
  126. package/examples/dashboard/src/Main.tsx +0 -234
  127. package/examples/dashboard/src/SettingsManager.ts +0 -67
  128. package/examples/dashboard/src/SettingsScreen.tsx +0 -174
  129. package/examples/dashboard/src/StatusPanel.tsx +0 -71
  130. package/examples/dashboard/src/SystemTypeCog/CogStateInfo.ts +0 -163
  131. package/examples/dashboard/src/SystemTypeCog/SystemTypeCog.ts +0 -91
  132. package/examples/dashboard/src/SystemTypeGeneric/StateInfoGeneric.ts +0 -30
  133. package/examples/dashboard/src/SystemTypeGeneric/SystemTypeGeneric.ts +0 -91
  134. package/examples/dashboard/src/SystemTypeMarty/RICAddOn.ts +0 -70
  135. package/examples/dashboard/src/SystemTypeMarty/RICAddOnBase.ts +0 -33
  136. package/examples/dashboard/src/SystemTypeMarty/RICAddOnManager.ts +0 -342
  137. package/examples/dashboard/src/SystemTypeMarty/RICCommsStats.ts +0 -170
  138. package/examples/dashboard/src/SystemTypeMarty/RICHWElem.ts +0 -123
  139. package/examples/dashboard/src/SystemTypeMarty/RICLEDPatternChecker.ts +0 -207
  140. package/examples/dashboard/src/SystemTypeMarty/RICROSSerial.ts +0 -464
  141. package/examples/dashboard/src/SystemTypeMarty/RICServoFaultDetector.ts +0 -146
  142. package/examples/dashboard/src/SystemTypeMarty/RICStateInfo.ts +0 -97
  143. package/examples/dashboard/src/SystemTypeMarty/RICSystemUtils.ts +0 -371
  144. package/examples/dashboard/src/SystemTypeMarty/RICTypes.ts +0 -20
  145. package/examples/dashboard/src/SystemTypeMarty/SystemTypeMarty.ts +0 -119
  146. package/examples/dashboard/src/index.html +0 -15
  147. package/examples/dashboard/src/index.tsx +0 -13
  148. package/examples/dashboard/src/styles.css +0 -408
  149. package/examples/dashboard/tsconfig.json +0 -18
  150. package/jest.config.js +0 -11
  151. package/src/RaftAttributeHandler.ts +0 -298
  152. package/src/RaftChannel.ts +0 -32
  153. package/src/RaftChannelBLE.native.ts +0 -616
  154. package/src/RaftChannelBLE.web.ts +0 -372
  155. package/src/RaftChannelBLEFactory.ts +0 -13
  156. package/src/RaftChannelBLEScanner.native.ts +0 -184
  157. package/src/RaftChannelSimulated.ts +0 -482
  158. package/src/RaftChannelWebSerial.ts +0 -420
  159. package/src/RaftChannelWebSocket.ts +0 -258
  160. package/src/RaftCommsStats.ts +0 -142
  161. package/src/RaftConnEvents.ts +0 -58
  162. package/src/RaftConnector.ts +0 -743
  163. package/src/RaftCustomAttrHandler.ts +0 -54
  164. package/src/RaftDeviceInfo.ts +0 -102
  165. package/src/RaftDeviceManager.ts +0 -607
  166. package/src/RaftDeviceMgrIF.ts +0 -33
  167. package/src/RaftDeviceMsg.ts +0 -20
  168. package/src/RaftDeviceStates.ts +0 -92
  169. package/src/RaftFileHandler.ts +0 -668
  170. package/src/RaftLog.ts +0 -70
  171. package/src/RaftMiniHDLC.ts +0 -396
  172. package/src/RaftMsgHandler.ts +0 -813
  173. package/src/RaftMsgTrackInfo.ts +0 -51
  174. package/src/RaftProtocolDefs.ts +0 -46
  175. package/src/RaftStreamHandler.ts +0 -328
  176. package/src/RaftStruct.ts +0 -209
  177. package/src/RaftSysTypeManager.ts +0 -60
  178. package/src/RaftSystemType.ts +0 -32
  179. package/src/RaftSystemUtils.ts +0 -487
  180. package/src/RaftTypes.ts +0 -278
  181. package/src/RaftUpdateEvents.ts +0 -48
  182. package/src/RaftUpdateManager.ts +0 -781
  183. package/src/RaftUtils.ts +0 -494
  184. package/src/RaftWifiTypes.ts +0 -36
  185. package/src/main.ts +0 -38
  186. package/testdata/TestDeviceTypeRecs.json +0 -492
  187. package/tsconfig.json +0 -30
  188. package/tsconfig.react-native.json +0 -29
@@ -1,372 +0,0 @@
1
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
- //
3
- // RaftChannelBLE
4
- // Part of RaftJS
5
- //
6
- // Rob Dobson & Chris Greening 2020-2024
7
- // (C) 2020-2024 All rights reserved
8
- //
9
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10
-
11
- import RaftChannel from "./RaftChannel";
12
- import { RaftConnEvent, RaftConnEventFn } from "./RaftConnEvents";
13
- import RaftLog from "./RaftLog";
14
- import RaftMsgHandler from "./RaftMsgHandler";
15
- import { ConnectorOptions } from "./RaftSystemType";
16
- import RaftUtils from "./RaftUtils";
17
-
18
- export default class RaftChannelBLE implements RaftChannel {
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
- // Device and characteristics
26
- private _bleDevice: BluetoothDevice | null = null;
27
- private _characteristicTx: BluetoothRemoteGATTCharacteristic | null = null;
28
- private _characteristicRx: BluetoothRemoteGATTCharacteristic | null = null;
29
-
30
- // Message handler
31
- private _raftMsgHandler: RaftMsgHandler | null = null;
32
-
33
- // Conn event fn
34
- private _onConnEvent: RaftConnEventFn | null = null;
35
-
36
- // Last message tx time
37
- private _msgTxTimeLast = Date.now();
38
- private _msgTxMinTimeBetweenMs = 1;
39
- private readonly maxRetries = 1;
40
-
41
- // Connected flag and retries
42
- private _isConnected = false;
43
- private readonly _maxConnRetries = 3;
44
-
45
- // Event listener fn
46
- private _eventListenerFn: ((event: Event) => void) | null = null;
47
-
48
- // File Handler parameters
49
- private _requestedBatchAckSize = 10;
50
- private _requestedFileBlockSize = 500;
51
-
52
- fhBatchAckSize(): number {
53
- return this._requestedBatchAckSize;
54
- }
55
- fhFileBlockSize(): number {
56
- return this._requestedFileBlockSize;
57
- }
58
-
59
- // Set message handler
60
- setMsgHandler(raftMsgHandler: RaftMsgHandler): void {
61
- this._raftMsgHandler = raftMsgHandler;
62
- }
63
-
64
- requiresSubscription(): boolean {
65
- return true;
66
- }
67
-
68
- // RICREST command before disconnect
69
- ricRestCmdBeforeDisconnect(): string | null {
70
- return "bledisconnect";
71
- }
72
-
73
- // isEnabled
74
- isEnabled() {
75
- if (navigator.bluetooth) {
76
- RaftLog.error("Web Bluetooth is supported in your browser.");
77
- return true;
78
- } else {
79
- window.alert(
80
- "Web Bluetooth API is not available.\n" +
81
- 'Please make sure the "Experimental Web Platform features" flag is enabled.'
82
- );
83
- return false;
84
- }
85
- }
86
-
87
- // isConnected
88
- isConnected(): boolean {
89
- return this._bleDevice !== null && this._isConnected;
90
- }
91
-
92
- // Set onConnEvent handler
93
- setOnConnEvent(connEventFn: RaftConnEventFn): void {
94
- this._onConnEvent = connEventFn;
95
- }
96
-
97
- // Disconnection event
98
- onDisconnected(event: Event): void {
99
- const device = event.target as BluetoothDevice;
100
- RaftLog.debug(`RaftChannelBLE.onDisconnected ${device.name}`);
101
- if (this._bleDevice && this._eventListenerFn) {
102
- this._bleDevice.removeEventListener(
103
- "gattserverdisconnected",
104
- this._eventListenerFn
105
- );
106
- }
107
- this._isConnected = false;
108
- if (this._onConnEvent) {
109
- this._onConnEvent(RaftConnEvent.CONN_DISCONNECTED);
110
- }
111
- }
112
-
113
- // Get connected locator
114
- getConnectedLocator(): string | object {
115
- return this._bleDevice || "";
116
- }
117
-
118
- // Connect to a device
119
- async connect(locator: string | object, _connectorOptions: ConnectorOptions): Promise<boolean> {
120
- // RaftLog.debug(`Selected device: ${deviceID}`);
121
- this._bleDevice = locator as BluetoothDevice;
122
- if (this._bleDevice && this._bleDevice.gatt) {
123
- try {
124
- // Connect
125
- for (let connRetry = 0; connRetry < this._maxConnRetries; connRetry++) {
126
- // Connect
127
- const connTimeoutMs = _connectorOptions.connTimeoutMs || 5000;
128
- await RaftUtils.withTimeout(connTimeoutMs, this._bleDevice.gatt.connect());
129
- RaftLog.debug(
130
- `RaftChannelBLE.connect - ${this._bleDevice.gatt.connected ? "OK" : "FAILED"
131
- } attempt ${connRetry + 1} connection to device ${this._bleDevice.name}`
132
- );
133
-
134
- if (this._bleDevice.gatt.connected) {
135
-
136
- // Delay a bit
137
- await new Promise(resolve => setTimeout(resolve, 100));
138
-
139
- // Get service
140
- try {
141
- let service: BluetoothRemoteGATTService | null = null;
142
- // iterate over known services
143
- for (const serviceUUID of this._serviceUUIDs) {
144
- try {
145
- service = await this._bleDevice.gatt.getPrimaryService(serviceUUID);
146
- if (service) {
147
- break;
148
- }
149
- } catch (error) {
150
- RaftLog.warn(
151
- `RaftChannelBLE.connect - cannot get primary service ${error}`
152
- );
153
- }
154
- }
155
-
156
- if (!service) {
157
- RaftLog.error(
158
- `RaftChannelBLE.connect - cannot get primary service - giving up`
159
- );
160
- return false;
161
- }
162
- RaftLog.debug(
163
- `RaftChannelBLE.connect - found service: ${service.uuid}`
164
- );
165
-
166
- try {
167
- // Get Tx and Rx characteristics
168
- this._characteristicTx = await service.getCharacteristic(
169
- this._cmdUUID
170
- );
171
- RaftLog.debug(
172
- `RaftChannelBLE.connect - found char ${this._characteristicTx.uuid}`
173
- );
174
- this._characteristicRx = await service.getCharacteristic(
175
- this._respUUID
176
- );
177
- RaftLog.debug(
178
- `RaftChannelBLE.connect - found char ${this._characteristicRx.uuid}`
179
- );
180
-
181
- // Notifications of received messages
182
- try {
183
- await this._characteristicRx.startNotifications();
184
- RaftLog.debug(
185
- "RaftChannelBLE.connect - notifications started"
186
- );
187
- this._characteristicRx.addEventListener(
188
- "characteristicvaluechanged",
189
- this._onMsgRx.bind(this)
190
- );
191
- } catch (error) {
192
- RaftLog.debug(
193
- "RaftChannelBLE.connnect - addEventListener failed " + error
194
- );
195
- }
196
-
197
- // Connected ok
198
- RaftLog.debug(`RaftChannelBLE.connect ${this._bleDevice.name}`);
199
-
200
- // Add disconnect listener
201
- this._eventListenerFn = this.onDisconnected.bind(this);
202
- this._bleDevice.addEventListener(
203
- "gattserverdisconnected",
204
- this._eventListenerFn
205
- );
206
-
207
- // Connected
208
- this._isConnected = true;
209
- return true;
210
- } catch (error) {
211
- RaftLog.error(
212
- `RaftChannelBLE.connect - cannot find characteristic: ${error}`
213
- );
214
- }
215
- } catch (error) {
216
- if (connRetry === this._maxConnRetries - 1) {
217
- RaftLog.error(
218
- `RaftChannelBLE.connect - cannot get primary service ${error} - attempt #${connRetry + 1} - giving up`
219
- );
220
- } else {
221
- RaftLog.debug(
222
- `RaftChannelBLE.connect - cannot get primary service - attempt #${connRetry + 1} ${error}`
223
- );
224
- }
225
- }
226
- }
227
- }
228
- } catch (error: unknown) {
229
- RaftLog.warn(`RaftChannelBLE.connect - cannot connect ${error}`);
230
- }
231
-
232
- // Disconnect
233
- if (
234
- this._bleDevice &&
235
- this._bleDevice.gatt &&
236
- this._bleDevice.gatt.connected
237
- ) {
238
- try {
239
- await this._bleDevice.gatt.disconnect();
240
- } catch (error) {
241
- RaftLog.warn(`RaftChannelBLE.connect - cannot disconnect ${error}`);
242
- }
243
- }
244
- }
245
-
246
- return false;
247
- }
248
-
249
- // Disconnect
250
- async disconnect(): Promise<void> {
251
- if (this._bleDevice && this._bleDevice.gatt) {
252
- try {
253
- RaftLog.debug(`RaftChannelBLE.disconnect GATT`);
254
- await this._bleDevice.gatt.disconnect();
255
- } catch (error) {
256
- RaftLog.debug(`RaftChannelBLE.disconnect ${error}`);
257
- }
258
- }
259
- }
260
-
261
- pauseConnection(pause: boolean): void {
262
- RaftLog.verbose(
263
- `pauseConnection ${pause} - no effect for this channel type`
264
- );
265
- return;
266
- }
267
-
268
- // Handle notifications
269
- _onMsgRx(event: Event): void {
270
- // Get characteristic
271
- const characteristic = event.target as BluetoothRemoteGATTCharacteristic;
272
-
273
- // Get value
274
- const value = characteristic.value;
275
- if (value !== undefined) {
276
- const msg = new Uint8Array(value.buffer);
277
-
278
- // Handle message
279
- if (this._raftMsgHandler) {
280
- try {
281
- this._raftMsgHandler.handleNewRxMsg(msg);
282
- } catch (error) {
283
- RaftLog.debug(`RaftChannelBLE.onMsgRx ${error}`);
284
- }
285
- }
286
- }
287
- }
288
-
289
- // Send a message
290
- async sendTxMsg(
291
- msg: Uint8Array
292
- // _sendWithResponse: boolean
293
- ): Promise<boolean> {
294
- // Check valid
295
- if (this._bleDevice === null) {
296
- return false;
297
- }
298
-
299
- // Retry upto maxRetries
300
- for (let retryIdx = 0; retryIdx < this.maxRetries; retryIdx++) {
301
- // Check for min time between messages
302
- while (Date.now() - this._msgTxTimeLast < this._msgTxMinTimeBetweenMs) {
303
- await new Promise((resolve) => setTimeout(resolve, 5));
304
- }
305
- this._msgTxTimeLast = Date.now();
306
-
307
- // Write to the characteristic
308
- try {
309
- if (this._characteristicTx) {
310
- if (this._characteristicTx.writeValueWithoutResponse) {
311
- await this._characteristicTx.writeValueWithoutResponse(msg);
312
- } else if (this._characteristicTx.writeValue) {
313
- await this._characteristicTx.writeValue(msg);
314
- } else if (this._characteristicTx.writeValueWithResponse) {
315
- await this._characteristicTx.writeValueWithResponse(msg);
316
- }
317
- }
318
- break;
319
- } catch (error) {
320
- if (retryIdx === this.maxRetries - 1) {
321
- RaftLog.info(
322
- `RaftChannelBLE.sendTxMsg ${error} retried ${retryIdx} times`
323
- );
324
- }
325
- }
326
- }
327
- return true;
328
- }
329
-
330
- // Send message without awaiting response
331
- async sendTxMsgNoAwait(
332
- msg: Uint8Array
333
- // _sendWithResponse: boolean
334
- ): Promise<boolean> {
335
- // Check valid
336
- if (this._bleDevice === null) {
337
- return false;
338
- }
339
-
340
- // Check for min time between messages
341
- while (Date.now() - this._msgTxTimeLast < this._msgTxMinTimeBetweenMs) {
342
- await new Promise((resolve) => setTimeout(resolve, 5));
343
- }
344
- this._msgTxTimeLast = Date.now();
345
-
346
- // Write to the characteristic
347
- if (this._characteristicTx) {
348
- if (this._characteristicTx.writeValueWithoutResponse) {
349
- this._characteristicTx.writeValueWithoutResponse(msg);
350
- } else if (this._characteristicTx.writeValue) {
351
- this._characteristicTx.writeValue(msg);
352
- } else if (this._characteristicTx.writeValueWithResponse) {
353
- this._characteristicTx.writeValueWithResponse(msg);
354
- }
355
- return true;
356
- }
357
- return false;
358
- }
359
-
360
- // Method used for testing and simulation should never be called
361
- sendTxMsgRaw(): boolean {
362
- RaftLog.debug(`sendTxMsgRaw - not implemented`);
363
- return false;
364
- }
365
-
366
- // Method used for testing and simulation should never be called
367
- sendTxMsgRawAndWaitForReply<T>(): T {
368
- RaftLog.debug(`sendTxMsgRawAndWaitForReply - not implemented`);
369
- return null as T;
370
- }
371
-
372
- }
@@ -1,13 +0,0 @@
1
- /**
2
- * @description
3
- * This file is used to create a new instance of the RaftChannelBLE class.
4
- * RaftChannelBLE exists in 2 files: RaftChannelBLE.native.ts and RaftChannelBLE.web.ts
5
- * each of these files contains the same class but with different implementations (native and web).
6
- * The import statement at the top of the file will import the correct implementation based on the platform
7
- * (depending on the build configuration).
8
- */
9
- import RaftChannelBLE from './RaftChannelBLE';
10
-
11
- export function createBLEChannel() {
12
- return RaftChannelBLE;
13
- }
@@ -1,184 +0,0 @@
1
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
- //
3
- // RaftChannelBLEScanner.native.ts
4
- // Communications Connector for RaftJS
5
- //
6
- // Rob Dobson 2022-24
7
- // (C) Robotical 2022-24
8
- //
9
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10
-
11
- import {
12
- BleManager,
13
- Device,
14
- BleError,
15
- } from 'react-native-ble-plx';
16
- import { DiscoveredDevice } from './RaftTypes';
17
- import RaftLog from './RaftLog';
18
- import { RaftConnEvent, RaftConnEventFn } from './RaftConnEvents';
19
-
20
- export default class RaftChannelBLEScanner {
21
-
22
- // BleManager
23
- _bleManager: BleManager;
24
-
25
- // Services to scan for
26
- _defaultUUIDsOfServicesToScanFor: Array<string> = [];
27
-
28
- // Scanned devices found on BLE
29
- _discoveredDevices: DiscoveredDevice[] = [];
30
- static _scanInProgress = false;
31
-
32
- // Time to scan for
33
- _discoveryTimeMs = 10000;
34
-
35
- // Event callback
36
- _eventCallback: RaftConnEventFn;
37
-
38
- constructor(bleManager: BleManager, uuidsOfServicesToScanFor: Array<string>, eventCallback: RaftConnEventFn) {
39
- this._bleManager = bleManager;
40
- this._defaultUUIDsOfServicesToScanFor = uuidsOfServicesToScanFor;
41
- this._eventCallback = eventCallback;
42
- }
43
-
44
- // Get discovered Devices
45
- getDiscoveredDevices(): DiscoveredDevice[] {
46
- return this._discoveredDevices;
47
- }
48
-
49
- // Check is a scan is in progress
50
- isScanInProgress(): boolean {
51
- return RaftChannelBLEScanner._scanInProgress;
52
- }
53
-
54
- async scanningStart(uuids: string[]): Promise<boolean> {
55
- // Handle discovery
56
- RaftLog.debug('Starting Scanning...');
57
-
58
- // Clear list
59
- this._discoveredDevices = [];
60
-
61
- // Disconnect any connections
62
- RaftChannelBLEScanner._scanInProgress = true;
63
-
64
- // Start scan
65
- try {
66
- this._bleManager.startDeviceScan(
67
- uuids.length > 0 ? uuids : this._defaultUUIDsOfServicesToScanFor,
68
- { allowDuplicates: true },
69
- (error: BleError | null, device: Device | null) => {
70
- // RaftLog.debug(`discoveryFoundCB error ${error}`);
71
- this._discoveryFoundCB(error, device);
72
- },
73
- );
74
- } catch (e) {
75
- RaftLog.warn(`Error starting scan ${e}`);
76
- return false;
77
- }
78
-
79
- // Set a time limit
80
- this._discoverySetTimeLimit(this._discoveryTimeMs);
81
- return true;
82
- }
83
-
84
- scanningStop(): void {
85
-
86
- RaftLog.debug('scanningStop');
87
-
88
- // Emit finished if we were scanning
89
- RaftLog.debug(`IS SCANNING IN PROGRESS: ${RaftChannelBLEScanner._scanInProgress}`);
90
- if (RaftChannelBLEScanner._scanInProgress) {
91
- RaftLog.debug(`sending BLE_SCANNING_FINISHED event`);
92
- this._eventCallback(RaftConnEvent.BLE_SCANNING_FINISHED, { discoveredDevices: this._discoveredDevices });
93
- }
94
-
95
- // Cancel scanning
96
- this._bleManager.stopDeviceScan();
97
- RaftChannelBLEScanner._scanInProgress = false;
98
- }
99
-
100
- // Callback from BLE-PLX library on device discovered
101
- _discoveryFoundCB(
102
- error: BleError | null,
103
- scannedDevice: Device | null,
104
- ): void {
105
- if (error) {
106
- //RaftLog.warn(`⚠️ Scan Error >> ${error.toString()}`);
107
- RaftLog.warn(`⚠️ Scan Error >> ${JSON.stringify(error)}`);
108
- // Event if we were scanning
109
- if (RaftChannelBLEScanner._scanInProgress) {
110
- this._eventCallback(RaftConnEvent.BLE_SCANNING_FINISHED, {
111
- discoveredDevices: this._discoveredDevices,
112
- });
113
- RaftChannelBLEScanner._scanInProgress = false;
114
- }
115
- return;
116
- }
117
-
118
- // See if already in the list
119
- const deviceAlreadyFound = this._discoveredDevices.find(
120
- item => item.id === scannedDevice!.id,
121
- );
122
-
123
- RaftLog.debug(`✅ Scanning... >> ${scannedDevice}`);
124
-
125
- if (deviceAlreadyFound) {
126
- // update the rssi value if it's not 127
127
- if (scannedDevice!.rssi !== null && scannedDevice!.rssi !== 127) {
128
- deviceAlreadyFound._rssi = scannedDevice!.rssi;
129
- this._eventCallback(RaftConnEvent.BLE_DEVICE_FOUND, {
130
- discoveredDevice: deviceAlreadyFound
131
- });
132
- }
133
- return;
134
- }
135
-
136
- if (scannedDevice !== null) {
137
- if (!scannedDevice.localName) {
138
- // if the name of the device is null, don't add this device
139
- // the first time round. Second time this device comes up
140
- // will have all the needed info
141
- return;
142
- }
143
- const newDiscoveredDevice = new DiscoveredDevice(
144
- scannedDevice.localName !== null ? scannedDevice.localName : '',
145
- scannedDevice.name !== null ? scannedDevice.name : '',
146
- scannedDevice.id,
147
- scannedDevice.rssi !== null ? scannedDevice.rssi : -150,
148
- scannedDevice.serviceUUIDs,
149
- );
150
- this._discoveredDevices.push(newDiscoveredDevice);
151
- // send the newly found ric to the state so it can pop-up on the front-end
152
- this._eventCallback(RaftConnEvent.BLE_DEVICE_FOUND, {
153
- discoveredDevice: newDiscoveredDevice
154
- });
155
- }
156
- RaftLog.debug(`🤖 Scanned RICs >> ${this._discoveredDevices}`);
157
- }
158
-
159
- // Time-limit on device scanning
160
- _discoverySetTimeLimit(timeLimitMs: number) {
161
- setTimeout(() => {
162
- // Stop scanning
163
- this._bleManager.stopDeviceScan();
164
-
165
- // Check we were scanning
166
- if (RaftChannelBLEScanner._scanInProgress) {
167
- // Sort by signal strength
168
- // this._discoveredDevices.sort((a, b) => {
169
- // return b!.rssi! - a!.rssi!;
170
- // });
171
-
172
- // Debug
173
- const msg = `🤖 ${this._discoveredDevices.length} RICs found! Choose one to connect`;
174
- RaftLog.debug(msg);
175
-
176
- // Finished event
177
- this._eventCallback(RaftConnEvent.BLE_SCANNING_FINISHED, {
178
- discoveredDevices: this._discoveredDevices,
179
- });
180
- }
181
- RaftChannelBLEScanner._scanInProgress = false;
182
- }, timeLimitMs);
183
- }
184
- }