extract key-light-air

fixes #15
main
Alpha Chen 2 years ago
parent 1c8c15372f
commit 2a6f7baf76
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -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

@ -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}
Loading…
Cancel
Save