@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,777 +0,0 @@
1
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
- //
3
- // RaftMsgHandler
4
- // Part of RaftJS
5
- //
6
- // Rob Dobson & Chris Greening 2020-2024
7
- // (C) 2020-2024 All rights reserved
8
- //
9
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10
-
11
- import RaftCommsStats from './RaftCommsStats';
12
- import { RaftMsgTrackInfo } from './RaftMsgTrackInfo';
13
- import RaftLog from './RaftLog';
14
- import RaftUtils from './RaftUtils';
15
- import {
16
- RICSERIAL_MSG_NUM_POS,
17
- RICSERIAL_PAYLOAD_POS,
18
- RICSERIAL_PROTOCOL_POS,
19
- RICREST_REST_ELEM_CODE_POS,
20
- RICREST_HEADER_PAYLOAD_POS,
21
- RICREST_FILEBLOCK_FILEPOS_POS,
22
- RICREST_FILEBLOCK_PAYLOAD_POS,
23
- RICREST_BRIDGE_PAYLOAD_POS,
24
- RICREST_BRIDGE_ID_POS,
25
- RICRESTElemCode,
26
- RaftCommsMsgProtocol,
27
- RaftCommsMsgTypeCode,
28
- } from './RaftProtocolDefs';
29
- import RaftMiniHDLC from './RaftMiniHDLC';
30
- import { RaftBridgeSetupResp, RaftReportMsg } from './RaftTypes';
31
-
32
- // Message results
33
- export enum RaftMsgResultCode {
34
- MESSAGE_RESULT_TIMEOUT,
35
- MESSAGE_RESULT_OK,
36
- MESSAGE_RESULT_FAIL,
37
- MESSAGE_RESULT_UNKNOWN,
38
- }
39
-
40
- export interface RaftMessageResult {
41
- onRxReply(
42
- msgHandle: number,
43
- msgRsltCode: RaftMsgResultCode,
44
- msgRsltJsonObj: object | null,
45
- ): void;
46
- onRxUnnumberedMsg(msgRsltJsonObj: object): void;
47
- onRxFileBlock(
48
- filePos: number,
49
- fileBlockData: Uint8Array
50
- ): void;
51
- onRxOtherMsgType(payload: Uint8Array, frameTimeMs: number): void;
52
- }
53
-
54
- export interface RaftMessageSender {
55
- sendTxMsg(
56
- msg: Uint8Array,
57
- sendWithResponse: boolean,
58
- ): Promise<boolean>;
59
- sendTxMsgNoAwait(
60
- msg: Uint8Array,
61
- sendWithResponse: boolean,
62
- ): Promise<boolean>;
63
- }
64
-
65
- export default class RaftMsgHandler {
66
- // Message numbering and tracking
67
- private _currentMsgNumber = 1;
68
- private _currentMsgHandle = 1;
69
- private _msgTrackInfos: Array<RaftMsgTrackInfo> = new Array<RaftMsgTrackInfo>(
70
- RaftMsgTrackInfo.MAX_MSG_NUM + 1,
71
- );
72
- private _msgTrackTimerMs = 50;
73
- private _msgTrackLastCheckIdx = 0;
74
-
75
- // report message callback dictionary. Add a callback to subscribe to report messages
76
- private _reportMsgCallbacks = new Map<string, (report: RaftReportMsg) => void>();
77
-
78
- // Interface to inform of message results
79
- private _msgResultHandler: RaftMessageResult | null = null;
80
-
81
- // Interface to send messages
82
- private _msgSender: RaftMessageSender | null = null;
83
-
84
- // Comms stats
85
- private _commsStats: RaftCommsStats;
86
-
87
- // RaftMiniHDLC - handles part of RICSerial protocol
88
- private _miniHDLC: RaftMiniHDLC;
89
-
90
- // Constructor
91
- constructor(commsStats: RaftCommsStats) {
92
- this._commsStats = commsStats;
93
- RaftLog.debug('RaftMsgHandler constructor');
94
-
95
- // Message tracking
96
- for (let i = 0; i < this._msgTrackInfos.length; i++) {
97
- this._msgTrackInfos[i] = new RaftMsgTrackInfo();
98
- }
99
-
100
- // Timer for checking messages
101
- setTimeout(async () => {
102
- this._onMsgTrackTimer(true);
103
- }, this._msgTrackTimerMs);
104
-
105
- // HDLC used to encode/decode the RICREST protocol
106
- this._miniHDLC = new RaftMiniHDLC();
107
- this._miniHDLC.setOnRxFrame(this._onHDLCFrameDecode.bind(this));
108
- }
109
-
110
- registerForResults(msgResultHandler: RaftMessageResult) {
111
- this._msgResultHandler = msgResultHandler;
112
- }
113
-
114
- registerMsgSender(RaftMessageSender: RaftMessageSender) {
115
- this._msgSender = RaftMessageSender;
116
- }
117
-
118
- handleNewRxMsg(rxMsg: Uint8Array): void {
119
- this._miniHDLC.addRxBytes(rxMsg);
120
- // RaftLog.verbose(`handleNewRxMsg len ${rxMsg.length} ${RaftUtils.bufferToHex(rxMsg)}`)
121
- }
122
-
123
- reportMsgCallbacksSet(callbackName: string, callback: (report: RaftReportMsg) => void): void {
124
- this._reportMsgCallbacks.set(callbackName, callback);
125
- }
126
-
127
- reportMsgCallbacksDelete(callbackName: string) {
128
- this._reportMsgCallbacks.delete(callbackName);
129
- }
130
-
131
- _onHDLCFrameDecode(rxMsg: Uint8Array, frameTimeMs: number): void {
132
- // Add to stats
133
- this._commsStats.msgRx();
134
-
135
- // Validity
136
- if (rxMsg.length < RICSERIAL_PAYLOAD_POS) {
137
- this._commsStats.msgTooShort();
138
- return;
139
- }
140
-
141
- // RaftLog.verbose(`_onHDLCFrameDecode len ${rxMsg.length}`);
142
-
143
- // Decode the RICFrame header
144
- let rxMsgNum = rxMsg[RICSERIAL_MSG_NUM_POS] & 0xff;
145
- let rxProtocol = rxMsg[RICSERIAL_PROTOCOL_POS] & 0x3f;
146
- let rxMsgType = (rxMsg[RICSERIAL_PROTOCOL_POS] >> 6) & 0x03;
147
-
148
- // Check for RICREST bridging protocol
149
- if (rxProtocol == RaftCommsMsgProtocol.MSG_PROTOCOL_BRIDGE_RICREST) {
150
-
151
- // Debug
152
- const bridgeID = rxMsg.length > RICREST_BRIDGE_ID_POS ? rxMsg[RICREST_BRIDGE_ID_POS] : 0;
153
- RaftLog.info(
154
- `_onHDLCFrameDecode RICREST bridge rx bridgeID ${bridgeID} len ${rxMsg.length}`
155
- );
156
-
157
- // Simply remove the wrapper
158
- rxMsg = rxMsg.slice(RICREST_BRIDGE_PAYLOAD_POS);
159
-
160
- // Get the message info from the unwrapped message
161
- rxMsgNum = rxMsg[RICSERIAL_MSG_NUM_POS] & 0xff;
162
- rxProtocol = rxMsg[RICSERIAL_PROTOCOL_POS] & 0x3f;
163
- rxMsgType = (rxMsg[RICSERIAL_PROTOCOL_POS] >> 6) & 0x03;
164
- }
165
-
166
- // Check for RICREST protocol
167
- if (rxProtocol == RaftCommsMsgProtocol.MSG_PROTOCOL_RICREST) {
168
- RaftLog.verbose(
169
- `_onHDLCFrameDecode RICREST rx msgNum ${rxMsgNum} msgDirn ${rxMsgType} ${RaftUtils.bufferToHex(
170
- rxMsg,
171
- )}`,
172
- );
173
- // Extract payload
174
- const ricRestElemCode =
175
- rxMsg[RICSERIAL_PAYLOAD_POS + RICREST_REST_ELEM_CODE_POS] & 0xff;
176
- if (
177
- ricRestElemCode == RICRESTElemCode.RICREST_ELEM_CODE_URL ||
178
- ricRestElemCode == RICRESTElemCode.RICREST_ELEM_CODE_CMDRESPJSON ||
179
- ricRestElemCode == RICRESTElemCode.RICREST_ELEM_CODE_COMMAND_FRAME
180
- ) {
181
- // These are all text-based messages
182
- const restStr = RaftUtils.getStringFromBuffer(
183
- rxMsg,
184
- RICSERIAL_PAYLOAD_POS + RICREST_HEADER_PAYLOAD_POS,
185
- rxMsg.length - RICSERIAL_PAYLOAD_POS - RICREST_HEADER_PAYLOAD_POS - 1,
186
- );
187
- RaftLog.verbose(
188
- `_onHDLCFrameDecode RICREST rx elemCode ${ricRestElemCode} ${restStr}`,
189
- );
190
-
191
- // Check message types
192
- if (rxMsgType == RaftCommsMsgTypeCode.MSG_TYPE_RESPONSE) {
193
-
194
- // Handle response messages
195
- this._handleResponseMessages(restStr, rxMsgNum);
196
-
197
- } else if (rxMsgType == RaftCommsMsgTypeCode.MSG_TYPE_REPORT) {
198
-
199
- // Handle report messages
200
- this._handleReportMessages(restStr);
201
-
202
- }
203
-
204
- } else {
205
- const binMsgLen = rxMsg.length - RICSERIAL_PAYLOAD_POS - RICREST_HEADER_PAYLOAD_POS;
206
- RaftLog.verbose(
207
- `_onHDLCFrameDecode RICREST rx binary message elemCode ${ricRestElemCode} len ${binMsgLen} data ${RaftUtils.bufferToHex(rxMsg)}`,
208
- );
209
- if (ricRestElemCode == RICRESTElemCode.RICREST_ELEM_CODE_FILEBLOCK) {
210
- const filePos = RaftUtils.getBEUint32FromBuf(rxMsg, RICSERIAL_PAYLOAD_POS + RICREST_HEADER_PAYLOAD_POS + RICREST_FILEBLOCK_FILEPOS_POS);
211
- this._msgResultHandler?.onRxFileBlock(
212
- filePos,
213
- rxMsg.slice(RICSERIAL_PAYLOAD_POS + RICREST_HEADER_PAYLOAD_POS + RICREST_FILEBLOCK_PAYLOAD_POS, rxMsg.length));
214
- }
215
- }
216
-
217
- // Other message types
218
- } else {
219
- this._msgResultHandler?.onRxOtherMsgType(rxMsg, frameTimeMs);
220
- }
221
- }
222
-
223
- _handleResponseMessages(restStr: string, rxMsgNum: number): void {
224
- try {
225
- let msgRsltCode = RaftMsgResultCode.MESSAGE_RESULT_UNKNOWN;
226
- const msgRsltJsonObj = JSON.parse(restStr);
227
- if ('rslt' in msgRsltJsonObj) {
228
- const rsltStr = msgRsltJsonObj.rslt.toLowerCase();
229
- if (rsltStr === 'ok') {
230
- RaftLog.verbose(
231
- `_handleResponseMessages RICREST rslt Ok ${rxMsgNum == 0 ? "unnumbered" : "msgNum " + rxMsgNum.toString()} resp ${msgRsltJsonObj.rslt}`,
232
- );
233
- msgRsltCode = RaftMsgResultCode.MESSAGE_RESULT_OK;
234
- } else if (rsltStr === 'fail') {
235
- msgRsltCode = RaftMsgResultCode.MESSAGE_RESULT_FAIL;
236
- RaftLog.warn(
237
- `_handleResponseMessages RICREST rslt fail ${rxMsgNum == 0 ? "unnumbered" : "msgNum " + rxMsgNum.toString()} resp ${restStr}`,
238
- );
239
- } else {
240
- RaftLog.warn(
241
- `_handleResponseMessages RICREST rslt not recognized ${rxMsgNum == 0 ? "unnumbered" : "msgNum " + rxMsgNum.toString()}resp ${restStr}`,
242
- );
243
- }
244
-
245
- } else {
246
- RaftLog.warn(
247
- `_handleResponseMessages RICREST response doesn't contain rslt ${rxMsgNum == 0 ? "unnumbered" : "msgNum " + rxMsgNum.toString()}resp ${restStr}`,
248
- );
249
- }
250
-
251
- // Handle matching of request and response
252
- this.msgTrackingRxRespMsg(rxMsgNum, msgRsltCode, msgRsltJsonObj);
253
-
254
- } catch (excp: unknown) {
255
- if (excp instanceof Error) {
256
- RaftLog.warn(
257
- `_handleResponseMessages Failed to parse JSON ${rxMsgNum == 0 ? "unnumbered" : "msgNum " + rxMsgNum.toString()} JSON STR ${restStr} resp ${excp.toString()}`,
258
- );
259
- }
260
- }
261
-
262
- }
263
-
264
- _handleReportMessages(restStr: string): void {
265
- try {
266
- const reportMsg: RaftReportMsg = JSON.parse(restStr);
267
- reportMsg.timeReceived = Date.now();
268
- RaftLog.debug(`_handleReportMessages ${JSON.stringify(reportMsg)}`);
269
- this._reportMsgCallbacks.forEach((callback) => callback(reportMsg));
270
- } catch (excp: unknown) {
271
- if (excp instanceof Error) {
272
- RaftLog.warn(
273
- `_handleReportMessages Failed to parse JSON report ${excp.toString()}`,
274
- );
275
- }
276
- }
277
- }
278
-
279
- async sendRICRESTURL<T>(
280
- cmdStr: string,
281
- bridgeID: number | undefined = undefined,
282
- msgTimeoutMs: number | undefined = undefined,
283
- ): Promise<T> {
284
- // Send
285
- return this.sendRICREST(
286
- cmdStr,
287
- RICRESTElemCode.RICREST_ELEM_CODE_URL,
288
- bridgeID,
289
- msgTimeoutMs,
290
- );
291
- }
292
-
293
- async sendRICRESTCmdFrame<T>(
294
- cmdStr: string,
295
- bridgeID: number | undefined = undefined,
296
- msgTimeoutMs: number | undefined = undefined,
297
- ): Promise<T> {
298
- // Send
299
- return this.sendRICREST(
300
- cmdStr,
301
- RICRESTElemCode.RICREST_ELEM_CODE_COMMAND_FRAME,
302
- bridgeID,
303
- msgTimeoutMs,
304
- );
305
- }
306
-
307
- async sendRICREST<T>(
308
- cmdStr: string,
309
- ricRESTElemCode: RICRESTElemCode,
310
- bridgeID: number | undefined = undefined,
311
- msgTimeoutMs: number | undefined = undefined,
312
- ): Promise<T> {
313
- // Put cmdStr into buffer
314
- const cmdStrTerm = new Uint8Array(cmdStr.length + 1);
315
- RaftUtils.addStringToBuffer(cmdStrTerm, cmdStr, 0);
316
- cmdStrTerm[cmdStrTerm.length - 1] = 0;
317
-
318
- // Send
319
- return this.sendRICRESTBytes(
320
- cmdStrTerm,
321
- ricRESTElemCode,
322
- true,
323
- bridgeID,
324
- msgTimeoutMs,
325
- );
326
- }
327
-
328
- async sendRICRESTNoResp(
329
- cmdStr: string,
330
- ricRESTElemCode: RICRESTElemCode,
331
- bridgeID: number | undefined = undefined,
332
- ): Promise<boolean> {
333
-
334
- // Check there is a sender
335
- if (!this._msgSender) {
336
- return false;
337
- }
338
-
339
- // Put cmdStr into buffer
340
- const cmdBytes = new Uint8Array(cmdStr.length + 1);
341
- RaftUtils.addStringToBuffer(cmdBytes, cmdStr, 0);
342
- cmdBytes[cmdBytes.length - 1] = 0;
343
-
344
- // Form message
345
- const cmdMsg = new Uint8Array(cmdBytes.length + RICREST_HEADER_PAYLOAD_POS);
346
- cmdMsg[RICREST_REST_ELEM_CODE_POS] = ricRESTElemCode;
347
- cmdMsg.set(cmdBytes, RICREST_HEADER_PAYLOAD_POS);
348
-
349
- // Frame the message
350
- let framedMsg = this.frameCommsMsg(cmdMsg,
351
- RaftCommsMsgTypeCode.MSG_TYPE_COMMAND,
352
- RaftCommsMsgProtocol.MSG_PROTOCOL_RICREST,
353
- true);
354
-
355
- // Wrap if bridged
356
- if (bridgeID !== undefined) {
357
- framedMsg = this.bridgeCommsMsg(framedMsg, bridgeID);
358
- }
359
-
360
- // Encode like HDLC
361
- const encodedMsg = this._miniHDLC.encode(framedMsg);
362
-
363
- // Send
364
- if (!await this._msgSender.sendTxMsg(encodedMsg, false)) {
365
- RaftLog.warn(`sendRICRESTNoResp failed to send message`);
366
- this._commsStats.recordMsgNoConnection();
367
- }
368
-
369
- return true;
370
- }
371
-
372
- async sendRICRESTBytes<T>(
373
- cmdBytes: Uint8Array,
374
- ricRESTElemCode: RICRESTElemCode,
375
- withResponse: boolean,
376
- bridgeID: number | undefined = undefined,
377
- msgTimeoutMs: number | undefined = undefined,
378
- ): Promise<T> {
379
- // Form message
380
- const cmdMsg = new Uint8Array(cmdBytes.length + RICREST_HEADER_PAYLOAD_POS);
381
- cmdMsg[RICREST_REST_ELEM_CODE_POS] = ricRESTElemCode;
382
- cmdMsg.set(cmdBytes, RICREST_HEADER_PAYLOAD_POS);
383
-
384
- // Send
385
- return this.sendMsgAndWaitForReply<T>(
386
- cmdMsg,
387
- RaftCommsMsgTypeCode.MSG_TYPE_COMMAND,
388
- RaftCommsMsgProtocol.MSG_PROTOCOL_RICREST,
389
- withResponse,
390
- bridgeID,
391
- msgTimeoutMs,
392
- );
393
- }
394
-
395
- async sendMsgAndWaitForReply<T>(
396
- msgPayload: Uint8Array,
397
- msgDirection: RaftCommsMsgTypeCode,
398
- msgProtocol: RaftCommsMsgProtocol,
399
- withResponse: boolean,
400
- bridgeID: number | undefined = undefined,
401
- msgTimeoutMs: number | undefined,
402
- ): Promise<T> {
403
-
404
- // Check there is a sender
405
- if (!this._msgSender) {
406
- throw new Error('sendMsgAndWaitForReply failed no sender');
407
- }
408
-
409
- // Frame the message
410
- let framedMsg = this.frameCommsMsg(msgPayload, msgDirection, msgProtocol, true);
411
-
412
- // Wrap if bridged
413
- if (bridgeID !== undefined) {
414
- framedMsg = this.bridgeCommsMsg(framedMsg, bridgeID);
415
- // RaftLog.debug(`sendMsgAndWaitForReply - bridged idx ${bridgeID}`)
416
- } else {
417
- // RaftLog.debug(`sendMsgAndWaitForReply - not bridged`)
418
- }
419
-
420
- // Encode like HDLC
421
- const encodedMsg = this._miniHDLC.encode(framedMsg);
422
-
423
- // Debug
424
- // RaftLog.debug(
425
- // `sendMsgAndWaitForReply ${RaftUtils.bufferToHex(encodedMsg)}`,
426
- // );
427
-
428
- // Return a promise that will be resolved when a reply is received or timeout occurs
429
- const promise = new Promise<T>((resolve, reject) => {
430
-
431
- // Update message tracking
432
- this.msgTrackingTxCmdMsg<T>(
433
- encodedMsg,
434
- withResponse,
435
- bridgeID,
436
- msgTimeoutMs,
437
- resolve,
438
- reject,
439
- );
440
- this._currentMsgHandle++;
441
- });
442
-
443
- return promise;
444
- }
445
-
446
- frameCommsMsg(
447
- msgPayload: Uint8Array,
448
- msgDirection: RaftCommsMsgTypeCode,
449
- msgProtocol: RaftCommsMsgProtocol,
450
- isNumbered: boolean,
451
- ): Uint8Array {
452
- // Header
453
- const msgBuf = new Uint8Array(
454
- msgPayload.length + RICSERIAL_PAYLOAD_POS,
455
- );
456
- msgBuf[RICSERIAL_MSG_NUM_POS] = isNumbered ? this._currentMsgNumber & 0xff : 0;
457
- msgBuf[RICSERIAL_PROTOCOL_POS] = (msgDirection << 6) + msgProtocol;
458
-
459
- // Payload
460
- msgBuf.set(msgPayload, RICSERIAL_PAYLOAD_POS);
461
-
462
- // Return framed message
463
- return msgBuf;
464
- }
465
-
466
- bridgeCommsMsg(
467
- msgBuf: Uint8Array,
468
- bridgeID: number
469
- ) {
470
- //
471
- const bridgedMsg = new Uint8Array(msgBuf.length + RICREST_BRIDGE_PAYLOAD_POS);
472
-
473
- // Bridged messages are unnumbered (msgNum == 0)
474
- bridgedMsg[RICSERIAL_MSG_NUM_POS] = 0;
475
- bridgedMsg[RICSERIAL_PROTOCOL_POS] = (RaftCommsMsgTypeCode.MSG_TYPE_COMMAND << 6) + RaftCommsMsgProtocol.MSG_PROTOCOL_BRIDGE_RICREST;
476
- bridgedMsg[RICREST_BRIDGE_ID_POS] = bridgeID;
477
- bridgedMsg.set(msgBuf, RICREST_BRIDGE_PAYLOAD_POS);
478
- return bridgedMsg;
479
- }
480
-
481
- msgTrackingTxCmdMsg<T>(
482
- msgFrame: Uint8Array,
483
- withResponse: boolean,
484
- bridgeID: number | undefined = undefined,
485
- msgTimeoutMs: number | undefined,
486
- resolve: (arg: T) => void,
487
- reject: (reason: Error) => void,
488
- ): void {
489
- // Record message re-use of number
490
- if (this._msgTrackInfos[this._currentMsgNumber].msgOutstanding) {
491
- this._commsStats.recordMsgNumCollision();
492
- }
493
- // Set tracking info
494
- this._msgTrackInfos[this._currentMsgNumber].set(
495
- true,
496
- msgFrame,
497
- withResponse,
498
- bridgeID,
499
- this._currentMsgHandle,
500
- msgTimeoutMs,
501
- resolve,
502
- reject,
503
- );
504
-
505
- // Debug
506
- RaftLog.debug(
507
- `msgTrackingTxCmdMsg msgNum ${this._currentMsgNumber} bridgeID ${bridgeID} msg ${
508
- RaftUtils.bufferToHex(msgFrame)} msgOutstanding ${this._msgTrackInfos[this._currentMsgNumber].msgOutstanding
509
- }`,
510
- );
511
-
512
- // Stats
513
- this._commsStats.msgTx();
514
-
515
- // Bump msg number
516
- if (this._currentMsgNumber == RaftMsgTrackInfo.MAX_MSG_NUM) {
517
- this._currentMsgNumber = 1;
518
- } else {
519
- this._currentMsgNumber++;
520
- }
521
- }
522
-
523
- msgTrackingRxRespMsg(
524
- msgNum: number,
525
- msgRsltCode: RaftMsgResultCode,
526
- msgRsltJsonObj: object,
527
- ) {
528
- // Check message number
529
- if (msgNum == 0) {
530
- // Callback on unnumbered message
531
- if (this._msgResultHandler !== null)
532
- this._msgResultHandler.onRxUnnumberedMsg(msgRsltJsonObj);
533
- return;
534
- }
535
- if (msgNum > RaftMsgTrackInfo.MAX_MSG_NUM) {
536
- RaftLog.warn('msgTrackingRxRespMsg msgNum > 255');
537
- return;
538
- }
539
- if (!this._msgTrackInfos[msgNum].msgOutstanding) {
540
- RaftLog.warn(`msgTrackingRxRespMsg unmatched msgNum ${msgNum}`);
541
- this._commsStats.recordMsgNumUnmatched();
542
- return;
543
- }
544
-
545
- // Handle message
546
- RaftLog.verbose(
547
- `msgTrackingRxRespMsg Message response received msgNum ${msgNum}`,
548
- );
549
- this._commsStats.recordMsgResp(
550
- Date.now() - this._msgTrackInfos[msgNum].msgSentMs,
551
- );
552
- this._msgCompleted(msgNum, msgRsltCode, msgRsltJsonObj);
553
- }
554
-
555
- _msgCompleted(
556
- msgNum: number,
557
- msgRsltCode: RaftMsgResultCode,
558
- msgRsltObj: object | null,
559
- ) {
560
-
561
- // Lookup message in tracking
562
- const msgHandle = this._msgTrackInfos[msgNum].msgHandle;
563
- this._msgTrackInfos[msgNum].msgOutstanding = false;
564
-
565
- // Check if message result handler should be informed
566
- if (this._msgResultHandler !== null) {
567
- this._msgResultHandler.onRxReply(msgHandle, msgRsltCode, msgRsltObj);
568
- }
569
-
570
- // Handle reply
571
- // if (msgRsltCode === RaftMsgResultCode.MESSAGE_RESULT_OK) {
572
- const resolve = this._msgTrackInfos[msgNum].resolve;
573
- if (resolve) {
574
- RaftLog.verbose(`_msgCompleted msgNum ${msgNum} result ${msgRsltCode.toString()} ${JSON.stringify(msgRsltObj)}`);
575
- (resolve as ((arg: object | null) => void))(msgRsltObj);
576
- }
577
- // } else {
578
- // const reject = this._msgTrackInfos[msgNum].reject;
579
- // if (reject) {
580
- // // eslint-disable-next-line @typescript-eslint/no-explicit-any
581
- // try {
582
- // RaftLog.debug(`_msgCompleted reject rsltCode ${msgRsltCode}`);
583
- // // (reject as any)(new Error(`Message failed msgNum ${msgNum} rslt ${msgRsltCode}`));
584
- // } catch (excp: unknown) {
585
- // RaftLog.warn(`_msgCompleted reject ${excp}`);
586
- // }
587
- // }
588
- // }
589
-
590
- // No longer waiting for reply
591
- this._msgTrackInfos[msgNum].resolve = null;
592
- this._msgTrackInfos[msgNum].reject = null;
593
- }
594
-
595
- // Check message timeouts
596
- async _onMsgTrackTimer(chainRecall: boolean): Promise<void> {
597
-
598
- if (this._msgSender !== null) {
599
- // Handle message tracking
600
- for (let loopIdx = 0; loopIdx < this._msgTrackInfos.length; loopIdx++) {
601
-
602
- // Index to check
603
- const checkIdx = this._msgTrackLastCheckIdx;
604
- this._msgTrackLastCheckIdx = (checkIdx + 1) % this._msgTrackInfos.length;
605
-
606
- // Check if message is outstanding
607
- if (!this._msgTrackInfos[checkIdx].msgOutstanding) continue;
608
-
609
- // Get message timeout and ensure valid
610
- let msgTimeoutMs = this._msgTrackInfos[checkIdx].msgTimeoutMs;
611
- if (msgTimeoutMs === undefined) {
612
- msgTimeoutMs = RaftMsgTrackInfo.MSG_RESPONSE_TIMEOUT_MS;
613
- }
614
-
615
- // Check for timeout (or never sent)
616
- if ((this._msgTrackInfos[checkIdx].retryCount === 0) || (Date.now() > this._msgTrackInfos[checkIdx].msgSentMs + (msgTimeoutMs * (this._msgTrackInfos[checkIdx].retryCount)))) {
617
-
618
- // Debug
619
- RaftLog.verbose(`msgTrackTimer msgNum ${checkIdx} ${this._msgTrackInfos[checkIdx].retryCount === 0 ? 'first send' : 'timeout - retrying'} ${RaftUtils.bufferToHex(this._msgTrackInfos[checkIdx].msgFrame)}`);
620
- // RaftLog.verbose(`msgTrackTimer msg ${RaftUtils.bufferToHex(this._msgTrackInfos[i].msgFrame)}`);
621
-
622
- // Handle timeout (or first send)
623
- if (this._msgTrackInfos[checkIdx].retryCount < RaftMsgTrackInfo.MSG_RETRY_COUNT) {
624
- this._msgTrackInfos[checkIdx].retryCount++;
625
- try {
626
-
627
- // Send the message
628
- if (!await this._msgSender.sendTxMsg(
629
- this._msgTrackInfos[checkIdx].msgFrame,
630
- this._msgTrackInfos[checkIdx].withResponse)) {
631
- RaftLog.warn(`msgTrackTimer Message send failed msgNum ${checkIdx} ${RaftUtils.bufferToHex(this._msgTrackInfos[checkIdx].msgFrame)}`);
632
- this._msgCompleted(checkIdx, RaftMsgResultCode.MESSAGE_RESULT_FAIL, null);
633
- this._commsStats.recordMsgNoConnection();
634
- }
635
-
636
- // Message sent ok so break here
637
- break;
638
-
639
- } catch (error: unknown) {
640
- RaftLog.warn(`Retry message failed ${error}`);
641
- }
642
- this._commsStats.recordMsgRetry();
643
- this._msgTrackInfos[checkIdx].msgSentMs = Date.now();
644
- } else {
645
- RaftLog.warn(
646
- `msgTrackTimer TIMEOUT msgNum ${checkIdx} after ${RaftMsgTrackInfo.MSG_RETRY_COUNT} retries ${RaftUtils.bufferToHex(this._msgTrackInfos[checkIdx].msgFrame)}`,
647
- );
648
- this._msgCompleted(checkIdx, RaftMsgResultCode.MESSAGE_RESULT_TIMEOUT, null);
649
- this._commsStats.recordMsgTimeout();
650
- }
651
- }
652
- }
653
- }
654
-
655
- // Call again if required
656
- if (chainRecall) {
657
- setTimeout(async () => {
658
- this._onMsgTrackTimer(true);
659
- }, this._msgTrackTimerMs);
660
- }
661
- }
662
-
663
- encodeFileStreamBlock(blockContents: Uint8Array,
664
- blockStart: number,
665
- streamID: number): Uint8Array {
666
- // Create entire message buffer (including protocol wrappers)
667
- const msgBuf = new Uint8Array(
668
- blockContents.length + 4 + RICREST_HEADER_PAYLOAD_POS + RICSERIAL_PAYLOAD_POS,
669
- );
670
- let msgBufPos = 0;
671
-
672
- // RICSERIAL protocol
673
- msgBuf[msgBufPos++] = 0; // not numbered
674
- msgBuf[msgBufPos++] =
675
- (RaftCommsMsgTypeCode.MSG_TYPE_COMMAND << 6) +
676
- RaftCommsMsgProtocol.MSG_PROTOCOL_RICREST;
677
-
678
- // RICREST protocol
679
- msgBuf[msgBufPos++] = RICRESTElemCode.RICREST_ELEM_CODE_FILEBLOCK;
680
-
681
- // Header
682
- msgBuf[msgBufPos++] = streamID & 0xff;
683
- msgBuf[msgBufPos++] = (blockStart >> 16) & 0xff;
684
- msgBuf[msgBufPos++] = (blockStart >> 8) & 0xff;
685
- msgBuf[msgBufPos++] = blockStart & 0xff;
686
-
687
- // Copy block info
688
- msgBuf.set(blockContents, msgBufPos);
689
- return msgBuf;
690
- }
691
-
692
- async sendFileBlock(
693
- blockContents: Uint8Array,
694
- blockStart: number
695
- ): Promise<boolean> {
696
- const msgBuf = this.encodeFileStreamBlock(blockContents, blockStart, 0);
697
-
698
- // // Debug
699
- // RaftLog.debug(
700
- // `sendFileBlock frameLen ${msgBuf.length} start ${blockStart} end ${blockEnd} len ${blockLen}`,
701
- // );
702
-
703
- // Send
704
- try {
705
- // Send
706
- if (this._msgSender) {
707
-
708
- // Wrap into HDLC
709
- const framedMsg = this._miniHDLC.encode(msgBuf);
710
-
711
- // Send
712
- return this._msgSender.sendTxMsg(
713
- framedMsg,
714
- true,
715
- // Platform.OS === 'ios',
716
- );
717
- }
718
- } catch (error: unknown) {
719
- RaftLog.warn(`RaftMsgHandler sendFileBlock error${error}`);
720
- }
721
- return false;
722
- }
723
-
724
- async sendStreamBlock(
725
- blockContents: Uint8Array,
726
- blockStart: number,
727
- streamID: number,
728
- ): Promise<boolean> {
729
-
730
- // Ensure any waiting messages are sent first
731
- await this._onMsgTrackTimer(false);
732
-
733
- // Encode message
734
- const msgBuf = this.encodeFileStreamBlock(blockContents, blockStart, streamID);
735
-
736
- // // Debug
737
- // RaftLog.debug(
738
- // `sendStreamBlock frameLen ${msgBuf.length} start ${blockStart} end ${blockEnd} len ${blockLen}`,
739
- // );
740
-
741
- // Send
742
- try {
743
- // Send
744
- if (this._msgSender) {
745
-
746
- // Wrap into HDLC
747
- const framedMsg = this._miniHDLC.encode(msgBuf);
748
-
749
- // Send
750
- return await this._msgSender.sendTxMsg(
751
- framedMsg,
752
- true,
753
- // Platform.OS === 'ios',
754
- );
755
- }
756
- } catch (error: unknown) {
757
- RaftLog.warn(`RaftMsgHandler sendStreamBlock error${error}`);
758
- }
759
- return false;
760
- }
761
-
762
- async createCommsBridge(bridgeSource: string, bridgeName: string, idleCloseSecs = 0): Promise<RaftBridgeSetupResp> {
763
-
764
- // Establish a bridge
765
- return await this.sendRICRESTURL<RaftBridgeSetupResp>(
766
- `commandserial/bridge/setup?port=${bridgeSource}&name=${bridgeName}&idleCloseSecs=${idleCloseSecs}`,
767
- )
768
- }
769
-
770
- async removeCommsBridge(bridgeID: number): Promise<boolean> {
771
-
772
- // Remove a bridge
773
- return await this.sendRICRESTURL<boolean>(
774
- `commandserial/bridge/remove?id=${bridgeID}`,
775
- )
776
- }
777
- }