Skip to content

Commit

Permalink
refactored the Room code using some of the reversed app code commands…
Browse files Browse the repository at this point in the history
…. Doesn't appear to have a huge performance impact on adjusting the lighting, but better for maintainability.
  • Loading branch information
bren1818 committed Aug 16, 2016
1 parent d850ea6 commit d6abbb4
Showing 1 changed file with 14 additions and 66 deletions.
80 changes: 14 additions & 66 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
$UID = isset($_REQUEST['uid']) ? $_REQUEST['uid'] : ""; //DeviceID or Room ID
$val = isset($_REQUEST['val']) ? $_REQUEST['val'] : ""; //DeviceID or Room ID

$val = $val < 0 ? 0 : $val;
$val = $val > 100 ? 100 : $val;

if( $function != "" && $type != "" && $UID != "" && $val != ""){
include "include.php";

Expand All @@ -26,10 +29,6 @@
break;
case "dim": //SAMPLE CALL: /api.php?fx=dim&type=device&uid=360187068559174100&val=80

//$val = 0 - 100;
$val = $val < 0 ? 0 : $val;
$val = $val > 100 ? 100 : $val;

$CMD = "cmd=DeviceSendCommand&data=<gip><version>1</version><token>".TOKEN."</token><did>".$UID."</did><value>".$val."</value><type>level</type></gip>";
$result = getCurlReturn($CMD);
$array = xmlToArray($result);
Expand All @@ -40,64 +39,15 @@
}
}elseif($type == "room"){

$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);
$DATA = $array["gwrcmd"]["gdata"]["gip"]["room"];
$complete = 0;
foreach($DATA as $room){
if( $room["rid"] == $UID ){
$complete = 1;
$DEVICES = array();
if( ! is_array($room["device"]) ){
//$DEVICES[] = $room["device"]; //singular device in a room
}else{
$device = (array)$room["device"];
if( isset($device["did"]) ){
//item is singular device
//TODO should check if device has power
$DEVICES[] = $room["device"];
}else{
for( $x = 0; $x < sizeof($device); $x++ ){
if( isset($device[$x]) && is_array($device[$x]) && ! empty($device[$x]) ){
//TODO should check if device has power
$DEVICES[] = $device[$x];
}
}
}
}

if( sizeof($DEVICES) > 0 ){
foreach($DEVICES as $device){
if( $function == "toggle" ){
//apply if toggle doesnt already matches value
if( isset($device['state']) && $device['state'] != $val ){
$CMD = "cmd=DeviceSendCommand&data=<gip><version>1</version><token>".TOKEN."</token><did>".$device['did']."</did><value>".$val."</value></gip>";
$result = getCurlReturn($CMD);
}
}elseif( $function == "dim"){
//turn light on if off
if( isset($device['state']) && $device['state'] != 1 ){
$CMD = "cmd=DeviceSendCommand&data=<gip><version>1</version><token>".TOKEN."</token><did>".$device['did']."</did><value>1</value></gip>";
$result = getCurlReturn($CMD);
}
//only dim if the light is on
if( isset($device['state']) && $device['state'] != 0 ){
$CMD = "cmd=DeviceSendCommand&data=<gip><version>1</version><token>".TOKEN."</token><did>".$device['did']."</did><value>".$val."</value><type>level</type></gip>";
$result = getCurlReturn($CMD);
}
}
}
}else{
echo json_encode( array("error" => "no devices in room") );
}
}
}
if($complete == 1){
echo json_encode( array("room" => $UID, "fx" => $function, "val" => $val) );
if( $function == "toggle" ){
$CMD = "cmd=RoomSendCommand&data=<gip><version>1</version><token>".TOKEN."</token><rid>".$UID."</rid><value>".$val."</value></gip>";
}else{
echo json_encode( array("error" => "room not found") );
$CMD = "cmd=RoomSendCommand&data=<gip><version>1</version><token>".TOKEN."</token><rid>".$UID."</rid><value>".$val."</value><type>level</type></gip>";
}
$result = getCurlReturn($CMD);
$array = xmlToArray($result);
echo json_encode( array("room" => $UID, "fx" => $function, "val" => $val, "return" => $array) );

}elseif($type == "all"){

$DEVICES = getDevices();
Expand All @@ -112,15 +62,15 @@
}

}elseif( $function == "dim"){

$CMD = "cmd=DeviceSendCommand&data=<gip><version>1</version><token>".TOKEN."</token><did>".$device['did']."</did><value>".$val."</value><type>level</type></gip>";
$result = getCurlReturn($CMD);

//turn light on if it is not in order to dim it
if( isset($device['state']) && $device['state'] == 0 ){
$CMD = "cmd=DeviceSendCommand&data=<gip><version>1</version><token>".TOKEN."</token><did>".$device['did']."</did><value>1</value></gip>";
$result = getCurlReturn($CMD);
}

$CMD = "cmd=DeviceSendCommand&data=<gip><version>1</version><token>".TOKEN."</token><did>".$device['did']."</did><value>".$val."</value><type>level</type></gip>";
$result = getCurlReturn($CMD);

}
}

Expand All @@ -131,8 +81,6 @@
}




}else{
echo json_encode( array("error" => "unknown type, required: device | room") );
}
Expand Down

0 comments on commit d6abbb4

Please sign in to comment.