-
Notifications
You must be signed in to change notification settings - Fork 15
/
queryBuilder.php
328 lines (265 loc) · 18.2 KB
/
queryBuilder.php
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
include "include.php";
pageHeader("IFTTT Query Builder");
?>
<style>
p{margin: 10px 0; }
.response{ cursor: pointer; }
.response:hover{ color: #f00; }
</style>
<div class="container" style="padding: 20px; background-color: #fff; border: 1px solid #000;">
<p>Use this to assist with creation of the IFTTT web-hooks quickly</p>
<p>I suggest setting up a Google Assistant Hook which then calls a webhook with the API string. Follow the <a target="_blank" href="https://github.com/bren1818/TCPLightingWebInterface/wiki/IFTTT-Integration">excellent guide</a> written by <a target="_blank" href="https://github.com/sktaylortrash">@sktaylortrash</a> in the wiki and feel free to ask a question in the forum</p>
<p>It is recommended that use SSL if you're able. This will work on non-standard ports, (ports other than 80/443) but you will need to setup port-forwarding and setup your webserver to listen on those ports. <a target="_blank" href="https://github.com/sktaylortrash">@sktaylortrash</a> has <a target="_blank" href="https://github.com/bren1818/TCPLightingWebInterface/wiki/Installation">documented</a> how to set everything up on both a <a target="_blank" href="https://github.com/bren1818/TCPLightingWebInterface/wiki/Installation-for-Apache-on-Raspbian">Raspberry Pi / Linux Environment</a>, or using <a target="_blanks" href="https://github.com/bren1818/TCPLightingWebInterface/wiki/Installation-for-Apache-on-XAMPP-Windows">XAMPP on Windows</a>.</p>
<?php
if( ALLOW_EXTERNAL_API_ACCESS == 1 ){
?>
<script>
var allowExternalAPI = "<?php echo ALLOW_EXTERNAL_API_ACCESS; ?>";
var externalAddr = "<?php echo EXTERNAL_DDNS_URL; ?>";
var externalPort = "<?php echo EXTERNAL_PORT; ?>";
var externalPass = "<?php echo EXTERNAL_API_PASSWORD; ?>";
var requireExtPass = "<?php echo REQUIRE_EXTERNAL_API_PASSWORD; ?>";
$(function(){
$('#sceneLights').hide();
$('#roomLights').hide();
$('#builder').change(function(){
console.log( $(this).val() );
var id = $(this).find('option:selected').attr('data-device-id');
var type = $(this).find('option:selected').attr('data-device-type');
var roomName = "";
var preface = $('#preface').val();
if( preface.length > 0 ){ preface = ' ' + preface + ' '; }
$('#appletImage').html('');
if( type == "room" || type == "light"){
$('#sceneLights').hide();
$('#roomLights').show();
roomName = $(this).find('option:selected').attr('data-room-name');
console.log("Type: " + type + ", id: " + id + ", RN: " + roomName );
$('#appletImage').html('<img src="css/images/IFTTTAppletPhrase.png"/>');
if( type == "room" ){
$('#command_on_1').html(roomName + preface + " lights ON");
$('#command_on_2').html("Switch " + roomName + preface + " lights ON");
$('#command_on_3').html("Turn " + roomName + preface + " lights on");
$('#command_on_response').html("Turning " + roomName + preface + " lights ON");
$('#command_on_url').html( externalAddr + ( (externalPort != 80 || externalPort != 443) ? ':' + externalPort : '' ) + '/api.php?fx=toggle&type=room&uid=' + id + '&val=1' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
$('#command_off_1').html(roomName + preface + " lights off");
$('#command_off_2').html("Switch " + roomName + preface +" Lights off");
$('#command_off_3').html("Turn " + roomName + preface + " lights off");
$('#command_off_response').html("Turning off " + roomName + " lights");
$('#command_off_url').html( externalAddr + ( (externalPort != 80 || externalPort != 443) ? ':' + externalPort : '' ) + '/api.php?fx=toggle&type=room&uid=' + id + '&val=0' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
$('#command_dim_1').html(roomName + preface + " lights to # % brightness");
$('#command_dim_2').html("Dim " + roomName + preface + " lights to # %");
$('#command_dim_3').html("Set " + roomName + preface + " lights to # %");
$('#command_dim_response').html("Adjusting " + roomName + " lights");
$('#command_dim_url').html( externalAddr + ( (externalPort != 80 || externalPort != 443) ? ':' + externalPort : '' ) + '/api.php?fx=dim&type=room&uid=' + id + '&val={{NumberField}}' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
$('#command_brighten_1').html("Increase " + roomName + preface + " lights brightness by # %");
$('#command_brighten_2').html("Brighten " + roomName + preface + " lights by # %");
$('#command_brighten_3').html("Up " + roomName + preface + " lights brightness # %");
$('#command_brighten_response').html("Brightening " + roomName + " lights");
$('#command_brighten_url').html( externalAddr + ( (externalPort != 80 || externalPort != 443) ? ':' + externalPort : '' ) + '/api.php?fx=brightenby&type=room&uid=' + id + '&val={{NumberField}}' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
$('#command_dimby_1').html("Decrease " + roomName + preface + " lights brightness by # %");
$('#command_dimby_2').html("Darken " + roomName + preface +" lights by # %");
$('#command_dimby_3').html("Reduce " + roomName + preface + " lights brightness by # %");
$('#command_dimby_response').html("Dimming " + roomName + " lights");
$('#command_dimby_url').html( externalAddr + ( (externalPort != 80 || externalPort != 443) ? ':' + externalPort : '' ) + '/api.php?fx=dimby&type=room&uid=' + id + '&val={{NumberField}}' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
}
if( type == "light" ){
roomName = $(this).find('option:selected').attr('data-device-name');
$('#command_on_1').html( roomName + preface + " light ON" );
$('#command_on_2').html("Switch " + roomName + preface + " light ON");
$('#command_on_3').html("Turn " + roomName + preface + " light on");
$('#command_on_response').html("Turning " + roomName + " light ON");
$('#command_on_url').html( externalAddr + ( (externalPort != 80 || externalPort != 443) ? ':' + externalPort : '' ) + '/api.php?fx=toggle&type=device&uid=' + id + '&val=1' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
$('#command_off_1').html( roomName + preface + " light off" );
$('#command_off_2').html("Switch " + roomName + preface + " light off");
$('#command_off_3').html("Turn " + roomName + preface + " light off");
$('#command_off_response').html("Turning off " + roomName + " light");
$('#command_off_url').html( externalAddr + ( (externalPort != 80 || externalPort != 443) ? ':' + externalPort : '' ) + '/api.php?fx=toggle&type=device&uid=' + id + '&val=0' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
$('#command_dim_1').html( roomName + preface + " light to # % brightness" );
$('#command_dim_2').html("Dim " + roomName + preface + " light to # %");
$('#command_dim_3').html("Set " + roomName + preface + " light brightness to # %");
$('#command_dim_response').html("Adjusting " + roomName + " brightness");
$('#command_dim_url').html( externalAddr + ( (externalPort != 80 || externalPort != 443) ? ':' + externalPort : '' ) + '/api.php?fx=dim&type=device&uid=' + id + '&val={{NumberField}}' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
$('#command_brighten_1').html("Increase " + roomName + preface + " light brightness by # %");
$('#command_brighten_2').html("Brighten " + roomName + preface + " light by # %");
$('#command_brighten_3').html("Up " + roomName + preface + " light brightness to # %");
$('#command_brighten_response').html("Brightening " + roomName + " light");
$('#command_brighten_url').html( externalAddr + ( (externalPort != 80 || externalPort != 443) ? ':' + externalPort : '' ) + '/api.php?fx=brightenby&type=device&uid=' + id + '&val={{NumberField}}' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
$('#command_dimby_1').html("Decrease " + roomName + preface + " light brightness by # %");
$('#command_dimby_2').html("Darken " + roomName + preface +" light by # %");
$('#command_dimby_3').html("Reduce " + roomName + preface + " light brightness by # %");
$('#command_dimby_response').html("Dimming " + roomName + " light");
$('#command_dimby_url').html( externalAddr + ( (externalPort != 80 || externalPort != 443) ? ':' + externalPort : '' ) + '/api.php?fx=dimby&type=device&uid=' + id + '&val={{NumberField}}' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
}
}
if( type == "scene" ){
$('#sceneLights').show();
$('#roomLights').hide();
var sceneName = $(this).find('option:selected').attr('data-device-name');
//console.log("Type: " + type + ", id: " + id + ", scene: " + sceneName );
$('#appletImage').html('<img src="css/images/IFTTTAppletPhrase.png"/>');
$('#scene_command_on_1').html("Activate " + sceneName + preface +" scene" );
$('#scene_command_on_2').html("Turn " + sceneName + preface + " scene ON");
$('#scene_command_on_3').html(sceneName + preface + " scene on");
$('#scene_command_on_response').html("Turning " + sceneName + preface + " devices ON");
$('#scene_command_on_url').html( externalAddr + ( externalPort != 80 ? ':' + externalPort : '' ) + '/api.php?fx=scene&type=on&uid=' + id + '' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
$('#scene_command_off_1').html("Deactivate " + sceneName + preface + " scene");
$('#scene_command_off_2').html("Turn " + sceneName + preface + " scene off");
$('#scene_command_off_3').html(sceneName + preface + " scene off");
$('#scene_command_off_response').html("Turning " + sceneName + preface + " devices off");
$('#scene_command_off_url').html( externalAddr + ( externalPort != 80 ? ':' + externalPort : '' ) + '/api.php?fx=scene&type=off&uid=' + id + '' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
$('#command_run_1').html("Activate " + sceneName + preface +" scene");
$('#command_run_2').html("Turn " + sceneName + preface + " scene on");
$('#command_run_3').html(sceneName + " scene on");
$('#command_run_response').html("Running " + preface + sceneName + " scene");
$('#command_run_url').html( externalAddr + ( externalPort != 80 ? ':' + externalPort : '' ) + '/api.php?fx=scene&type=run&uid=' + id + '' + ( requireExtPass == 1 ? '&password=' + externalPass : '') );
}
});
$('.response').click(function(event){
event.preventDefault();
var $temp = $("<input>");
$("body").append($temp);
$temp.val( $(this).text() ).select();
document.execCommand("copy");
$temp.remove();
console.log("Copying " + $(this).html() + " to Clipboard");
});
});
</script>
<p>Click on the <b>bolded</b> suggested responses to copy them to your clipboard</p>
<p>Preface TCP Light commands with: <input type="text" id="preface" value="TCP" placeholder="TCP" /> (Can be helpful if you have multiple lighting systems with similar naming conventions.</p>
<p>
<select id="builder">
<option>Select a Device | Room | Scene</option>
<?php
//Get State of System Data
$CMD = "cmd=GWRBatch&data=<gwrcmds><gwrcmd><gcmd>RoomGetCarousel</gcmd><gdata><gip><version>1</version><token>".TOKEN."</token><fields>name,image,imageurl,control,power,product,class,realtype,status</fields></gip></gdata></gwrcmd></gwrcmds>&fmt=xml";
$result = getCurlReturn($CMD);
$array = xmlToArray($result);
if( isset( $array["gwrcmd"]["gdata"]["gip"]["room"] ) ){
$DATA = $array["gwrcmd"]["gdata"]["gip"]["room"];
}
if( sizeof($DATA) > 0 ){
if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }
echo '<optgroup label="Rooms">';
foreach($DATA as $room){
echo '<option data-device-type="room" data-device-id="'.$room["rid"].'" data-room-name="'.$room["name"].'">' . $room["name"] . '</option>';
if( ! is_array($room["device"]) ){
}else{
$device = (array)$room["device"];
if( isset($device["did"]) ){
$DEVICES[] = "<option data-device-name='".$room["device"]["name"]."' data-device-type='light' data-device-id='".$room["device"]["did"]."' data-room-name='".$room["name"]."'>". $room["device"]["name"] ."</option>";
}else{
for( $x = 0; $x < sizeof($device); $x++ ){
if( isset($device[$x]) && is_array($device[$x]) && ! empty($device[$x]) ){
$DEVICES[] = '<option data-device-name="'.$device[$x]["name"].'" data-device-type="light" data-device-id="'.$device[$x]["did"].'" data-room-name="'.$room["name"].'">'. $device[$x]["name"] ."</option>";
}
}
}
}
}
echo '</optgroup>';
}
echo '<optgroup label="Devices">';
foreach($DEVICES as $device){
echo $device;
}
echo '</optgroup>';
$CMD = "cmd=SceneGetListDetails&data=<gip><version>1</version><token>".TOKEN."</token><bigicon>1</bigicon></gip>";
$result = getCurlReturn($CMD);
$array = xmlToArray($result);
$scenes = $array["scene"];
if( is_array($scenes) ){
echo '<optgroup label="Scenes">';
for($x = 0; $x < sizeof($scenes); $x++){
echo '<option data-device-type="scene" data-device-id="'.$scenes[$x]["sid"] .'" data-device-name="'.$scenes[$x]["name"] .'">'.$scenes[$x]["name"] . " - " . $scenes[$x]["sid"] . "</option>";
}
echo '</optgroup>';
}
?>
</select>
</p>
<br />
<style>
#instructions p{ margin: 10px 0; }
#select{ margin-bottom: 10px; }
.response{ font-weight: bold; font-style: italic; font-size: 16px; }
</style>
<div id="instructions">
<div id="appletImage" style="float: left; margin-right: 20px; overflow: hidden;">
</div>
<div class="fields">
<div id="roomLights">
<h1>On Commands</h1>
<p>What do you want to say: <span class="response" id="command_on_1"></span></p>
<p>Whats another way to say it? (Optional): <span class="response" id="command_on_2"></span></p>
<p>And another way?(optional): <span class="response" id="command_on_3"></span></p>
<p>What do you want the Assistant to say in Response: <span class="response" id="command_on_response"></span></p>
<p>URL: <span class="response" id="command_on_url"></span></p>
<p>Method: Get</p>
<h1>Off Commands</h1>
<p>What do you want to say: <span class="response" id="command_off_1"></span></p>
<p>Whats another way to say it? (Optional): <span class="response" id="command_off_2"></span></p>
<p>And another way?(optional): <span class="response" id="command_off_3"></span></p>
<p>What do you want the Assistant to say in Response: <span class="response" id="command_off_response"></span></p>
<p>URL: <span class="response" id="command_off_url"></span></p>
<p>Method: Get</p>
<h1>Dim To Command (Say a phrase with a number)</h1>
<p>What do you want to say: <span class="response" id="command_dim_1"></span></p>
<p>Whats another way to say it? (Optional): <span class="response" id="command_dim_2"></span></p>
<p>And another way?(optional): <span class="response" id="command_dim_3"></span></p>
<p>What do you want the Assistant to say in Response: <span class="response" id="command_dim_response"></span></p>
<p>URL: <span class="response" id="command_dim_url"></span></p>
<p>Method: Get</p>
<h1>Brighten By (Say a phrase with a number)</h1>
<p>What do you want to say: <span class="response" id="command_brighten_1"></span></p>
<p>Whats another way to say it? (Optional): <span class="response" id="command_brighten_2"></span></p>
<p>And another way?(optional): <span class="response" id="command_brighten_3"></span></p>
<p>What do you want the Assistant to say in Response: <span class="response" id="command_brighten_response"></span></p>
<p>URL: <span class="response" id="command_brighten_url"></span></p>
<p>Method: Get</p>
<h1>Dim By (Darken By) (Say a phrase with a number)</h1>
<p>What do you want to say: <span class="response" id="command_dimby_1"></span></p>
<p>Whats another way to say it? (Optional): <span class="response" id="command_dimby_2"></span></p>
<p>And another way?(optional): <span class="response" id="command_dimby_3"></span></p>
<p>What do you want the Assistant to say in Response: <span class="response" id="command_dimby_response"></span></p>
<p>URL: <span class="response" id="command_dimby_url"></span></p>
<p>Method: Get</p>
<!-- Brighten By, Dim By -->
</div>
<div id="sceneLights">
<h1>Run Command</h1>
<p>What do you want to say: <span class="response" id="command_run_1"></span></p>
<p>Whats another way to say it? (Optional): <span class="response" id="command_run_2"></span></p>
<p>And another way?(optional): <span class="response" id="command_run_3"></span></p>
<p>What do you want the Assistant to say in Response: <span class="response" id="command_run_response"></span></p>
<p>URL: <span class="response" id="command_run_url"></span></p>
<p>Method: Get</p>
<h1>Off Command</h1>
<p>What do you want to say: <span class="response" id="scene_command_off_1"></span></p>
<p>Whats another way to say it? (Optional): <span class="response" id="scene_command_off_2"></span></p>
<p>And another way?(optional): <span class="response" id="scene_command_off_3"></span></p>
<p>What do you want the Assistant to say in Response: <span class="response" id="scene_command_off_response"></span></p>
<p>URL: <span class="response" id="scene_command_off_url"></span></p>
<p>Method: Get</p>
<h1>All On Command</h1>
<p>What do you want to say: <span class="response" id="scene_command_on_1"></span></p>
<p>Whats another way to say it? (Optional): <span class="response" id="scene_command_on_2"></span></p>
<p>And another way?(optional): <span class="response" id="scene_command_on_3"></span></p>
<p>What do you want the Assistant to say in Response: <span class="response" id="scene_command_on_response"></span></p>
<p>URL: <span class="response" id="scene_command_on_url"></span></p>
<p>Method: Get</p>
</div>
</div>
<div class="clear"></div>
</div>
<?php
}else{
echo '<p>To use IFTTT you must enable the external API access</p>';
}
?>
</div>
<?php
pageFooter();
?>