Skip to content

Commit

Permalink
Creating Virtual Devices!
Browse files Browse the repository at this point in the history
  • Loading branch information
bren1818 committed Oct 25, 2016
1 parent 51af5d0 commit a5606e3
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 5 deletions.
201 changes: 201 additions & 0 deletions createDevice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php
/*
*
* TCP Ligthing Web UI Info Script - By Brendon Irwin
*
*/

include "include.php";

if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'){
error_reporting(E_ALL);
$sel = $_POST['selected'];
$name = $_POST['name'];
$colourID = $_POST['color'];
$remoteControl = $_POST['remote'];

$devices = "";
if( is_array($sel) ){
for($d=0; $d < sizeof($sel); $d++){
$devices.= '<device><did>'.$sel[$d].'</did></device>';
}

$CMD = "cmd=DeviceVirtualCreate&data=<gip><version>1</version><token>".TOKEN."</token><color>".$colourID."</color><name>".( $name != "" ? $name : "NEW Virtual Device" )."</name>".$devices. ( $remoteControl != "" ? "<other><rcgroup>".$remoteControl."</rcgroup></other>" : "" )."</gip>";
$result = getCurlReturn($CMD);
$array = xmlToArray($result);

$newDevice = $array['did'];

if( $newDevice != ""){
header("Location: info.php?did=".$newDevice);
}
}

/*
if (this.image != null && this.image.length() > 0) {
dataString.append(String.format("<image>%s</image>", new Object[]{xmlEscape(this.image)}));
}
if (this.producttype != null) {
dataString.append(String.format("<producttype>%s</producttype>", new Object[]{xmlEscape(this.producttype)}));
}
*/


exit;
}


pageHeader("TCP Lighting Controller - Create Device");
?>
<div id="toolBar"><a href="index.php">Lighting Controls</a> | <a href="scheduler.php">Lighting Scheduler</a> | <a href="apitest.php">API Test Zone</a> | <a href="scenes.php">Scenes/Smart Control</a></div>
<?php
echo '<div class="container">';
?>
<script type="text/javascript">
function SelectMoveRows(SS1,SS2){
//function shamelessly borrowed from http://johnwbartlett.com/cf_tipsntricks/index.cfm?TopicID=86
var SelID='';
var SelText='';
// Move rows from SS1 to SS2 from bottom to top
for (i=SS1.options.length - 1; i>=0; i--){
if (SS1.options[i].selected == true){
SelID=SS1.options[i].value;
SelText=SS1.options[i].text;
var newRow = new Option(SelText,SelID);
SS2.options[SS2.length]=newRow;
SS1.options[i]=null;
}
}
}
$(function(){
$('.btnAdd, .btnRemove').click(function(){
var available = $(this).closest('.deviceList').find('select.available')[0];
var selected = $(this).closest('.deviceList').find('select.selected')[0];

if( $(this).hasClass('btnAdd') ){
SelectMoveRows(available, selected );
}else{
SelectMoveRows( selected, available );
}
});

$('select[name="remote"]').change(function(){
if( $(this).find('option:selected').val() != "" ){
$('#remote').show();
}else{
$('#remote').hdie();
}

});

$('#post').click(function(event){

var count = $('#selected option').length;
console.log( count );
if( count > 1){
console.log("Submit");
//select all the selected
$('#selected option').prop('selected', true);
$('#createDevice').submit();
}else{
event.preventDefault();
window.alert("You need to select 2 or more devices");
}
});

});
</script>
<h1>Create Virtual Device</h1>

<p>Add two or more devices to the selected list</p>
<div class="deviceList">
<form id="createDevice" method="post" action="createDevice.php">

<?php
$CMD = "cmd=RoomGetList&data=<gip><version>1</version><token>".TOKEN."</token></gip>";
$result = getCurlReturn($CMD);
$array = xmlToArray($result);
$ROOM_COLOURS = array();
//pa($array);
foreach( $array['room'] as $room ){
$ROOM_COLOURS[ $room["colorid"] ]["name"] = $room["name"];
$ROOM_COLOURS[ $room["colorid"] ]["hex"] = $room["color"];
$ROOM_COLOURS[ $room["colorid"] ]["colorid"] = $room["colorid"];
$ROOM_COLOURS[ $room["colorid"] ]["image"] = $room["img"];
$ROOM_COLOURS[ $room["colorid"] ]["room"] = $room["rid"];
}
?>
<label for="name">New Device Name:<br /> <input name="name" id="name" value="" placeholder="NEW_VIRTUAL_DEVICE" /></label><br />

