From db1f07d9f08afac63a06161e35cecd9d66f680a7 Mon Sep 17 00:00:00 2001 From: niten Date: Thu, 18 May 2023 13:44:44 -0700 Subject: [PATCH] Switch to numeric urgency --- src/tattler/cli.clj | 18 +++++++++++------- src/tattler/core.clj | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/tattler/cli.clj b/src/tattler/cli.clj index 72f45de..df13e05 100644 --- a/src/tattler/cli.clj +++ b/src/tattler/cli.clj @@ -19,7 +19,10 @@ [nil "--mqtt-user USER" "User as which to connect to MQTT server."] [nil "--mqtt-password-file PASSWD_FILE" "File containing password for MQTT user."] - [nil "--notification-topic TOPIC" "MQTT topic to which events should be published."]]) + [nil "--notification-topic TOPIC" "MQTT topic to which events should be published."] + [nil "--urgency-threshold THRESHOLD" "Minimum urgency at which to send notification (0 is least and 10 is most)" + :parse-fn #(Integer/parseInt %) + :default 5]]) (defn- msg-quit [status msg] (println msg) @@ -42,10 +45,10 @@ (update result :errors concat missing-errors))) (defn -main [& args] - (let [required-args #{:mqtt-host :mqtt-port :app-name :notification-topic} + (let [required-args #{:mqtt-host :mqtt-port :app-name :notification-topic :urgency-threshold} {: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 app-name notification-topic]} options + (let [{:keys [mqtt-host mqtt-port mqtt-user mqtt-password-file app-name notification-topic urgency-threshold]} options catch-shutdown (async/chan) mqtt-client (if mqtt-user (mqtt/connect-json! :host mqtt-host @@ -56,10 +59,11 @@ (str/trim))) (mqtt/connect-json! :host mqtt-host :port mqtt-port)) logger (log/print-logger)] - (tattler/listen! :app app-name - :mqtt-client mqtt-client - :topic notification-topic - :logger logger) + (tattler/listen! :app app-name + :mqtt-client mqtt-client + :topic notification-topic + :urgency-threshold urgency-threshold + :logger logger) (.addShutdownHook (Runtime/getRuntime) (Thread. (fn [] (>!! catch-shutdown true)))) (= 0] [:<= 10]]]])) -(defn listen! [& {app :app mqtt-client :mqtt-client topic :topic logger :logger}] +(defn listen! [& {app :app mqtt-client + :mqtt-client topic + :topic logger + :logger urgency-threshold + :urgency-threshold}] (let [note-chan (mqtt/subscribe! mqtt-client topic)] (go-loop [note-msg ( note-msg :payload (update :urgency keyword))] + (let [note (:payload note-msg)] (if (t/validate Notification note) - (notify/send-notification! mqtt-client (assoc note :app app)) + (when (>= (:urgency note) urgency-threshold) + (notify/send-notification! mqtt-client (assoc note :app app)) + (log/info! logger (format "ignoring low-urgency message (%s): %s" + (:urgency note) + (:summary note)))) (let [err (humanize (t/explain Notification note))] (log/error! logger (format "rejecting invalid notification: %s (%s)\n%s" (:summary err) (:body err)