<?php
/* very simple demonstration script for FritzDect200 powerswitch
* with integrated powermeter and temperature sensor.
* works maybe with other dect and powerline devices from AVM.
*
*/
$version = "0.2";
$date = "05.12.2014";
/* author: Felix (fe-X / Felix_)
*
* function get_sid() stolen from fritz_aha_reader2.phps V2.2 25.09.2014
* http://www.tdressler.net/ipsymcon/fritz_aha.html
* extended for use with username and https
*/
//config
$host = ''; //IP or URL (e.g. abc123.myfritz.net)
$user = ''; //username
$password = ''; //your password
//
$loginURL = "https://" . $host . "/login_sid.lua";
$fd200URL = "https://" . $host . "/webservices/homeautoswitch.lua";
$context = stream_context_create(array (
'http' => array (
'method' => 'GET',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
)
)
);
$sid = get_sid($loginURL, $user, $password, $context);
if(!empty($_GET['switchcmd'])){
file_get_contents($fd200URL . '?ain=' . $_GET['ain'] . '&switchcmd=' . $_GET['switchcmd'] . '&sid=' . $sid, false, $context);
sleep(4);
}
$xmlstring = rtrim(file_get_contents($fd200URL . '?sid='
. $sid
. "&switchcmd=getdevicelistinfos", false, $context));
$xml = simplexml_load_string($xmlstring);
//var_dump($xml);
foreach($xml->device as $device){
$info = $device->attributes();
$productname = $info->productname;
$manufacturer = $info->manufacturer;
$identifier = $info->identifier;
$id = $info->id;
$fwversion = $info->fwversion;
$functionbitmask = $info->functionbitmask;
$functionDect = (($functionbitmask & bindec('10000000000')) == 0) ? "no" : "yes";
$functionPowerswitch= (($functionbitmask & bindec('01000000000')) == 0) ? "no" : "yes";
$functionTempsensor = (($functionbitmask & bindec('00100000000')) == 0) ? "no" : "yes";
$functionPowermeter = (($functionbitmask & bindec('00010000000')) == 0) ? "no" : "yes";
$present = $device->present;
$name = $device->name;
$switch = $device->switch;
$switch_state = $switch->state;
$switch_mode = $switch->mode;
$switch_lock = $switch->lock;
$powermeter = $device->powermeter;
$powermeter_power = $powermeter->power;
$powermeter_energy = $powermeter->energy;
$temperature = $device->temperature;
$temperature_celsius= $temperature->celsius;
$temperature_offset = $temperature->offset;
if(!empty($_GET['getdata'])){
echo time() . ",";
}elseif(!empty($_GET['set'])){
echo "OK";
}else{
$on = '<a href="' . $_SERVER['PHP_SELF'] . '?ain=' . str_replace(" ", "", $identifier) . '&switchcmd=setswitchon">on</a>';
$off = '<a href="' . $_SERVER['PHP_SELF'] . '?ain=' . str_replace(" ", "", $identifier) . '&switchcmd=setswitchoff">off</a>';
$toggle = '<a href="' . $_SERVER['PHP_SELF'] . '?ain=' . str_replace(" ", "", $identifier) . '&switchcmd=setswitchtoggle">toggle</a>';
$renew = '<a href="' . $_SERVER['PHP_SELF'] . '">renew</a>';
echo "<pre>
very simple demonstration script for FritzDect!200
version $version / $date
productname $productname
manufacturer $manufacturer
identifier $identifier
id $id
firmware version $fwversion
function bitmask $functionbitmask
dect repeater $functionDect
powerswitch $functionPowerswitch
temperaturesensor $functionTempsensor
powermeter $functionPowermeter
name $name
present (0/1) $present
switch
state (0/1) $switch_state $on $off $toggle
mode (manuell/auto) $switch_mode
lock (0/1) $switch_lock
powermeter
power (mW) $powermeter_power
energy (Wh) $powermeter_energy
temperature
celsius (0.1C) $temperature_celsius
offset (K) $temperature_offset
$renew
</pre>";
}
}
function get_sid ($loginurl, $user, $password, $context) {
// get challenge string
$http_response = file_get_contents($loginurl, false, $context);
$xml = simplexml_load_string($http_response);
$challenge=(string)$xml->Challenge;
$sid=(string)$xml->SID;
if ((strlen($sid)>0) && (preg_match("/^[0]+$/",$sid)) && $challenge) {
//sid is null, got challenge
$sid="";
//build password response
$pass=$challenge."-".$password;
//UTF-16LE encoding as required
$pass=mb_convert_encoding($pass, "UTF-16LE");
//md5hash on top
$md5 = md5($pass);
//final answer string
$challenge_response = $challenge."-".$md5;
//send to box
$url=$loginurl."?response=".$challenge_response . "&username=" . $user;
$http_response = file_get_contents($url, false, $context);
//check answer
$xml = simplexml_load_string($http_response);
$sid=(string)$xml->SID;
if ((strlen($sid)>0) && !preg_match("/^[0]+$/",$sid)) {
//is not null, bingo!
return $sid;
}
}else{
//use existing sid if $sid matches an hex string
if ((strlen($sid)>0) && (preg_match("/^[0-9a-f]+$/",$sid))){
return $sid;
}
}
return null;
}//function
?>