-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeylight.bb
55 lines (45 loc) · 1.52 KB
/
keylight.bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env bb
(require '[babashka.http-client :as http]
'[cheshire.core :as json])
(def usage
"Usage: bb keylight.bb <ip-address> [on|off]
Example: bb keylight.bb 192.168.1.123 on")
(defn get-light-state [ip]
(try
(-> (http/get (str "http://" ip ":9123/elgato/lights"))
:body
(json/parse-string true)
(get-in [:lights 0]))
(catch Exception e
(println "Error connecting to light:" (ex-message e))
(System/exit 1))))
(defn set-light-state [ip on?]
(try
(http/put (str "http://" ip ":9123/elgato/lights")
{:body (json/generate-string
{:numberOfLights 1
:lights [{:on (if on? 1 0)}]})})
(catch Exception e
(println "Error setting light state:" (ex-message e))
(System/exit 1))))
(defn -main [& args]
(when (or (empty? args) (> (count args) 2))
(println usage)
(System/exit 1))
(let [ip (first args)
command (second args)
current-state (get-light-state ip)]
(case command
"on" (set-light-state ip true)
"off" (set-light-state ip false)
nil (if (= 1 (:on current-state))
(set-light-state ip false)
(set-light-state ip true)))
(let [new-state (get-light-state ip)]
(println "Light is now" (if (= 1 (:on new-state)) "ON" "OFF")))))
(when (= *file* (System/getProperty "babashka.file"))
(apply -main *command-line-args*))
(comment
(-main "192.168.0.249")
(-main "192.168.0.249" "on")
(-main "192.168.0.249" "off"))