If we get an exception, just quit.

Let systemd handle restarting.
This commit is contained in:
niten 2023-01-11 11:00:52 -08:00
parent 90ba657a24
commit 6421ba08ae
1 changed files with 13 additions and 7 deletions

View File

@ -14,14 +14,21 @@
org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)
(:gen-class)) (:gen-class))
(defn- exit! [status msg]
(println msg)
(System/exit status))
(defn- create-mqtt-client [broker-uri client-id mqtt-username passwd] (defn- create-mqtt-client [broker-uri client-id mqtt-username passwd]
(let [opts (doto (MqttConnectOptions.) (let [opts (doto (MqttConnectOptions.)
(.setCleanSession true) (.setCleanSession true)
(.setAutomaticReconnect true) (.setAutomaticReconnect true)
(.setPassword (char-array passwd)) (.setPassword (char-array passwd))
(.setUserName mqtt-username))] (.setUserName mqtt-username))]
(doto (MqttClient. broker-uri client-id (MemoryPersistence.)) (try
(.connect opts)))) (doto (MqttClient. broker-uri client-id (MemoryPersistence.))
(.connect opts))
(catch Exception e
(exit! 1 (.getMessage e))))))
(defn- shell-exec [& args] (defn- shell-exec [& args]
(let [{:keys [exit out err]} (apply shell/sh args)] (let [{:keys [exit out err]} (apply shell/sh args)]
@ -45,7 +52,10 @@
(.setRetained retained))) (.setRetained retained)))
(defn- send-message [client topic msg & {:keys [retained] :or {retained false}}] (defn- send-message [client topic msg & {:keys [retained] :or {retained false}}]
(.publish client topic (create-message msg retained))) (try
(.publish client topic (create-message msg retained))
(catch Exception e
(exit! 1 (.getMessage e)))))
(defn- create-reporter [client time-to-idle location user host host-device] (defn- create-reporter [client time-to-idle location user host host-device]
(let [base-topic (format "homeassistant/binary_sensor/wallfly_%s_%s" (let [base-topic (format "homeassistant/binary_sensor/wallfly_%s_%s"
@ -93,10 +103,6 @@
["-t" "--time-to-idle SECONDS" "Number of seconds before considering this host idle."] ["-t" "--time-to-idle SECONDS" "Number of seconds before considering this host idle."]
["-d" "--delay-time SECONDS" "Time to wait before polling for idle time."]]) ["-d" "--delay-time SECONDS" "Time to wait before polling for idle time."]])
(defn- exit! [status msg]
(println msg)
(System/exit status))
(defn- get-key [opts k] (defn- get-key [opts k]
(if-let [opt (get opts k)] (if-let [opt (get opts k)]
[k opt] [k opt]