From d6abbb4f4543977db29b9f6257fc3131058df443 Mon Sep 17 00:00:00 2001 From: Brendon Irwin Date: Tue, 16 Aug 2016 18:45:51 -0400 Subject: [PATCH] refactored the Room code using some of the reversed app code commands. Doesn't appear to have a huge performance impact on adjusting the lighting, but better for maintainability. --- api.php | 80 ++++++++++----------------------------------------------- 1 file changed, 14 insertions(+), 66 deletions(-) diff --git a/api.php b/api.php index 95e9967..ba12f44 100644 --- a/api.php +++ b/api.php @@ -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"; @@ -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=1".TOKEN."".$UID."".$val."level"; $result = getCurlReturn($CMD); $array = xmlToArray($result); @@ -40,64 +39,15 @@ } }elseif($type == "room"){ - $CMD = "cmd=GWRBatch&data=RoomGetCarousel1".TOKEN."name,image,imageurl,control,power,product,class,realtype,status&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=1".TOKEN."".$device['did']."".$val.""; - $result = getCurlReturn($CMD); - } - }elseif( $function == "dim"){ - //turn light on if off - if( isset($device['state']) && $device['state'] != 1 ){ - $CMD = "cmd=DeviceSendCommand&data=1".TOKEN."".$device['did']."1"; - $result = getCurlReturn($CMD); - } - //only dim if the light is on - if( isset($device['state']) && $device['state'] != 0 ){ - $CMD = "cmd=DeviceSendCommand&data=1".TOKEN."".$device['did']."".$val."level"; - $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=1".TOKEN."".$UID."".$val.""; }else{ - echo json_encode( array("error" => "room not found") ); + $CMD = "cmd=RoomSendCommand&data=1".TOKEN."".$UID."".$val."level"; } + $result = getCurlReturn($CMD); + $array = xmlToArray($result); + echo json_encode( array("room" => $UID, "fx" => $function, "val" => $val, "return" => $array) ); + }elseif($type == "all"){ $DEVICES = getDevices(); @@ -112,15 +62,15 @@ } }elseif( $function == "dim"){ + + $CMD = "cmd=DeviceSendCommand&data=1".TOKEN."".$device['did']."".$val."level"; + $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=1".TOKEN."".$device['did']."1"; $result = getCurlReturn($CMD); } - - $CMD = "cmd=DeviceSendCommand&data=1".TOKEN."".$device['did']."".$val."level"; - $result = getCurlReturn($CMD); - } } @@ -131,8 +81,6 @@ } - - }else{ echo json_encode( array("error" => "unknown type, required: device | room") ); }