@sedge-hq/mqtt 0.1.0 → 0.2.0
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.
- package/package.json +1 -1
- package/src/mqtt.ts +13 -12
package/package.json
CHANGED
package/src/mqtt.ts
CHANGED
|
@@ -7,8 +7,11 @@ const KEEP = 20_000;
|
|
|
7
7
|
const clip = (text: string) =>
|
|
8
8
|
text.length > KEEP ? `${text.slice(0, KEEP)}\n… (${text.length - KEEP} more characters)` : text;
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
|
|
10
|
+
/**
|
|
11
|
+
* A payload as data: JSON as its value (so `payload.moisture < 30` and `payload > 30` work), text
|
|
12
|
+
* as it is, binary only by its size. Anything very long is cut, as text.
|
|
13
|
+
*/
|
|
14
|
+
export const decode = (payload: Uint8Array): unknown => {
|
|
12
15
|
let text: string;
|
|
13
16
|
try {
|
|
14
17
|
text = new TextDecoder('utf-8', { fatal: true }).decode(payload);
|
|
@@ -16,21 +19,19 @@ export const readable = (payload: Uint8Array): string => {
|
|
|
16
19
|
return `(${payload.length} bytes of binary)`;
|
|
17
20
|
}
|
|
18
21
|
if (text.includes('\u0000')) return `(${payload.length} bytes of binary)`;
|
|
22
|
+
if (text.length > KEEP) return clip(text);
|
|
19
23
|
try {
|
|
20
|
-
|
|
21
|
-
if (typeof value === 'object' && value !== null)
|
|
22
|
-
return clip(JSON.stringify(value, null, 2));
|
|
24
|
+
return JSON.parse(text) as unknown;
|
|
23
25
|
} catch {
|
|
24
|
-
|
|
26
|
+
return text;
|
|
25
27
|
}
|
|
26
|
-
return clip(text);
|
|
27
28
|
};
|
|
28
29
|
|
|
29
|
-
/** One event per message
|
|
30
|
-
export const happening = (topic: string, payload: Uint8Array): Happening => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
};
|
|
30
|
+
/** One event per message, with its topic and payload as details the agent and conditions read. */
|
|
31
|
+
export const happening = (topic: string, payload: Uint8Array): Happening => ({
|
|
32
|
+
text: `Message on ${topic}`,
|
|
33
|
+
details: { topic, payload: decode(payload) },
|
|
34
|
+
});
|
|
34
35
|
|
|
35
36
|
/** A sensor, a home hub, a device: anything that publishes to an MQTT broker. */
|
|
36
37
|
export default defineSignal({
|