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"; // Note: setting temperatures will use the units you set on the device. I'm using celsius on my device, so I'm using celsius here.
$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"); $tpl_foot = new Template("../includes/templates/foot.tpl");