From 13b36a85020d6d61b454190fdb0490ceb078b462 Mon Sep 17 00:00:00 2001 From: niten Date: Sat, 23 Mar 2024 11:27:28 -0700 Subject: [PATCH] Updates --- launch-cmd.txt | 1 + src/suanni/watcher.clj | 33 +++++++++++++++++++++++++++++++++ suanni.org | 27 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 launch-cmd.txt create mode 100644 src/suanni/watcher.clj create mode 100644 suanni.org diff --git a/launch-cmd.txt b/launch-cmd.txt new file mode 100644 index 0000000..5ca1bbd --- /dev/null +++ b/launch-cmd.txt @@ -0,0 +1 @@ +suanni.server --hostname=0.0.0.0 --port=5354 --synology-host=cargo.sea.fudo.org --synology-port=5001 --synology-user=suanni --synology-password-file=/tmp/synology.passwd --mqtt-host=mqtt.sea.fudo.org --mqtt-port=1883 --mqtt-user=suanni --mqtt-password-file=/tmp/mqtt.passwd --mqtt-topic=suanni/events/motion --objectifier-host=objectifier.sea.fudo.org --objectifier-port=5121 --verbose diff --git a/src/suanni/watcher.clj b/src/suanni/watcher.clj new file mode 100644 index 0000000..68632c5 --- /dev/null +++ b/src/suanni/watcher.clj @@ -0,0 +1,33 @@ +(ns suanni.watcher + (:require [milquetoast.client :as mqtt] + [clojure.core.async :as async :refer [go-loop !]]) + (:import java.time.Instant)) + +(defn start-watching! [& {:keys [mqtt-client event-chan sensor-location-map verbose] + :or {verbose false}}] + (letfn [(exposes-occupancy? [d] + (some (fn [exp] (-> exp :property (= "occupancy"))) + (-> d :definition :exposes))) + (assoc-location [sensor] + (assoc sensor :location (get sensor-location-map + (:ieee_address sensor))))] + (let [motion-sensors (->> (mqtt/get! mqtt-client "zigbee2mqtt/bridge/devices") + :payload + (filter exposes-occupancy?) + (map assoc-location))] + (doseq [sensor motion-sensors] + (let [sensor-topic (format "zigbee2mqtt/%s" (:ieee_address sensor))] + (when verbose (println (format "listening to channel: %s" sensor-topic))) + (let [sensor-chan (mqtt/subscribe! mqtt-client sensor-topic)] + (go-loop [evt (! event-chan + { + :type :motion-detected-sensor + :sensor sensor + :location (:location sensor) + :sensor-id (:ieee_address sensor) + :time (Instant/now) + }) + (recur (