-
Notifications
You must be signed in to change notification settings - Fork 0
/
hue-flash.applescript
219 lines (182 loc) · 9.41 KB
/
hue-flash.applescript
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
global BridgeAddress
global apiKey
set BridgeAddress to "YOUR BRIDGE IP"
set apiKey to "YOUR API KEY"
global turnOn
global turnOff
set turnOn to the quoted form of "{\"on\": true,\"hue\": 0, \"sat\": 0,\"bri\": 254,\"transitiontime\": 0}"
set turnOff to the quoted form of "{\"on\": false,\"hue\": 0, \"sat\": 0,\"transitiontime\": 0}"
run evaluateState
script flasher
set Lamp1State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/"
set Lamp2State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/"
set the_String to Lamp1State
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":", ","}
-- do script steps here
set these_items to the text items of the_String
set AppleScript's text item delimiters to oldDelims
on error
set AppleScript's text item delimiters to oldDelims
end try
set Lamp1power to (item 3 of these_items)
set Lamp1bri to (item 5 of these_items)
set Lamp1hue to (item 7 of these_items)
set Lamp1sat to (item 9 of these_items)
set Lamp1ct to (item 14 of these_items)
set Lamp1x to (item 11 of these_items)
set Lamp1y to (item 12 of these_items)
set the_String to Lamp2State
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":", ","}
set these_items to the text items of the_String
set AppleScript's text item delimiters to oldDelims
on error
set AppleScript's text item delimiters to oldDelims
end try
set Lamp2power to (item 3 of these_items)
set Lamp2bri to (item 5 of these_items)
set Lamp2hue to (item 7 of these_items)
set Lamp2sat to (item 9 of these_items)
set Lamp2ct to (item 14 of these_items)
set Lamp2x to (item 11 of these_items)
set Lamp2y to (item 12 of these_items)
set BLUE1 to "{\"hue\": " & 45000 & ", \"sat\": 254,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
set BLUE1 to the quoted form of BLUE1
set BLUE2 to "{\"hue\": " & 45000 & ", \"sat\": 254,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
set BLUE2 to the quoted form of BLUE2
set RED1 to "{\"hue\": " & 0 & ", \"sat\": 254,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
set RED1 to the quoted form of RED1
set RED2 to "{\"hue\": " & 0 & ", \"sat\": 254,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
set RED2 to the quoted form of RED2
set WHITE1 to "{\"hue\": " & 0 & ", \"sat\": 0,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
set WHITE1 to the quoted form of WHITE1
set WHITE2 to "{\"hue\": " & 0 & ", \"sat\": 0,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
set WHITE2 to the quoted form of WHITE2
delay 0.2
repeat 10 times
delay 0.1
do shell script "curl --request PUT --data " & RED1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/state/"
do shell script "curl --request PUT --data " & BLUE2 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
delay 0.1
do shell script "curl --request PUT --data " & WHITE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/state/"
do shell script "curl --request PUT --data " & WHITE2 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
delay 0.1
do shell script "curl --request PUT --data " & BLUE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/state/"
do shell script "curl --request PUT --data " & RED2 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
end repeat
set lamp1Restore to "{\"on\": true,\"bri\": " & Lamp1bri & ",\"hue\": " & Lamp1hue & ",\"xy\": " & Lamp1x & "," & Lamp1y & ",\"sat\": " & Lamp1sat & ",\"ct\": " & Lamp1ct & ",\"transitiontime\": 0}"
set lamp1Restore to the quoted form of lamp1Restore
do shell script "curl --request PUT --data " & lamp1Restore & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/state/"
set lamp2Restore to "{\"on\": true,\"bri\": " & Lamp2bri & ",\"hue\": " & Lamp2hue & ",\"xy\": " & Lamp2x & "," & Lamp2y & ",\"sat\": " & Lamp2sat & ",\"ct\": " & Lamp2ct & ",\"transitiontime\": 0}"
set lamp2Restore to the quoted form of lamp2Restore
do shell script "curl --request PUT --data " & lamp2Restore & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
end script
script evaluateState
set Lamp1State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/"
set Lamp2State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/"
set the_String to Lamp1State
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":", ","}
-- do script steps here
set these_items to the text items of the_String
set AppleScript's text item delimiters to oldDelims
on error
set AppleScript's text item delimiters to oldDelims
end try
set Lamp1power to (item 3 of these_items)
set Lamp1bri to (item 5 of these_items)
set Lamp1hue to (item 7 of these_items)
set Lamp1sat to (item 9 of these_items)
set Lamp1ct to (item 14 of these_items)
set Lamp1x to (item 11 of these_items)
set Lamp1y to (item 12 of these_items)
set the_String to Lamp2State
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":", ","}
set these_items to the text items of the_String
set AppleScript's text item delimiters to oldDelims
on error
set AppleScript's text item delimiters to oldDelims
end try
set Lamp2power to (item 3 of these_items)
set Lamp2bri to (item 5 of these_items)
set Lamp2hue to (item 7 of these_items)
set Lamp2sat to (item 9 of these_items)
set Lamp2ct to (item 14 of these_items)
set Lamp2x to (item 11 of these_items)
set Lamp2y to (item 12 of these_items)
if Lamp1power = "true" and Lamp2power = "true" then
run flasher
else
do shell script "curl --request PUT --data " & turnOn & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/state/"
do shell script "curl --request PUT --data " & turnOn & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
delay 1
set Lamp1State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/"
set Lamp2State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/"
set the_String to Lamp1State
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":", ","}
-- do script steps here
set these_items to the text items of the_String
set AppleScript's text item delimiters to oldDelims
on error
set AppleScript's text item delimiters to oldDelims
end try
set Lamp1power to (item 3 of these_items)
set Lamp1bri to (item 5 of these_items)
set Lamp1hue to (item 7 of these_items)
set Lamp1sat to (item 9 of these_items)
set Lamp1ct to (item 14 of these_items)
set Lamp1x to (item 11 of these_items)
set Lamp1y to (item 12 of these_items)
set the_String to Lamp2State
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":", ","}
set these_items to the text items of the_String
set AppleScript's text item delimiters to oldDelims
on error
set AppleScript's text item delimiters to oldDelims
end try
set Lamp2power to (item 3 of these_items)
set Lamp2bri to (item 5 of these_items)
set Lamp2hue to (item 7 of these_items)
set Lamp2sat to (item 9 of these_items)
set Lamp2ct to (item 14 of these_items)
set Lamp2x to (item 11 of these_items)
set Lamp2y to (item 12 of these_items)
set BLUE1 to "{\"hue\": " & 45000 & ", \"sat\": 254,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
set BLUE1 to the quoted form of BLUE1
set BLUE2 to "{\"hue\": " & 45000 & ", \"sat\": 254,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
set BLUE2 to the quoted form of BLUE2
set RED1 to "{\"hue\": " & 0 & ", \"sat\": 254,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
set RED1 to the quoted form of RED1
set RED2 to "{\"hue\": " & 0 & ", \"sat\": 254,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
set RED2 to the quoted form of RED2
set WHITE1 to "{\"hue\": " & 0 & ", \"sat\": 0,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
set WHITE1 to the quoted form of WHITE1
set WHITE2 to "{\"hue\": " & 0 & ", \"sat\": 0,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
set WHITE2 to the quoted form of WHITE2
delay 0.2
repeat 10 times
delay 0.1
do shell script "curl --request PUT --data " & RED1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/state/"
do shell script "curl --request PUT --data " & BLUE2 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
delay 0.1
do shell script "curl --request PUT --data " & WHITE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/state/"
do shell script "curl --request PUT --data " & WHITE2 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
delay 0.1
do shell script "curl --request PUT --data " & BLUE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/state/"
do shell script "curl --request PUT --data " & RED2 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
end repeat
do shell script "curl --request PUT --data " & turnOff & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/1/state/"
delay 0.5
do shell script "curl --request PUT --data " & turnOff & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
end if
end script