@robdobsn/raftjs 1.1.1

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 (149) hide show
  1. package/.editorconfig +14 -0
  2. package/.gitattributes +11 -0
  3. package/.nvmrc +1 -0
  4. package/LICENSE +22 -0
  5. package/README.md +11 -0
  6. package/TODO.md +1 -0
  7. package/dist/RaftAttributeHandler.d.ts +12 -0
  8. package/dist/RaftAttributeHandler.js +241 -0
  9. package/dist/RaftAttributeHandler.js.map +1 -0
  10. package/dist/RaftChannel.d.ts +18 -0
  11. package/dist/RaftChannel.js +12 -0
  12. package/dist/RaftChannel.js.map +1 -0
  13. package/dist/RaftChannelWebBLE.d.ts +38 -0
  14. package/dist/RaftChannelWebBLE.js +274 -0
  15. package/dist/RaftChannelWebBLE.js.map +1 -0
  16. package/dist/RaftChannelWebSerial.d.ts +37 -0
  17. package/dist/RaftChannelWebSerial.js +319 -0
  18. package/dist/RaftChannelWebSerial.js.map +1 -0
  19. package/dist/RaftChannelWebSocket.d.ts +28 -0
  20. package/dist/RaftChannelWebSocket.js +197 -0
  21. package/dist/RaftChannelWebSocket.js.map +1 -0
  22. package/dist/RaftCommsStats.d.ts +39 -0
  23. package/dist/RaftCommsStats.js +128 -0
  24. package/dist/RaftCommsStats.js.map +1 -0
  25. package/dist/RaftConnEvents.d.ts +31 -0
  26. package/dist/RaftConnEvents.js +44 -0
  27. package/dist/RaftConnEvents.js.map +1 -0
  28. package/dist/RaftConnector.d.ts +242 -0
  29. package/dist/RaftConnector.js +613 -0
  30. package/dist/RaftConnector.js.map +1 -0
  31. package/dist/RaftCustomAttrHandler.d.ts +4 -0
  32. package/dist/RaftCustomAttrHandler.js +50 -0
  33. package/dist/RaftCustomAttrHandler.js.map +1 -0
  34. package/dist/RaftDeviceInfo.d.ts +64 -0
  35. package/dist/RaftDeviceInfo.js +36 -0
  36. package/dist/RaftDeviceInfo.js.map +1 -0
  37. package/dist/RaftDeviceManager.d.ts +37 -0
  38. package/dist/RaftDeviceManager.js +450 -0
  39. package/dist/RaftDeviceManager.js.map +1 -0
  40. package/dist/RaftDeviceMsg.d.ts +9 -0
  41. package/dist/RaftDeviceMsg.js +11 -0
  42. package/dist/RaftDeviceMsg.js.map +1 -0
  43. package/dist/RaftDeviceStates.d.ts +33 -0
  44. package/dist/RaftDeviceStates.js +60 -0
  45. package/dist/RaftDeviceStates.js.map +1 -0
  46. package/dist/RaftFileHandler.d.ts +52 -0
  47. package/dist/RaftFileHandler.js +502 -0
  48. package/dist/RaftFileHandler.js.map +1 -0
  49. package/dist/RaftLog.d.ts +22 -0
  50. package/dist/RaftLog.js +63 -0
  51. package/dist/RaftLog.js.map +1 -0
  52. package/dist/RaftMiniHDLC.d.ts +18 -0
  53. package/dist/RaftMiniHDLC.js +383 -0
  54. package/dist/RaftMiniHDLC.js.map +1 -0
  55. package/dist/RaftMsgHandler.d.ts +57 -0
  56. package/dist/RaftMsgHandler.js +480 -0
  57. package/dist/RaftMsgHandler.js.map +1 -0
  58. package/dist/RaftMsgTrackInfo.d.ts +17 -0
  59. package/dist/RaftMsgTrackInfo.js +42 -0
  60. package/dist/RaftMsgTrackInfo.js.map +1 -0
  61. package/dist/RaftProtocolDefs.d.ts +30 -0
  62. package/dist/RaftProtocolDefs.js +48 -0
  63. package/dist/RaftProtocolDefs.js.map +1 -0
  64. package/dist/RaftStreamHandler.d.ts +38 -0
  65. package/dist/RaftStreamHandler.js +257 -0
  66. package/dist/RaftStreamHandler.js.map +1 -0
  67. package/dist/RaftSystemType.d.ts +21 -0
  68. package/dist/RaftSystemType.js +3 -0
  69. package/dist/RaftSystemType.js.map +1 -0
  70. package/dist/RaftSystemUtils.d.ts +136 -0
  71. package/dist/RaftSystemUtils.js +410 -0
  72. package/dist/RaftSystemUtils.js.map +1 -0
  73. package/dist/RaftTypes.d.ts +184 -0
  74. package/dist/RaftTypes.js +157 -0
  75. package/dist/RaftTypes.js.map +1 -0
  76. package/dist/RaftUpdateEvents.d.ts +33 -0
  77. package/dist/RaftUpdateEvents.js +46 -0
  78. package/dist/RaftUpdateEvents.js.map +1 -0
  79. package/dist/RaftUpdateManager.d.ts +61 -0
  80. package/dist/RaftUpdateManager.js +618 -0
  81. package/dist/RaftUpdateManager.js.map +1 -0
  82. package/dist/RaftUtils.d.ts +125 -0
  83. package/dist/RaftUtils.js +454 -0
  84. package/dist/RaftUtils.js.map +1 -0
  85. package/dist/RaftWifiTypes.d.ts +23 -0
  86. package/dist/RaftWifiTypes.js +43 -0
  87. package/dist/RaftWifiTypes.js.map +1 -0
  88. package/dist/TestDataGen.d.ts +7 -0
  89. package/dist/TestDataGen.js +133 -0
  90. package/dist/TestDataGen.js.map +1 -0
  91. package/dist/main.d.ts +18 -0
  92. package/dist/main.js +42 -0
  93. package/dist/main.js.map +1 -0
  94. package/eslint.config.mjs +33 -0
  95. package/examples/dashboard/package.json +39 -0
  96. package/examples/dashboard/src/ConnManager.ts +86 -0
  97. package/examples/dashboard/src/Main.tsx +100 -0
  98. package/examples/dashboard/src/StatusScreen.tsx +72 -0
  99. package/examples/dashboard/src/SystemTypeCog/CogStateInfo.ts +144 -0
  100. package/examples/dashboard/src/SystemTypeCog/SystemTypeCog.ts +77 -0
  101. package/examples/dashboard/src/SystemTypeMarty/RICAddOn.ts +70 -0
  102. package/examples/dashboard/src/SystemTypeMarty/RICAddOnBase.ts +33 -0
  103. package/examples/dashboard/src/SystemTypeMarty/RICAddOnManager.ts +342 -0
  104. package/examples/dashboard/src/SystemTypeMarty/RICCommsStats.ts +170 -0
  105. package/examples/dashboard/src/SystemTypeMarty/RICHWElem.ts +123 -0
  106. package/examples/dashboard/src/SystemTypeMarty/RICLEDPatternChecker.ts +207 -0
  107. package/examples/dashboard/src/SystemTypeMarty/RICROSSerial.ts +464 -0
  108. package/examples/dashboard/src/SystemTypeMarty/RICServoFaultDetector.ts +146 -0
  109. package/examples/dashboard/src/SystemTypeMarty/RICStateInfo.ts +32 -0
  110. package/examples/dashboard/src/SystemTypeMarty/RICSystemUtils.ts +371 -0
  111. package/examples/dashboard/src/SystemTypeMarty/RICTypes.ts +20 -0
  112. package/examples/dashboard/src/SystemTypeMarty/SystemTypeMarty.ts +113 -0
  113. package/examples/dashboard/src/index.html +15 -0
  114. package/examples/dashboard/src/index.tsx +15 -0
  115. package/examples/dashboard/src/styles.css +122 -0
  116. package/examples/dashboard/tsconfig.json +18 -0
  117. package/jest.config.js +11 -0
  118. package/package.json +50 -0
  119. package/src/RaftAttributeHandler.ts +289 -0
  120. package/src/RaftChannel.ts +30 -0
  121. package/src/RaftChannelWebBLE.ts +342 -0
  122. package/src/RaftChannelWebSerial.ts +408 -0
  123. package/src/RaftChannelWebSocket.ts +245 -0
  124. package/src/RaftCommsStats.ts +142 -0
  125. package/src/RaftConnEvents.ts +46 -0
  126. package/src/RaftConnector.ts +745 -0
  127. package/src/RaftCustomAttrHandler.ts +54 -0
  128. package/src/RaftDeviceInfo.ts +104 -0
  129. package/src/RaftDeviceManager.ts +542 -0
  130. package/src/RaftDeviceMsg.ts +20 -0
  131. package/src/RaftDeviceStates.ts +89 -0
  132. package/src/RaftFileHandler.ts +668 -0
  133. package/src/RaftLog.ts +70 -0
  134. package/src/RaftMiniHDLC.ts +396 -0
  135. package/src/RaftMsgHandler.ts +778 -0
  136. package/src/RaftMsgTrackInfo.ts +51 -0
  137. package/src/RaftProtocolDefs.ts +46 -0
  138. package/src/RaftStreamHandler.ts +328 -0
  139. package/src/RaftSystemType.ts +25 -0
  140. package/src/RaftSystemUtils.ts +487 -0
  141. package/src/RaftTypes.ts +250 -0
  142. package/src/RaftUpdateEvents.ts +48 -0
  143. package/src/RaftUpdateManager.ts +778 -0
  144. package/src/RaftUtils.ts +484 -0
  145. package/src/RaftWifiTypes.ts +36 -0
  146. package/src/TestDataGen.ts +157 -0
  147. package/src/main.ts +28 -0
  148. package/testdata/TestDeviceTypeRecs.json +492 -0
  149. package/tsconfig.json +27 -0
