extract key-light-air
fixes #15
change
commit 2a6f7baf76907eb088008c03b80e27b560593b81
author Alpha Chen <alpha@kejadlen.dev>
date
parent 1c8c1537
diff --git a/.hammerspoon/init.fnl b/.hammerspoon/init.fnl
index 3b022211..53f7c47e 100644
--- a/.hammerspoon/init.fnl
+++ b/.hammerspoon/init.fnl
@@ -63,42 +63,9 @@
 
 ;;; Elgato Key Light Air
 
-;; finding the hostname for the key light air
-; (local browser (: (hs.bonjour.new) :findServices :_elg._tcp.
-;                   (fn [browser _ _ service _]
-;                     (service:resolve #(log :info (: ($1:hostname) :sub 1 -2)))
-;                     (browser:stop))))
-
-(fn update-key-light-air [on?]
-  (let [url "http://elgato-key-light-air-5c9e.local:9123/elgato/lights"
-        on (if on? 1 0)
-        data (hs.json.encode {:lights [{: on}]})]
-    (hs.http.doRequest url :PUT data)))
-
-(fn docked? []
-  (accumulate [docked? false _ v (pairs (hs.usb.attachedDevices)) &until docked?]
-    (or docked? (= v.productName "CalDigit Thunderbolt 3 Audio"))))
-
-(local key-light-air-watcher (let [{: watcher} hs.caffeinate
-                                   w (watcher.new #(when (docked?)
-                                                     (match $1
-                                                       watcher.screensDidLock (update-key-light-air false)
-                                                       watcher.screensDidSleep (update-key-light-air false)
-                                                       watcher.screensDidUnlock (update-key-light-air true)
-                                                       watcher.screensDidWake (update-key-light-air true))))]
-                               (w:start)))
-
-; (local usb-watcher (: (hs.usb.watcher.new #(let [{: eventType : productName} $1]
-;                                              (when (= productName
-;                                                       "CalDigit Thunderbolt 3 Audio")
-;                                                (match eventType
-;                                                  :added (do
-;                                                           (update-key-light-air true)
-;                                                           (key-light-air-watcher:start))
-;                                                  :removed (do
-;                                                             (update-key-light-air false)
-;                                                             (key-light-air-watcher:stop))))))
-;                       :start))
+; package.loaded["key-light-air"]["find-hostname"]()
+; package.loaded["key-light-air"]["update"](false)
+(require :key-light-air)
 
 ;;; Spoons
 
diff --git a/.hammerspoon/key-light-air.fnl b/.hammerspoon/key-light-air.fnl
new file mode 100644
index 00000000..785b468c
--- /dev/null
+++ b/.hammerspoon/key-light-air.fnl
@@ -0,0 +1,45 @@
+;;; Elgato Key Light Air
+
+(local {: bonjour : caffeinate : http : json : logger : usb} hs)
+(local log (logger.new :key-light-air :info))
+
+(fn find-hostname []
+  (let [browser (bonjour.new)
+        cb (fn [browser _ _ service _]
+             (service:resolve #(log.i (: ($1:hostname) :sub 1 -2)))
+             (browser:stop))]
+    (browser:findServices :_elg._tcp. cb)))
+
+(fn update [on?]
+  (let [url "http://elgato-key-light-air-5c9e.local:9123/elgato/lights"
+        on (if on? 1 0)
+        data (json.encode {:lights [{: on}]})]
+    (http.doRequest url :PUT data)))
+
+(fn docked? []
+  (accumulate [docked? false _ v (pairs (usb.attachedDevices)) &until docked?]
+    (or docked? (= v.productName "CalDigit Thunderbolt 3 Audio"))))
+
+(local watcher (let [{: watcher} caffeinate
+                     w (watcher.new #(when (docked?)
+                                       (match $1
+                                         watcher.screensDidLock (update false)
+                                         watcher.screensDidSleep (update false)
+                                         watcher.screensDidUnlock (update true)
+                                         watcher.screensDidWake (update true))))]
+                 (w:start)))
+
+; (local usb-watcher (: (hs.usb.watcher.new #(let [{: eventType : productName} $1]
+;                                              (when (= productName
+;                                                       "CalDigit Thunderbolt 3 Audio")
+;                                                (match eventType
+;                                                  :added (do
+;                                                           (update-key-light-air true)
+;                                                           (key-light-air-watcher:start))
+;                                                  :removed (do
+;                                                             (update-key-light-air false)
+;                                                             (key-light-air-watcher:stop))))))
+;                       :start))
+
+{: find-hostname : log : update : watcher}
+