Hello,
Voici le XML, les infos que je n'arrive pas à recuperer sont celle du Sensor, je mets à la suite la conf des peripheriques qui sont toutes identiques seul change le chemin XPATH /GARDENA/AMBIENTTEMPERATURE /GARDENA/LIGHT /GARDENA/SOILTEMPERATURE /GARDENA/HUMIDITY
- Code : Tout sélectionner
<?php $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>"; //*********************************************************************************************************************** // V2.04 : Gardena / copyright Influman 2020 /** * API officiel Husqvarna - Gardena Smart System API - https://developer.1689.cloud/ */ $action = getArg("action", true); $server = getArg("server", true); // VAR1 $clientid = getArg("clientid", true); $value = getArg("value", false); $location = getArg("location", false,1); $appareil = getArg("device", false,1); $valve = getArg("valve", false,1); $timer = getArg("timer", false); $debug = getArg("debug", false, 0); $periph_id = getArg('eedomus_controller_module_id'); if ($action == '' ) { die(); } $xml .= "<GARDENA>"; // SESSIONS // Ouverture de sessions if ($action != "polling") { $token_expire = loadVariable("GARDENA_EXPIRE"); if (time() > $token_expire) { $tab_param = explode(",",$server); $login = $tab_param[0]; $pass = utf8_decode($tab_param[1]); $credentials = "grant_type=password&client_id=".$clientid."&username=".$login."&password=".$pass; $url = "https://api.authentication.husqvarnagroup.dev/v1/oauth2/token"; $response = httpQuery($url, 'POST', $credentials, NULL, NULL, true, false); $return_api = sdk_json_decode($response); if (array_key_exists("access_token",$return_api)) { $token = $return_api['access_token']; $user_id = $return_api['user_id']; $provider = $return_api['provider']; saveVariable("GARDENA_TOKEN", $token); saveVariable("GARDENA_PROVIDER", $provider); $token_expire = $return_api['expires_in'] + time(); saveVariable("GARDENA_EXPIRE", $token_expire); } else { $xml .= "<STATUS>Erreur Authentification: ".$return_api['error_description']."</STATUS>"; $xml .= "</GARDENA>"; sdk_header('text/xml'); echo $xml; die(); } } else { $token = loadVariable("GARDENA_TOKEN"); $provider = loadVariable("GARDENA_PROVIDER"); } $headers = array( 'Authorization: Bearer '.$token, 'Authorization-Provider: '.$provider, 'X-Api-Key: '.$clientid); } // GLOBAL STATUS // Lecture des donnees API if ($action == "getstatus") { $status = ""; $tab_devices = array(); $url = "https://api.smart.gardena.dev/v1/locations"; $response = httpQuery($url, 'GET', NULL, NULL, $headers, true, false); $return_api = sdk_json_decode($response); $i = 0; $j = 0; $k = 0; $l = 0; $tab_location = array(); $tab_devices = array(); $tab_services = array(); $tab_common = array(); $tab_valve_set = array(); $tab_valves = array(); $tab_sensor = array(); $tab_mower = array(); $tab_power_socket = array(); foreach($return_api['data'] as $location) { $i++; $l = 0; $tab_location[$i]['id'] = $location['id']; $tab_location[$i]['name'] = $location['attributes']['name']; // recherche des devices de cette location $url = "https://api.smart.gardena.dev/v1/locations/".$location['id']; $response_device = httpQuery($url, 'GET', NULL, NULL, $headers, true, false); $return_device = sdk_json_decode(utf8_encode($response_device)); // récupération des devices et services foreach($return_device['included'] as $device) { if ($device['type'] == "DEVICE") { $j++; $l++; $tab_devices[$j]['location_id'] = $location['id']; $tab_devices[$j]['id'] = $device['id']; $tab_devices[$j]['num'] = $l; $tab_devices[$j]['is_valve'] = false; $tab_devices[$j]['is_sensor'] = false; $tab_devices[$j]['is_mower'] = false; $tab_devices[$j]['is_power_socket'] = false; $nb_valve = 0; foreach($device['relationships']['services']['data'] as $service) { $k++; $tab_services[$k]['device_id'] = $device['id']; $tab_services[$k]['id'] = $service['id']; $tab_services[$k]['type'] = $service['type']; if ($service['type'] == 'VALVE') { $nb_valve++; $tab_devices[$j]['is_valve'] = true; $tab_devices[$j]['nbvalve'] = $nb_valve; } if ($service['type'] == 'SENSOR') { $tab_devices[$j]['is_sensor'] = true; } if ($service['type'] == 'MOWER') { $tab_devices[$j]['is_mower'] = true; } if ($service['type'] == 'POWER_SOCKET') { $tab_devices[$j]['is_power_socket'] = true; } } // fin boucle services du device } if ($device['type'] == "COMMON") { $tab_common[$device['id']]['name'] = $device['attributes']['name']['value']; $tab_common[$device['id']]['batterylevel'] = $device['attributes']['batteryLevel']['value']; $tab_common[$device['id']]['batterystate'] = $device['attributes']['batteryState']['value']; $tab_common[$device['id']]['rflinklevel'] = $device['attributes']['rfLinkLevel']['value']; $tab_common[$device['id']]['rflinkstate'] = $device['attributes']['rfLinkState']['value']; $tab_common[$device['id']]['modeltype'] = $device['attributes']['modelType']['value']; } if ($device['type'] == "VALVE_SET") { $tab_valve_set[$device['id']]['state'] = $device['attributes']['state']['value']; } if ($device['type'] == "VALVE") { $tab_valves[$device['id']]['state'] = $device['attributes']['state']['value']; $tab_valves[$device['id']]['name'] = $device['attributes']['name']['value']; $tab_valves[$device['id']]['activity'] = $device['attributes']['activity']['value']; if (strpos($device['id'], ":") !== false) { $tab_valves[$device['id']]['num'] = substr($device['id'],-1); } else { $tab_valves[$device['id']]['num'] = 1; } } if ($device['type'] == "SENSOR") { $tab_sensor[$device['id']]['soilHumidity'] = $device['attributes']['soilHumidity']['value']; $tab_sensor[$device['id']]['soilTemperature'] = $device['attributes']['soilTemperature']['value']; $tab_sensor[$device['id']]['ambientTemperature'] = $device['attributes']['ambientTemperature']['value']; $tab_sensor[$device['id']]['lightIntensity'] = $device['attributes']['lightIntensity']['value']; } if ($device['type'] == "MOWER") { $tab_mower[$device['id']]['state'] = $device['attributes']['state']['value']; $tab_mower[$device['id']]['activity'] = $device['attributes']['activity']['value']; $tab_mower[$device['id']]['operatingHours'] = $device['attributes']['operatingHours']['value']; } if ($device['type'] == "POWER_SOCKET") { $tab_power_socket[$device['id']]['state'] = $device['attributes']['state']['value']; $tab_power_socket[$device['id']]['activity'] = $device['attributes']['activity']['value']; $tab_power_socket[$device['id']]['duration'] = $device['attributes']['duration']['value']; } } // fin boucle devices de la location } // fin boucle Location // alimentation du XML $i = 0; foreach($tab_location as $location) { $i++; $k = 0; $l = 0; $xml .= "<TOKEN>".$token."</TOKEN>"; $xml .= "<LOCATION_".$i.">"; $xml .= "<ID>".$location['id']."</ID>"; $xml .= "<NAME>".$location['name']."</NAME>"; $xml .= "<DEVICES>"; $nbdevices = 0; $nbvalves = 0; foreach($tab_devices as $device) { if ($device['location_id'] == $location['id']) { $k++; $nbdevices++; $xml .= "<DEVICE_".$device['num'].">"; $xml .= "<ID>".$device['id']."</ID>"; if ($device['is_valve']) { $xml .= "<NBVALVE>".$device['nbvalve']."</NBVALVE>"; } foreach($tab_services as $service) { if ($service['device_id'] == $device['id']) { //$xml .= "<TYPE>".$service['type']."</TYPE>"; if ($service['type'] == 'COMMON') { $xml .= "<MODEL>".$tab_common[$service['id']]['modeltype']."</MODEL>"; $xml .= "<BATTERYLVL>".$tab_common[$service['id']]['batterylevel']."</BATTERYLVL>"; $xml .= "<BATTERYSTA>".$tab_common[$service['id']]['batterystate']."</BATTERYSTA>"; $xml .= "<RFLINKLVL>".$tab_common[$service['id']]['rflinklevel']."</RFLINKLVL>"; $xml .= "<RFLINKSTA>".$tab_common[$service['id']]['rflinkstate']."</RFLINKSTA>"; } if ($service['type'] == 'VALVE_SET') { $xml .= "<VALVE_SET>".$tab_valve_set[$service['id']]['state']."</VALVE_SET>"; } if ($service['type'] == 'VALVE') { $l++; $xml .= "<VALVE_".$tab_valves[$service['id']]['num'].">"; $xml .= "<NAME>".$tab_valves[$service['id']]['name']."</NAME>"; $xml .= "<STATE>".$tab_valves[$service['id']]['state']."</STATE>"; $xml .= "<ACTIVITY>".$tab_valves[$service['id']]['activity']."</ACTIVITY>"; $xml .= "<SERVICE_ID>".$service['id']."</SERVICE_ID>"; $xml .= "</VALVE_".$tab_valves[$service['id']]['num'].">"; if ($tab_valves[$service['id']]['state'] != "UNAVAILABLE") { if ($l>1) {$status_valve .= " | ";} $status_valve .= "D".$device['num'].".V".$tab_valves[$service['id']]['num'].". ".$tab_valves[$service['id']]['state']; } $nbvalves ++; } if ($service['type'] == 'SENSOR') { $xml .= "<HUMIDITY>".$tab_sensor[$service['id']]['soilHumidity']."</HUMIDITY>"; $xml .= "<SOILTEMPERATURE>".$tab_sensor[$service['id']]['soilTemperature']."</SOILTEMPERATURE>"; $xml .= "<AMBIENTTEMPERATURE>".$tab_sensor[$service['id']]['ambientTemperature']."</AMBIENTTEMPERATURE>"; $xml .= "<LIGHT>".$tab_sensor[$service['id']]['lightIntensity']."</LIGHT>"; } if ($service['type'] == 'MOWER') { $xml .= "<STATE>".$tab_mower[$service['id']]['state']."</STATE>"; $xml .= "<ACTIVITY>".$tab_mower[$service['id']]['activity']."</ACTIVITY>"; $xml .= "<HOURS>".$tab_mower[$service['id']]['operatingHours']."</HOURS>"; } if ($service['type'] == 'POWER_SOCKET') { $xml .= "<STATE>".$tab_power_socket[$service['id']]['state']."</STATE>"; $xml .= "<ACTIVITY>".$tab_power_socket[$service['id']]['activity']."</ACTIVITY>"; $xml .= "<DURATION>".$tab_power_socket[$service['id']]['duration']."</DURATION>"; } } } $nbvalves += $mydevice['nbvalve']; $xml .= "</DEVICE_".$device['num'].">"; } } $xml .= "</DEVICES>"; } $xml .= "</LOCATION_".$i.">"; if ($status_valve == "") { $status = "Gardena Smart System | ".$nbdevices." device"; if ($nbdevices > 1) { $status .= "s";} $status .= " ".$nbvalves. " valve"; if ($nbvalves > 1) { $status .= "s";} } else { $status = $status_valve; } saveVariable("GARDENA_LOCATION",$tab_location); saveVariable("GARDENA_DEVICES",$tab_devices); saveVariable("GARDENA_SERVICES",$tab_services); saveVariable("GARDENA_COMMON",$tab_common); saveVariable("GARDENA_VALVE_SET",$tab_valve_set); saveVariable("GARDENA_VALVE",$tab_valves); saveVariable("GARDENA_SENSOR",$tab_sensor); saveVariable("GARDENA_MOWER",$tab_mower); saveVariable("GARDENA_POWER_SOCKET",$tab_power_socket); $xml .= "<STATUS>".$status."</STATUS>"; $xml .= "</GARDENA>"; sdk_header('text/xml'); echo $xml; die(); } // COMMANDS // Actionner un appareil if (($action == "water" || $action == "mower" || $action == "power") && $location != "" && $appareil != "") { $tab_location = loadVariable("GARDENA_LOCATION"); $tab_devices = loadVariable("GARDENA_DEVICES"); $tab_services = loadVariable("GARDENA_SERVICES"); $tab_valves = loadVariable("GARDENA_VALVE"); $deviceid = sdk_get_deviceid($location, $appareil, $tab_location, $tab_devices); if ($action == "water") { if ($value == "stop") { $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"VALVE_CONTROL\", \"attributes\": {\"command\": \"STOP_UNTIL_NEXT_TASK\"}}}"; } if ($value == "stop" && $valve == 0) { $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"VALVE_SET_CONTROL\", \"attributes\": {\"command\": \"STOP_UNTIL_NEXT_TASK\"}}}"; } if ($value == "pause") { $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"VALVE_CONTROL\", \"attributes\": {\"command\": \"PAUSE\"}}}"; } if ($value == "unpause") { $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"VALVE_CONTROL\", \"attributes\": {\"command\": \"UNPAUSE\"}}}"; } if ($value == "start") { if ($timer == "" || $timer == 0) { $timer = 600; } $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"VALVE_CONTROL\", \"attributes\": {\"command\": \"START_SECONDS_TO_OVERRIDE\", \"seconds\": ".$timer."}}}"; } if ($valve == 0) { $serviceid = sdk_get_serviceid($deviceid, "VALVE_SET", null, $tab_services, $tab_valves); } else { $serviceid = sdk_get_serviceid($deviceid, "VALVE", $valve, $tab_services, $tab_valves); } $url = "https://api.smart.gardena.dev/v1/command/".$serviceid; } if ($action == "power") { if ($value == "stop") { $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"POWER_SOCKET_CONTROL\", \"attributes\": {\"command\": \"STOP_UNTIL_NEXT_TASK\"}}}"; } if ($value == "pause") { $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"POWER_SOCKET_CONTROL\", \"attributes\": {\"command\": \"PAUSE\"}}}"; } if ($value == "unpause") { $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"POWER_SOCKET_CONTROL\", \"attributes\": {\"command\": \"UNPAUSE\"}}}"; } if ($value == "startseconds") { if ($timer == "" || $timer == 0) { $timer = 600; } $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"POWER_SOCKET_CONTROL\", \"attributes\": {\"command\": \"START_SECONDS_TO_OVERRIDE\", \"seconds\": ".$timer."}}}"; } if ($value == "start") { $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"POWER_SOCKET_CONTROL\", \"attributes\": {\"command\": \"START_OVERRIDE\"}}}"; } $serviceid = sdk_get_serviceid($deviceid, "POWER_SOCKET", null, $tab_services, $tab_valves); $url = "https://api.smart.gardena.dev/v1/command/".$serviceid; } if ($action == "mower") { if ($value == "park_timer") { $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"MOWER_CONTROL\", \"attributes\": {\"command\": \"PARK_UNTIL_NEXT_TASK\"}}}"; } if ($value == "park_notice") { $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"MOWER_CONTROL\", \"attributes\": {\"command\": \"PARK_UNTIL_FURTHER_NOTICE\"}}}"; } if ($value == "resume") { $commands = "{\"data\": { \"id\": \"request-1\", \"type\": \"MOWER_CONTROL\", \"attributes\": {\"command\": \"START_DONT_OVERRIDE\"}}}"; } if ($value == "start") { if ($timer == "" || $timer == 0) { $timer = 3600; } $commands = "{\"data\": {\"id\": \"request-1\", \"type\": \"MOWER_CONTROL\", \"attributes\": {\"command\": \"START_SECONDS_TO_OVERRIDE\", \"seconds\": ".$timer."}}}"; } $serviceid = sdk_get_serviceid($deviceid, "MOWER",null, $tab_services, $tab_valves); $url = "https://api.smart.gardena.dev/v1/command/".$serviceid; } $headers = array( "Content-Type: application/vnd.api+json", "Authorization: Bearer ".$token, "Authorization-Provider: ".$provider, "X-Api-Key: ".$clientid); $response = httpQuery($url, "PUT", $commands, NULL, $headers, true, false); die(); } // POLLING // Lecture des données par capteur if ($action == "polling") { $tab_location = loadVariable("GARDENA_LOCATION"); $tab_devices = loadVariable("GARDENA_DEVICES"); $tab_services = loadVariable("GARDENA_SERVICES"); $tab_sensor = loadVariable("GARDENA_SENSOR"); $tab_valves = loadVariable("GARDENA_VALVE"); $tab_common = loadVariable("GARDENA_COMMON"); $tab_valve_set = loadVariable("GARDENA_VALVE_SET"); $tab_mower = loadVariable("GARDENA_MOWER"); $tab_power_socket = loadVariable("GARDENA_POWER_SOCKET"); $deviceid = sdk_get_deviceid($location, $appareil, $tab_location, $tab_devices); $xml .= "<LOCATION_ID>".$tab_location[$location]['id']."</LOCATION_ID>"; $xml .= "<LOCATION_NAME>".$tab_location[$location]['name']."</LOCATION_NAME>"; $xml .= "<DEVICEID>".$deviceid."</DEVICEID>"; foreach($tab_services as $service) { if ($service['device_id'] == $deviceid) { if ($service['type'] == 'COMMON') { $xml .= "<MODEL>".$tab_common[$service['id']]['modeltype']."</MODEL>"; $xml .= "<BATTERYLVL>".$tab_common[$service['id']]['batterylevel']."</BATTERYLVL>"; $xml .= "<BATTERYSTA>".$tab_common[$service['id']]['batterystate']."</BATTERYSTA>"; $xml .= "<RFLINKLVL>".$tab_common[$service['id']]['rflinklevel']."</RFLINKLVL>"; $xml .= "<RFLINKSTA>".$tab_common[$service['id']]['rflinkstate']."</RFLINKSTA>"; } if ($service['type'] == 'VALVE_SET') { $xml .= "<VALVE_SET>".$tab_valve_set[$service['id']]['state']."</VALVE_SET>"; } if ($service['type'] == 'VALVE') { if ($tab_valves[$service['id']]['num'] == $valve) { $xml .= "<VALVE>"; $xml .= "<NAME>".$tab_valves[$service['id']]['name']."</NAME>"; $xml .= "<STATE>".$tab_valves[$service['id']]['state']."</STATE>"; $activity = $tab_valves[$service['id']]['activity']; $xml .= "<ACTIVITY>".$activity."</ACTIVITY>"; if ($activity == "CLOSED") { $activity = "0";} if ($activity == "MANUAL_WATERING") { $activity = "50";} if ($activity == "SCHEDULED_WATERING") { $activity = "100";} $xml .= "<VALUE>".$activity."</VALUE>"; $xml .= "</VALVE>"; } } if ($service['type'] == 'SENSOR') { $xml .= "<HUMIDITY>".$tab_sensor[$service['id']]['soilHumidity']."</HUMIDITY>"; $xml .= "<SOILTEMPERATURE>".$tab_sensor[$service['id']]['soilTemperature']."</SOILTEMPERATURE>"; $xml .= "<AMBIENTTEMPERATURE>".$tab_sensor[$service['id']]['ambientTemperature']."</AMBIENTTEMPERATURE>"; $xml .= "<LIGHT>".$tab_sensor[$service['id']]['lightIntensity']."</LIGHT>"; } if ($service['type'] == 'MOWER') { $xml .= "<STATE>".$tab_mower[$service['id']]['state']."</STATE>"; $xml .= "<ACTIVITY>".$tab_mower[$service['id']]['activity']."</ACTIVITY>"; $xml .= "<HOURS>".$tab_mower[$service['id']]['operatingHours']."</HOURS>"; } if ($service['type'] == 'POWER_SOCKET') { $xml .= "<STATE>".$tab_power_socket[$service['id']]['state']."</STATE>"; $xml .= "<ACTIVITY>".$tab_power_socket[$service['id']]['activity']."</ACTIVITY>"; $xml .= "<DURATION>".$tab_power_socket[$service['id']]['duration']."</DURATION>"; } } } $xml .= "</GARDENA>"; sdk_header('text/xml'); echo $xml; die(); } function sdk_get_deviceid($location, $appareil, $tab_location, $tab_devices) { $locationid = $tab_location[$location]['id']; foreach ($tab_devices as $devices) { if ($devices['location_id'] == $locationid && $devices['num'] == $appareil) { return $devices['id']; } } } function sdk_get_serviceid($deviceid, $type, $valve, $tab_services, $tab_valves) { foreach ($tab_services as $services) { if ($services['device_id'] == $deviceid && $services['type'] == $type) { if ($type == "MOWER" || $type == "VALVE_SET" || $type == "POWER_CONTROL") { return $services['id']; } if ($type == "VALVE") { if ($tab_valves[$services['id']]['num'] == $valve) { return $services['id']; } } } } } ?>
- sensor_Humidity.png (18.05 Kio) Consulté 8541 fois
Merci beaucoup
|