@robdobsn/raftjs 1.10.7 → 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 (75) hide show
  1. package/dist/react-native/RaftChannelSimulated.js +4 -3
  2. package/dist/react-native/RaftChannelSimulated.js.map +1 -1
  3. package/dist/react-native/RaftConnector.d.ts +10 -1
  4. package/dist/react-native/RaftConnector.js +23 -10
  5. package/dist/react-native/RaftConnector.js.map +1 -1
  6. package/dist/react-native/RaftDeviceManager.d.ts +13 -1
  7. package/dist/react-native/RaftDeviceManager.js +224 -77
  8. package/dist/react-native/RaftDeviceManager.js.map +1 -1
  9. package/dist/react-native/RaftDeviceMgrIF.d.ts +5 -1
  10. package/dist/react-native/RaftDeviceStates.d.ts +20 -2
  11. package/dist/react-native/RaftDeviceStates.js +25 -4
  12. package/dist/react-native/RaftDeviceStates.js.map +1 -1
  13. package/dist/react-native/RaftPublish.d.ts +2 -0
  14. package/dist/react-native/RaftPublish.js +81 -0
  15. package/dist/react-native/RaftPublish.js.map +1 -0
  16. package/dist/react-native/RaftStreamHandler.d.ts +11 -0
  17. package/dist/react-native/RaftStreamHandler.js +66 -0
  18. package/dist/react-native/RaftStreamHandler.js.map +1 -1
  19. package/dist/react-native/RaftSystemUtils.d.ts +17 -1
  20. package/dist/react-native/RaftSystemUtils.js +51 -0
  21. package/dist/react-native/RaftSystemUtils.js.map +1 -1
  22. package/dist/react-native/RaftTypes.d.ts +21 -0
  23. package/dist/react-native/RaftTypes.js.map +1 -1
  24. package/dist/react-native/main.d.ts +1 -0
  25. package/dist/react-native/main.js +1 -0
  26. package/dist/react-native/main.js.map +1 -1
  27. package/dist/web/RaftChannelSimulated.js +4 -3
  28. package/dist/web/RaftChannelSimulated.js.map +1 -1
  29. package/dist/web/RaftConnector.d.ts +10 -1
  30. package/dist/web/RaftConnector.js +23 -10
  31. package/dist/web/RaftConnector.js.map +1 -1
  32. package/dist/web/RaftDeviceManager.d.ts +13 -1
  33. package/dist/web/RaftDeviceManager.js +224 -77
  34. package/dist/web/RaftDeviceManager.js.map +1 -1
  35. package/dist/web/RaftDeviceMgrIF.d.ts +5 -1
  36. package/dist/web/RaftDeviceStates.d.ts +20 -2
  37. package/dist/web/RaftDeviceStates.js +25 -4
  38. package/dist/web/RaftDeviceStates.js.map +1 -1
  39. package/dist/web/RaftPublish.d.ts +2 -0
  40. package/dist/web/RaftPublish.js +81 -0
  41. package/dist/web/RaftPublish.js.map +1 -0
  42. package/dist/web/RaftStreamHandler.d.ts +11 -0
  43. package/dist/web/RaftStreamHandler.js +66 -0
  44. package/dist/web/RaftStreamHandler.js.map +1 -1
  45. package/dist/web/RaftSystemUtils.d.ts +17 -1
  46. package/dist/web/RaftSystemUtils.js +51 -0
  47. package/dist/web/RaftSystemUtils.js.map +1 -1
  48. package/dist/web/RaftTypes.d.ts +21 -0
  49. package/dist/web/RaftTypes.js.map +1 -1
  50. package/dist/web/main.d.ts +1 -0
  51. package/dist/web/main.js +1 -0
  52. package/dist/web/main.js.map +1 -1
  53. package/examples/dashboard/package.json +1 -1
  54. package/examples/dashboard/src/DeviceActionsForm.tsx +2 -2
  55. package/examples/dashboard/src/DevicePanel.tsx +79 -3
  56. package/examples/dashboard/src/DeviceStatsPanel.tsx +65 -0
  57. package/examples/dashboard/src/DevicesPanel.tsx +11 -0
  58. package/examples/dashboard/src/SettingsScreen.tsx +9 -4
  59. package/examples/dashboard/src/SystemTypeCog/CogStateInfo.ts +10 -2
  60. package/examples/dashboard/src/SystemTypeCog/SystemTypeCog.ts +37 -3
  61. package/examples/dashboard/src/SystemTypeGeneric/StateInfoGeneric.ts +10 -2
  62. package/examples/dashboard/src/SystemTypeGeneric/SystemTypeGeneric.ts +38 -4
  63. package/examples/dashboard/src/SystemTypeMarty/RICStateInfo.ts +10 -2
  64. package/examples/dashboard/src/styles.css +162 -0
  65. package/package.json +49 -49
  66. package/src/RaftChannelSimulated.ts +4 -3
  67. package/src/RaftConnector.ts +34 -13
  68. package/src/RaftDeviceManager.ts +251 -81
  69. package/src/RaftDeviceMgrIF.ts +5 -1
  70. package/src/RaftDeviceStates.ts +35 -5
  71. package/src/RaftPublish.ts +92 -0
  72. package/src/RaftStreamHandler.ts +84 -1
  73. package/src/RaftSystemUtils.ts +59 -0
  74. package/src/RaftTypes.ts +27 -0
  75. package/src/main.ts +1 -0
