} 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 { echo "# Using cache\n"; $last_mesures = unserialize(file_get_contents('/tmp/netatmo_cache'));
function getDevicelist() { $client = $_SESSION['client']; $helper = new NAApiHelper($client); try { //$devicelist = $client->api("devicelist", "POST"); $devicelist = $helper->simplifyDeviceList(); } catch (NAClientException $ex) { $_SESSION['ex'] = $ex; logMsg("NAClientException:devicelist"); logout(); } //$devicelist = $helper->SimplifyDeviceList($devicelist); return $devicelist; }
*/ require_once 'NAApiClient.php'; require_once 'Config.php'; $client = new NAApiClient(array("client_id" => $client_id, "client_secret" => $client_secret, "scope" => NAScopes::SCOPE_READ_STATION)); //Test if code is provided in get parameters (that means user has already accepted the app and has been redirected here) if (isset($_GET["code"])) { try { // Get the token for later usage.(you can store $tokens["refresh_token"] for retrieving a new access_token next time) $tokens = $client->getAccessToken(); } catch (NAClientException $ex) { echo "An error happend while trying to retrieve your tokens\n"; echo "Reason : " . $ex->getMessage() . "\n"; die; } try { $helper = new NAApiHelper($client); $user = $helper->api("getuser", "POST"); $devicelist = $helper->simplifyDeviceList(); $mesures = $helper->getLastMeasures($client, $devicelist); ?> <html><body><pre><code> <?php echo json_format(json_encode($mesures)); ?> </code></pre></body></html> <?php } catch (NAClientException $ex) { echo "An error happend while trying to retrieve your last measures\n"; echo $ex->getMessage() . "\n"; } } else {
#!/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_STATION; $client = new NAApiClient(array("client_id" => $client_id, "client_secret" => $client_secret, "username" => $test_username, "password" => $test_password, "scope" => $scope)); $helper = new NAApiHelper($client); try { $tokens = $client->getAccessToken(); } catch (NAClientException $ex) { echo "An error happend while trying to retrieve your tokens\n"; exit(-1); } // Retrieve User Info : $user = $helper->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 = $helper->simplifyDeviceList(); echo "OK\n"; echo "-----------------\n"; echo "- Last Measures -\n"; echo "-----------------\n";