forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelgato-key-light-decrease-brightness.template.rb
executable file
·77 lines (58 loc) · 1.41 KB
/
elgato-key-light-decrease-brightness.template.rb
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env ruby
# Parameters
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Decrease Brightness
# @raycast.mode silent
# Optional parameters:
# @raycast.icon images/elgato.png
# @raycast.packageName Elgato Key Light
# Documentation:
# @raycast.author Thomas Paul Mann
# @raycast.authorURL https://github.com/thomaspaulmann
# @raycast.description Decrease brightness of Elgato Key Light by 5%.
# Configuration
HOST=""
PORT=""
# Main program
require "json"
require "net/http"
require "uri"
uri = URI("http://#{HOST}:#{PORT}/elgato/lights")
req = Net::HTTP::Get.new(uri)
res = Net::HTTP.start(uri.hostname, uri.port) { |http|
http.request(req)
}
if res.code == "200"
result = JSON.parse(res.body)
first_light = result["lights"].first()
if first_light.nil?
puts "Failed parsing first light"
exit(1)
end
brightness = first_light["brightness"]
brightness -= 5
brightness = brightness.clamp(0, 100)
uri = URI("http://#{HOST}:#{PORT}/elgato/lights")
req = Net::HTTP::Put.new(uri)
req.body = {
"numberOfLights": 1,
"lights": [
{
"brightness": brightness
}
]
}.to_json
res = Net::HTTP.start(uri.hostname, uri.port) { |http|
http.request(req)
}
if res.code == "200"
puts "Decreased brightness to #{brightness}%"
else
puts "Failed decreasing brightness"
exit(1)
end
else
puts "Failed loading lights"
exit(1)
end