diff --git a/send.php b/send.php new file mode 100644 index 0000000..3600c80 --- /dev/null +++ b/send.php @@ -0,0 +1,95 @@ +array("verify_peer"=>false,"verify_peer_name"=>false))); + + $SIDContainer=simplexml_load_string(file_get_contents($GlobalData['IP']."login_sid.lua", false, $context)); + + $SID=$SIDContainer->SID; + if ($SID=="0000000000000000") { + $Ch=$SIDContainer->Challenge; + $passwordhash=$Ch."-".md5(mb_convert_encoding($Ch."-".$password,"UCS-2LE","UTF-8")); + + $loginURL = $GlobalData['IP']."login_sid.lua?username=".$username."&response=".$passwordhash; + $SIDContainer=simplexml_load_string(file_get_contents($loginURL, false, $context)); + $SID=$SIDContainer->SID; + } + + return object2array($SID)[0]; +} + +function startsWith($haystack, $needle) +{ + $length = strlen($needle); + return (substr($haystack, 0, $length) === $needle); +} + +function getZustandSteckdose($DeviceID) +{ + global $GlobalData; + + $sid = GetSessionID($GlobalData['Username'], $GlobalData['Password']); + $context = stream_context_create(array("ssl"=>array("verify_peer"=>false,"verify_peer_name"=>false), 'http' => array('method' => 'GET', 'header' => 'Content-type: application/x-www-form-urlencoded'))); + $serverURL = $GlobalData['IP']."net/home_auto_query.lua?sid=".$sid."&command=AllOutletStates&xhr=1&t.".time()."=nocache"; + $data = json_decode(file_get_contents($serverURL, false, $context)); + + $currentDeviceID = 0; + foreach ($data as $key => $value) + { + //echo "start=".$key.";".$value."\n"; + + if(startsWith($key, "DeviceID_") == true) + $currentDeviceID = $value; + + if($currentDeviceID == $DeviceID) + { + + if(startsWith($key, "DeviceSwitchState_") == true) + { + return $value; + } + } + } + + return null; +} + +function setZustandSteckdose($deviceID, $state) +{ + global $GlobalData; + + $sid = GetSessionID($GlobalData['Username'], $GlobalData['Password']); + $postdata = http_build_query(array('sid' => $sid, 'command' => 'SwitchOnOff', 'id' => $deviceID, 'value_to_set' => $state, 'xhr' => "1")); + $context = stream_context_create(array("ssl"=>array("verify_peer"=>false,"verify_peer_name"=>false), 'http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata))); + $serverURL = $GlobalData['IP']."net/home_auto_query.lua?sid=".$sid."&command=SwitchOnOff&value_to_set=".$state."&id=".$deviceID."&xhr=1&t.".time()."=nocache"; + + json_decode(file_get_contents($serverURL, false, $context)); + } + +if (!isset($argv[1])) + die("no command lines"); + +if (isset($argv[2])) +{ + $newState = $argv[2]; +}else{ + $currentState = getZustandSteckdose($argv[1]); + if($currentState == "1"){$newState = "0";}else{$newState = "1";} +} + +setZustandSteckdose($argv[1], $newState); + +?> \ No newline at end of file