From cc05911a99702dd93d682fedbae05957dd450c62 Mon Sep 17 00:00:00 2001 From: niten Date: Sun, 23 Apr 2023 09:13:27 -0700 Subject: [PATCH] Initial checkin --- .gitignore | 9 ++++ deps.edn | 16 +++++++ src/snooper/cli.clj | 71 ++++++++++++++++++++++++++++++ src/snooper/core.clj | 100 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 196 insertions(+) create mode 100644 .gitignore create mode 100644 deps.edn create mode 100644 src/snooper/cli.clj create mode 100644 src/snooper/core.clj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b9f7c3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.DS_Store +.idea +*.log +tmp/ + +.cpcache/ +.nrepl-port +target/ +result diff --git a/deps.edn b/deps.edn new file mode 100644 index 0000000..1995228 --- /dev/null +++ b/deps.edn @@ -0,0 +1,16 @@ +{ + :paths ["src"] + :deps { + org.clojure/clojure { :mvn/version "1.11.1" } + org.clojure/tools.cli {:mvn/version "1.0.214"} + metosin/malli { :mvn/version "0.11.0" } + org.fudo/fudo-clojure { + :git/url "https://git.fudo.org/fudo-public/fudo-clojure.git" + :git/sha "2352892ad7d7cf7c6bd294005a28d55ef224862a" + } + org.fudo/milquetoast { + :git/url "https://git.fudo.org/fudo-public/milquetoast.git" + :git/sha "ae81b91f0c710632f55b43f0193e16ab0dd81dde" + } + } +} diff --git a/src/snooper/cli.clj b/src/snooper/cli.clj new file mode 100644 index 0000000..c5fb158 --- /dev/null +++ b/src/snooper/cli.clj @@ -0,0 +1,71 @@ +(ns snooper.cli + (:require [clojure.core.async :as async :refer [>!! > (concat errors + ["usage: snooper-client [opts]" + "" + "Options:" + summary]) + (str/join \newline)))) + +(defn- parse-opts [args required cli-opts] + (let [{:keys [options] :as result} (cli/parse-opts args cli-opts) + missing (set/difference required (-> options (keys) (set))) + missing-errors (map #(format "missing required parameter: %s" (name %)) + missing)] + (update result :errors concat missing-errors))) + +(defn -main [& args] + (let [required-args #{:mqtt-host :mqtt-port :mqtt-user :mqtt-password-file :event-topic :notification-topic} + {:keys [options _ errors summary]} (parse-opts args required-args cli-opts)] + (when (seq errors) (msg-quit 1 (usage summary errors))) + (let [{:keys [mqtt-host + mqtt-port + mqtt-user + mqtt-password-file + notification-topic + event-topic]} options + catch-shutdown (async/chan) + mqtt-client (mqtt/connect-json! :host mqtt-host + :port mqtt-port + :username mqtt-user + :password (-> mqtt-password-file + (slurp) + (str/trim))) + logger (log/print-logger)] + (snooper/listen! :mqtt-client mqtt-client + :notification-topic notification-topic + :event-topics event-topic + :logger logger) + (.addShutdownHook (Runtime/getRuntime) + (Thread. (fn [] (>!! catch-shutdown true)))) + (