-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
executable file
·304 lines (276 loc) · 7.85 KB
/
functions.php
File metadata and controls
executable file
·304 lines (276 loc) · 7.85 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
require 'secrets.php'; // AIO credentials
$hostname = "huehub.kenkl.org";
$aiohostname = "io.adafruit.com";
$apikey = "RPVo8wEXziF6OeLtCaCUqMdqWm28DrKqVQL7ftgG";
$baseexe = '/usr/bin/env curl -s -k -X PUT -H "Content-Type: application/json"';
$aiobaseexe = '/usr/bin/env curl -s -H "Content-Type: application/json"';
$baseurl = "https://".$hostname."/api/".$apikey."/lights/";
$mybase = "http://max.kenkl.org/lights/";
$callcurl = '/usr/bin/env curl -k -s -X GET ';
# Let's codify Warm/Cool CT for use in all the calls to stay consistent
$ctWarm = 400;
$ctCool = 330;
function oneState(string $on,int $id, int $bri, int $hue, int $sat) {
# Let's shift to using the setHSState() form, to harmonize with setCTState somewhat.
# We do have callers in many scripts, so let's just thunk it over for now.
setHSState($on,$id,$bri,$hue,$sat);
}
function setHSState(string $on,int $id, int $bri, int $hue, int $sat) {
global $baseexe, $baseurl;
$dothis = $baseexe." -d '{\"on\": ".$on.",\"bri\": ".$bri.",\"hue\": ".$hue.",\"sat\": ".$sat."}' ".$baseurl.$id."/state";
$output = `$dothis`;
}
function setHS(int $id, int $hue, int $sat) { # Set HS only; existing brightness and on state left as-is
global $baseexe, $baseurl;
$dothis = $baseexe." -d '{\"hue\": ".$hue.",\"sat\": ".$sat."}' ".$baseurl.$id."/state";
$output = `$dothis`;
}
function setCTState(string $on,int $id, int $bri, int $ct) {
global $baseexe, $baseurl;
$dothis = $baseexe." -d '{\"on\": ".$on.",\"bri\": ".$bri.",\"ct\": ".$ct."}' ".$baseurl.$id."/state";
$output = `$dothis`;
}
function setCT(int $id, int $ct) {
global $baseexe, $baseurl;
$dothis = $baseexe." -d '{\"ct\": ".$ct."}' ".$baseurl.$id."/state";
$output = `$dothis`;
}
function stateFileName(int $id) {
$statefile = "/tmp/lights.".$id.".state";
return $statefile;
}
function saveHueState(int $id) {
# normalising function names without breaking existing calls...
saveHSState($id);
}
function saveHSState(int $id) {
global $hostname, $apikey;
$statefile = stateFileName($id);
$output = `/usr/bin/env curl -k -s -X GET https://$hostname/api/$apikey/lights/$id`;
$results = json_decode($output,true);
$state = $results["state"];
$on = $state["on"];
$bri = $state["bri"];
$hue = $state["hue"];
$sat = $state["sat"];
if ($on) {
$on = "true";
}
else {
$on = "false";
}
$writethis = "'{\"on\": ".$on.",\"bri\": ".$bri.",\"hue\": ".$hue.",\"sat\": ".$sat."}' \n";
$myfile = fopen($statefile, "w");
if($myfile != FALSE) {
fwrite($myfile, $writethis);
fclose($myfile);
}
else {
echo "FFS. That didn't work.";
}
}
function saveCTState(int $id) {
global $hostname, $apikey;
$statefile = stateFileName($id);
$output = `/usr/bin/env curl -k -s -X GET https://$hostname/api/$apikey/lights/$id`;
$results = json_decode($output,true);
$state = $results["state"];
$on = $state["on"];
$bri = $state["bri"];
$ct = $state["ct"];
if ($on) {
$on = "true";
}
else {
$on = "false";
}
$writethis = "'{\"on\": ".$on.",\"bri\": ".$bri.",\"ct\": ".$ct."}' \n";
$myfile = fopen($statefile, "w");
if($myfile != FALSE) {
fwrite($myfile, $writethis);
fclose($myfile);
}
else {
echo "FFS. That didn't work.";
}
}
function getCTState(int $id) {
global $hostname, $apikey;
$statefile = stateFileName($id);
$output = `/usr/bin/env curl -k -s -X GET https://$hostname/api/$apikey/lights/$id`;
$results = json_decode($output,true);
$state = $results["state"];
$ct = $state["ct"];
return $ct;
}
function saveBriState(int $id) {
global $hostname, $apikey;
$statefile = stateFileName($id);
$output = `/usr/bin/env curl -k -s -X GET https://$hostname/api/$apikey/lights/$id`;
$results = json_decode($output,true);
$state = $results["state"];
$on = $state["on"];
$bri = $state["bri"];
if ($on) {
$on = "true";
}
else {
$on = "false";
}
$writethis = "'{\"on\": ".$on.",\"bri\": ".$bri."}' \n";
$myfile = fopen($statefile, "w");
if($myfile != FALSE) {
fwrite($myfile, $writethis);
fclose($myfile);
}
else {
echo "FFS. That didn't work.";
}
}
function saveOnState(int $id) {
global $hostname, $apikey;
$statefile = stateFileName($id);
$output = `/usr/bin/env curl -k -s -X GET https://$hostname/api/$apikey/lights/$id`;
$results = json_decode($output,true);
$state = $results["state"];
$on = $state["on"];
if ($on) {
$on = "true";
}
else {
$on = "false";
}
$writethis = "'{\"on\": ".$on."}' \n";
$myfile = fopen($statefile, "w");
if($myfile != FALSE) {
fwrite($myfile, $writethis);
fclose($myfile);
}
else {
echo "FFS. That didn't work.";
}
}
function checkState(int $id) { # Is this light in-progress with conditional toggle or PIRThing?
$statefile = stateFileName($id);
if(is_readable($statefile)) {
return TRUE;
}
else {
return FALSE;
}
}
function restoreState(int $id) {
//global $hostname, $apikey;
global $baseexe, $baseurl;
$statefile = stateFileName($id);
if(is_readable($statefile)) {
$contents = file_get_contents($statefile);
$contents = rtrim($contents);
$dothis = $baseexe." -d ".$contents." ".$baseurl.$id."/state";
$output = `$dothis`;
unlink($statefile);
return TRUE;
}
else {
return FALSE;
}
}
function clearState(int $id) {
array_map('unlink', glob(stateFileName($id)));
}
function clearStates() {
array_map('unlink', glob("/tmp/lights.*.state"));
}
function oneOn(int $id) {
global $baseexe, $baseurl;
$dothis = $baseexe." -d '{\"on\": true}' ".$baseurl.$id."/state";
$output = `$dothis`;
}
function oneOff(int $id) {
global $baseexe, $baseurl;
$dothis = $baseexe." -d '{\"on\": false}' ".$baseurl.$id."/state";
$output = `$dothis`;
}
function oneOnBright(int $id) {
setHSState('true',$id,254,7676,143);
}
function oneOnRelax(int $id) {
setHSState('true',$id,144,7688,199);
}
function oneOnConcentrate(int $id) {
setHSState('true',$id,254,39391,14);
}
function oneOnSpotCool(int $id) {
global $ctCool;
setCTState('true',$id,254,$ctCool);
}
function oneOnSpotWarm(int $id) {
global $ctWarm;
setCTState('true',$id,254,$ctWarm);
}
function setLevel(int $id,int $bri) {
global $baseexe,$baseurl;
$dothis = $baseexe." -d '{\"on\": true,\"bri\": ".$bri."}' ".$baseurl.$id."/state";
$output = `$dothis`;
}
function isOn(int $id) {
global $hostname, $apikey;
$output = `/usr/bin/env curl -k -s -X GET https://$hostname/api/$apikey/lights/$id`;
$results = json_decode($output,true);
$state = $results["state"];
$ison = $state["on"];
if ($ison == True) {
return True;
}
else {
return False;
}
}
function sp2_on(int $id) {
setHSState('true',$id,76,3771,240);
}
function game_on(int $id) {
setHSState('true',$id,144,5262,201);
}
function onFullRed(int $id){
setHSState('true', $id, 254, 0, 254);
}
function onFullGreen(int $id){
setHSState('true', $id, 254, 25500, 254);
}
function onFullBlue(int $id){
setHSState('true', $id, 254, 46920, 254);
}
function toggle(int $id){
//global $hostname, $apikey;
if (isOn($id)){
oneOff($id);
}
else {
oneOn($id);
}
}
function doThing($script) {
global $mybase;
$do = $mybase.$script;
$output = `/usr/bin/env curl -s $do`;
#echo $output."\n";
}
function getBri(int $id) {
global $hostname, $apikey;
$statefile = stateFileName($id);
$output = `/usr/bin/env curl -k -s -X GET https://$hostname/api/$apikey/lights/$id`;
$results = json_decode($output,true);
$state = $results["state"];
$bri = $state["bri"];
return $bri;
}
# Let's make A Thing to send things to Adafruit.IO for some very good reasons.
function setToggle(string $feedkey, int $value) { # not really a toggle; we can send any int we want here. Originally concieved to track PIR sensor, 1 and 0 are typical.
global $aiobaseexe,$aiobaseurl,$io_key,$username,$aiohostname;
$aiobaseurl = "https://".$aiohostname."/api/v2/".$username."/feeds/".$feedkey."/data";
$dothis = $aiobaseexe." -d '{\"value\": ".$value."}' -H \"X-AIO-Key: ".$io_key."\" ".$aiobaseurl;
$output = `$dothis`;
}
?>