<?php
// script créé par G4FA pour eedomus
// ce script permet de piloter depuis l'eedomus les clim MITSUBISHI via MELCLOUD
$api_url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/';
$token='';
$code = 'xxxx@xxxx.xxx';
$pwd='xxxxxx';
$BuildingID='9659';
$action=getArg('action');
$DeviceId=getArg('DevideId');
echo ('rrrrrrr');
$token=sdk_connect ($api_url,$code,$pwd);
if ($action=='read') {
// permet d'avoir une mise en forme plus lisible dans un browser
sdk_header('text/xml');
// XML de sortie
$cached_xml = '<?xml version="1.0" encoding="utf8" ?>';
$cached_xml .= '<melcloud>';
$cached_xml .= '<cached>0</cached>';
$cached_xml .= sdk_melcloudversxml($api_url,$token);
$cached_xml .= '</melcloud>';
echo $cached_xml;
$cached_xml = str_replace('<cached>0</cached>', '<cached>1</cached>', $cached_xml);
if ($xml_content != '') // non vide
{
saveVariable('cached_xml', $cached_xml);
saveVariable('last_xml_success', time());
}
};
if ($action=='update') {
sdk_melcloudupdate ($api_url,$token,$DeviceId,$BuildingID);
};
function sdk_connect ($api_url,$code,$pwd){
$postdata = 'Email=' . $code . "&Password=" . $pwd . '&Language=7&AppVersion=1.10.1.0&Persist=true&CaptchaChallenge=null&CaptchaChallenge=null';
$response = httpQuery($api_url.'Login/ClientLogin', 'POST', $postdata);
$params = sdk_json_decode($response);
if ($params['error'] != ''){
die('Erreur lors authentification');
}
return $params['LoginData']['ContextKey'];
};
function sdk_melcloudupdate ($api_url,$token,$DeviceId,$BuildingID){
$choix=getArg('choix');
$val_choix=getArg('val_choix');
$response = httpQuery($api_url.'Device/Get?id=' . $DeviceId . '&BuildingID=' .$BuildingID, 'GET', $post = NULL, $oauth_token=null , array('X-MitsContextKey: '.$token ), $use_cookies = false);
$device =sdk_json_decode(utf8_decode($response));
$device[$choix] = $val_choix;
$device['EffectiveFlags'] = 6;
$device['HasPendingCommand'] = 'true';
$newdevice=str_replace('"EffectiveFlags":0','"EffectiveFlags":6',utf8_decode($response));
$newdevice1=str_replace('"HasPendingCommand":false','"HasPendingCommand":true',$newdevice);
$newdevice2=str_replace('"OperationMode":3','"OperationMode":1',$newdevice1);
$response2 = httpQuery($api_url.'Device/SetAta', 'POST', $newdevice, $oauth_token=null , array('X-MitsContextKey: '.$token.', content-type: application/json' ), $use_cookies = false);
echo(utf8_decode($response));
echo($newdevice2);
echo($response2);
echo (array('X-MitsContextKey: '.$token.', content-type: application/json' ));
};
function sdk_json_encode ($device){
$ret='';
foreach($device as $cle=>$valeur) {
if ($cle<>'WeatherObservations') {
if ($ret==''){
$ret.='{';
}
else {
$ret.=',';
};
$ret.='"'.$cle.'":';
if ($valeur==''){
$ret.='null';
}
else {
if (is_numeric($valeur)==true || $valeur=='true' || $valeur=='false' ){
$ret.=$valeur;
}
else {
$ret.= '"'.$valeur.'"' ;
}
};};
if ($cle=='WeatherObservations') {
$ret.=',"WeatherObservations":[';
for ($i = 0; $i < count($valeur); $i++) {
$ret.=sdk_json_encode ($valeur[$i] );
};
$ret.=']';
};
};
$ret.='}';
return $ret;
}
function sdk_melcloudversxml ($api_url,$token){
$response = httpQuery($api_url.'User/ListDevices', 'GET', $post = NULL, $oauth_token=null , array('X-MitsContextKey: '.$token ), $use_cookies = false);
$values = sdk_json_decode($response);
$ret='';
foreach ($values as $maison) {
$ret.='<'.str_replace(' ','',$maison['Name']).'>';
for ($i = 0; $i < count($maison['Structure']['Devices']); $i++) {
$device = $maison['Structure']['Devices'][$i];
$ret.=sdk_melclouddeviceversxml($device);
}
// FLOORS
for ($a = 0; $a < count($maison['Structure']['Floors']); $a++) {
$ret.='<'.str_replace(' ','',$maison['Structure']['Floors'][$a]['Name']).'>';
// AREAS IN FLOORS
for ($i = 0; $i < count($maison['Structure']['Floors'][$a]['Areas']); $i++) {
for ($d = 0; $d < count($maison['Structure']['Floors'][$a]['Areas'][$i]['Devices']); $d++) {
$device = $maison['Structure']['Floors'][$a]['Areas'][$i]['Devices'][$d];
$ret.=sdk_melclouddeviceversxml($device);
}
}
// FLOORS
for ($i = 0; $i < count($maison['Structure']['Floors'][$a]['Devices']); $i++) {
$device = $maison['Structure']['Floors'][$a]['Devices'][$i];
$ret.=sdk_melclouddeviceversxml($device);
}
$ret.='</'.str_replace(' ','',$maison['Structure']['Floors'][$a]['Name']).'>';
}
// AREAS
for ($a = 0; $a < count($maison['Structure']['Areas']); $a++) {
for ($i = 0; $i < count($maison['Structure']['Areas'][$a]['Devices']); $i++) {
$device = $maison['Structure']['Areas'][$a]['Devices'][$i];
$ret.=sdk_melclouddeviceversxml($device);
}
}
$ret.='</'.str_replace(' ','',$maison['Name']).'>';
};
return $ret;
};
function sdk_melclouddeviceversxml ($device){
$ret='';
foreach($device['Device'] as $cle=>$valeur)
{
if ($cle == 'Power' || $cle =='AutomaticFanSpeed' || $cle == 'VaneVerticalSwing' || $cle == 'VaneHorizontalSwing' ) { // ||'SetTemperature' || 'ActualFanSpeed' || 'FanSpeed' || 'OperationMode' || 'VaneVerticalDirection' || || 'VaneHorizontalDirection' || )){
$ret.='<'.$cle.'>'.($valeur==true ? 'true' : 'false').'</'.$cle.'>';
}
} ;
$ret='<' . str_replace(' ','',$device['DeviceName']).'>'.$ret.'</'.str_replace(' ','',$device['DeviceName']).'>';
return $ret;
};
?>
Utilisateurs parcourant ce forum : Aucun utilisateur inscrit et 25 invité(s)