@robdobsn/raftjs 1.7.1 → 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 (205) hide show
  1. package/dist/web/RaftAttributeHandler.js +5 -5
  2. package/dist/web/RaftAttributeHandler.js.map +1 -1
  3. package/dist/web/RaftChannel.d.ts +2 -0
  4. package/dist/web/RaftChannelBLE.web.d.ts +2 -0
  5. package/dist/web/RaftChannelBLE.web.js +10 -0
  6. package/dist/web/RaftChannelBLE.web.js.map +1 -1
  7. package/dist/{react-native/RaftChannelWebSocket.d.ts → web/RaftChannelSimulated.d.ts} +9 -5
  8. package/dist/web/RaftChannelSimulated.js +418 -0
  9. package/dist/web/RaftChannelSimulated.js.map +1 -0
  10. package/dist/web/RaftChannelWebSerial.d.ts +2 -0
  11. package/dist/web/RaftChannelWebSerial.js +10 -0
  12. package/dist/web/RaftChannelWebSerial.js.map +1 -1
  13. package/dist/web/RaftChannelWebSocket.d.ts +2 -0
  14. package/dist/web/RaftChannelWebSocket.js +10 -0
  15. package/dist/web/RaftChannelWebSocket.js.map +1 -1
  16. package/dist/web/RaftConnector.js +5 -0
  17. package/dist/web/RaftConnector.js.map +1 -1
  18. package/dist/web/RaftDeviceInfo.d.ts +1 -1
  19. package/dist/web/RaftMsgHandler.d.ts +5 -0
  20. package/dist/web/RaftMsgHandler.js +32 -0
  21. package/dist/web/RaftMsgHandler.js.map +1 -1
  22. package/dist/web/main.d.ts +1 -0
  23. package/dist/web/main.js +3 -1
  24. package/dist/web/main.js.map +1 -1
  25. package/package.json +5 -2
  26. package/.editorconfig +0 -14
  27. package/.gitattributes +0 -11
  28. package/.nvmrc +0 -1
  29. package/TODO.md +0 -1
  30. package/dist/react-native/RaftAttributeHandler.d.ts +0 -12
  31. package/dist/react-native/RaftAttributeHandler.js +0 -249
  32. package/dist/react-native/RaftAttributeHandler.js.map +0 -1
  33. package/dist/react-native/RaftChannel.d.ts +0 -18
  34. package/dist/react-native/RaftChannel.js +0 -12
  35. package/dist/react-native/RaftChannel.js.map +0 -1
  36. package/dist/react-native/RaftChannelBLE.native.d.ts +0 -93
  37. package/dist/react-native/RaftChannelBLE.native.js +0 -472
  38. package/dist/react-native/RaftChannelBLE.native.js.map +0 -1
  39. package/dist/react-native/RaftChannelBLE.web.d.ts +0 -38
  40. package/dist/react-native/RaftChannelBLE.web.js +0 -290
  41. package/dist/react-native/RaftChannelBLE.web.js.map +0 -1
  42. package/dist/react-native/RaftChannelBLEFactory.d.ts +0 -10
  43. package/dist/react-native/RaftChannelBLEFactory.js +0 -17
  44. package/dist/react-native/RaftChannelBLEFactory.js.map +0 -1
  45. package/dist/react-native/RaftChannelBLEScanner.native.d.ts +0 -18
  46. package/dist/react-native/RaftChannelBLEScanner.native.js +0 -138
  47. package/dist/react-native/RaftChannelBLEScanner.native.js.map +0 -1
  48. package/dist/react-native/RaftChannelWebSerial.d.ts +0 -37
  49. package/dist/react-native/RaftChannelWebSerial.js +0 -319
  50. package/dist/react-native/RaftChannelWebSerial.js.map +0 -1
  51. package/dist/react-native/RaftChannelWebSocket.js +0 -197
  52. package/dist/react-native/RaftChannelWebSocket.js.map +0 -1
  53. package/dist/react-native/RaftCommsStats.d.ts +0 -39
  54. package/dist/react-native/RaftCommsStats.js +0 -128
  55. package/dist/react-native/RaftCommsStats.js.map +0 -1
  56. package/dist/react-native/RaftConnEvents.d.ts +0 -39
  57. package/dist/react-native/RaftConnEvents.js +0 -54
  58. package/dist/react-native/RaftConnEvents.js.map +0 -1
  59. package/dist/react-native/RaftConnector.d.ts +0 -245
  60. package/dist/react-native/RaftConnector.js +0 -616
  61. package/dist/react-native/RaftConnector.js.map +0 -1
  62. package/dist/react-native/RaftCustomAttrHandler.d.ts +0 -4
  63. package/dist/react-native/RaftCustomAttrHandler.js +0 -50
  64. package/dist/react-native/RaftCustomAttrHandler.js.map +0 -1
  65. package/dist/react-native/RaftDeviceInfo.d.ts +0 -63
  66. package/dist/react-native/RaftDeviceInfo.js +0 -36
  67. package/dist/react-native/RaftDeviceInfo.js.map +0 -1
  68. package/dist/react-native/RaftDeviceManager.d.ts +0 -40
  69. package/dist/react-native/RaftDeviceManager.js +0 -499
  70. package/dist/react-native/RaftDeviceManager.js.map +0 -1
  71. package/dist/react-native/RaftDeviceMgrIF.d.ts +0 -15
  72. package/dist/react-native/RaftDeviceMgrIF.js +0 -11
  73. package/dist/react-native/RaftDeviceMgrIF.js.map +0 -1
  74. package/dist/react-native/RaftDeviceMsg.d.ts +0 -9
  75. package/dist/react-native/RaftDeviceMsg.js +0 -11
  76. package/dist/react-native/RaftDeviceMsg.js.map +0 -1
  77. package/dist/react-native/RaftDeviceStates.d.ts +0 -37
  78. package/dist/react-native/RaftDeviceStates.js +0 -60
  79. package/dist/react-native/RaftDeviceStates.js.map +0 -1
  80. package/dist/react-native/RaftFileHandler.d.ts +0 -52
  81. package/dist/react-native/RaftFileHandler.js +0 -502
  82. package/dist/react-native/RaftFileHandler.js.map +0 -1
  83. package/dist/react-native/RaftLog.d.ts +0 -22
  84. package/dist/react-native/RaftLog.js +0 -63
  85. package/dist/react-native/RaftLog.js.map +0 -1
  86. package/dist/react-native/RaftMiniHDLC.d.ts +0 -18
  87. package/dist/react-native/RaftMiniHDLC.js +0 -383
  88. package/dist/react-native/RaftMiniHDLC.js.map +0 -1
  89. package/dist/react-native/RaftMsgHandler.d.ts +0 -57
  90. package/dist/react-native/RaftMsgHandler.js +0 -479
  91. package/dist/react-native/RaftMsgHandler.js.map +0 -1
  92. package/dist/react-native/RaftMsgTrackInfo.d.ts +0 -17
  93. package/dist/react-native/RaftMsgTrackInfo.js +0 -42
  94. package/dist/react-native/RaftMsgTrackInfo.js.map +0 -1
  95. package/dist/react-native/RaftProtocolDefs.d.ts +0 -30
  96. package/dist/react-native/RaftProtocolDefs.js +0 -48
  97. package/dist/react-native/RaftProtocolDefs.js.map +0 -1
  98. package/dist/react-native/RaftStreamHandler.d.ts +0 -38
  99. package/dist/react-native/RaftStreamHandler.js +0 -257
  100. package/dist/react-native/RaftStreamHandler.js.map +0 -1
  101. package/dist/react-native/RaftStruct.d.ts +0 -3
  102. package/dist/react-native/RaftStruct.js +0 -208
  103. package/dist/react-native/RaftStruct.js.map +0 -1
  104. package/dist/react-native/RaftSysTypeManager.d.ts +0 -14
  105. package/dist/react-native/RaftSysTypeManager.js +0 -53
  106. package/dist/react-native/RaftSysTypeManager.js.map +0 -1
  107. package/dist/react-native/RaftSystemType.d.ts +0 -28
  108. package/dist/react-native/RaftSystemType.js +0 -3
  109. package/dist/react-native/RaftSystemType.js.map +0 -1
  110. package/dist/react-native/RaftSystemUtils.d.ts +0 -136
  111. package/dist/react-native/RaftSystemUtils.js +0 -410
  112. package/dist/react-native/RaftSystemUtils.js.map +0 -1
  113. package/dist/react-native/RaftTypes.d.ts +0 -194
  114. package/dist/react-native/RaftTypes.js +0 -153
  115. package/dist/react-native/RaftTypes.js.map +0 -1
  116. package/dist/react-native/RaftUpdateEvents.d.ts +0 -33
  117. package/dist/react-native/RaftUpdateEvents.js +0 -46
  118. package/dist/react-native/RaftUpdateEvents.js.map +0 -1
  119. package/dist/react-native/RaftUpdateManager.d.ts +0 -61
  120. package/dist/react-native/RaftUpdateManager.js +0 -621
  121. package/dist/react-native/RaftUpdateManager.js.map +0 -1
  122. package/dist/react-native/RaftUtils.d.ts +0 -126
  123. package/dist/react-native/RaftUtils.js +0 -467
  124. package/dist/react-native/RaftUtils.js.map +0 -1
  125. package/dist/react-native/RaftWifiTypes.d.ts +0 -23
  126. package/dist/react-native/RaftWifiTypes.js +0 -43
  127. package/dist/react-native/RaftWifiTypes.js.map +0 -1
  128. package/dist/react-native/main.d.ts +0 -24
  129. package/dist/react-native/main.js +0 -49
  130. package/dist/react-native/main.js.map +0 -1
  131. package/eslint.config.mjs +0 -33
  132. package/examples/dashboard/package.json +0 -36
  133. package/examples/dashboard/src/CommandPanel.tsx +0 -147
  134. package/examples/dashboard/src/ConnManager.ts +0 -164
  135. package/examples/dashboard/src/DeviceActionsForm.tsx +0 -133
  136. package/examples/dashboard/src/DeviceAttrsForm.tsx +0 -49
  137. package/examples/dashboard/src/DeviceLineChart.tsx +0 -163
  138. package/examples/dashboard/src/DevicePanel.tsx +0 -166
  139. package/examples/dashboard/src/DevicesPanel.tsx +0 -58
  140. package/examples/dashboard/src/DispLedGrid.tsx +0 -110
  141. package/examples/dashboard/src/DispOneLed.tsx +0 -20
  142. package/examples/dashboard/src/LatencyTest.ts +0 -130
  143. package/examples/dashboard/src/LatencyTestPanel.tsx +0 -92
  144. package/examples/dashboard/src/Main.tsx +0 -234
  145. package/examples/dashboard/src/SettingsManager.ts +0 -67
  146. package/examples/dashboard/src/SettingsScreen.tsx +0 -174
  147. package/examples/dashboard/src/StatusPanel.tsx +0 -71
  148. package/examples/dashboard/src/SystemTypeCog/CogStateInfo.ts +0 -163
  149. package/examples/dashboard/src/SystemTypeCog/SystemTypeCog.ts +0 -91
  150. package/examples/dashboard/src/SystemTypeGeneric/StateInfoGeneric.ts +0 -30
  151. package/examples/dashboard/src/SystemTypeGeneric/SystemTypeGeneric.ts +0 -91
  152. package/examples/dashboard/src/SystemTypeMarty/RICAddOn.ts +0 -70
  153. package/examples/dashboard/src/SystemTypeMarty/RICAddOnBase.ts +0 -33
  154. package/examples/dashboard/src/SystemTypeMarty/RICAddOnManager.ts +0 -342
  155. package/examples/dashboard/src/SystemTypeMarty/RICCommsStats.ts +0 -170
  156. package/examples/dashboard/src/SystemTypeMarty/RICHWElem.ts +0 -123
  157. package/examples/dashboard/src/SystemTypeMarty/RICLEDPatternChecker.ts +0 -207
  158. package/examples/dashboard/src/SystemTypeMarty/RICROSSerial.ts +0 -464
  159. package/examples/dashboard/src/SystemTypeMarty/RICServoFaultDetector.ts +0 -146
  160. package/examples/dashboard/src/SystemTypeMarty/RICStateInfo.ts +0 -97
  161. package/examples/dashboard/src/SystemTypeMarty/RICSystemUtils.ts +0 -371
  162. package/examples/dashboard/src/SystemTypeMarty/RICTypes.ts +0 -20
  163. package/examples/dashboard/src/SystemTypeMarty/SystemTypeMarty.ts +0 -119
  164. package/examples/dashboard/src/index.html +0 -15
  165. package/examples/dashboard/src/index.tsx +0 -13
  166. package/examples/dashboard/src/styles.css +0 -408
  167. package/examples/dashboard/tsconfig.json +0 -18
  168. package/jest.config.js +0 -11
  169. package/src/RaftAttributeHandler.ts +0 -298
  170. package/src/RaftChannel.ts +0 -30
  171. package/src/RaftChannelBLE.native.ts +0 -604
  172. package/src/RaftChannelBLE.web.ts +0 -359
  173. package/src/RaftChannelBLEFactory.ts +0 -13
  174. package/src/RaftChannelBLEScanner.native.ts +0 -184
  175. package/src/RaftChannelWebSerial.ts +0 -408
  176. package/src/RaftChannelWebSocket.ts +0 -245
  177. package/src/RaftCommsStats.ts +0 -142
  178. package/src/RaftConnEvents.ts +0 -58
  179. package/src/RaftConnector.ts +0 -739
  180. package/src/RaftCustomAttrHandler.ts +0 -54
  181. package/src/RaftDeviceInfo.ts +0 -102
  182. package/src/RaftDeviceManager.ts +0 -607
  183. package/src/RaftDeviceMgrIF.ts +0 -33
  184. package/src/RaftDeviceMsg.ts +0 -20
  185. package/src/RaftDeviceStates.ts +0 -92
  186. package/src/RaftFileHandler.ts +0 -668
  187. package/src/RaftLog.ts +0 -70
  188. package/src/RaftMiniHDLC.ts +0 -396
  189. package/src/RaftMsgHandler.ts +0 -777
  190. package/src/RaftMsgTrackInfo.ts +0 -51
  191. package/src/RaftProtocolDefs.ts +0 -46
  192. package/src/RaftStreamHandler.ts +0 -328
  193. package/src/RaftStruct.ts +0 -209
  194. package/src/RaftSysTypeManager.ts +0 -60
  195. package/src/RaftSystemType.ts +0 -32
  196. package/src/RaftSystemUtils.ts +0 -487
  197. package/src/RaftTypes.ts +0 -278
  198. package/src/RaftUpdateEvents.ts +0 -48
  199. package/src/RaftUpdateManager.ts +0 -781
  200. package/src/RaftUtils.ts +0 -494
  201. package/src/RaftWifiTypes.ts +0 -36
  202. package/src/main.ts +0 -37
  203. package/testdata/TestDeviceTypeRecs.json +0 -492
  204. package/tsconfig.json +0 -30
  205. package/tsconfig.react-native.json +0 -29
