function refreshToken() { global $client_id, $client_secret, $timezone; if (!isset($_SESSION['refresh_token'])) { logMsg('NO refreshtoken'); logout(); } date_default_timezone_set($timezone); $token_url = "https://api.netatmo.net/oauth2/token"; $postdata = http_build_query(array('grant_type' => "refresh_token", 'refresh_token' => $_SESSION['refresh_token'], 'client_id' => $client_id, 'client_secret' => $client_secret)); $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)); if (!$response) { return -1; } $params = null; $params = json_decode($response, true); $access_token = $params['access_token']; $refresh_token = $params['refresh_token']; $expires_in = $params['expires_in']; $_SESSION['refresh_token'] = $refresh_token; $_SESSION['timeToken'] = time(); $_SESSION['expires_in'] = $expires_in; $client = new NAApiClient(array("access_token" => $access_token, "refresh_token" => $refresh_token)); $client->setVariable("client_id", $client_id); $client->setVariable("client_secret", $client_secret); if (isset($_SESSION['code'])) { $client->setVariable("code", $_SESSION['code']); } try { $tokens = $client->getAccessToken(); } catch (NAClientException $ex) { $_SESSION['ex'] = $ex; logMsg('NAClientException:refreshtoken'); logout(); } logMsg("Refresh token success"); $_SESSION['client'] = $client; return 1; }
echo "graph_category netatmo\n"; die; } if (key_exists("version", $_GET)) { echo "munin node on " . gethostname() . " version: 1.0.0 (munin-netatmo)\n"; die; } if (key_exists("quit", $_GET)) { die; } if (filemtime('/tmp/netatmo_cache') < time() - 900) { unlink('/tmp/netatmo_cache'); require_once "Netatmo-API/NAApiClient.php"; require_once "Netatmo-API/Config.php"; $client = new NAApiClient($config); $client->setVariable("username", $test_username); $client->setVariable("password", $test_password); $helper = new NAApiHelper(); try { $tokens = $client->getAccessToken(); } catch (NAClientException $ex) { echo "An error happend while trying to retrieve your tokens\n"; die; } // Retrieve User Info : $user = $client->api("getuser", "POST"); $devicelist = $client->api("devicelist", "POST"); $devicelist = $helper->SimplifyDeviceList($devicelist); $last_mesures = $helper->GetLastMeasures($client, $devicelist); file_put_contents('/tmp/netatmo_cache', serialize($last_mesures)); } else {
/** * 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; }