It get the status of your adsl and reboot the box if needed
It get the Adsl mode Download Upload Speed
I hope, this could help someone

First I just install (if not yest installed)
apt-get -y install php7.0-curl
apt-get -y install php7.0-xml
I created 2 devices in domoticz
1) a dummy text 2) a dummy selector switch The code to copy into a file in your script folder
In the comment of the file, I wrote a way to start it from LUA but can be done by an another way such as cron...
Code: Select all
<?php
/* list of all SFR BOX API
http://efixo-dev.pfd.sfr.net/doc/api-rest.html
*/
/*
Box Const to setup
*/
//box API method
$apiUrl = "http://192.168.1.1/api/1.0/?method=";
//username to connect to the box
$user = "admin";
//password to connect to the box
$password = "";
/*
Install the file
copy the file into /home/domoticz/scripts/
make the script executable chmod 755 yourfile.php
*/
/*
Setup to do in domoticz
create a dummy Selector switch
set 10 level Unconnected (or with your own language)
set 20 level Connected
set 30 level Reboot
create a dummy Text
in device event
--to update the dummy text to know whate is the speed of the adsl after a new connected state
if ( devicechanged['SfrBox'] == 'Connectée' ) then
os.execute ("php -f /home/domoticz/scripts/yourfile.php -- --action monitor")
end
in time event
--to check every 5 minutes the state of ADSL
-- we send the current state to the php file because
-- if the --oldstate was unconnected then php reboot the box
-- to update the selector switch only if there is a change
-- REPLACE SfrBox BELOW by the name of your selector switch
time = os.date("*t")
StrMinutes=tostring(time.min):sub(-1)
if StrMinutes=="5" or StrMinutes=="0" then
os.execute ("php -f /home/domoticz/scripts/yourfile.php -- --action check --oldstate "..otherdevices['SfrBox'])
end
*/
/*
Selector switch in domoticz
*/
//level 10 for Unconnected : Same as defined in domoticz
$ConstUnconnected="Déconnectée";
//level 20 for Connected : Same as defined in domoticz
$ConstConnected="Connectée";
//selector index
$SelectorSwitchIndex="415";
/*
Dummy text in domoticz (to receive ADSLMODE DOWNLOAD UPLOAD)
*/
$TextIndex="505";
$usage = 'php -f ' . basename(__FILE__) . " -- \\\n\t"
. "[ -a | --action check|monitor|reboot] \\\n\t"
. "[ -o | --oldstate ] \\\n\t";
$longopts = array(
'action:',
'oldstate:',
);
$options = getopt('a:o:', $longopts);
//var_dump($options);
// Process action argument
if (!isset($options['action']) && !isset($options['a'])) {
die("Missing --action argument.\nUsage:\t$usage\n");
}
$action = isset($options['action']) ? $options['action'] : $options['a'];
// Process oldstate argument
if (isset($options['oldstate'])) {
$oldstate = $options['oldstate'];
}
elseif (isset($options['o'])) {
$oldstate = $options['o'];
}
// check oldstate present if action is 'check'
if ($action=="check" && !isset($oldstate)) {
die("Missing 'oldstate' argument, ie: --action check --oldstate|-o $ConstUnconnected|$ConstConnected .\nUsage:\t$usage\n");
}
function testWan(){
$socket = @fsockopen("www.google.com", 80);
if ($socket === false) {
return 0;
} else {
return 1;
}
}
function PrivateRequest($request) {
$authToken=authhh();
$URL = $GLOBALS['apiUrl'].$request."&token=".$authToken;
$ch = curl_init();
print_r($URL);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "dummy");
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
}
function domoticzCommand($idx,$cmd,$choix) {
$domoticzurl='http://127.0.0.1:8080/json.htm?';
$chain="";
switch ($choix) {
case "switch":
$chain=$domoticzurl.'type=command¶m=switchlight&idx='.$idx.'&switchcmd='.$cmd;
break;
case "Custom Sensor":
$chain=$domoticzurl.'type=command¶m=udevice&idx='.$idx.'&svalue='.$cmd;
break;
case "Text":
$chain=$domoticzurl.'type=command¶m=udevice&idx='.$idx.'&svalue='.$cmd;
break;
case "Message":
$chain=$domoticzurl.'type=command¶m=addlogmessage&message='.$cmd;
break;
case "SelectorSwitch":
$chain=$domoticzurl.'type=command¶m=switchlight&idx='.$idx.'&switchcmd=Set%20Level&level='.$cmd;
break;
}
$reply=json_decode(file_get_contents($chain),true);
if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
echo($reply);
return $reply;
}
function authhh(){
//get token from the box
$tokenXML = simplexml_load_file( $GLOBALS['apiUrl'] . "auth.getToken" );
$token = $tokenXML->auth[0]->attributes()->token;
//hash the username with the token
$hmacUser = hash_hmac('sha256', hash('sha256', $GLOBALS['user'] ), $token);
//hash the passwd with the token
$hmacPassword = hash_hmac('sha256', hash('sha256', $GLOBALS['password'] ), $token);
//concat the hashes
$hash = $hmacUser . $hmacPassword;
//get the auth Token
$authXML = simplexml_load_file( $GLOBALS['apiUrl'] ."auth.checkToken&token=" . $token . "&hash=" . $hash );
if( $authXML->attributes()->stat == "ok" ) {
return $authXML->auth->attributes()->token;
}
else {
domoticzCommand("0","erreur de la fonction authhh",'Message');
exit('erreur de la fonction authhh');
}
}
switch($action) {
case 'check':
$val=testWan();
if ($val==1) {
if ($GLOBALS['oldstate']!=$GLOBALS['ConstConnected']) {
//adsl up update Dz
domoticzCommand($SelectorSwitchIndex,"10",'SelectorSwitch');
}
else{
//domoticzCommand("0","SfrBox_check:Nochange",'Message');
}
}
else {
if ($GLOBALS['oldstate']!=$GLOBALS['ConstUnconnected']) {
//adsl down update Dz
domoticzCommand($SelectorSwitchIndex,"20",'SelectorSwitch');
}
else{
//if box was already down last time then reboot the box
domoticzCommand($SelectorSwitchIndex,"30",'SelectorSwitch');
PrivateRequest('system.reboot');
}
}
break;
case 'monitor':
$MyXML = simplexml_load_file( $GLOBALS['apiUrl']."dsl.getInfo" );
$str = $MyXML->dsl->attributes()->linemode;
$str=str_replace('+',"plus",$str)."%20";
$str .= $MyXML->dsl->attributes()->rate_down."%20";
$str .= $MyXML->dsl->attributes()->rate_up."%20";
domoticzCommand($TextIndex,$str,"Text");
break;
case 'reboot':
PrivateRequest('system.reboot');
break;
default:
die("Unknown --action argument.\nUsage:\t$usage\n");
}