@robdobsn/raftjs 1.8.5 → 1.11.5

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