aboutsummaryrefslogtreecommitdiff
path: root/src/lib/helpers/action.ts
diff options
context:
space:
mode:
authorAutumn <git@autumnfo.rest>2026-05-12 19:28:23 +0100
committerAutumn <git@autumnfo.rest>2026-05-12 19:28:23 +0100
commitcc092aa0f4f07b7bc19ba028f5976e52da9ae3f3 (patch)
treef34e6c373cfd62f431c0d009d0ffd39948d1dba1 /src/lib/helpers/action.ts
parent57bdd284666dd2139ffa00b28745be9a7520278d (diff)
[actions] added basic button-press action handling
Diffstat (limited to 'src/lib/helpers/action.ts')
-rw-r--r--src/lib/helpers/action.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/lib/helpers/action.ts b/src/lib/helpers/action.ts
new file mode 100644
index 0000000..59677ea
--- /dev/null
+++ b/src/lib/helpers/action.ts
@@ -0,0 +1,46 @@
+//
+// ~~~ action helper utilities
+//
+
+// imports
+import { sendMessage } from "@lib/mqtt.ts"
+import allActions from "@lib/config/actions.ts"
+import allDevices from "@lib/config/devices.ts"
+
+// run on button press
+export function onPress(action) {
+
+ console.debug(`-> running ${action.id}`)
+
+ const actionDo = action.do
+
+ actionDo.forEach((action) => {
+
+ const device = allDevices.find((device) => device.id === action.device)
+
+ // todo: error handling
+
+ const topic = `${device.mqtt}/set`
+ const message = JSON.stringify({ state: action.action.toUpperCase() })
+
+ console.debug(` -> send state ${action.action} to ${action.device}`)
+ sendMessage(topic, message)
+
+ })
+}
+
+// validate actions config
+export function validate() {
+
+ if (allActions.length < 1) return true
+
+ return allActions.every((action) => {
+
+ const onDevice = allDevices.find((device) => device.id === action.on.device)
+ if (!onDevice) return false
+
+ // todo: rest of the props
+
+ return true
+ })
+}