<label for="color">Color:<br />
<select name="color">
<option value="">Not Specified</option>
<option value="0">Black <?php echo isset( $ROOM_COLOURS[0] ) ? ' ('.$ROOM_COLOURS[0]['name'].')' : ''; ?></option>
<option value="1">Green <?php echo isset( $ROOM_COLOURS[1] ) ? ' ('.$ROOM_COLOURS[1]['name'].')' : ''; ?></option>
<option value="2">Dark Blue <?php echo isset( $ROOM_COLOURS[2] ) ? ' ('.$ROOM_COLOURS[2]['name'].')' : ''; ?></option>
<option value="3">Red <?php echo isset( $ROOM_COLOURS[3] ) ? ' ('.$ROOM_COLOURS[3]['name'].')' : ''; ?></option>
<option value="4">Yellow <?php echo isset( $ROOM_COLOURS[4] ) ? ' ('.$ROOM_COLOURS[4]['name'].')' : ''; ?></option>
<option value="5">Purple <?php echo isset( $ROOM_COLOURS[5] ) ? ' ('.$ROOM_COLOURS[5]['name'].')' : ''; ?></option>
<option value="6">Orange <?php echo isset( $ROOM_COLOURS[6] ) ? ' ('.$ROOM_COLOURS[6]['name'].')' : ''; ?></option>
<option value="7">Light Blue <?php echo isset( $ROOM_COLOURS[7] ) ? ' ('.$ROOM_COLOURS[7]['name'].')' : ''; ?></option>
<option value="8">Pink <?php echo isset( $ROOM_COLOURS[8] ) ? ' ('.$ROOM_COLOURS[8]['name'].')' : ''; ?></option>
</select>
</label><br />

<label for="remote">Assign Remote Control Button: <br />
<select name="remote">
<option value="">Not Specified / No remote</option>
<option value="1">Button 1</option>
<option value="2">Button 2</option>
<option value="3">Button 3</option>
<option value="4">Button 4</option>
</select><br />
<img id="remote" style="display: none;" src="<?php echo LOCAL_URL; ?>/images/remote.png" />
</label><br />

<table>
<tr>
<td>Available</td><td>&nbsp;</td><td>Selected</td>
</tr>
<tr>
<td>
<?php
$devices = getDevices();
//pa($devices);



?>
<select size="9" class="available" name="DEVICE_AVAILABLE" multiple>
<?php
foreach($devices as $device){
echo '<option value="'.$device['did'].'">'.$device['prodtype'].' - '.$device['name'].' ('.$ROOM_COLOURS[ $device["colorid"] ]["name"].')</option>';
}
?>
</select>
</td>
<td>
<input type="Button" value="Add >>" class="btnAdd"><br />
<br />
<input type="Button" value="<< Remove" class="btnRemove">
</td>
<td>

<select id="selected" size="9" class="selected" name="selected[]" multiple>
</select>

</td>
</tr>
</table>

</div>

<input name="submit" id="post" type="submit" value="Create Device" />
</form>
</form>


<?php
echo '</div>';
pageFooter();
?>
Binary file added images/remote.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pageHeader("TCP Lighting Controller");
?>

<div id="toolBar"><a href="scheduler.php">Lighting Scheduler</a> | <a href="apitest.php">API Test Zone</a> | <a href="scenes.php">Scenes/Smart Control</a></div>
<div id="toolBar"><a href="scheduler.php">Lighting Scheduler</a> | <a href="apitest.php">API Test Zone</a> | <a href="scenes.php">Scenes/Smart Control</a> | <a href="createDevice.php">Create Virtual Device</a> </div>
<?php
/*
*
Expand Down
105 changes: 101 additions & 4 deletions info.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@
$ROOM_COLOURS[ $room["colorid"] ]["room"] = $room["rid"];
}


if (strtoupper($_SERVER['REQUEST_METHOD']) == 'GET'){
if(isset($_GET['delete']) && $_GET['delete'] != "" ){
/*
if (this.full != null && this.full.length() > 0 && this.full.equalsIgnoreCase("1")) {
fullDelete = "<full>1</full>";
}
dataString = String.format("<gip><version>1</version><token>%s</token><did>%s</did>%s</gip>", new Object[]{xmlEscape(this.token), xmlEscape(this.did), fullDelete});
this.postData.add(new BasicNameValuePair("cmd", "DeviceDelete"));
this.postData.add(new BasicNameValuePair("data", dataString));
*/

$did = $_REQUEST['delete'];
$fullDelete = $_REQUEST['fullDelete'];

$CMD = "cmd=DeviceDelete&data=<gip><version>1</version><token>".TOKEN."</token><did>".$did."</did>".($fullDelete == 1 ? '<full>1</full>' : '')."</gip>";
$result = getCurlReturn($CMD);
$array = xmlToArray($result);



echo json_encode( array("delete" => $did, "fullDelete" => $fullDelete, "Result" => $array) );

}
}


if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'){
Expand All @@ -28,6 +53,7 @@
$did = $_POST['did'];
$name = $_POST['name'];
$color = $_POST['color'];
$remote = $_POST['remote'];
$imdata = "";

