From 5287141cd9692fa3702b0221b9b814ddbdfe666d Mon Sep 17 00:00:00 2001 From: David Barksdale Date: Sun, 15 Jan 2017 21:13:18 -0600 Subject: Read temperature from Dallas DS18B20 sensors --- src/app/portmaster.clj | 18 +++++++++++------- src/app/temperature.clj | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 src/app/temperature.clj 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))) -- cgit v1.2.3-18-g5258