@@ -1,739 +0,0 @@
1
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
- //
3
- // RaftConnector
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 RaftMsgHandler, { RaftMsgResultCode } from "./RaftMsgHandler";
13
- import RaftChannelWebSocket from "./RaftChannelWebSocket";
14
- import RaftChannelWebSerial from "./RaftChannelWebSerial";
15
- import RaftCommsStats from "./RaftCommsStats";
16
- import { RaftEventFn, RaftOKFail, RaftFileSendType, RaftFileDownloadResult, RaftProgressCBType, RaftBridgeSetupResp, RaftFileDownloadFn } from "./RaftTypes";
17
- import RaftSystemUtils from "./RaftSystemUtils";
18
- import RaftFileHandler from "./RaftFileHandler";
19
- import RaftStreamHandler from "./RaftStreamHandler";
20
- import RaftLog from "./RaftLog";
21
- import { RaftConnEvent, RaftConnEventNames } from "./RaftConnEvents";
22
- import { RaftGetSystemTypeCBType, RaftSystemType } from "./RaftSystemType";
23
- import { RaftUpdateEvent, RaftUpdateEventNames } from "./RaftUpdateEvents";
24
- import RaftUpdateManager from "./RaftUpdateManager";
25
- import { createBLEChannel } from "./RaftChannelBLEFactory";
26
-
27
-
28
- export default class RaftConnector {
29
-
30
- // Get system type callback
31
- private _getSystemTypeCB: RaftGetSystemTypeCBType | null = null;
32
-
33
- // System type
34
- private _systemType: RaftSystemType | null = null;
35
-
36
- // Channel
37
- private _raftChannel: RaftChannel | null = null;
38
-
39
- // Channel connection method and locator
40
- private _channelConnMethod = "";
41
- private _channelConnLocator: string | object = "";
42
-
43
- // Comms stats
44
- private _commsStats: RaftCommsStats = new RaftCommsStats();
45
-
46
- // Message handler
47
- private _raftMsgHandler: RaftMsgHandler = new RaftMsgHandler(
48
- this._commsStats
49
- );
50
-
51
- // RaftSystem
52
- private _raftSystemUtils: RaftSystemUtils = new RaftSystemUtils(this._raftMsgHandler);
53
-
54
- // Connection performance checker
55
- private readonly _testConnPerfBlockSize = 500;
56
- private readonly _testConnPerfNumBlocks = 50;
57
- private readonly _connPerfRsltDelayMs = 4000;
58
-
59
- // Retry connection if lost
60
- private _retryIfLostEnabled = true;
61
- private _retryIfLostForSecs = 10;
62
- private _retryIfLostIsConnected = false;
63
- private _retryIfLostDisconnectTime: number | null = null;
64
- private readonly _retryIfLostRetryDelayMs = 500;
65
-
66
- // File handler
67
- private _raftFileHandler: RaftFileHandler = new RaftFileHandler(
68
- this._raftMsgHandler,
69
- this._commsStats,
70
- );
71
-
72
- // Stream handler
73
- private _raftStreamHandler: RaftStreamHandler = new RaftStreamHandler(
74
- this._raftMsgHandler,
75
- this._commsStats,
76
- this
77
- );
78
-
79
- // Event listener
80
- private _onEventFn: RaftEventFn | null = null;
81
-
82
- // Update manager
83
- private _raftUpdateManager: RaftUpdateManager | null = null;
84
-
85
- /**
86
- * RaftConnector constructor
87
- * @param getSystemTypeCB - callback to get system type
88
- */
89
- public constructor(getSystemTypeCB: RaftGetSystemTypeCBType | null = null) {
90
-
91
- // Get system type callback
92
- this._getSystemTypeCB = getSystemTypeCB;
93
-
94
- // Debug
95
- RaftLog.debug('RaftConnector starting up');
96
- }
97
-
98
- /**
99
- * Configure the file handler
100
- * @param fileBlockSize - size of file blocks to send
101
- * @param batchAckSize - number of blocks to send before waiting for ack
102
- * @returns void
103
- */
104
- configureFileHandler(fileBlockSize: number, batchAckSize: number) {
105
- this._raftFileHandler.setRequestedFileBlockSize(fileBlockSize);
106
- this._raftFileHandler.setRequestedBatchAckSize(batchAckSize);
107
- }
108
-
109
- /**
110
- * Set event listener
111
- * @param onEventFn - event listener
112
- * @returns void
113
- * */
114
- setEventListener(onEventFn: RaftEventFn): void {
115
- this._onEventFn = onEventFn;
116
- }
117
-
118
- /**
119
- * isConnected
120
- * @returns boolean - true if connected
121
- * */
122
- isConnected() {
123
- // Check if connected
124
- const isConnected = this._retryIfLostIsConnected || (this._raftChannel ? this._raftChannel.isConnected() : false);
125
- return isConnected;
126
- }
127
-
128
- /**
129
- * Set try to reconnect if connection lost
130
- * @param enableRetry - true to enable retry
131
- * @param retryForSecs - retry for this many seconds
132
- * @returns void
133
- * */
134
- setRetryConnectionIfLost(enableRetry: boolean, retryForSecs: number): void {
135
- this._retryIfLostEnabled = enableRetry;
136
- this._retryIfLostForSecs = retryForSecs;
137
- if (!this._retryIfLostEnabled) {
138
- this._retryIfLostIsConnected = false;
139
- }
140
- RaftLog.debug(`setRetryConnectionIfLost ${enableRetry} retry for ${retryForSecs}`);
141
- }
142
-
143
- /**
144
- * Get Raft system type (the type of hardware connected to - determined using the getSystemInfo API)
145
- * @returns RaftSystemType | null - Raft system type
146
- * */
147
- getSystemType(): RaftSystemType | null {
148
- return this._systemType;
149
- }
150
-
151
- /**
152
- * Get connection method
153
- * @returns string - connection method
154
- * */
155
- getConnMethod(): string {
156
- return this._channelConnMethod;
157
- }
158
-
159
- /**
160
- * Get connection locator
161
- * @returns string | object - connection locator
162
- * */
163
- getConnLocator(): any | null {
164
- return this._raftChannel ? this._raftChannel.getConnectedLocator() : null;
165
- }
166
-
167
- /**
168
- * Get Raft channel (this is the channel used for commuinications with the Raft application)
169
- * @returns RaftChannel | null - Raft channel
170
- * */
171
- getRaftChannel(): RaftChannel | null {
172
- return this._raftChannel;
173
- }
174
-
175
- /**
176
- * Get Raft system utils (access to system information and control)
177
- * @returns RaftSystemUtils - Raft system utils
178
- * */
179
- getRaftSystemUtils(): RaftSystemUtils {
180
- return this._raftSystemUtils;
181
- }
182
-
183
- /**
184
- * Get communication stats
185
- * @returns RaftCommsStats - communication stats
186
- * */
187
- getCommsStats(): RaftCommsStats {
188
- return this._commsStats;
189
- }
190
-
191
- /**
192
- * Get Raft message handler (to allow message sending and receiving)
193
- * @returns RaftMsgHandler - Raft message handler
194
- * */
195
- getRaftMsgHandler(): RaftMsgHandler {
196
- return this._raftMsgHandler;
197
- }
198
-
199
- /**
200
- * Pause connection
201
- * @param pause - true to pause, false to resume
202
- */
203
- pauseConnection(pause = true) {
204
- if (this._raftChannel) this._raftChannel.pauseConnection(pause);
205
- }
206
-
207
- /**
208
- * Initialize the Raft channel
209
- */
210
- async initializeChannel(method: string): Promise<boolean> {
211
- // Initialize raft channel
212
- if (method === 'WebBLE' || method === 'PhoneBLE') {
213
- const RaftChannelBLE = createBLEChannel();
214
- this._raftChannel = new RaftChannelBLE();
215
- this._channelConnMethod = method;
216
- } else if (method === 'WebSocket' || method === 'wifi') {
217
- this._raftChannel = new RaftChannelWebSocket();
218
- this._channelConnMethod = 'WebSocket';
219
- } else if (method === 'WebSerial') {
220
- this._raftChannel = new RaftChannelWebSerial();
221
- this._channelConnMethod = 'WebSerial';
222
- } else {
223
- RaftLog.error('Unknown method: ' + method);
224
- return false;
225
- }
226
-
227
- // Check channel established
228
- if (this._raftChannel !== null) {
229
- // Set message handler
230
- this._raftChannel.setMsgHandler(this._raftMsgHandler);
231
- this._raftChannel.setOnConnEvent(this.onConnEvent.bind(this));
232
-
233
- // Message handling in and out
234
- this._raftMsgHandler.registerForResults(this);
235
- this._raftMsgHandler.registerMsgSender(this._raftChannel);
236
-
237
- return true;
238
- } else {
239
- this._channelConnMethod = "";
240
- return false;
241
- }
242
- }
243
-
244
- /**
245
- * Connect to a Raft device
246
- *
247
- * @param {string | object} locator - either a string (WebSocket URL or serial port) or an object (WebBLE)
248
- * @returns Promise<boolean>
249
- *
250
- */
251
- async connect(locator: string | object): Promise<boolean> {
252
- if (!this._raftChannel) {
253
- RaftLog.error('Raft channel is not initialized.');
254
- return false;
255
- }
256
-
257
- // Store locator
258
- this._channelConnLocator = locator;
259
-
260
- // Connect
261
- let connOk = false;
262
- try {
263
- // Event
264
- this.onConnEvent(RaftConnEvent.CONN_CONNECTING);
265
-
266
- // Connect
267
- connOk = await this._connectToChannel();
268
- } catch (err) {
269
- RaftLog.error('RaftConnector.connect - error: ' + err);
270
- }
271
-
272
- if (connOk) {
273
- // Get system type
274
- if (this._getSystemTypeCB) {
275
- // Get system type
276
- this._systemType = await this._getSystemTypeCB(this._raftSystemUtils);
277
-
278
- // Set defaults
279
- if (this._systemType) {
280
- this._raftSystemUtils.setDefaultWiFiHostname(this._systemType.defaultWiFiHostname);
281
- }
282
- }
283
-
284
- // Setup system type
285
- if (this._systemType) {
286
- this._systemType.setup(this._raftSystemUtils, this._onEventFn);
287
- }
288
-
289
- // Check if subscription required
290
- if (this._systemType &&
291
- this._systemType.subscribeForUpdates &&
292
- this._raftChannel.requiresSubscription()) {
293
- try {
294
- // Subscription
295
- await this._systemType.subscribeForUpdates(this._raftSystemUtils, true);
296
- RaftLog.info(`connect subscribed for updates`);
297
- } catch (error: unknown) {
298
- RaftLog.warn(`connect subscribe for updates failed ${error}`)
299
- }
300
- }
301
-
302
- // Send connected event
303
- this.onConnEvent(RaftConnEvent.CONN_CONNECTED);
304
-
305
- } else {
306
- // Failed Event
307
- this.onConnEvent(RaftConnEvent.CONN_CONNECTION_FAILED);
308
- // configure file handler
309
- this.configureFileHandler(this._raftChannel.fhFileBlockSize(), this._raftChannel.fhBatchAckSize());
310
- }
311
-
312
- return connOk;
313
- }
314
-
315
- async disconnect(): Promise<void> {
316
- // Disconnect
317
- this._retryIfLostIsConnected = false;
318
- if (this._raftChannel) {
319
- // Check if there is a RICREST command to send before disconnecting
320
- const ricRestCommand = this._raftChannel.ricRestCmdBeforeDisconnect();
321
- if (ricRestCommand) {
322
- console.log(`sending RICREST command before disconnect: ${ricRestCommand}`);
323
- await this.sendRICRESTMsg(ricRestCommand, {});
324
- }
325
- // Pause a little before disconnecting
326
- await new Promise(resolve => setTimeout(resolve, 1000));
327
- // await this.sendRICRESTMsg("bledisc", {});
328
- await this._raftChannel.disconnect();
329
- this._raftChannel = null;
330
- }
331
- }
332
-
333
- // Mark: Tx Message handling -----------------------------------------------------------------------------------------
334
-
335
- /**
336
- *
337
- * sendRICRESTMsg
338
- * @param commandName command API string
339
- * @param params parameters (simple name value pairs only) to parameterize trajectory
340
- * @returns Promise<RaftOKFail>
341
- *
342
- */
343
- async sendRICRESTMsg(commandName: string, params: object,
344
- bridgeID: number | undefined = undefined): Promise<RaftOKFail> {
345
- try {
346
- // Format the paramList as query string
347
- const paramEntries = Object.entries(params);
348
- let paramQueryStr = '';
349
- for (const param of paramEntries) {
350
- if (paramQueryStr.length > 0) paramQueryStr += '&';
351
- paramQueryStr += param[0] + '=' + param[1];
352
- }
353
- // Format the url to send
354
- if (paramQueryStr.length > 0) commandName += '?' + paramQueryStr;
355
- return await this._raftMsgHandler.sendRICRESTURL<RaftOKFail>(commandName, bridgeID);
356
- } catch (error) {
357
- RaftLog.warn(`sendRICRESTMsg failed ${error}`);
358
- return { rslt: 'fail' };
359
- }
360
- }
361
-
362
- // Mark: Rx Message handling -----------------------------------------------------------------------------------------
363
-
364
- /**
365
- * onRxReply - handle a reply message
366
- * @param msgHandle number indicating the message that is being replied to (from the original message)
367
- * @param msgRsltCode result code
368
- * @param msgRsltJsonObj result object
369
- */
370
- onRxReply(
371
- msgHandle: number,
372
- msgRsltCode: RaftMsgResultCode,
373
- msgRsltJsonObj: object | null,
374
- ): void {
375
- RaftLog.verbose(
376
- `onRxReply msgHandle ${msgHandle} rsltCode ${msgRsltCode} obj ${JSON.stringify(
377
- msgRsltJsonObj,
378
- )}`,
379
- );
380
- }
381
-
382
- /**
383
- * onRxUnnumberedMsg - handle an unnumbered message
384
- * @param msgRsltJsonObj result object
385
- */
386
- onRxUnnumberedMsg(msgRsltJsonObj: { [key: string]: number | string }): void {
387
- RaftLog.verbose(
388
- `onRxUnnumberedMsg rsltCode obj ${JSON.stringify(msgRsltJsonObj)}`,
389
- );
390
-
391
- // Inform the file handler
392
- if ('okto' in msgRsltJsonObj) {
393
- this._raftFileHandler.onOktoMsg(msgRsltJsonObj.okto as number);
394
- } else if ('sokto' in msgRsltJsonObj) {
395
- this._raftStreamHandler.onSoktoMsg(msgRsltJsonObj.sokto as number);
396
- }
397
- }
398
-
399
- /**
400
- * onRxFileBlock - handle a file block
401
- * @param filePos file position
402
- * @param fileBlockData file block data
403
- */
404
- onRxFileBlock(
405
- filePos: number,
406
- fileBlockData: Uint8Array
407
- ): void {
408
- // RaftLog.info(`onRxFileBlock filePos ${filePos} fileBlockData ${RaftUtils.bufferToHex(fileBlockData)}`);
409
- this._raftFileHandler.onFileBlock(filePos, fileBlockData);
410
- }
411
-
412
-
413
- // Mark: Handling of other message types -----------------------------------------------------------------------------------------
414
-
415
- /**
416
- * onRxOtherMsgType - handle other message types
417
- * @param payload message payload
418
- * @param frameTimeMs time of frame
419
- */
420
- onRxOtherMsgType(payload: Uint8Array, _frameTimeMs: number): void {
421
- // RaftLog.debug(`onRxOtherMsgType payload ${RaftUtils.bufferToHex(payload)}`);
422
- RaftLog.verbose(`onRxOtherMsgType payloadLen ${payload.length}`);
423
-
424
- // Handle other messages
425
- if (this._systemType && this._systemType.rxOtherMsgType) {
426
- this._systemType.rxOtherMsgType(payload, _frameTimeMs);
427
- }
428
- }
429
-
430
- // Mark: File sending --------------------------------------------------------------------------------
431
-
432
- /**
433
- * sendFile - send a file
434
- * @param fileName name of file to send
435
- * @param fileContents file contents
436
- * @param progressCallback callback to receive progress updates
437
- * @returns Promise<boolean> - true if file sent successfully
438
- */
439
- async sendFile(fileName: string,
440
- fileContents: Uint8Array,
441
- progressCallback: ((sent: number, total: number, progress: number) => void) | undefined,
442
- ): Promise<boolean> {
443
- return this._raftFileHandler.fileSend(fileName, RaftFileSendType.NORMAL_FILE, "fs", fileContents, progressCallback);
444
- }
445
-
446
- // Mark: Streaming --------------------------------------------------------------------------------
447
-
448
- /**
449
- * streamAudio - stream audio
450
- * @param streamContents audio data
451
- * @param clearExisting true to clear existing audio
452
- * @param duration duration of audio
453
- */
454
- streamAudio(streamContents: Uint8Array, clearExisting: boolean, duration: number): void {
455
- if (this._raftStreamHandler && this.isConnected()) {
456
- this._raftStreamHandler.streamAudio(streamContents, clearExisting, duration);
457
- }
458
- }
459
-
460
- /**
461
- * isStreamStarting - check if stream is starting
462
- */
463
- isStreamStarting() {
464
- return this._raftStreamHandler.isStreamStarting();
465
- }
466
-
467
- // Mark: File system --------------------------------------------------------------------------------
468
-
469
- /**
470
- * fsGetContents - get file contents
471
- * @param fileName name of file to get
472
- * @param fileSource source of file to get (e.g. "fs" or "bridgeserial1", if omitted defaults to "fs")
473
- * @param progressCallback callback to receive progress updates
474
- * @returns Promise<RaftFileDownloadResult>
475
- */
476
- async fsGetContents(fileName: string,
477
- fileSource: string,
478
- progressCallback: RaftProgressCBType | undefined): Promise<RaftFileDownloadResult> {
479
- return await this._raftFileHandler.fileReceive(fileName, fileSource, progressCallback);
480
- }
481
-
482
- /**
483
- * setLegacySoktoMode - set legacy sokto mode
484
- * @param legacyMode true to set legacy mode
485
- */
486
- setLegacySoktoMode(legacyMode: boolean) {
487
- return this._raftStreamHandler.setLegacySoktoMode(legacyMode);
488
- }
489
-
490
- // Mark: Bridge serial --------------------------------------------------------------------------------
491
-
492
- /**
493
- * createCommsBridge - create a comms bridge
494
- * @param bridgeSource source of bridge (e.g. "Serial1")
495
- * @param bridgeName name of bridge
496
- * @param idleCloseSecs idle close time seconds
497
- * @returns Promise<RaftBridgeSetupResp>
498
- */
499
- async createCommsBridge(bridgeSource: string, bridgeName: string, idleCloseSecs = 0): Promise<RaftBridgeSetupResp> {
500
- return await this._raftMsgHandler.createCommsBridge(bridgeSource, bridgeName, idleCloseSecs);
501
- }
502
-
503
- // Mark: Connection performance--------------------------------------------------------------------------
504
-
505
- /**
506
- * ParkMiller random number generator
507
- * @param seed
508
- * @returns number
509
- */
510
- private parkmiller_next(seed: number) {
511
- const hi = Math.round(16807 * (seed & 0xffff));
512
- let lo = Math.round(16807 * (seed >> 16));
513
- lo += (hi & 0x7fff) << 16;
514
- lo += hi >> 15;
515
- if (lo > 0x7fffffff)
516
- lo -= 0x7fffffff;
517
- return lo;
518
- }
519
-
520
- /**
521
- * checkConnPerformance - check connection performance
522
- * @returns Promise<number | undefined> - connection performance
523
- */
524
- async checkConnPerformance(): Promise<number | undefined> {
525
-
526
- // Sends a magic sequence of bytes followed by blocks of random data
527
- // these will be ignored by the Raft library (as it recognises magic sequence)
528
- // and is used performance evaluation
529
- let prbsState = 1;
530
- const testData = new Uint8Array(this._testConnPerfBlockSize);
531
- for (let i = 0; i < this._testConnPerfNumBlocks; i++) {
532
- testData.set([0, (i >> 24) & 0xff, (i >> 16) & 0xff, (i >> 8) & 0xff, i & 0xff, 0x1f, 0x9d, 0xf4, 0x7a, 0xb5]);
533
- for (let j = 10; j < this._testConnPerfBlockSize; j++) {
534
- prbsState = this.parkmiller_next(prbsState);
535
- testData[j] = prbsState & 0xff;
536
- }
537
- if (this._raftChannel) {
538
- await this._raftChannel.sendTxMsg(testData, false);
539
- }
540
- }
541
-
542
- // Wait a little to allow the Raft app to process the data
543
- await new Promise(resolve => setTimeout(resolve, this._connPerfRsltDelayMs));
544
-
545
- // Get performance
546
- const blePerf = await this._raftSystemUtils.getSysModInfoBLEMan();
547
- if (blePerf) {
548
- console.log(`checkConnPerformance result rate = ${blePerf.tBPS}BytesPS`);
549
- return blePerf.tBPS;
550
- } else {
551
- throw new Error('checkConnPerformance: failed to get BLE performance');
552
- }
553
- }
554
-
555
- // Mark: Connection event --------------------------------------------------------------------------
556
-
557
- /**
558
- * onConnEvent - handle connection event
559
- * @param eventEnum connection event enumeration
560
- * @param data data associated with event
561
- * @returns void
562
- */
563
- onConnEvent(eventEnum: RaftConnEvent, data: object | string | null | undefined = undefined): void {
564
- // Handle information clearing on disconnect
565
- switch (eventEnum) {
566
- case RaftConnEvent.CONN_DISCONNECTED:
567
-
568
- // Disconnect time
569
- this._retryIfLostDisconnectTime = Date.now();
570
-
571
- // Check if retry required
572
- if (this._retryIfLostIsConnected && this._retryIfLostEnabled) {
573
-
574
- // Indicate connection disrupted
575
- if (this._onEventFn) {
576
- this._onEventFn("conn", RaftConnEvent.CONN_ISSUE_DETECTED, RaftConnEventNames[RaftConnEvent.CONN_ISSUE_DETECTED]);
577
- }
578
-
579
- // Retry connection
580
- this._retryConnection();
581
-
582
- // Don't allow disconnection to propagate until retries have occurred
583
- return;
584
-
585
- }
586
-
587
- // Invalidate connection details
588
- this._raftSystemUtils.invalidate();
589
-
590
- // Invalidate system-type info
591
- if (this._systemType && this._systemType.stateIsInvalid) {
592
- this._systemType.stateIsInvalid();
593
- }
594
- break;
595
- }
596
- // Notify
597
- if (this._onEventFn) {
598
- this._onEventFn("conn", eventEnum, RaftConnEventNames[eventEnum], data);
599
- }
600
- }
601
-
602
- /**
603
- * Retry connection
604
- */
605
- private _retryConnection(): void {
606
-
607
- // Check timeout
608
- if ((this._retryIfLostDisconnectTime !== null) &&
609
- (Date.now() - this._retryIfLostDisconnectTime < this._retryIfLostForSecs * 1000)) {
610
-
611
- // Set timer to try to reconnect
612
- setTimeout(async () => {
613
-
614
- // Try to connect
615
- const isConn = await this._connectToChannel();
616
- if (!isConn) {
617
- this._retryConnection();
618
- } else {
619
-
620
- // No longer retrying
621
- this._retryIfLostDisconnectTime = null;
622
-
623
- // Indicate connection problem resolved
624
- if (this._onEventFn) {
625
- this._onEventFn("conn", RaftConnEvent.CONN_ISSUE_RESOLVED, RaftConnEventNames[RaftConnEvent.CONN_ISSUE_RESOLVED]);
626
- }
627
-
628
- }
629
- }, this._retryIfLostRetryDelayMs);
630
- } else {
631
-
632
- // No longer connected after retry timeout
633
- this._retryIfLostIsConnected = false;
634
-
635
- // Indicate disconnection
636
- if (this._onEventFn) {
637
- this._onEventFn("conn", RaftConnEvent.CONN_DISCONNECTED, RaftConnEventNames[RaftConnEvent.CONN_DISCONNECTED]);
638
- }
639
-
640
- // Invalidate connection details
641
- this._raftSystemUtils.invalidate();
642
-
643
- // Invalidate system-type info
644
- if (this._systemType && this._systemType.stateIsInvalid) {
645
- this._systemType.stateIsInvalid();
646
- }
647
- }
648
- }
649
-
650
- /**
651
- * Connect to channel
652
- * @returns Promise<boolean> - true if connected
653
- */
654
- private async _connectToChannel(): Promise<boolean> {
655
- // Connect
656
- try {
657
- if (this._raftChannel) {
658
- const connected = await this._raftChannel.connect(this._channelConnLocator, this._systemType ? this._systemType.connectorOptions : { });
659
- if (connected) {
660
- this._retryIfLostIsConnected = true;
661
- return true;
662
- }
663
- }
664
- } catch (error) {
665
- RaftLog.error(`RaftConnector.connect() error: ${error}`);
666
- }
667
- return false;
668
- }
669
-
670
- // Mark: OTA Update -----------------------------------------------------------------------------------------
671
-
672
- /**
673
- * onUpdateEvent - handle update event
674
- * @param eventEnum
675
- * @param data
676
- */
677
- _onUpdateEvent(eventEnum: RaftUpdateEvent, data: object | string | null | undefined = undefined): void {
678
- // Notify
679
- if (this._onEventFn) {
680
- this._onEventFn("ota", eventEnum, RaftUpdateEventNames[eventEnum], data);
681
- }
682
- }
683
-
684
- /**
685
- * otaUpdateCheck - check for OTA update
686
- * @returns Promise<RaftUpdateEvent> - update event
687
- * */
688
- async otaUpdateCheck(): Promise<RaftUpdateEvent> {
689
- if (!this._raftUpdateManager)
690
- return RaftUpdateEvent.UPDATE_NOT_CONFIGURED;
691
- return await this._raftUpdateManager.checkForUpdate(this._raftSystemUtils.getCachedSystemInfo());
692
- }
693
-
694
- /**
695
- * otaUpdateStart - start OTA update
696
- * @returns Promise<RaftUpdateEvent> - update event
697
- * */
698
- async otaUpdateStart(): Promise<RaftUpdateEvent> {
699
- if (!this._raftUpdateManager)
700
- return RaftUpdateEvent.UPDATE_NOT_CONFIGURED;
701
- return await this._raftUpdateManager.firmwareUpdate();
702
- }
703
-
704
- /**
705
- * otaUpdateCancel - cancel OTA update
706
- * @returns Promise<void>
707
- * */
708
- async otaUpdateCancel(): Promise<void> {
709
- if (!this._raftUpdateManager)
710
- return;
711
- return await this._raftUpdateManager.firmwareUpdateCancel();
712
- }
713
-
714
- /**
715
- * setupUpdateManager - setup the update manager
716
- * @param appVersion - app version
717
- * @param appUpdateURL - app update URL
718
- * @param firmwareBaseURL - firmware base URL
719
- * @param fileDownloader - file downloader
720
- * @returns void
721
- * */
722
- setupUpdateManager(appVersion: string, appUpdateURL: string, firmwareBaseURL: string, fileDownloader: RaftFileDownloadFn): void {
723
- // Setup update manager
724
- const firmwareTypeStrForMainFw = 'main';
725
- this._raftUpdateManager = new RaftUpdateManager(
726
- this._systemType,
727
- this._raftMsgHandler,
728
- this._raftFileHandler,
729
- this._raftSystemUtils,
730
- this._onUpdateEvent.bind(this),
731
- firmwareTypeStrForMainFw,
732
- appVersion,
733
- fileDownloader,
734
- appUpdateURL,
735
- firmwareBaseURL,
736
- this._raftChannel
737
- );
738
- }
739
- }