function get_nest_data() { $nest = new Nest(); $info = $nest->getDeviceInfo(); $data = array('heating' => $info->current_state->heat == 1 ? 1 : 0, 'timestamp' => $info->network->last_connection, 'target_temp' => sprintf("%.02f", preg_match("/away/", $info->current_state->mode) ? $info->target->temperature[0] : $info->target->temperature), 'current_temp' => sprintf("%.02f", $info->current_state->temperature), 'humidity' => $info->current_state->humidity); return $data; }
public function pushNestData($db) { define('USERNAME', $this->getApplication()->getSilexApplication()['config']['nest']['username']); define('PASSWORD', $this->getApplication()->getSilexApplication()['config']['nest']['password']); $nest = new \Nest(); $info = $nest->getDeviceInfo(); $temperature = $info->current_state->temperature; $humidity = $info->current_state->humidity; $state = $info->target->mode; $db->insert("temperature", ['fields' => array('value' => $temperature)]); $db->insert("humidity", ['fields' => array('value' => $humidity)]); $db->insert("state", ['fields' => array('value' => $state)]); }
public static function sendArmy($x, $y, $ants) { $xCord = Nest::getNestCords($_SESSION['nestid']); $diff = abs($x - $xCord); $defenderId = User::getUserByMapCords($x, $y)[0][0]; $db = mysqli_connect("localhost", "root", "1234", "ant_rpg"); mysqli_query($db, "INSERT INTO attack (attacker_id,defender_id,cordinateDiffrence) VALUES ('" . $_SESSION['id'] . "','" . $defenderId . "','" . $diff . "')"); $request = mysqli_query($db, "SELECT * FROM attack Where attacker_id = " . $_SESSION['id'] . " order by idattack desc Limit 1"); $battleId = $request->fetch_all()[0][0]; for ($i = 0; $i < 2; $i++) { if (empty($ants[$i])) { continue; } mysqli_query($db, "INSERT INTO troops_for_battle (ant_id,quantity,battleid) VALUES ('" . ($i + 1) . "','" . $ants[$i] . "','" . $battleId . "')"); Ants::updateAnts($i + 1, $ants[$i]); } }
public function login() { if (isset($_POST['username'], $_POST['password'])) { try { htmlspecialchars($user = $_POST['username']); htmlspecialchars($pass = $_POST['password']); if (User::tryLog($user, $pass)) { $_SESSION['id'] = User::getUser($user)[0]; $nests = Nest::getUserNests($_SESSION['id']); if (empty($_SESSION['nestid'])) { $_SESSION['nestid'] = $nests[0]; } header("Location: /MVCProject/home/home"); exit; } else { throw new \Exception('Wrong Username or Password'); } } catch (Exception $e) { echo $e->getMessage(); } } $model = new UserRegisterViewModel(); return new View($model); }
<?php require_once 'nest.class.php'; // Your Nest username and password. $username = '******'; $password = '******'; // The timezone you're in. // See http://php.net/manual/en/timezones.php for the possible values. date_default_timezone_set('America/Montreal'); // Here's how to use this class: $nest = new Nest($username, $password); echo "Location information:\n"; $locations = $nest->getUserLocations(); jlog($locations); echo "----------\n\n"; echo "Devices list (thermostats):\n"; $devices_serials = $nest->getDevices(); jlog($devices_serials); echo "----------\n\n"; echo "Devices list (Nest Protect):\n"; $protects_serials = $nest->getDevices(DEVICE_TYPE_PROTECT); jlog($protects_serials); echo "----------\n\n"; echo "Device information:\n"; $infos = $nest->getDeviceInfo($devices_serials[0]); jlog($infos); echo "----------\n\n"; echo "Current temperature:\n"; printf("%.02f degrees %s\n", $infos->current_state->temperature, $infos->scale); echo "----------\n\n"; echo "Setting target temperature...\n";
#!/usr/bin/php <?php //////////////////////////// // Max automatic target humidity levels $maxhumidity = 40; // Your NEST credentials define('USERNAME', ''); define('PASSWORD', ''); // Don't change this unless you're very very sure, or you may cause damage to your home! $safelimit = 60; ///////////////////////////////////////// //// Nothing to change below here ////// require_once 'nest.class.php'; $nest = new Nest(); function setHumidity($nest, $humidity) { $target = intval($humidity); if ($target > $GLOBALS['safelimit']) { echo "Error: Requested target exceeds safety limit.\n"; return 1; } elseif ($target < 0) { $target = 0; } echo "Setting humidity target to " . $target . "%\n"; $success = $nest->setHumidity($target); return $success; } if (count($argv) == 1) { $infos = $nest->getDeviceInfo(); echo "Current humidity level: " . $infos->current_state->humidity . "%\n"; } elseif ($argv[1] == "auto") {
/** * _SET (static) * * Get nested value from array or object without having to check each level * * @static * * @param array|object $var The array or object to fetch data from * @param array|string $path String or array of array/object keys to the nested value * @param mixed $value The new value to use * * @return array|object The original var, but with updated data */ public static function _set() { $args = func_get_args(); $tmp = new Nest(array_shift($args)); call_user_func_array([$tmp, "set"], $args); return $tmp->data(); }
// The Nest-Extended Configuration require_once __DIR__ . '/../config.php'; // The Nest API Class file require_once __DIR__ . '/../libs/nest/nest.class.php'; if (defined('STDIN')) { $datatype = $argv[1]; } else { $datatype = $_GET['datatype']; } //Connect to the Database $con = new mysqli($hostname, $username, $password, $dbname); if ($con->connect_error) { trigger_error('Database connection failed: ' . $con->connect_error, E_USER_ERROR); } //Create a new Nest Object $nest = new Nest(); //Used to return current location, away status, and outside weather $locations = $nest->getUserLocations(); //Postal Code formatting if (in_array(date_default_timezone_get(), $us_timezones)) { $postal_code = $locations[0]->postal_code; } else { $postal_code = substr($locations[0]->postal_code, 0, -3) . " " . substr($locations[0]->postal_code, -3); } //Get current Nest data from server and save into an array. This should be set to a 5 minute cron job. I use curl to execute the php. if ($datatype === 'current') { //Used to return current inside temperature, current inside humidity, current mode, target temperature, time to target temperature, current heat state, current ac state $infos = $nest->getDeviceInfo(); print_r($infos); //print variable to browser for debug // for some reason Aux heat is returned as Celcius. Need to convert to F (if desired). Otherwise comment this out and log aux_threshold.
// The Nest-Extended Configuration require_once __DIR__ . '/../config.php'; // The Nest API Class file require_once __DIR__ . '/../libs/nest/nest.class.php'; if (defined('STDIN')) { $datatype = $argv[1]; } else { $datatype = $_GET['datatype']; } //Connect to the Database $con = new mysqli($hostname, $username, $password, $dbname); if ($con->connect_error) { trigger_error('Database connection failed: ' . $con->connect_error, E_USER_ERROR); } //Create a new Nest Object $nest = new Nest(); //Used to return current location, away status, and outside weather $locations = $nest->getUserLocations(); //Postal Code formatting if (in_array(date_default_timezone_get(), $us_timezones)) { $postal_code = $locations[0]->postal_code; } else { $postal_code = substr($locations[0]->postal_code, 0, -3) . " " . substr($locations[0]->postal_code, -3); } //Get current Nest data (5 minute cron) if ($datatype === 'current') { //Capture date and time of script execution $runTime = $date = date('Y-m-d H:i:s'); //Used to get current outdoor weather $weather_json = file_get_contents('http://api.wunderground.com/api/' . $wu_api_key . '/conditions/pws:' . $use_pws . '/q/' . $wu_loc . '.json'); $weather = json_decode($weather_json);
$user_lat = $geocord_array->results['0']->geometry->location->lat; $user_long = $geocord_array->results['0']->geometry->location->lng; $timestamp = time(); // Google maps api URL // This is required to get the timezone offset from the current user's location $google_json = "https://maps.googleapis.com/maps/api/timezone/json?location=" . $user_lat . "," . $user_long . "×tamp=" . $timestamp; $google_time = json_decode(file_get_contents($google_json)); $dst_offset = $google_time->dstOffset; $raw_offset = $google_time->rawOffset; $timestamp_offset = ($dst_offset + $raw_offset) / 60 / 60; ////////////// $update_statement = $db_connect->prepare("\n\t\t\t\tUPDATE users\n\t\t\t\tSET nest_username = :nest_username,\n\t\t\t\t nest_password = :nest_password,\n\t\t\t\t user_location = :user_location,\t\t\t\t \n\t\t\t\t timestamp_offset = :timestamp_offset\n\t\t\t\tWHERE user_id = :user_id\n\t\t\t"); $update_statement->execute(array('nest_username' => $nest_username, 'nest_password' => $nest_password_encrypt, 'user_location' => $nest_location, 'timestamp_offset' => $timestamp_offset, 'user_id' => $user_id)); define('USERNAME', $nest_username); define('PASSWORD', $nest_password); $nest = new Nest(); $nest_devices = $nest->getDevices(); foreach ($nest_devices as $device) { $device_info = $nest->getDeviceInfo($device); $insert_statement = $db_connect->prepare("\n\t\t REPLACE INTO devices\n\t\t SET device_serial_number = :device_serial_number,\n\t\t user_id = :user_id,\n\t\t device_location = :device_location,\n\t\t device_name = :device_name\n\t\t "); $insert_statement->execute(array('device_serial_number' => $device, 'user_id' => $user_id, 'device_location' => $nest_location, 'device_name' => $device_info->name)); } $user_location = $nest_location; $message = isset($message) ? $message : ''; $success_message = $message . "Updated user preferences"; $tpl_success = new Template("../includes/templates/success.tpl"); $tpl_success->set("success_text", $success_message); echo $tpl_success->fetch(); } $tpl_head = new Template("../includes/templates/head.tpl"); $tpl_nav = new Template("../includes/templates/nav-user.tpl");
$target = calculateHumidity($inside, MAX_DEWPOINT); $target = floor($target / 5.0) * 5; //round to nearest 5 $dp = calculateDewpoint($inside, $target); // If the outside temperature is lower than the current inside dewpoint, reduce max humidity while ($dp - $outside > MAX_DEWPOINT_DELTA && $target > MIN_HUMIDITY) { $target -= 5; $dp = calculateDewpoint($inside, $target); $dpo = $dp - $outside; } // Respect MIN/MAX settings $target = min($target, MAX_HUMIDITY); $target = max($target, MIN_HUMIDITY); return $target; } $nest = new Nest(); try { // Retrieve data $locations = $nest->getUserLocations(); $thermostat = $nest->getDeviceInfo(THERMOSTAT_SERIAL); $insideTemp = $thermostat->current_state->temperature; $outsideTemp = $locations[0]->outside_temperature; $insideHumidity = $thermostat->current_state->humidity; $targetHumidityCurrent = $thermostat->target->humidity; // Determine heat/cool target temperatures $mode = $thermostat->target->mode; $cool = $heat = $thermostat->target->temperature; if ($mode == 'range') { $heat = $heat[0]; $cool = $cool[0]; } else {
<?php require 'inc/config.php'; require 'nest-api-master/nest.class.php'; define('USERNAME', $config['nest_user']); define('PASSWORD', $config['nest_pass']); date_default_timezone_set($config['local_tz']); $nest = new Nest(); $status = $nest->getStatus(); print_r($status); $infos = $nest->getDeviceInfo(); print_r($infos); stuff_we_care_about($infos); function stuff_we_care_about($info) { echo "Heating : "; printf("%s\n", $info->current_state->heat == 1 ? 1 : 0); echo "Timestamp : "; printf("%s\n", $info->network->last_connection); echo "Target temperature : "; if (preg_match("/away/", $info->current_state->mode)) { printf("%.02f\n", $info->target->temperature[0]); } else { printf("%.02f\n", $info->target->temperature); } echo "Current temperature : "; printf("%.02f\n", $info->current_state->temperature); echo "Current humidity : "; printf("%d\n", $info->current_state->humidity); } function c_to_f($c)
$geocord_json = "http://maps.googleapis.com/maps/api/geocode/json?address=" . urlencode($user_location) . "&sensor=false"; $geocord_array = json_decode(file_get_contents($geocord_json)); $user_lat = $geocord_array->results['0']->geometry->location->lat; $user_long = $geocord_array->results['0']->geometry->location->lng; // Google maps api URL // This is required to get the timezone offset from the current user's location $google_json = "https://maps.googleapis.com/maps/api/timezone/json?location=" . $user_lat . "," . $user_long . "×tamp=" . $timestamp; $google_time = json_decode(file_get_contents($google_json)); $dst_offset = $google_time->dstOffset; $raw_offset = $google_time->rawOffset; $timestamp_offset = ( $dst_offset + $raw_offset ) / 60 / 60; //$local_time = $timestamp + $dst_offset + $raw_offset; */ $nest = new Nest($nest_username, $nest_password_decrypt); $nest_devices = $nest->getDevices(); foreach ($nest_devices as $device) { // Gather information from the Nest class object for storage $infos = $nest->getDeviceInfo($device); $energy = $nest->getEnergyLatest($device); $weather_nest = $nest->getWeather($user_location); // Gather the device information for storage $device_serial_number = $infos->serial_number; $nest_location = $infos->where; $device_name = $infos->name; $battery_level = $infos->current_state->battery_level; // Outside weather pulled from the nest class $outside_humidity = $weather_nest->outside_humidity; $outside_temperature = $weather_nest->outside_temperature; // Inside weather pulled from the nest class
<?php // The Nest-Extended Configuration require_once '../config.php'; // The Nest API Class file require_once '../libs/nest/nest.class.php'; if (defined('STDIN')) { $datatype = $argv[1]; } else { $datatype = $_GET['datatype']; } //Connect to the Database $con = mysql_connect($hostname, $username, $password) or die('Unable to connect to database! Please try again later.'); mysql_select_db($dbname); //Create a new Nest Object $nest = new Nest(); //Used to return current location and away status $locations = $nest->getUserLocations(); //Postal Code formatting if (in_array(date_default_timezone_get(), $us_timezones)) { $postal_code = $locations[0]->postal_code; } else { $postal_code = substr($locations[0]->postal_code, 0, -3) . " " . substr($locations[0]->postal_code, -3); } //Get current Nest data (5 minute cron) if ($datatype === 'current') { //Capture date and time of script execution $runTime = $date = date('Y-m-d H:i:s'); //Used to get current outdoor weather $weather_json = file_get_contents('http://api.wunderground.com/api/' . $wu_api_key . '/conditions/q/' . rawurlencode($postal_code) . '.json'); $weather = json_decode($weather_json);