/**
 *
 *  Demonstration for the unofficial_nest library
 *  logs in, reads status, constantly, for ever. :)
 *
 */

'use strict';
var util = require('util'),
    nest = require('../index.js');  // normally would be 'unofficial-nest-api'


function trimQuotes(s) {
    if (!s || s.length === 0) {
        return '';
    }
    var c = s.charAt(0);
    var start = (c === '\'' || c === '"') ? 1 : 0;
    var end = s.length;
    c = s.charAt(end - 1);
    end -= (c === '\'' || c === '"') ? 1 : 0;
    return s.substring(start, end);
}

if (process.argv.length < 4) {
    console.log('Usage: ' + process.argv[1] + ' USERNAME PASSWORD [OPTIONS]');
    console.log('');
    console.log('USERNAME and PASSWORD should be enclosed in quotes.');
    console.log('');

    process.exit(1); // failure to communicate with user app requirements. :)
}

var username = process.argv[2];
var password = process.argv[3];

if (username && password) {
    username = trimQuotes(username);
    password = trimQuotes(password);
    nest.login(username, password, function (err/*, status*/) {
        if (err) {
            console.log(err.message);
            process.exit(1);
            return;
        }
        console.log('Logged in');
        nest.fetchStatus(function (err, data) {
            for (var deviceId in data.device) {
                if (data.device.hasOwnProperty(deviceId)) {
                    var device = data.shared[deviceId];
                    console.log(util.format('%s [%s], Current temperature = %d F target=%d',
                        device.name, deviceId,
                        nest.ctof(device.current_temperature),
                        nest.ctof(device.target_temperature)));
                }
            }
            var ids = nest.getDeviceIds();
            console.log(ids[0 ]+': set target temp to 26C°');
            nest.setTargetTemperatureType(ids[0], 'heat');
            nest.setTemperature(ids[0], 26);
            //nest.setTemperature(26);
            //nest.setFanModeAuto();
            //subscribe();
            //nest.setAway();
            //nest.setHome();
        });
    });
}

function subscribeDone(deviceId, data, type) {
    // data if set, is also stored here: nest.lastStatus.shared[thermostatID]
    if (deviceId) {
        console.log('Device: ' + deviceId + ' type: ' + type);
        console.log(JSON.stringify(data));
    } else {
        console.log('No data');

    }
    setTimeout(subscribe, 2000);
}

function subscribe() {
    nest.subscribe(subscribeDone, ['shared', 'energy_latest']);
}

@@ -1558,10 +1558,10 @@
 = %25d
- F
+%C2%B0C
  tar
@@ -1566,16 +1566,18 @@
 arget=%25d
+%C2%B0C
 ',%0D%0A    
@@ -1640,34 +1640,24 @@
             
-nest.ctof(
 device.curre
@@ -1670,17 +1670,16 @@
 perature
-)
 ,%0D%0A     
@@ -1701,18 +1701,8 @@
     
-nest.ctof(
 devi
@@ -1724,17 +1724,16 @@
 rature))
-)
 ;%0D%0A     
@@ -1860,17 +1860,19 @@
 emp to 2
-6
+5.5
 C%C2%B0');%0D%0A 
@@ -1971,17 +1971,266 @@
 ds%5B0%5D, 2
-6
+5.5, function(error, res) %7B%0D%0A                if (error) %7B%0D%0A                    console.log('ERROR: '+JSON.stringify(error));%0D%0A                    return 2;%0D%0A                %7D%0D%0A                console.log('Reply: '+JSON.stringify(res));%0D%0A            %7D
 );%0D%0A    

update example