#!/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];
Exemplo n.º 2
0
 /**
  * @brief drop webhook notifications for the current user
  */
 public function dropWebhook()
 {
     parent::dropWebhook("app_camera");
 }
Exemplo n.º 3
0
    echo "graph_scale no\n";
    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));
Exemplo n.º 4
0
<meta charset='utf-8'>
<link rel="apple-touch-icon" href="icone/meteo.png" >
<link rel="apple-touch-startup-image" href="icone/startup.png">
</head>
<body>
<?php 
if (isset($_POST["username"])) {
    $username = $_POST["username"];
    $password = $_POST["password"];
} else {
    if (isset($_GET["username"])) {
        $username = $_GET["username"];
        $password = $_GET["password"];
    }
}
$client = new NAApiClient(array("client_id" => $client_id, "client_secret" => $client_secret, "username" => $username, "password" => $password, "scope" => NAScopes::SCOPE_READ_STATION));
try {
    $tokens = $client->getAccessToken();
} catch (NAClientException $ex) {
    $_SESSION['msg'] = 'Identifiant ou mot de passe incorrect';
    echo "<script>top.location.href = 'indexLogin.php';</script>";
}
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$_SESSION['client'] = $client;
$_SESSION['timeToken'] = time();
$_SESSION['refresh_token'] = $tokens['refresh_token'];
$_SESSION['expires_in'] = $tokens['expires_in'];
if (isset($_POST['saveCookie'])) {
    $_SESSION['saveCookie'] = 1;
}
<?php

/*
Authentication to Netatmo Server with the authorization grant
This script has to be hosted by your web server in order to make it work
*/
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 
#!/usr/bin/php
<?php 
/*
Authentication to Netatmo Server with the user credentials grant
*/
require_once 'NAApiClient.php';
require_once 'Config.php';
$client = new NAApiClient(array("client_id" => $client_id, "client_secret" => $client_secret, "username" => $test_username, "password" => $test_password));
$helper = new NAApiHelper();
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");
$devicelist = $helper->SimplifyDeviceList($devicelist);
//print_r($devicelist);
echo "OK\n";
echo "-----------------\n";
echo "- Last Measures -\n";
Exemplo n.º 7
0
/**
 * 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;
}
Exemplo n.º 8
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);
}
<?php

/*
Authentication to Netatmo Server with the user credentials grant
Show how to use the Partner API
In this example we will :
 * - retrieve the list of devices your application has access to (partnersdevice)
 * - for each device we will retrieve device information such as module_id (thermostat id in that case), battery status etc ...
 * - for each couple device/thermostat we will retrieve current state
 * - finally for each device we will set device in frost-guard mode 
*/
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));
/*Retrieve user access_token*/
/* This user is the user created to access your partner application */
try {
    $tokens = $client->getAccessToken();
} catch (NAClientException $ex) {
    echo "An error happend while trying to retrieve your tokens\n";
    echo $ex->getMessage() . "\n";
    exit(-1);
}
try {
    //Retrieve all your partner devices
    $devicelist = $client->api("partnerdevices", "POST");
    foreach ($devicelist as $device_id) {
        //retrieve device information from api
        $devices = $client->api("devicelist", "POST", array("app_type" => "app_thermostat", "device_id" => $device_id));
        if (isset($devices["devices"][0])) {