alors le voila.
C'est sale et j'ai cleané des trucs sans rapport mais ca doit marcher en gros. Il y a les fonctions principales à appeler
Code : Tout sélectionner
<?php
$now = round(microtime(true)*1000,0);
$get = 'https://www.rika-firenet.com/api/client/31711030/status?nocache=';
$controls_url = 'https://www.rika-firenet.com/api/client/31711030/controls';
$login_url = 'https://www.rika-firenet.com/web/login';
$username_rika = 'toto%40coucou.fr';
$password_rika = 'monpassword';
$login = 'https://www.rika-firenet.com/web/login';
$log = 'https://www.rika-firenet.com/api/client';
$timeout1 = 30;
$timeout2 = 10;
$nb_tentative_config = 3;
$interval_entre_tentative = 15;
$interval_verification_commande = 10;
$path_cookie = "/root/xavier/scripts/rika/Cookie_connexion_Rika.txt";
$StatusFile = "/root/xavier/scripts/rika/status.json";
$script_controle_poele = '/usr/bin/php /root/xavier/scripts/rika/set_power_manual.php';
$script_poele = '/root/xavier/scripts/rika/set_power_manual.php';
function stop_poele()
{
global $script_controle_poele;
logs("Action Stop");
$output = programme_poele_manuel(0);
echo "$output";
return;
}
function programme_poele ($mode,$value)
{
global $config,$script_controle_poele;
//$corrected_value = roundUpToAny($value);
$corrected_value = $value;
logs ('Poele programme pour '.$mode." ".$corrected_value."%");
//$output = shell_exec($script_controle_poele." ".$corrected_value);
$output = programme_poele_manuel($corrected_value);
echo "$output";
change_config('cur_state','heat',$config['timestamp']);
change_config('lastState','heat',$config['timestamp']);
return;
}
function logs($string)
{
global $debug;
$da = date('Y-m-d H:i:s');
if ($debug)
{
echo "$da : $string \n";
}
}
function programme_poele_manuel ($power_consigne)
{
echo date("Y-m-d H:i:s")."\n";
$status = login();
if ($status['connected'])
{
//echo "Connected\n";
$result_controls = set_power_manual($power_consigne);
logout();
}
else
{
echo 'Pb de connection au site rika : '.$status['curl_errno'].' : '.$status['curl_error']."\n";
}
}
function login ()
{
global $login_url,$username_rika,$password_rika,$path_cookie;
$postinfo = "email=".$username_rika."&password=".$password_rika;
$status = false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $path_cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $path_cookie); // file to read cookies in
//set the cookie the site has for certain features, this is optional
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
$return = curl_exec($ch);
// Retourne le numéro d'erreur de la dernière opération cURL.
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if ($curl_errno > 0)
{
echo "cURL Error ($curl_errno): $curl_error\n";
$status['connected'] = false;
$status['curl_errno'] = curl_errno($ch);
$status['curl_error'] = curl_error($ch);
exit; // mettre en veille en mode développement
}
else
{
//echo "Data received phase 1 : $return\n";
$status['connected'] = true;
}
curl_close($ch);
return $status;
}
function logout()
{
global $timeout2,$path_cookie;
$url = 'https://www.rika-firenet.com/web/logout';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout2);
if (preg_match('`^https://`i', $url))
{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
curl_close($ch);
unlink($path_cookie); // Supprime le fichier cookie
//echo "Logout ! \n";
}
function set_power_manual($power)
{
global $connect_id,$controls_url,$path_cookie,$timeout2;
global $nb_tentative_config ,$interval_entre_tentative ,$interval_verification_commande;
$power_valide = array (0,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100);
$power_requested = $power;
echo "power requested : ".$power_requested."\n";
if ($power < 30) {$power = 0;}
if ($power > 100) {$power = 100;}
if (($power >= 30) && ($power <= 100))
{
$power = (int) (round($power / 5)) * 5;
echo "power config : ".$power."\n";
}
for ($i=1; $i <= $nb_tentative_config; $i++)
{
$status_array = get_status();
if ($status_array['ok'])
{
$status_values = $status_array['info'];
if (in_array($power, $power_valide,true))
{
$on_off = 0;
if ($status_values['controls']['onOff']) {$on_off = 1;}
if ((($on_off) == $power) || (($status_values['controls']['onOff']) && ($power == $status_values['controls']['heatingPower']) && ($status_values['controls']['operatingMode'] == 0)))
{
$retour['message'] = "Aucun changement necessaire puissance $power en cours \n";
echo $retour['message']."\n";
return $retour;
}
else
{
echo "configuration du poele pour passer en manuel puissance $power\n";
$control = '';
// preparation du fichier de config control //
foreach ($status_values['controls'] as $key => $value) {
$value_back = $value;
if ($key == 'heatingPower') {$value = $power;}
if ($key == 'operatingMode') {$value = 0;}
if ($key == 'onOff') {$value = 'true';}
if ($power == 0)
{
if ($key == 'heatingPower') {$value = $value_back;}
if ($key == 'operatingMode') {$value = $value_back;}
if ($key == 'onOff') {$value = 'false';}
}
if (is_bool($value))
{
if ($value) {$value = "true";} else {$value = "false";}
}
$control = $control.'&'.$key.'='.$value;
}
$control = substr($control, 1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $controls_url);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout2); // 10s timeout time for cURL connection
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // allow https verification if true
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // check common name and verify with host name
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "VeriSignClass3PublicPrimaryCertificationAuthority-G5.pem"); // allow ssl cert direct comparison
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_COOKIEFILE, $path_cookie); // file to read cookies in
curl_setopt($ch, CURLOPT_POSTFIELDS,$control);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/x-www-form-urlencoded'));
$result=curl_exec ($ch);
$retour['message'] = $result;
echo "Code retour passage commande : ".$result."\n";
}
}
else
{
echo "Puissance non valide : $power \n";
}
}
sleep($interval_verification_commande);
$status_array = get_status();
if ($status_array['ok'])
{
if (($status_array['info']['controls']['onOff']) && ($status_array['info']['controls']['heatingPower'] == $power) && ($status_array['info']['controls']['operatingMode'] == 0))
{
echo "Ordre confirme apres $i tentative \n ";
break;
}
else
{
echo "Ordre non valide apres $i tentative \n ";
sleep($interval_entre_tentative);
}
}
else
{
sleep($interval_entre_tentative);
}
}
}
function get_status()
{
global $get,$now,$timeout2,$path_cookie,$StatusFile;
// Initiate curl
$ch = curl_init();
$status['ok'] = false;
$status['error'] = "";
$status['curl_errno'] = "";
$status['curl_error'] = "";
// Options
curl_setopt($ch, CURLOPT_URL, $get.$now); // set url
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout2); // 10s timeout time for cURL connection
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // allow https verification if true
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // check common name and verify with host name
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "VeriSignClass3PublicPrimaryCertificationAuthority-G5.pem"); // allow ssl cert direct comparison
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_COOKIEFILE, $path_cookie); // file to read cookies in
// Execution
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if ($curl_errno > 0)
{
echo "Erreur lors de la collecte des infos \n";
echo "cURL Error ($curl_errno): $curl_error\n";
$status['ok'] = false;
$status['error'] = "erreur curl";
$status['curl_errno'] = curl_errno($ch);
$status['curl_error'] = curl_error($ch);
exit; // mettre en veille en mode développement
}
else
{
$status['ok'] = true;
$File_Data = $data;
if (!$File_Data || strlen($File_Data) == 0 || ($File_Data == "Authorisation required!"))
{
$status['ok'] = false;
echo "Erreur lors de la collecte des infos \n";
$status['error'] = "json status file incorrect : $File_Data \n";
echo $status['error']."\n";
}
else
{
$status['info'] = json_decode($File_Data,true);
}
}
curl_close($ch);
return $status;
}
?>