@@ -0,0 +1,51 @@
1
+ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // RaftMsgTrackInfo
4
+ // Part of RaftJS
5
+ //
6
+ // Rob Dobson & Chris Greening 2020-2024
7
+ // (C) 2020-2024 All rights reserved
8
+ //
9
+ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10
+
11
+ export class RaftMsgTrackInfo {
12
+ static readonly MAX_MSG_NUM = 255;
13
+ static readonly MSG_RESPONSE_TIMEOUT_MS = 2000;
14
+ static readonly MSG_RETRY_COUNT = 5;
15
+ msgOutstanding = false;
16
+ msgFrame: Uint8Array = new Uint8Array();
17
+ msgSentMs = 0;
18
+ retryCount = 0;
19
+ withResponse = false;
20
+ bridgeID: number | undefined = undefined;
21
+ msgHandle = 0;
22
+ msgTimeoutMs: number | undefined = undefined;
23
+ resolve: unknown;
24
+ reject: unknown;
25
+
26
+ constructor() {
27
+ this.msgOutstanding = false;
28
+ }
29
+
30
+ set(
31
+ msgOutstanding: boolean,
32
+ msgFrame: Uint8Array,
33
+ withResponse: boolean,
34
+ bridgeID: number | undefined = undefined,
35
+ msgHandle: number,
36
+ msgTimeoutMs: number | undefined,
37
+ resolve: unknown,
38
+ reject: unknown,
39
+ ) {
40
+ this.msgOutstanding = msgOutstanding;
41
+ this.msgFrame = msgFrame;
42
+ this.retryCount = 0;
43
+ this.msgSentMs = Date.now();
44
+ this.withResponse = withResponse;
45
+ this.bridgeID = bridgeID;
46
+ this.msgHandle = msgHandle;
47
+ this.msgTimeoutMs = msgTimeoutMs;
48
+ this.resolve = resolve;
49
+ this.reject = reject;
50
+ }
51
+ }
@@ -0,0 +1,46 @@
1
+ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // Raft Protocol Definitions
4
+ // Part of RaftJS
5
+ //
6
+ // Rob Dobson & Chris Greening 2020-2024
7
+ // (C) 2020-2024 All rights reserved
8
+ //
9
+ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10
+
11
+ // RIC Protocols
12
+ export const RICSERIAL_MSG_NUM_POS = 0;
13
+ export const RICSERIAL_PROTOCOL_POS = 1;
14
+ export const RICSERIAL_PAYLOAD_POS = 2;
15
+ export const RICREST_REST_ELEM_CODE_POS = 0;
16
+ export const RICREST_HEADER_PAYLOAD_POS = 1;
17
+ export const RICREST_FILEBLOCK_CHANNEL_POS = 0;
18
+ export const RICREST_FILEBLOCK_FILEPOS_POS = 0;
19
+ export const RICREST_FILEBLOCK_FILEPOS_POS_BYTES = 4;
20
+ export const RICREST_FILEBLOCK_PAYLOAD_POS = 4;
21
+ export const RICREST_BRIDGE_ID_POS = 2;
22
+ export const RICREST_BRIDGE_PAYLOAD_POS = 3;
23
+
24
+ // Protocol enums
25
+ export enum RICRESTElemCode {
26
+ RICREST_ELEM_CODE_URL,
27
+ RICREST_ELEM_CODE_CMDRESPJSON,
28
+ RICREST_ELEM_CODE_BODY,
29
+ RICREST_ELEM_CODE_COMMAND_FRAME,
30
+ RICREST_ELEM_CODE_FILEBLOCK,
31
+ }
32
+
33
+ export enum RaftCommsMsgTypeCode {
34
+ MSG_TYPE_COMMAND,
35
+ MSG_TYPE_RESPONSE,
36
+ MSG_TYPE_PUBLISH,
37
+ MSG_TYPE_REPORT,
38
+ }
39
+
40
+ export enum RaftCommsMsgProtocol {
41
+ MSG_PROTOCOL_ROSSERIAL = 0,
42
+ MSG_PROTOCOL_RESERVED_1 = 1,
43
+ MSG_PROTOCOL_RICREST = 2,
44
+ MSG_PROTOCOL_BRIDGE_RICREST = 3,
45
+ }
46
+
@@ -0,0 +1,328 @@
1
+ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // RafStreamHandler
4
+ // Part of RaftJS
5
+ //
6
+ // Rob Dobson & Chris Greening 2020-2024
7
+ // (C) 2020-2024 All rights reserved
8
+ //
9
+ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10
+
11
+ import RaftLog from './RaftLog'
12
+ import RaftMsgHandler from './RaftMsgHandler';
13
+ import RaftCommsStats from './RaftCommsStats';
14
+ import { RaftOKFail, RaftStreamStartResp, RaftStreamType } from './RaftTypes';
15
+ import RaftConnector from './RaftConnector';
16
+ import { RaftConnEvent } from './RaftConnEvents';
17
+ import { RICRESTElemCode } from './RaftProtocolDefs'
18
+
19
+ export default class RaftStreamHandler {
20
+
21
+ // Queue of audio stream requests
22
+ /*
23
+ private _streamAudioQueue: {
24
+ streamContents: Uint8Array;
25
+ audioDuration: number;
26
+ }[] = [];
27
+ */
28
+
29
+ // Stream state
30
+ private _streamID: number | null = null;
31
+ DEFAULT_MAX_BLOCK_SIZE = 475;
32
+ private _maxBlockSize: number = this.DEFAULT_MAX_BLOCK_SIZE;
33
+
34
+ // Handler of messages
35
+ private _msgHandler: RaftMsgHandler;
36
+
37
+ // RaftCommsStats
38
+ private _commsStats: RaftCommsStats;
39
+
40
+ // RaftConnector
41
+ private _raftConnector: RaftConnector;
42
+
43
+ // Flow control
44
+ private _soktoReceived = false;
45
+ private _soktoPos = 0;
46
+
47
+ private _streamIsStarting = false;
48
+ private _lastStreamStartTime = 0;
49
+
50
+ private _isStreaming = false;
51
+ private _isPaused = false;
52
+ private _streamBuffer = new Uint8Array();
53
+ private _audioDuration = 0;
54
+ private _audioByteRate = 0;
55
+ private _streamPos = 0;
56
+ private _numBlocksWithoutPause = 15;
57
+ private _legacySoktoMode = false;
58
+
59
+
60
+ // soundFinishPoint timer
61
+ private soundFinishPoint: NodeJS.Timeout | null = null;
62
+
63
+ constructor(msgHandler: RaftMsgHandler, commsStats: RaftCommsStats, raftConnector: RaftConnector) {
64
+ this._raftConnector = raftConnector;
65
+ this._msgHandler = msgHandler;
66
+ this._commsStats = commsStats;
67
+ this.onSoktoMsg = this.onSoktoMsg.bind(this);
68
+ }
69
+
70
+ setNumBlocksWithoutPause(numBlocks: number){
71
+ this._numBlocksWithoutPause = numBlocks;
72
+ }
73
+
74
+ setLegacySoktoMode(legacyMode: boolean){
75
+ RaftLog.debug(`Setting legacy sokto mode to ${legacyMode}`);
76
+ this._legacySoktoMode = legacyMode;
77
+ }
78
+
79
+ // Start streaming audio
80
+ streamAudio(streamContents: Uint8Array, clearExisting: boolean, audioDuration: number): void {
81
+ if (!clearExisting)
82
+ RaftLog.debug(`only clearExisting = true is supported right now.`);
83
+
84
+ // TODO - if clearExisting is not set, form a queue
85
+ if (this._streamIsStarting || this._lastStreamStartTime > (Date.now() - 500)) {
86
+ RaftLog.error(`Unable to start sound, too soon since last request`);
87
+ return;
88
+ }
89
+
90
+ this._isPaused = true;
91
+ this._streamIsStarting = true;
92
+ this._lastStreamStartTime = Date.now();
93
+
94
+ this._soktoReceived = false;
95
+ this._soktoPos = 0;
96
+ this._streamPos = 0;
97
+ this._streamBuffer = streamContents;
98
+ this._audioDuration = audioDuration;
99
+ this._audioByteRate = (streamContents.length / audioDuration)*1000;
100
+
101
+ this.clearFinishPointTimeout();
102
+
103
+ this._sendStreamStartMsg("audio.mp3", "streamaudio", RaftStreamType.REAL_TIME_STREAM, streamContents).then(
104
+ (result: boolean) => {
105
+ this._isPaused = false;
106
+ this._streamIsStarting = false;
107
+ if (!result){
108
+ RaftLog.error(`Unable to start stream. ufStart message send failed`);
109
+ return;
110
+ }
111
+ //this.streamingPerformanceChecker();
112
+ if (!this._isStreaming){
113
+ this._isStreaming = true;
114
+ this._sendStreamBuffer();
115
+ }
116
+
117
+ }
118
+ );
119
+ }
120
+
121
+ async streamCancel(): Promise<void> {
122
+ this._streamBuffer = new Uint8Array();
123
+ this.clearFinishPointTimeout();
124
+ }
125
+
126
+ public isStreamStarting() {
127
+ return this._streamIsStarting;
128
+ }
129
+
130
+
131
+ clearFinishPointTimeout() {
132
+ if (this.soundFinishPoint) {
133
+ clearTimeout(this.soundFinishPoint);
134
+ this.soundFinishPoint = null;
135
+ }
136
+ }
137
+
138
+ streamingPerformanceChecker() {
139
+ if (this._audioDuration) {
140
+ this.clearFinishPointTimeout();
141
+ this.soundFinishPoint = setTimeout(() => {
142
+ // if the streaming hasn't finished before the end of the audio
143
+ // we can assume we are having streaming issues
144
+
145
+ // publish event in case we are having issues
146
+ this._raftConnector.onConnEvent(RaftConnEvent.CONN_STREAMING_ISSUE);
147
+
148
+ this.clearFinishPointTimeout();
149
+ }, this._audioDuration + 500);
150
+ }
151
+ }
152
+
153
+ // Send the start message
154
+ private async _sendStreamStartMsg(
155
+ streamName: string,
156
+ targetEndpoint: string,
157
+ streamTypeEnum: RaftStreamType,
158
+ streamContents: Uint8Array,
159
+ ): Promise<boolean> {
160
+ // Stream start command message
161
+ const streamType = 'rtstream';
162
+ const cmdMsg = `{"cmdName":"ufStart","reqStr":"ufStart","fileType":"${streamType}","fileName":"${streamName}","endpoint":"${targetEndpoint}","fileLen":${streamContents.length}}`;
163
+
164
+ // Debug
165
+ RaftLog.debug(`sendStreamStartMsg ${cmdMsg}`);
166
+
167
+ // Send
168
+ let streamStartResp = null;
169
+ try {
170
+ streamStartResp = await this._msgHandler.sendRICREST<RaftStreamStartResp>(
171
+ cmdMsg,
172
+ RICRESTElemCode.RICREST_ELEM_CODE_COMMAND_FRAME,
173
+ );
174
+ } catch (err) {
175
+ RaftLog.error(`sendStreamStartMsg error ${err}`);
176
+ return false;
177
+ }
178
+
179
+ // Extract params
180
+ if (streamStartResp && (streamStartResp.rslt === 'ok')) {
181
+ this._streamID = streamStartResp.streamID;
182
+ this._maxBlockSize = streamStartResp.maxBlockSize || this.DEFAULT_MAX_BLOCK_SIZE;
183
+ this.streamingPerformanceChecker();
184
+ RaftLog.verbose(
185
+ `sendStreamStartMsg streamID ${this._streamID} maxBlockSize ${this._maxBlockSize} streamType ${streamTypeEnum}`,
186
+ );
187
+ } else {
188
+ RaftLog.warn(`sendStreamStartMsg failed ${streamStartResp ? streamStartResp.rslt : 'no response'}`);
189
+ return false;
190
+ }
191
+ return true;
192
+ }
193
+
194
+ get maxBlockSize () {
195
+ return this._maxBlockSize;
196
+ }
197
+
198
+ set maxBlockSize (maxBlockSize: number) {
199
+ this._maxBlockSize = maxBlockSize;
200
+ this.DEFAULT_MAX_BLOCK_SIZE = maxBlockSize;
201
+ }
202
+
203
+ private async _sendStreamEndMsg(
204
+ streamID: number | null,
205
+ ): Promise<boolean> {
206
+ if (streamID === null) {
207
+ return false;
208
+ }
209
+ // Stram end command message
210
+ const cmdMsg = `{"cmdName":"ufEnd","reqStr":"ufEnd","streamID":${streamID}}`;
211
+
212
+ // Debug
213
+ RaftLog.debug(`sendStreamEndMsg ${cmdMsg}`);
214
+
215
+ // Send
216
+ let streamEndResp = null;
217
+ try {
218
+ streamEndResp = await this._msgHandler.sendRICREST<RaftOKFail>(
219
+ cmdMsg,
220
+ RICRESTElemCode.RICREST_ELEM_CODE_COMMAND_FRAME,
221
+ );
222
+ } catch (err) {
223
+ RaftLog.error(`sendStreamEndMsg error ${err}`);
224
+ return false;
225
+ }
226
+ return streamEndResp.rslt === 'ok';
227
+ }
228
+
229
+ /*
230
+ private async _sendAudioStopMsg(): Promise<RaftOKFail> {
231
+ const cmdMsg = `{"cmdName":"audio/stop"}`;
232
+
233
+ // Debug
234
+ RaftLog.debug(`sendAudioStopMsg ${cmdMsg}`);
235
+
236
+ // Send
237
+ return this._msgHandler.sendRICREST<RaftOKFail>(
238
+ cmdMsg,
239
+ RICRESTElemCode.RICREST_ELEM_CODE_COMMAND_FRAME,
240
+ );
241
+ }
242
+
243
+
244
+ private async _sendStreamCancelMsg(): Promise<RaftOKFail> {
245
+ // File cancel command message
246
+ const cmdMsg = `{"cmdName":"ufCancel","reqStr":"ufCancel","streamID":${this._streamID}}`;
247
+
248
+ // Debug
249
+ RaftLog.debug(`sendStreamCancelMsg ${cmdMsg}`);
250
+
251
+ // Send
252
+ return this._msgHandler.sendRICREST<RaftOKFail>(
253
+ cmdMsg,
254
+ RICRESTElemCode.RICREST_ELEM_CODE_COMMAND_FRAME,
255
+ );
256
+ }
257
+ */
258
+
259
+ private async _sendStreamBuffer(): Promise<boolean> {
260
+ const streamStartTime = Date.now();
261
+
262
+ // Check streamID is valid
263
+ if (this._streamID === null) {
264
+ return false;
265
+ }
266
+
267
+ let blockNum = 0;
268
+ // Send stream blocks
269
+ while (this._soktoPos < this._streamBuffer.length || this._isPaused) {
270
+ if (this._isPaused){
271
+ await new Promise((resolve) => setTimeout(resolve, 5));
272
+ continue;
273
+ }
274
+
275
+ // Check for new sokto
276
+ if (this._soktoReceived) {
277
+ if (this._legacySoktoMode)
278
+ this._streamPos = this._soktoPos;
279
+ // apart from when in legacy mode, the sokto message is now informational only,
280
+ // to allow the central to slow down sending of data if it is swamping the peripheral
281
+ RaftLog.verbose(`sendStreamContents ${Date.now() - streamStartTime}ms soktoReceived for ${this._streamPos}`);
282
+ this._soktoReceived = false;
283
+
284
+ // receiving an sokto message before the completion of the stream means that the streaming is not keeping up
285
+ this._raftConnector.onConnEvent(RaftConnEvent.CONN_STREAMING_ISSUE);
286
+ }
287
+
288
+ // Send stream block
289
+ const blockSize = Math.min(this._streamBuffer.length - this._streamPos, this._maxBlockSize);
290
+ const block = this._streamBuffer.slice(this._streamPos, this._streamPos + blockSize);
291
+ if (block.length > 0) {
292
+ const sentOk = await this._msgHandler.sendStreamBlock(block, this._streamPos, this._streamID);
293
+ this._commsStats.recordStreamBytes(block.length);
294
+
295
+ RaftLog.verbose(
296
+ `sendStreamContents ${sentOk ? "OK" : "FAILED"} ${Date.now() - streamStartTime}ms pos ${this._streamPos} ${blockSize} ${block.length} ${this._soktoPos}`,
297
+ );
298
+ if (!sentOk) {
299
+ return false;
300
+ }
301
+ this._streamPos += blockSize;
302
+ blockNum += 1;
303
+
304
+ if (this._audioByteRate && blockNum > this._numBlocksWithoutPause){
305
+ const pauseTime = ((blockSize / this._audioByteRate)*1000) - 10;
306
+ RaftLog.verbose(`Pausing for ${pauseTime} ms between audio packets. Bit rate ${this._audioByteRate * 8}`)
307
+ await new Promise((resolve) => setTimeout(resolve, pauseTime));
308
+ }
309
+ }
310
+
311
+ // Wait to ensure we don't hog the CPU
312
+ await new Promise((resolve) => setTimeout(resolve, 1));
313
+ }
314
+
315
+ this._isStreaming = false;
316
+ this.clearFinishPointTimeout();
317
+ await this._sendStreamEndMsg(this._streamID);
318
+
319
+ return true;
320
+ }
321
+
322
+ onSoktoMsg(soktoPos: number) {
323
+ // Get how far we've progressed in file
324
+ this._soktoPos = soktoPos;
325
+ this._soktoReceived = true;
326
+ RaftLog.debug(`onSoktoMsg received file up to ${this._soktoPos}`);
327
+ }
328
+ }
@@ -0,0 +1,25 @@
1
+ import RaftSystemUtils from "./RaftSystemUtils";
2
+ import { RaftEventFn } from "./RaftTypes";
3
+
4
+ export type RaftSubscribeForUpdatesCBType = (systemUtils: RaftSystemUtils, enable: boolean) => Promise<void>;
5
+ export type RaftStateIsInvalidCBType = () => void;
6
+ export type RaftRxOtherMsgType = (payload: Uint8Array, _frameTimeMs: number) => void;
7
+
8
+ export interface ConnectorOptions {
9
+ wsSuffix?: string;
10
+ connTimeoutMs?: number;
11
+ }
12
+
13
+ export interface RaftSystemType {
14
+ nameForDialogs: string;
15
+ defaultWiFiHostname: string;
16
+ firmwareDestName: string;
17
+ normalFileDestName: string;
18
+ connectorOptions: ConnectorOptions;
19
+ setup: (systemUtils: RaftSystemUtils, onEvent: RaftEventFn | null) => void;
20
+ subscribeForUpdates: RaftSubscribeForUpdatesCBType | null;
21
+ stateIsInvalid: RaftStateIsInvalidCBType | null;
22
+ rxOtherMsgType: RaftRxOtherMsgType | null;
23
+ }
24
+
25
+ export type RaftGetSystemTypeCBType = (systemUtils: RaftSystemUtils) => Promise<RaftSystemType | null>;