<?php

$GlobalData['Username'] = "unhb";
$GlobalData['Password'] = "wurstwasser";
$GlobalData['IP'] = "https://".$GlobalData['Username'].":".$GlobalData['Password']."@192.168.88.67/";


//sudo apt-get install php7.2-xml
//sudo apt-get install php7.2-mbstring

function object2array($object) { return @json_decode(@json_encode($object),1); } 

function GetSessionID($username, $password) 
{
    global $GlobalData;

    $context  = stream_context_create(array("ssl"=>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);

?>