Code : Tout sélectionner
<?php
$id_power = 2221;
$now = round(microtime(true)*1000,0);
$get = 'https://www.rika-firenet.com/api/client/33222556/status?nocache=';
$on = false;
$login_url = 'https://www.rika-firenet.com/web/login';
$log = 'https://www.rika-firenet.com/api/client';
echo date("Y-m-d H:i:s")."\n";
$connect_sid = login();
$status = get_status('connect.sid='.$connect_sid);
$status_array = json_decode($status,true);
var_dump($status_array);
function login ()
{
global $login_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,'email=xxxxxxxxxxx@gmail.com&password=yyyyyyyyy');
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/x-www-form-urlencoded'));
$result=curl_exec ($ch);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches); // get cookie
$cookies = array();
foreach($matches[1] as $item)
{
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
return $cookies['connect_sid'];
}
function get_status($cookie)
{
global $get,$now;
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $get.$now);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_COOKIE, $cookie);
curl_setopt($c, CURLOPT_HEADER, false);
$result=curl_exec ($c);
return $result;
}
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 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);
}
}
}
?>