/** * Return array with Netatmo informations * * @return mixed an array with all weather station values or an exception object if error happens */ function get_netatmo($scale_device = '1day', $scale_module = '1day') { global $NAconfig, $NAusername, $NApwd; if (!isset($NAusername) || !isset($NApwd) || !isset($NAconfig) || !isset($NAconfig['client_id']) || !isset($NAconfig['client_secret'])) { die("Oups! The widget is not configured! Take a look on this page : <a href='https://github.com/potsky/Netatmo#configuration'>https://github.com/potsky/Netatmo#configuration</a>"); } if (function_exists('apc_exists')) { if (apc_exists(NETATMO_CACHE_KEY)) { $return = @unserialize(apc_fetch(NETATMO_CACHE_KEY)); if (is_array($return)) { if (count($return) > 0) { return $return; } } } } $scale_device = in_array($scale_device, explode(',', NETATMO_DEVICE_SCALES)) ? $scale_device : '1day'; $scale_module = in_array($scale_module, explode(',', NETATMO_DEVICE_SCALES)) ? $scale_module : $scale_device; $return = array('scales' => array('device' => $scale_device, 'module' => $scale_module)); /* Netatmo job */ $client = new NAApiClient($NAconfig); $client->setVariable("username", $NAusername); $client->setVariable("password", $NApwd); try { $tokens = $client->getAccessToken(); $refresh_token = $tokens["refresh_token"]; $access_token = $tokens["access_token"]; } catch (NAClientException $ex) { return $ex; } $userinfo = array(); try { $userinfo = $client->api("getuser"); } catch (NAClientException $ex) { return $ex; } $return['user'] = $userinfo; $device_id = ''; try { $deviceList = $client->api("devicelist"); if (is_array($deviceList["devices"])) { foreach ($deviceList["devices"] as $device) { $device_id = $device["_id"]; $return[$device_id]['dashboard'] = $device['dashboard_data']; $params = array("scale" => "max", "type" => remove_unknown_values(NETATMO_DEVICE_TYPE_MAIN), "date_end" => "last", "device_id" => $device_id); $res = $client->api("getmeasure", 'GET', $params); if (@defined('DEBUG') && DEBUG === 1) { echo '<pre>'; var_dump(NETATMO_DEVICE_TYPE_MAIN, $res); echo '<hr/>'; } if (isset($res[0]) && isset($res[0]["beg_time"])) { $time = $res[0]["beg_time"]; $vals = explode(',', NETATMO_DEVICE_TYPE_MAIN); foreach ($res[0]["value"][0] as $key => $value) { $return[$device_id]['results'][$vals[$key]] = $value; } $return[$device_id]['name'] = $device["module_name"]; $return[$device_id]['station'] = $device["station_name"]; $return[$device_id]['type'] = $device["type"]; $return[$device_id]['time'] = $res[0]["beg_time"]; } $params = array("scale" => $scale_device, "type" => remove_unknown_values(NETATMO_DEVICE_TYPE_MISC), "date_end" => "last", "device_id" => $device_id); $res = $client->api("getmeasure", 'GET', $params); if (@defined('DEBUG') && DEBUG === 1) { echo '<pre>'; var_dump(NETATMO_DEVICE_TYPE_MISC, $res); echo '<hr/>'; } if (isset($res[0]) && isset($res[0]["beg_time"])) { $vals = explode(',', NETATMO_DEVICE_TYPE_MISC); foreach ($res[0]["value"][0] as $key => $value) { $return[$device_id]['misc'][$vals[$key]] = $value; } } } } } catch (NAClientException $ex) { return $ex; } if ($device_id != '') { if (is_array($deviceList["modules"])) { foreach ($deviceList["modules"] as $module) { try { $module_id = $module["_id"]; $device_id = $module["main_device"]; $return[$device_id]['m'][$module_id]['dashboard'] = $module['dashboard_data']; $params = array("scale" => "max", "type" => remove_unknown_values(NETATMO_MODULE_TYPE_MAIN), "date_end" => "last", "device_id" => $device_id, "module_id" => $module_id); $res = $client->api("getmeasure", 'GET', $params); if (@defined('DEBUG') && DEBUG === 1) { echo '<pre>'; var_dump(NETATMO_MODULE_TYPE_MAIN, $res); echo '<hr/>'; } if (isset($res[0]) && isset($res[0]["beg_time"])) { $vals = explode(',', NETATMO_MODULE_TYPE_MAIN); foreach ($res[0]["value"][0] as $key => $value) { $return[$device_id]['m'][$module_id]['results'][$vals[$key]] = $value; } $return[$device_id]['m'][$module_id]['name'] = $module["module_name"]; $return[$device_id]['m'][$module_id]['time'] = $res[0]["beg_time"]; } $params = array("scale" => $scale_module, "type" => remove_unknown_values(NETATMO_MODULE_TYPE_MISC), "date_end" => "last", "device_id" => $device_id, "module_id" => $module_id); $res = $client->api("getmeasure", 'GET', $params); if (@defined('DEBUG') && DEBUG === 1) { echo '<pre>'; var_dump(NETATMO_MODULE_TYPE_MISC, $res); echo '<hr/>'; } if (isset($res[0]) && isset($res[0]["beg_time"])) { $vals = explode(',', NETATMO_MODULE_TYPE_MISC); foreach ($res[0]["value"][0] as $key => $value) { $return[$device_id]['m'][$module_id]['misc'][$vals[$key]] = $value; } } } catch (NAClientException $ex) { return $ex; } } } } if (function_exists('apc_exists')) { apc_store(NETATMO_CACHE_KEY, serialize($return), NETATMO_CACHE_TTL); } return $return; }
#!/usr/bin/php <?php /* Authentication to Netatmo Server with the user credentials grant */ require_once 'NAApiClient.php'; require_once 'Config.php'; $scope = NAScopes::SCOPE_READ_THERM . " " . NAScopes::SCOPE_WRITE_THERM; $client = new NAApiClient(array("client_id" => $client_id, "client_secret" => $client_secret, "username" => $test_username, "password" => $test_password, "scope" => $scope)); try { $tokens = $client->getAccessToken(); } catch (NAClientException $ex) { echo "An error happend while trying to retrieve your tokens\n"; exit(-1); } // Retrieve User Info : $user = $client->api("getuser", "POST"); echo "-------------\n"; echo "- User Info -\n"; echo "-------------\n"; //print_r($user); echo "OK\n"; echo "---------------\n"; echo "- Device List -\n"; echo "---------------\n"; $devicelist = $client->api("devicelist", "POST", array("app_type" => "app_thermostat")); echo "OK\n"; if (isset($devicelist["devices"]) && isset($devicelist["devices"][0])) { $device = $devicelist["devices"][0]; if (isset($device["modules"]) && isset($device["modules"][0])) { $module = $device["modules"][0];
function initClient() { global $client_id, $client_secret, $test_username, $test_password, $timezone; //date_default_timezone_set($timezone); if (isset($_SESSION['expires_in'])) { checkToken(); } // seule action effectuer chaque fois if (!isset($_SESSION['LogMsg'])) { $date = date("d/m H:i:s", time()); $server = $_SERVER['SERVER_NAME']; $_SESSION['LogMsg'] = "Log start :{$date}<br>Serveur:{$server}<br>"; } if (isset($_GET["code"]) && !isset($_SESSION['client'])) { if (isset($_GET["error"])) { if ($_GET["error"] == "access_denied") { logMsg("You refused the application's access"); } else { logMsg("Unknown error"); } logout(); } if (isset($_SESSION['state']) && $_SESSION['state'] != $_GET['state']) { logMsg("The state does not match"); logout(); } $code = $_GET["code"]; $my_url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']; $token_url = "https://api.netatmo.net/oauth2/token"; $postdata = http_build_query(array('grant_type' => "authorization_code", 'client_id' => $client_id, 'client_secret' => $client_secret, 'code' => $code, 'redirect_uri' => $my_url, 'scope' => "read_station read_thermostat write_thermostat")); $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded;charset=UTF-8', 'content' => $postdata)); $context = stream_context_create($opts); $response = file_get_contents($token_url, false, $context); $params = null; $params = json_decode($response, true); $access_token = $params['access_token']; $refresh_token = $params['refresh_token']; $expires_in = $params['expires_in']; $expire_in = $params['expire_in']; $_SESSION['refresh_token'] = $refresh_token; $_SESSION['timeToken'] = time(); $_SESSION['expires_in'] = $expires_in; // celui que j'utilise $_SESSION['expire_in'] = $expire_in; try { $client = new NAApiClient(array("access_token" => $access_token, "refresh_token" => $refresh_token)); } catch (NAClientException $ex) { $_SESSION['ex'] = $ex; logMsg('NAClientException:client from token'); alert("NAClientException:client from token"); logout(); } $_SESSION['client'] = $client; logMsg('client from token'); } if (!isset($_SESSION['client']) && empty($test_username) || empty($test_password)) { if (isset($_SESSION['username']) && isset($_SESSION['password'])) { $test_username = $_SESSION['username']; //login with indexLogin.php $test_password = $_SESSION['password']; } else { if (!isset($_SESSION['client'])) { //alert("no password"); logMsg("No password in Config.php"); echo " <script>top.location.href='indexLogin.php' </script> "; } } } if (isset($_SESSION['client'])) { $client = $_SESSION['client']; } else { $client = new NAApiClient(array("client_id" => $client_id, "client_secret" => $client_secret, "username" => $test_username, "password" => $test_password, "scope" => NAScopes::SCOPE_READ_STATION)); try { $tokens = $client->getAccessToken(); } catch (NAClientException $ex) { if (!isset($_SESSION['state'])) { logMsg("User:{$test_username}\n\t\t\t\t\t<br>ou mot de passe:{$test_password}\n\t\t\t\t\t<br> ou id:{$client_id}\n\t\t\t\t\t<br> ou secret:{$client_secret} incorrect"); } $_SESSION['ex'] = $ex; logMsg('NAClientException:client from password'); alert("wrong password"); logout(); } $_SESSION['timeToken'] = time(); $_SESSION['refresh_token'] = $tokens['refresh_token']; $_SESSION['expires_in'] = $tokens['expires_in']; $_SESSION['client'] = $client; logMsg("client from password"); } // if(!isset($_SESSION['mydevices'])) if (1) { $helper = new NAApiHelper($client); try { //$devicelist = $client->api("devicelist", "POST"); $devicelist = $helper->simplifyDeviceList(); } catch (NAClientException $ex) { $_SESSION['ex'] = $ex; logMsg("NAClientException:devicelist"); alert("NAClientException:devicelist"); logout(); } //$devicelist = $helper->SimplifyDeviceList($devicelist); //$_SESSION['devicelist'] = $devicelist; $numStations = count($devicelist["devices"]); if (!isset($_SESSION['mydevices'])) { $mydevices = createDevicelist($devicelist); $_SESSION['mydevices'] = $mydevices; init($numStations); } getDashBoard($devicelist); } $user = $client->api("getuser", "POST"); $Temperature_unit = 1 - $user['administrative']['unit']; $_SESSION['Temperature_unit'] = $Temperature_unit; $_SESSION['lang'] = $user['administrative']['lang']; $timezone = $_SESSION['mydevices'][0]['timezone']; date_default_timezone_set($timezone); }