if( isset($_FILES["image"]) && $_FILES["image"]["tmp_name"] != "" ){
Expand All @@ -53,7 +79,7 @@



$CMD = "cmd=DeviceSetInfo&data=<gip><version>1</version><token>".TOKEN."</token><did>".$did."</did><name>".$name."</name><color>".$color."</color>".($imdata != "" ? "<image>".$imdata."</image>" : "")."</gip>";
$CMD = "cmd=DeviceSetInfo&data=<gip><version>1</version><token>".TOKEN."</token><did>".$did."</did><name>".$name."</name><color>".$color."</color>".($imdata != "" ? "<image>".$imdata."</image>" : "").( $remote != "" ? "<other><rcgroup>".$remote."</rcgroup></other>" : "" )."</gip>";

//echo htmlentities($CMD);
$result = getCurlReturn($CMD);
Expand All @@ -68,9 +94,42 @@
}

if(isset($_POST['rid']) && $_POST['rid'] != "" ){



/*
StringBuilder dataString = new StringBuilder(String.format("<gip><version>1</version><token>%s</token>", new Object[]{xmlEscape(this.token)}));
if (this.bycolor == null && this.rid != null) {
dataString.append(String.format("<rid>%s</rid>", new Object[]{xmlEscape(this.rid)}));
}
if (!(this.bycolor == null || this.colorid == null)) {
dataString.append(String.format("<colorid>%s</colorid>", new Object[]{xmlEscape(this.colorid)}));
}
if (this.name != null) {
dataString.append(String.format("<name>%s</name>", new Object[]{xmlEscape(this.name)}));
}
if (this.type != null) {
dataString.append(String.format("<type>%s</type>", new Object[]{xmlEscape(this.type)}));
}
dataString.append("</gip>");
if (this.preprocessForBatch) {
this.gcmdDictionary = new HashMap();
this.gcmdDictionary.put("gdata", dataString.toString());
this.gcmdDictionary.put("gcmd", "RoomSetInfo");
this.gcmdDictionary.put("cmdOwner", this);
}
this.postData.add(new BasicNameValuePair("cmd", "RoomSetInfo"));
this.postData.add(new BasicNameValuePair("data", dataString.toString()));
*/



$_REQUEST['rid'] = $_POST['rid'];
}




}


Expand All @@ -83,15 +142,31 @@
echo '<div class="container">';
//echo '<h2>Device Info</h2>';
//echo '<p><b>Device ID:'.$did.'</b></p>';



$CMD = "cmd=DeviceGetInfo&data=<gip><version>1</version><token>".TOKEN."</token><did>".$did."</did><fields>name,power,product,class,image,control,realtype,other,status</fields></gip>";

$result = getCurlReturn($CMD);
$array = xmlToArray($result);

?>
<script>
$(function(){
$('#deleteDevice').click(function(){
var did = $(this).attr('data-deviceID');
var c = confirm("Are you sure you wish to delete the device?");
if(c){
//var fd = confirm("Do you want to 'Full' Delete? - No puts device into detection mode and will add it to first room");
//if(fd) { fd = 1; }else{ fd = 0; }
var fd = 0;

$.get( "info.php?delete=" + did + "&fullDelete=" + fd, function( data ) {
console.log( data );
window.location = "index.php";
});
}
});
});
</script>
<form method="post" action="info.php" enctype="multipart/form-data">
<fieldset>
<legend>Update Device</legend>
Expand Down Expand Up @@ -126,6 +201,24 @@
<?php
}
?>
<br />
<label for="remote">Assigned Remote Control Button: <br />
<?php
if( isset($array['other']['rcgroup']) && $array['other']['rcgroup'] != "" ){
$rcID = $array['other']['rcgroup'];
}else{
$rcID = "";
}
?>
<select name="remote">
<option value="">Not Specified / No remote</option>
<option value="1" <?php echo ($rcID == 1 ? " Selected" : "" ); ?>>Button 1</option>
<option value="2" <?php echo ($rcID == 2 ? " Selected" : "" ); ?>>Button 2</option>
<option value="3" <?php echo ($rcID == 3 ? " Selected" : "" ); ?>>Button 3</option>
<option value="4" <?php echo ($rcID == 4 ? " Selected" : "" ); ?>>Button 4</option>
</select><br />
<img id="remote" style="display: none;" src="<?php echo LOCAL_URL; ?>/images/remote.png" />
</label><br />

<input type="hidden" name="did" value="<?php echo $did; ?>" /><br />
<input type="submit" value="SAVE" />
Expand All @@ -146,6 +239,10 @@
echo '<div class="device-slider" data-value="'.(isset($device['level']) ? $device['level'] : 100).'" data-device-id="'. $device["did"].'"></div>';
echo '</div>';
echo '</div>';

?>
<button id="deleteDevice" data-deviceID="<?php echo $device['did']; ?>">Delete Device</button>
<?php
echo '</div>';
?>
</div>
Expand Down

0 comments on commit a5606e3

Please sign in to comment.