From 5a843fd6e503726c720d6743dbed1f5de906d67c Mon Sep 17 00:00:00 2001 From: niten Date: Sun, 8 Jun 2025 09:33:51 -0700 Subject: [PATCH] Changes per Aider --- .gitignore | 2 + deps.edn | 2 +- src/milquetoast/client.clj | 205 +----------------- src/milquetoast/core.clj | 17 +- src/milquetoast/utils.clj | 4 +- .../{client.clj => client_test.clj} | 2 +- test/milquetoast/core_test.clj | 3 + test/milquetoast/utils_test.clj | 4 + 8 files changed, 25 insertions(+), 214 deletions(-) rename test/milquetoast/{client.clj => client_test.clj} (98%) create mode 100644 test/milquetoast/core_test.clj create mode 100644 test/milquetoast/utils_test.clj diff --git a/.gitignore b/.gitignore index 88823c4..66f455f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ target/ result .lsp/ .clj-kondo/ +.aider* +.env diff --git a/deps.edn b/deps.edn index 03589c6..ae8e5c1 100644 --- a/deps.edn +++ b/deps.edn @@ -5,7 +5,7 @@ org.clojure/core.async { :mvn/version "1.8.741" } org.clojure/data.json { :mvn/version "2.4.0" } org.eclipse.paho/org.eclipse.paho.client.mqttv3 { :mvn/version "1.2.5" } - org.clojure/tools.logging { :mvn/version "1.1.0" } + org.clojure/tools.logging { :mvn/version "1.3.0" } } :aliases { :test { diff --git a/src/milquetoast/client.clj b/src/milquetoast/client.clj index 01a7f27..3afb1a2 100644 --- a/src/milquetoast/client.clj +++ b/src/milquetoast/client.clj @@ -1,204 +1 @@ -(ns milquetoast.client - (:require [clojure.core.async :as async :refer [go go-loop ! alts!! ! chan (assoc (parse-message mqtt-message) - :topic topic)))))) - #(.reconnect client)) - chan)) - (get-topic! [_ topic opts] - (let [{:keys [qos timeout] :or {qos 0 timeout 5}} opts - result-chan (async/chan)] - (retry-attempt verbose - #(.subscribe client topic qos - (proxy [IMqttMessageListener] [] - (messageArrived [topic mqtt-message] - (go (>! result-chan (assoc (parse-message mqtt-message) - :topic topic)) - (async/close! result-chan)) - (.unsubscribe client topic)))) - #(.reconnect client)) - (first (alts!! [result-chan - (async/timeout (* timeout 1000))])))) - (get-topic-raw! [c topic opts] (get-topic! c topic opts))) - -(defn parallelism [] - (-> (Runtime/getRuntime) - (.availableProcessors) - (+ 1))) - -(defn pipe [in xf] - (let [out (async/chan)] - (async/pipeline (parallelism) out xf in) - out)) - -(defn json-parse-message [msg] - (-> msg - (update :payload (fn [payload] - (json/read-str payload :key-fn keyword))) - (assoc :timestamp (Instant/now)))) - -(defrecord MilquetoastJsonClient - [client] - IMilquetoastClient - (send-message! [_ topic msg opts] - (send-message! client topic (json/write-str msg) opts)) - (stop! [_] (stop! client)) - (add-channel! [_ chan] (add-channel! client chan)) - (subscribe-topic! [_ topic opts] - (pipe (subscribe-topic! client topic opts) - (map json-parse-message))) - (get-topic! [_ topic opts] - (if-let [msg (get-topic! client topic opts)] - (json-parse-message msg) - nil)) - (get-topic-raw! [_ topic opts] - (if-let [msg (get-topic! client topic opts)] - msg - nil))) - -(defn send! - "Sends a message to a topic on the provided client with the specified QoS and retain options." - [client topic msg & {:keys [qos retain] - :or {qos 1 retain false}}] - (send-message! client topic msg {:qos qos :retain retain})) - -(defn get! - "Gets a message from a topic on the provided client with the specified options." - [client topic & options] - (get-topic! client topic options)) - -(defn get-raw! - "Gets a raw message from a topic on the provided client with the specified options." - [client topic & options] - (get-topic-raw! client topic options)) - -(defn open-channel! - "Opens a channel for sending messages to a topic on the provided client with the specified buffer size, QoS, and retain options." - [client topic & {:keys [buffer-size qos retain] - :or {buffer-size 1 - qos 1 - retain false}}] - (let [chan (async/chan buffer-size)] - (add-channel! client chan) - (go-loop [msg (