diff --git a/.hammerspoon/init.fnl b/.hammerspoon/init.fnl index 3b02221..53f7c47 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 0000000..785b468 --- /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} +