@sedge-hq/mqtt 0.1.0 → 0.2.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 (3) hide show
  1. package/README.md +6 -4
  2. package/package.json +2 -2
  3. package/src/mqtt.ts +13 -12
package/README.md CHANGED
@@ -5,11 +5,13 @@ topics on your broker (Mosquitto, Home Assistant's, EMQX, HiveMQ), with `+` and
5
5
  each message wakes sedge with its topic and payload.
6
6
 
7
7
  - `mqtt://`, `mqtts://` (TLS) and `wss://` (WebSocket) brokers; a password goes to your keychain.
8
- - A session kept on the broker: messages sent while sedge was stopped arrive when it's back.
8
+ - A session kept on the broker: messages sent at QoS 1 or 2 while sedge was stopped arrive when
9
+ it's back (QoS 0 messages aren't kept by brokers).
9
10
  - Retained values, sent again on every subscribe, aren't reported.
10
11
 
11
- In `sedge`, say "tell me when the basil's soil sensor on home/basil/soil reads dry, at most every
12
- hour". sedge's `match`, `ignore` and `batch` settings keep a chatty sensor from waking it for every
13
- reading.
12
+ Each message's details are its `topic` and `payload`, with JSON as values, so a condition can read
13
+ a field. In `sedge`, say "tell me when the basil's soil sensor on home/basil/soil reads dry, at most
14
+ once an hour": that's `when: payload.moisture < 30` and `throttle: 1h`, so a chatty sensor doesn't
15
+ wake it for every reading.
14
16
 
15
17
  [Channels and signals](https://github.com/sedge-hq/sedge/blob/main/docs/channels-and-signals.md) · MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sedge-hq/mqtt",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "MQTT for sedge: wake it when a message arrives on a topic, from a sensor, a home hub or a device.",
5
5
  "keywords": [
6
6
  "iot",
@@ -31,7 +31,7 @@
31
31
  "test": "bun --bun vitest run --passWithNoTests"
32
32
  },
33
33
  "dependencies": {
34
- "@sedge-hq/kit": "0.3.2",
34
+ "@sedge-hq/kit": "0.3.3",
35
35
  "mqtt": "5.16.1"
36
36
  },
37
37
  "devDependencies": {
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
- /** A payload as the agent reads it: JSON laid out, text as it is, binary only by its size. */
11
- export const readable = (payload: Uint8Array): string => {
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
- const value: unknown = JSON.parse(text);
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
- // Not JSON: plain text.
26
+ return text;
25
27
  }
26
- return clip(text);
27
28
  };
28
29
 
29
- /** One event per message: the topic, then what was sent. */
30
- export const happening = (topic: string, payload: Uint8Array): Happening => {
31
- const body = readable(payload);
32
- return { text: body.includes('\n') ? `${topic}\n${body}` : `${topic}: ${body}` };
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({