diff options
author | David Barksdale <amatus@amatus.name> | 2017-01-15 21:13:18 -0600 |
---|---|---|
committer | David Barksdale <amatus@amatus.name> | 2017-01-15 21:13:18 -0600 |
commit | 5287141cd9692fa3702b0221b9b814ddbdfe666d (patch) | |
tree | ad21ea73ad34f4335b4506375806e535f377c3e3 | |
parent | 48f423ca49aa62a8aa9928d3085ce89032703ff2 (diff) |
Read temperature from Dallas DS18B20 sensors
-rw-r--r-- | src/app/portmaster.clj | 18 | ||||
-rw-r--r-- | src/app/temperature.clj | 28 |
2 files changed, 39 insertions, 7 deletions
diff --git a/src/app/portmaster.clj b/src/app/portmaster.clj index 1387750..3fb57e2 100644 --- a/src/app/portmaster.clj +++ b/src/app/portmaster.clj @@ -85,12 +85,12 @@ (defn init [port-name username password] - (let [socat (.. (new ProcessBuilder - (into-array ["socat" - "-v" - (str "OPEN:" port-name ",b9600,raw") - "-"])) - (redirectError (new File "/tmp/portmaster.log")) + (let [socat (.. (ProcessBuilder. + (into-array ["socat" + "-v" + (str "OPEN:" port-name ",b9600,raw") + "-"])) + (redirectError (File. "/tmp/portmaster.log")) (start)) expect (.. (new ExpectBuilder) (withInputs (into-array [(.getInputStream socat)])) @@ -100,4 +100,8 @@ (build)) executor (Executors/newSingleThreadScheduledExecutor)] (send pm assoc :socat socat :expect expect) - (.scheduleAtFixedRate executor #(send pm poll username password) 2 10 TimeUnit/SECONDS))) + (.scheduleAtFixedRate executor + #(send pm poll username password) + 2 + 10 + TimeUnit/SECONDS))) diff --git a/src/app/temperature.clj b/src/app/temperature.clj new file mode 100644 index 0000000..1bdeac8 --- /dev/null +++ b/src/app/temperature.clj @@ -0,0 +1,28 @@ +(ns app.temperature + (:import (java.nio.file Files Paths) + (java.util.concurrent Executors TimeUnit))) + +(def temp (agent nil)) + +(defn poll + [state] + (let [path (Paths/get "/sys/bus/w1/devices" (make-array String 0)) + devices (Files/newDirectoryStream path "28-*") + files (map #(.resolve % "w1_slave") devices) + temperatures (map (fn [file] + (let [id (.. file + (getName 4) + (toString) + (substring 3)) + temp (-> (Files/readAllLines + file StandardCharsets/UTF_8) + (second) + (.split "t=") + (second))] + [id temp])) files)] + (assoc state :state temperatures))) + +(defn init + [] + (let [executor (Executors/newSingleThreadScheduledExecutor)] + (.scheduleAtFixedRate executor #(send temp poll) 0 10 TimeUnit/SECONDS))) |