Bonsoir, Je suis en train de faire un plugin pour TV Sony Bravia, en utilisant l'API. Voilà la documentation:API BRAVIA SONY J'ai réussi à avoir toutes les informations du téléviseur et passer les commandes. Voilà le premier script qui permet de tester toutes les instructions disponibles:
- Code : Tout sélectionner
<?php /** Développement du script pour tester TV SONY BRAVIA. V1.0 par dommarion le 24/04/2024. Pré-requis pour régler la TV afin de la commander via API: https://braviaproapi.readthedocs.io/en/latest/gettingstarted.html#configuring-your-television Documentation API: https://pro-bravia.sony.net/develop/integrate/ip-control/index.html Instructions API: https://pro-bravia.sony.net/develop/integrate/rest-api/spec/service/appcontrol/v1_0/getApplicationList/index.html
**/ //Fonctions développées
// Function to convert array to XML function sdk_array2Xml($array, &$xml) { foreach ($array as $key => $value) { if (is_numeric($key)) {$key="n".$key;} if (is_array($value)) { $xml .= "<$key>"; sdk_array2Xml($value, $xml); $xml .= "</$key>"; } else { if ($value == false) {$value = 0;} $xml .= "<$key>" . htmlspecialchars($value) . "</$key>"; } } }
// Function to convert json to XML function sdk_json2xml($json) { // Convert JSON to array $array = sdk_json_decode($json, true); // Set up header and XML encoding sdk_header('text/xml'); $xml = '<?xml version="1.0" encoding="UTF-8"?><root>'; sdk_array2xml($array, $xml); $xml .= '</root>'; return $xml; }
// Fonction json_encode du PHP pour eedomus function sdk_json_encode($val){ if (is_string($val)) return '"'.addslashes($val).'"'; if (is_numeric($val)) return $val; if ($val === null) return 'null'; if ($val === true) return 'true'; if ($val === false) return 'false';
$assoc = false; $i = 0; foreach ($val as $k=>$v){ if ($k !== $i++){ $assoc = true; break; } } $res = array(); foreach ($val as $k=>$v){ $v = sdk_json_encode($v); if ($assoc){ $k = '"'.addslashes($k).'"'; $v = $k.':'.$v; } $res[] = $v; } $res = implode(',', $res); if ($assoc) {return '{'.$res.'}';} else {return '['.$res.']';} }
/*httpQuery($url, $action = 'GET'/GET,POST,PUT,DELETE/, $post = NULL, $oauth_token = NULL, $headers = NULL, $use_cookies = false, $ignore_errors = false, &$info = null, $user_pwd = NULL) Exécute une requête HTTP/HTTPS et retourne son résultat sous forme de chaine de caractère. Les arguments $action et $post peuvent être omis, ils peuvent être utilisés dans le cas de requêtes avancées comme un POST. L'argument $oauth_token est utilisé pour les scripts des objets connectés, dans les scripts personnels il peut donc être ommis ou passé à NULL L'argument $headers doit être fourni sous la forme d'un tableau, par exemple : $headers = array("X-Fbx-App-Auth: xxxx"); L'argument $cookies, vous permet d'activer la gestion des cookies pour la 1ère requête et celles qui suivront L'argument $info permet de récupérer un tableau contenant entre autre le code HTTP, le header, ... L'argument $user_pwd permet de passer un éventuel "user:password" dans la requête */
// Fonction d'appel à l'API function sdk_call_API($url,$method,$data,$header) { global $debug,$header_received; $result = httpQuery($url,$method,$data,NULL,$header,false,false,&$header_received,NULL); return $result; } //-----------------------PROGRAMME---------------------------------------------------------- $commands=array('setPlayContent','setAudioMute','setAudioVolume','setSceneSetting','getPowerStatus','setPowerStatus','setActiveApp','terminateApps'); $header_received = array('timeout' => 5); $debug = getArg('debug',false,0); $ip = getArg('ip',false,'192.168.1.108');//IP TV SONY BRAVIA $psk = getArg('psk',false,'Ardeche07');//psk configuré dans le TV SONY BRAVIA $url = 'http://'.$ip.'/sony'; /* $data_array = array('method'=> "setPowerStatus",'id' => 55,'params' => array(array('status' => false)),'version' => "1.0"); $data = sdk_json_encode($data_array); */ $rest=array(); $data=array(); $i=getArg('n',false,1);//$i est le numéro de la fonction a lancer $p1 = getArg('p1',false,NULL);//$p1 est le paramètre n°1 lors d'un envoi de commande $p2 = getArg('p2',false,NULL);//$p2 est le paramètre n°2 lors d'un envoi de commande
//------GET------- $rest[1]='/appControl'; $data[1]='{ "method": "getApplicationList", "id": 60, "params": [], "version": "1.0" }'; $rest[2]='/appControl'; $data[2]='{ "method": "getApplicationStatusList", "id": 55, "params": [], "version": "1.0" }'; $rest[3]='/avContent'; $data[3]='{ "method": "getContentCount", "id": 11, "params": [{"source": "extInput:hdmi"}], "version": "1.0" }'; $rest[4]='/avContent'; $data[4]='{ "method": "getContentCount", "id": 11, "params": [{"source": "extInput:hdmi"}], "version": "1.1" }'; $rest[5]='/avContent'; $data[5]='{ "method": "getContentList", "id": 88, "params": [{ "stIdx": 0, "cnt": 50, "uri": "extInput:hdmi" }], "version": "1.5" }'; $rest[6]='/avContent'; $data[6]='{ "method": "getCurrentExternalInputsStatus", "id": 105, "params": [], "version": "1.0" }'; $rest[7]='/avContent'; $data[7]='{ "method": "getCurrentExternalInputsStatus", "id": 105, "params": [], "version": "1.1" }'; $rest[8]='/system'; $data[8]='{ "method": "getCurrentTime", "id": 51, "params": [], "version": "1.0" }'; $rest[9]='/system'; $data[9]='{ "method": "getCurrentTime", "id": 51, "params": [], "version": "1.1" }'; $rest[10]='/system'; $data[10]='{ "method": "getInterfaceInformation", "id": 33, "params": [], "version": "1.0" }'; $rest[11]='/system'; $data[11]='{ "method": "getLEDIndicatorStatus", "id": 45, "params": [], "version": "1.0" }'; $rest[12]='/system'; $data[12]='{ "method": "getNetworkSettings", "id": 2, "params": [{"netif": "eth0"}], "version": "1.0" }'; $rest[13]='/video'; $data[13]='{ "method": "getPictureQualitySettings", "id": 52, "params": [{ "target": "color" }], "version": "1.0" }'; $rest[14]='/avContent'; $data[14]='{ "method": "getPlayingContentInfo", "id": 103, "params": [], "version": "1.0" }'; $rest[15]='/system'; $data[15]='{ "method": "getPowerSavingMode", "id": 51, "params": [], "version": "1.0" }'; $rest[16]='/system'; $data[16]='{ "method": "getPowerStatus", "id": 50, "params": [], "version": "1.0" }'; $rest[17]='/encryption'; $data[17]='{ "method": "getPublicKey", "id": 1, "params": [], "version": "1.0" }'; $rest[18]='/system'; $data[18]='{ "method": "getRemoteControllerInfo", "id": 54, "params": [], "version": "1.0" }'; $rest[19]='/system'; $data[19]='{ "method": "getRemoteDeviceSettings", "id": 44, "params": [{"target": "accessPermission"}], "version": "1.0" }'; $rest[20]='/avContent'; $data[20]='{ "method": "getSchemeList", "id": 1, "params": [], "version": "1.0" }'; $rest[21]='/audio'; $data[21]='{ "method": "getSoundSettings", "id": 73, "params": [{"target": "outputTerminal"}], "version": "1.1" }'; $rest[22]='/avContent'; $data[22]='{ "method": "getSourceList", "id": 1, "params": [{"scheme": "extInput"}], "version": "1.0" }'; $rest[23]='/audio'; $data[23]='{ "method": "getSpeakerSettings", "id": 67, "params": [{"target": "tvPosition"}], "version": "1.0" }'; $rest[24]='/guide'; $data[24]='{ "method": "getSupportedApiInfo", "id": 5, "params": [{"services": [ "system", "avContent" ]}], "version": "1.0" }'; $rest[25]='/system'; $data[25]='{ "method": "getSystemInformation", "id": 33, "params": [], "version": "1.0" }'; $rest[26]='/system'; $data[26]='{ "method": "getSystemSupportedFunction", "id": 55, "params": [], "version": "1.0" }'; $rest[27]='/appControl'; $data[27]='{ "method": "getTextForm", "id": 60, "params": [{"encKey": "AAAAB3NzaC1yc2EAAAABIwAAAQEA3p6TmGYDRtnnmzckD5leg7lHIUY9cuV6vFvacew1uZ7Bmx2MF9a7SqmtiLDkLS3P9y9eobRjuWriSfgmqDPRFRU2mdwAmRm2aIvYa6WkzvnrfUhGR+SCT/Z62j7V9ps6Mt5HB8mFQj3494p4StTPVS1nFqvEUazEx13EJnJyHsdYqsV6UJV169e43oLSSccb3lr8BzeMUnGEfY+NKlAxDpEycr5jJYyTkLfrbX0lyAPs+vLwLRYhm+h2qJYAZUwknus4vD7aki4G69S+gnENClglh/e9ut9Q5BrtxiBQCEikn9V9rlnVkbp1eEUf89XFiHRWMVrRAINtJyQFvvoPOQ=="}], "version": "1.1" }'; $rest[28]='/audio'; $data[28]='{ "method": "getVolumeInformation", "id": 33, "params": [], "version": "1.0" }'; $rest[29]='/appControl'; $data[29]='{ "method": "getWebAppStatus", "id": 1, "params": [], "version": "1.0" }'; $rest[30]='/system'; $data[30]='{ "method": "getWolMode", "id": 50, "params": [], "version": "1.0" }';
//------POST-------
$rest[101]='/appControl'; $data[101]='{ "method": "setActiveApp", "id": 601, "params": [{ "uri": "localapp://webappruntime?'.$p1.'"//url=http%3A%2F%2Fexample.com%2F,manifest=manifest_url,auid=application_unique_id }], "version": "1.0" }'; $rest[102]='/audio'; $data[102]='{ "method": "setAudioMute", "id": 601, "params": [{"status": '.$p1.'}], //true ou false "version": "1.0" }'; $rest[103]='/audio'; $data[103]='{ "method": "setAudioVolume", "id": 601, "params": [{ "volume": "'.$p1.'", //integer [0-100] "target": "speaker" }], "version": "1.0" }'; $rest[104]='/audio'; $data[104]='{ "method": "setAudioVolume", "id": 98, "params": [{ "volume": "'.$p1.'", //integer [0-100] "ui": "on", "target": "speaker" }], "version": "1.2" }'; $rest[105]='/system'; $data[105]='{ "method": "setLEDIndicatorStatus", "id": 53, "params": [{ "mode": "'.$p1.'",//"Demo","AutoBrightnessAdjust","Dark","SimpleResponse","Off" "status": "'.$p2.'"//true ou false }], "version": "1.1" }'; $rest[106]='/system'; $data[106]='{ "method": "setLanguage", "id": 55, "params": [{"language": "'.$p1.'"}],//eng ou fra ou ...selon ISO-639 alpha-3 "version": "1.0" }'; $rest[107]='/video'; $data[107]='{ "method": "setPictureQualitySettings", "id": 12, "params": [{"settings": [{ "target": "'.$p1.'",//"color","brightness","contrast","sharpness","pictureMode","lightSensor","colorSpace","colorTemperature","autoPictureMode","hdrMode","autoLocalDimming","xtendedDynamicRange" "value": "'.$p2.'"//integer [0-100] }]}], "version": "1.0" }'; $rest[108]='/avContent'; $data[108]='{ "method": "setPlayContent", "id": 101, "params": [{"uri": "extInput:'.$p1.'"}],//"hdmi?port=1","hdmi?port=2","hdmi?port=3","hdmi?port=4","scart?port=1","composite?port=1","widi?port=1" "version": "1.0" }'; $rest[109]='/system'; $data[109]='{ "method": "setPowerSavingMode", "id": 52, "params": [{"mode": "'.$p1.'"}],//"off","low","high","pictureOff" "version": "1.0" }'; $rest[110]='/system'; $data[110]='{ "method": "setPowerStatus", "id": 55, "params": [{"status": '.$p1.'}],//true ou false "version": "1.0" }'; $rest[111]='/videoScreen'; $data[111]='{ "method": "setSceneSetting", "id": 40, "params": [{"value": "'.$p1.'"}],//"auto","auto24pSync","general" "version": "1.0" }'; $rest[112]='/audio'; $data[112]='{ "method": "setSoundSettings", "id": 5, "params": [{"settings": [{ "value": "'.$p1.'",//"speaker","speaker_hdmi","hdmi","audioSystem" "target": "outputTerminal" }]}], "version": "1.1" }'; $rest[113]='/audio'; $data[113]='{ "method": "setSpeakerSettings", "id": 62, "params": [{"settings": [{ "target": "'.$p1.'",//"tvPosition","subwooferLevel","subwooferFreq","subwooferPhase","subwooferPower" "value": "'.$p2.'"//if "target" is "tvPosition"=>"tableTop","wallMount". if "target" is "subwooferLevel"=>[0,24]. If "target" is "subwooferFreq"=>[0,30]. If "target" is "subwooferPhase"=>"normal","reverse". If "target" is "subwooferPower"=>"on","off" }]}], "version": "1.0" }'; $rest[114]='/appControl'; $data[114]='{ "method": "setTextForm", "id": 601, "params": ["'.$p1.'"],//text en UFT8 "version": "1.0" }'; $rest[115]='/appControl'; $data[115]='{ "method": "setTextForm", "id": 601, "params": [{ "encKey": "AAAAB3NzaC1yc2EAAAABIwAAAQEA3p6TmGYDRtnnmzckD5leg7lHIUY9cuV6vFvacew1uZ7Bmx2MF9a7SqmtiLDkLS3P9y9eobRjuWriSfgmqDPRFRU2mdwAmRm2aIvYa6WkzvnrfUhGR+SCT/Z62j7V9ps6Mt5HB8mFQj3494p4StTPVS1nFqvEUazEx13EJnJyHsdYqsV6UJV169e43oLSSccb3lr8BzeMUnGEfY+NKlAxDpEycr5jJYyTkLfrbX0lyAPs+vLwLRYhm+h2qJYAZUwknus4vD7aki4G69S+gnENClglh/e9ut9Q5BrtxiBQCEikn9V9rlnVkbp1eEUf89XFiHRWMVrRAINtJyQFvvoPOQ==", "text": "'.$p1.'"//text en UFT8 }], "version": "1.1" }'; $rest[116]='/system'; $data[116]='{ "method": "setWolMode", "id": 55, "params": [{"enabled": '.$p1.'}],//true ou false "version": "1.0" }'; $rest[117]='/appControl'; $data[117]='{ "method": "terminateApps", "id": 55, "params": [], "version": "1.0" }';
$url .=$rest[$i]; if ($debug == 1) {echo "url complète:"; var_dump($url); echo "fonction n°:".$i."\n"; echo "paramètre 1:".$p1."\n"; echo "paramètre 2:".$p2."\n"; echo "data"; var_dump($data[$i]); echo "header"; var_dump($header); } $method = 'POST';
$content_length = strlen($data[$i]); $header = array('X-Auth-PSK: '.$psk,'Content-Type: application/json; charset=UTF-8','Content-Length: '.$content_length);
$result = sdk_call_API($url,$method,$data[$i],$header);
if ($debug == 1) {echo "résultat requête:".$result."\n"; echo "header_received"; var_dump($header_received);}
$response = sdk_json_decode($result, true);
if (array_key_exists('error',$response)) {$error = $response['error'][0]; $description = $response['error'][1]; echo "Code erreur:".$error."\n"; echo "Description:".$description; } else { if ($response) {echo sdk_json2xml($result);} else {echo $result;} }
?>
Ensuite il faudra développer le fichier JSON, adapter les script et créer l'imagerie... Si cela intéresse quelqu'un, merci de vous proposer comme testeur/testeuse. Bonne soirée, dommarion
|