@@ -69,14 +69,29 @@ export interface DeviceTimeline {
69
69
  lastReportTimestampUs: number;
70
70
  reportTimestampOffsetUs: number;
71
71
  }
72
-
72
+
73
+ export interface DeviceStats {
74
+ totalSamples: number;
75
+ windowMs: number;
76
+ windowSamples: number;
77
+ sampleRateHz: number;
78
+ lastSampleTimeMs: number | null;
79
+ lastUpdateTimeMs: number | null;
80
+ }
81
+
82
+ export enum DeviceOnlineState {
83
+ Offline = 0,
84
+ Online = 1,
85
+ PendingDeletion = 2,
86
+ }
87
+
73
88
  export interface DeviceState {
74
89
  deviceTypeInfo: DeviceTypeInfo | undefined;
75
90
  deviceTimeline: DeviceTimeline;
76
91
  deviceAttributes: DeviceAttributesState;
77
92
  deviceIsNew: boolean;
78
93
  stateChanged: boolean;
79
- isOnline: boolean;
94
+ onlineState: DeviceOnlineState;
80
95
  deviceAddress: string;
81
96
  deviceType: string;
82
97
  busName: string;
@@ -86,7 +101,22 @@ export class DevicesState {
86
101
  [deviceKey: string]: DeviceState;
87
102
  }
88
103
 
89
- // Add the getDeviceKey method to generate a composite key
90
- export function getDeviceKey(busName: string, devAddr: string): string {
91
- return `${busName}_${devAddr}`;
104
+ // Format a numeric device address as canonical hex string:
105
+ // lowercase, no "0x" prefix, no leading zeros
106
+ export function formatDeviceAddrHex(addr: number): string {
107
+ return addr.toString(16);
108
+ }
109
+
110
+ // Generate a composite device key from bus number and hex address
111
+ export function getDeviceKey(busNumberAsString: string, devAddrHexStr: string): string {
112
+ return `${busNumberAsString}_${devAddrHexStr}`;
113
+ }
114
+
115
+ // Parse a device key into its bus and address components
116
+ export function parseDeviceKey(deviceKey: string): { bus: string; addr: string } {
117
+ const sep = deviceKey.indexOf('_');
118
+ if (sep < 0) {
119
+ return { bus: deviceKey, addr: '' };
120
+ }
121
+ return { bus: deviceKey.substring(0, sep), addr: deviceKey.substring(sep + 1) };
92
122
  }
@@ -0,0 +1,92 @@
1
+ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // RaftSystem
4
+ // Part of RaftJS
5
+ //
6
+ // Rob Dobson 2025
7
+ // (C) 2020-2025 All rights reserved
8
+ //
9
+ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10
+
11
+ import { RaftPublishFrameMeta } from "./RaftTypes";
12
+
13
+ const RAFT_PUBLISH_PREFIX_LEN = 2;
14
+ const DEVBIN_MAGIC_MIN = 0xDB;
15
+ const DEVBIN_MAGIC_MAX = 0xDF;
16
+ const DEVBIN_VERSION_BASE = 0xDA;
17
+
18
+ export function inspectPublishFrame(
19
+ payload: Uint8Array,
20
+ topicLookup?: (topicIndex: number) => string | undefined,
21
+ ): RaftPublishFrameMeta {
22
+
23
+ if (payload.length < RAFT_PUBLISH_PREFIX_LEN) {
24
+ return { frameType: "unknown" };
25
+ }
26
+
27
+ const payloadStartPos = RAFT_PUBLISH_PREFIX_LEN;
28
+ if (payload.length <= payloadStartPos) {
29
+ return { frameType: "unknown" };
30
+ }
31
+
32
+ // Try JSON first
33
+ if (payload[payloadStartPos] === 0x7B) {
34
+ try {
35
+ const jsonString = new TextDecoder("utf-8").decode(payload.slice(payloadStartPos));
36
+ const jsonObj = JSON.parse(jsonString) as Record<string, unknown>;
37
+
38
+ let topicIndex: number | undefined = undefined;
39
+ let topicName: string | undefined = undefined;
40
+ if (typeof jsonObj._t === "number") {
41
+ topicIndex = jsonObj._t;
42
+ topicName = topicLookup ? topicLookup(topicIndex) : undefined;
43
+ } else if (typeof jsonObj._t === "string") {
44
+ topicName = jsonObj._t;
45
+ }
46
+
47
+ const version = typeof jsonObj._v === "number" ? jsonObj._v : undefined;
48
+
49
+ return {
50
+ frameType: "json",
51
+ topicIndex,
52
+ topicName,
53
+ version,
54
+ jsonString,
55
+ };
56
+ } catch {
57
+ return { frameType: "unknown" };
58
+ }
59
+ }
60
+
61
+ // Binary (devbin legacy or enveloped)
62
+ const firstBinaryByte = payload[payloadStartPos];
63
+ if ((firstBinaryByte & 0xF0) === 0xD0) {
64
+ if (firstBinaryByte < DEVBIN_MAGIC_MIN || firstBinaryByte > DEVBIN_MAGIC_MAX) {
65
+ return {
66
+ frameType: "binary",
67
+ binaryHasEnvelope: true,
68
+ };
69
+ }
70
+
71
+ const topicIndex = payload.length > payloadStartPos + 1 ? payload[payloadStartPos + 1] : undefined;
72
+ const topicName = (topicIndex !== undefined && topicIndex !== 0xFF && topicLookup)
73
+ ? topicLookup(topicIndex)
74
+ : undefined;
75
+
76
+ return {
77
+ frameType: "binary",
78
+ topicIndex,
79
+ topicName,
80
+ version: firstBinaryByte - DEVBIN_VERSION_BASE,
81
+ binaryHasEnvelope: true,
82
+ binaryPayloadOffset: payloadStartPos + 2,
83
+ };
84
+ }
85
+
86
+ // Legacy binary format (no envelope)
87
+ return {
88
+ frameType: "binary",
89
+ binaryHasEnvelope: false,
90
+ binaryPayloadOffset: payloadStartPos,
91
+ };
92
+ }
@@ -11,7 +11,7 @@
11
11
  import RaftLog from './RaftLog'
12
12
  import RaftMsgHandler from './RaftMsgHandler';
13
13
  import RaftCommsStats from './RaftCommsStats';
14
- import { RaftOKFail, RaftStreamStartResp, RaftStreamType } from './RaftTypes';
14
+ import { RaftOKFail, RaftStreamStartResp, RaftStreamType, RaftStreamDataProgressCBType } from './RaftTypes';
15
15
  import RaftConnector from './RaftConnector';
16
16
  import { RaftConnEvent } from './RaftConnEvents';
17
17
  import { RICRESTElemCode } from './RaftProtocolDefs'
@@ -77,6 +77,89 @@ export default class RaftStreamHandler {
77
77
  this._legacySoktoMode = legacyMode;
78
78
  }
79
79
 
80
+ /**
81
+ * streamData - stream arbitrary data to a named firmware endpoint using the RT_STREAM protocol.
82
+ * Returns a promise that resolves when the stream is complete or rejects on failure.
83
+ * @param streamContents data to stream
84
+ * @param fileName logical filename sent in ufStart (used by firmware to detect format, e.g. "pattern.thr")
85
+ * @param targetEndpoint REST API endpoint name registered on the firmware (e.g. "streampattern")
86
+ * @param progressCallback optional callback reporting (bytesSent, totalBytes, progressFraction)
87
+ * @returns Promise<boolean> true if stream completed successfully
88
+ */
89
+ async streamData(
90
+ streamContents: Uint8Array,
91
+ fileName: string,
92
+ targetEndpoint: string,
93
+ progressCallback?: RaftStreamDataProgressCBType,
94
+ ): Promise<boolean> {
95
+ // Reject if another stream is starting
96
+ if (this._streamIsStarting || this._lastStreamStartTime > (Date.now() - 500)) {
97
+ RaftLog.warn(`streamData: unable to start, too soon since last request`);
98
+ return false;
99
+ }
100
+
101
+ this._streamIsStarting = true;
102
+ this._lastStreamStartTime = Date.now();
103
+ this._soktoReceived = false;
104
+ this._soktoPos = 0;
105
+ this._streamPos = 0;
106
+ this._streamBuffer = RaftUtils.toArrayBufferView(streamContents);
107
+
108
+ // Send ufStart
109
+ const startOk = await this._sendStreamStartMsg(
110
+ fileName, targetEndpoint, RaftStreamType.REAL_TIME_STREAM, streamContents,
111
+ );
112
+ this._streamIsStarting = false;
113
+
114
+ if (!startOk) {
115
+ RaftLog.warn(`streamData: ufStart failed`);
116
+ return false;
117
+ }
118
+
119
+ // Send blocks
120
+ if (this._streamID === null) {
121
+ return false;
122
+ }
123
+
124
+ let pos = 0;
125
+ while (pos < this._streamBuffer.length) {
126
+ // Respect SOKTO feedback — if firmware reported a position mismatch, rewind
127
+ if (this._soktoReceived) {
128
+ RaftLog.verbose(`streamData: sokto received, pos was ${pos}, soktoPos ${this._soktoPos}`);
129
+ this._soktoReceived = false;
130
+ // Slow down on backpressure
131
+ await new Promise(r => setTimeout(r, 50));
132
+ }
133
+
134
+ const blockSize = Math.min(this._streamBuffer.length - pos, this._maxBlockSize);
135
+ const block = this._streamBuffer.slice(pos, pos + blockSize);
136
+ if (block.length > 0) {
137
+ const sentOk = await this._msgHandler.sendStreamBlock(block, pos, this._streamID);
138
+ this._commsStats.recordStreamBytes(block.length);
139
+ if (!sentOk) {
140
+ RaftLog.warn(`streamData: sendStreamBlock failed at pos ${pos}`);
141
+ return false;
142
+ }
143
+ pos += blockSize;
144
+ this._streamPos = pos;
145
+
146
+ // Progress callback
147
+ if (progressCallback) {
148
+ const progress = this._streamBuffer.length > 0 ? pos / this._streamBuffer.length : 1;
149
+ progressCallback(pos, this._streamBuffer.length, progress);
150
+ }
151
+ }
152
+
153
+ // Yield to avoid hogging the event loop
154
+ await new Promise(r => setTimeout(r, 1));
155
+ }
156
+
157
+ // Send ufEnd
158
+ const endOk = await this._sendStreamEndMsg(this._streamID);
159
+ RaftLog.debug(`streamData: complete, endOk=${endOk}`);
160
+ return endOk;
161
+ }
162
+
80
163
  // Start streaming audio
81
164
  streamAudio(streamContents: Uint8Array, clearExisting: boolean, audioDuration: number): void {
82
165
  if (!clearExisting)
@@ -20,6 +20,9 @@ import {
20
20
  RaftFileList,
21
21
  RaftFriendlyName,
22
22
  RaftOKFail,
23
+ RaftPubTopicRec,
24
+ RaftPubTopicsResponse,
25
+ RaftSubscriptionUpdateResponse,
23
26
  RaftSysModInfoBLEMan,
24
27
  RaftSystemInfo,
25
28
  RaftWifiScanResults,
@@ -40,6 +43,10 @@ export default class RaftSystemUtils {
40
43
  private _defaultWiFiHostname = "Raft";
41
44
  private _maxSecsToWaitForWiFiConn = 20;
42
45
 
46
+ // Publish topic index/name lookup tables (session scoped)
47
+ private _pubTopicIdxToName: { [idx: number]: string } = {};
48
+ private _pubTopicNameToIdx: { [name: string]: number } = {};
49
+
43
50
  /**
44
51
  * constructor
45
52
  * @param raftMsgHandler
@@ -56,6 +63,58 @@ export default class RaftSystemUtils {
56
63
  return this._msgHandler;
57
64
  }
58
65
 
66
+ /**
67
+ * Update publish topic maps from topic records.
68
+ */
69
+ updatePublishTopicMap(topicRecs: Array<RaftPubTopicRec> | undefined): void {
70
+ if (!topicRecs || !Array.isArray(topicRecs)) {
71
+ return;
72
+ }
73
+ for (const topicRec of topicRecs) {
74
+ if (!topicRec || typeof topicRec.name !== "string" || typeof topicRec.idx !== "number") {
75
+ continue;
76
+ }
77
+ this._pubTopicIdxToName[topicRec.idx] = topicRec.name;
78
+ this._pubTopicNameToIdx[topicRec.name] = topicRec.idx;
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Update publish topic maps from subscription response.
84
+ */
85
+ updatePublishTopicMapFromSubscriptionResponse(resp: RaftSubscriptionUpdateResponse | RaftOKFail | null | undefined): void {
86
+ if (!resp || typeof resp !== "object") {
87
+ return;
88
+ }
89
+ if ("topics" in resp) {
90
+ this.updatePublishTopicMap((resp as RaftSubscriptionUpdateResponse).topics);
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Fetch publish topic map from firmware endpoint.
96
+ */
97
+ async refreshPublishTopicMap(): Promise<boolean> {
98
+ try {
99
+ const pubTopicsResp = await this._msgHandler.sendRICRESTURL<RaftPubTopicsResponse>("pubtopics");
100
+ if (pubTopicsResp && pubTopicsResp.rslt === "ok") {
101
+ this.updatePublishTopicMap(pubTopicsResp.topics);
102
+ return true;
103
+ }
104
+ } catch (error) {
105
+ RaftLog.debug(`refreshPublishTopicMap failed ${error}`);
106
+ }
107
+ return false;
108
+ }
109
+
110
+ getPublishTopicName(topicIndex: number): string | undefined {
111
+ return this._pubTopicIdxToName[topicIndex];
112
+ }
113
+
114
+ getPublishTopicIndex(topicName: string): number | undefined {
115
+ return this._pubTopicNameToIdx[topicName];
116
+ }
117
+
59
118
  /**
60
119
  * setDefaultWiFiHostname
61
120
  * @param defaultWiFiHostname
package/src/RaftTypes.ts CHANGED
@@ -72,6 +72,31 @@ export class RaftOKFail {
72
72
  rslt = 'failComms';
73
73
  }
74
74
 
75
+ export type RaftPubTopicRec = {
76
+ name: string;
77
+ idx: number;
78
+ };
79
+
80
+ export type RaftSubscriptionUpdateResponse = RaftOKFail & {
81
+ topics?: Array<RaftPubTopicRec>;
82
+ };
83
+
84
+ export type RaftPubTopicsResponse = RaftOKFail & {
85
+ topics?: Array<RaftPubTopicRec>;
86
+ };
87
+
88
+ export type RaftPublishFrameType = 'json' | 'binary' | 'unknown';
89
+
90
+ export type RaftPublishFrameMeta = {
91
+ frameType: RaftPublishFrameType;
92
+ topicIndex?: number;
93
+ topicName?: string;
94
+ version?: number;
95
+ binaryHasEnvelope?: boolean;
96
+ binaryPayloadOffset?: number;
97
+ jsonString?: string;
98
+ };
99
+
75
100
  export type RaftReportMsg = {
76
101
  msgType?: string;
77
102
  rslt?: string;
@@ -181,6 +206,8 @@ export class RaftSysModInfoBLEMan {
181
206
 
182
207
  export type RaftProgressCBType = (received: number, total: number) => void;
183
208
 
209
+ export type RaftStreamDataProgressCBType = (sent: number, total: number, progress: number) => void;
210
+
184
211
  export class RaftFileDownloadResult {
185
212
  fileData: Uint8Array | null = null;
186
213
  downloadedOk = false;
package/src/main.ts CHANGED
@@ -29,6 +29,7 @@ export { default as RaftDeviceMgrIF } from './RaftDeviceMgrIF';
29
29
  export { DeviceManager as RaftDeviceManager } from './RaftDeviceManager';
30
30
  export type { DeviceDecodedData } from './RaftDeviceManager';
31
31
 
32
+ export * from './RaftPublish';
32
33
  export * from './RaftTypes';
33
34
  export * from './RaftSystemType';
34
35
  export * from './RaftWifiTypes';