-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write sunrise-sunset script in Clojure
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env bb | ||
(require '[babashka.process :refer [shell process exec]] | ||
'[org.httpkit.client :as hk-client]) | ||
|
||
(def NTFY_ADDRESS "http://nuc:8080/home-thewagner-ec1") | ||
(def LAT "46.519833N") | ||
(def LON "6.6335E") | ||
|
||
(defn poll! [] | ||
(-> (shell {:continue true :out :string} "sunwait poll" LAT LON) | ||
:out | ||
str/split-lines | ||
first)) | ||
|
||
(defn twilights! [] | ||
(-> (shell {:out :string} "sunwait list" LAT LON) | ||
:out | ||
str/split-lines | ||
first)) | ||
|
||
(defn wait! [] | ||
(shell "sunwait wait" LAT LON)) | ||
|
||
(let [now (poll!) | ||
lst (twilights!) | ||
opts (merge | ||
{:next-twilights lst} | ||
(case now | ||
"NIGHT" {:title "Sunrise!" :message "Good morning! 🌅"} | ||
"DAY" {:title "Sunset!" :message "Good evening! 🌇"}))] | ||
(println opts) | ||
(try | ||
(wait!) | ||
@(hk-client/post NTFY_ADDRESS {:headers {"Title" (opts :title)} | ||
:body (opts :message)}) | ||
(catch Exception e | ||
(if (= 130 (:exit (ex-data e))) | ||
(println "Interrupted") | ||
(println "An Error occured" e))))) |