@sedge-hq/mqtt 0.2.1 → 0.3.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 +4 -0
  2. package/package.json +5 -5
  3. package/src/mqtt.ts +47 -0
package/README.md CHANGED
@@ -14,4 +14,8 @@ a field. In `sedge`, say "tell me when the basil's soil sensor on home/basil/soi
14
14
  once an hour": that's `when: payload.moisture < 30` and `throttle: 1h`, so a chatty sensor doesn't
15
15
  wake it for every reading.
16
16
 
17
+ It can publish too: the agent's `act` tool has a **`publish`** action on the same broker, with
18
+ the same login ("water the basil" can switch the pump), behind your permissions: it asks first
19
+ unless you allow it.
20
+
17
21
  [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.2.1",
3
+ "version": "0.3.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,15 +31,15 @@
31
31
  "test": "bun --bun vitest run --passWithNoTests"
32
32
  },
33
33
  "dependencies": {
34
- "@sedge-hq/kit": "0.3.3",
35
- "mqtt": "5.16.1"
34
+ "@sedge-hq/kit": "0.5.0",
35
+ "mqtt": "5.17.0"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@sedge-hq/config": "0.0.0",
39
- "@types/bun": "1.4.4",
39
+ "@types/bun": "1.4.5",
40
40
  "aedes": "1.2.0",
41
41
  "typescript": "7.0.2",
42
- "vitest": "5.1.1"
42
+ "vitest": "5.1.2"
43
43
  },
44
44
  "sedge": {
45
45
  "plugins": [
package/src/mqtt.ts CHANGED
@@ -33,6 +33,39 @@ export const happening = (topic: string, payload: Uint8Array): Happening => ({
33
33
  details: { topic, payload: decode(payload) },
34
34
  });
35
35
 
36
+ /** Publishes one message on its own short connection, and closes it. */
37
+ export const publish = (
38
+ {
39
+ url,
40
+ username,
41
+ password,
42
+ }: { url: string; username?: string | undefined; password?: string | undefined },
43
+ message: { topic: string; payload: string; retain?: boolean | undefined },
44
+ ) =>
45
+ new Promise<string>((resolve, reject) => {
46
+ const client = mqtt.connect(url, {
47
+ clientId: `sedge-act-${randomBytes(6).toString('hex')}`,
48
+ reconnectPeriod: 0,
49
+ connectTimeout: 30_000,
50
+ ...(username ? { username } : {}),
51
+ ...(password ? { password } : {}),
52
+ });
53
+ const done = (error?: Error) => {
54
+ client.end(true);
55
+ if (error) reject(error);
56
+ else resolve(`Published to ${message.topic}.`);
57
+ };
58
+ client.on('error', done);
59
+ client.on('connect', () =>
60
+ client.publish(
61
+ message.topic,
62
+ message.payload,
63
+ { qos: 1, retain: message.retain ?? false },
64
+ (error) => done(error ?? undefined),
65
+ ),
66
+ );
67
+ });
68
+
36
69
  /** A sensor, a home hub, a device: anything that publishes to an MQTT broker. */
37
70
  export default defineSignal({
38
71
  name: 'mqtt',
@@ -47,6 +80,20 @@ export default defineSignal({
47
80
  username: field.optional(field.text('The username, if the broker asks for one.')),
48
81
  password: field.optional(secret('The password, if the broker asks for one.')),
49
82
  },
83
+ actions: {
84
+ publish: {
85
+ description:
86
+ 'Publish a message to a topic on this broker: switch a device, set a value. The payload is sent as written (JSON is text too).',
87
+ input: {
88
+ topic: field.text('The topic, like home/garden/pump/set.'),
89
+ payload: field.text('What to send, like on, 21 or {"state": "on"}.'),
90
+ retain: field.optional(
91
+ field.flag("Keep it as the topic's last value for later subscribers."),
92
+ ),
93
+ },
94
+ run: (input, settings) => publish(settings, input),
95
+ },
96
+ },
50
97
  watch: ({ url, topics, username, password }, kit) => {
51
98
  const items: Array<Happening> = [];
52
99
  let wake: (() => void) | undefined;