Skip to content

Commit

Permalink
1.2.0: Get data through a socket notification (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael5r authored Dec 10, 2018
1 parent 2c09e35 commit 761a7c4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 38 deletions.
24 changes: 12 additions & 12 deletions getToken.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

clear
echo -e "\033[00;37m"
echo " __ __ __ __ __ __ _ _ _ "
echo "| \/ | \/ | \/ | | \ | | | | "
echo "| \ / | \ / | \ / |______| \| | ___ ___| |_ "
echo "| |\/| | |\/| | |\/| |______| . \` |/ _ \/ __| __|"
echo "| | | | | | | | | | | |\ | __/\__ \ |_ "
echo "|_| |_|_| |_|_| |_| |_| \_|\___||___/\__|"
echo " _ _ _ "
echo "| \ | | | | "
echo "| \| | ___ ___| |_ "
echo "| . \` |/ _ \/ __| __|"
echo "| |\ | __/\__ \ |_ "
echo "|_| \_|\___||___/\__|"
echo -e "\e[0m"
echo ""
echo "This script will walk you through getting a Nest access token which you will need to use this module."
Expand All @@ -26,12 +26,12 @@ echo -n "Press any button to continue ..."
read -n 1 -s
clear
echo -e "\033[00;37m"
echo " __ __ __ __ __ __ _ _ _ "
echo "| \/ | \/ | \/ | | \ | | | | "
echo "| \ / | \ / | \ / |______| \| | ___ ___| |_ "
echo "| |\/| | |\/| | |\/| |______| . \` |/ _ \/ __| __|"
echo "| | | | | | | | | | | |\ | __/\__ \ |_ "
echo "|_| |_|_| |_|_| |_| |_| \_|\___||___/\__|"
echo " _ _ _ "
echo "| \ | | | | "
echo "| \| | ___ ___| |_ "
echo "| . \` |/ _ \/ __| __|"
echo "| |\ | __/\__ \ |_ "
echo "|_| \_|\___||___/\__|"
echo -e "\e[0m"
echo ""
echo "After setting up your account, it is now time to create a new OAuth client which is what you'll need to get your access token."
Expand Down
47 changes: 21 additions & 26 deletions mmm-nest-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Module.register('mmm-nest-status', {
updateInterval: 60 * 1000,
animationSpeed: 2 * 1000,
initialLoadDelay: 0,
version: '1.1.0'
version: '1.2.0'
},

getScripts: function() {
Expand Down Expand Up @@ -59,6 +59,8 @@ Module.register('mmm-nest-status', {
this.errMsg = '';

this.loaded = false;


this.scheduleUpdate(this.config.initialLoadDelay);

this.thermostats = [];
Expand Down Expand Up @@ -492,39 +494,32 @@ Module.register('mmm-nest-status', {

},

updateNests: function() {
// use the Nest API to get the data
getData: function() {

if (this.config.token === '') {
this.errMsg = 'Please run getToken.sh and add your Nest API token to the MagicMirror config.js file.';
this.updateDom(this.config.animationSpeed);
} else {

var token = this.config.token;
var url = 'https://developer-api.nest.com/?auth=' + token;
var self = this;
var xhr = new XMLHttpRequest();

xhr.addEventListener('readystatechange', function () {
if (this.readyState === 4) {
if (this.status === 200) {
if (this.response == '{}') {
self.errMsg = 'Token works, but no data was received.<br>Make sure you are using the master account for your Nest.';
self.updateDom(self.config.animationSpeed);
} else {
self.processNestData(JSON.parse(this.response));
}
} else {
console.log('Nest API Error: ' + this.status);
}
}
this.sendSocketNotification('MMM_NEST_STATUS_GET', {
token: this.config.token
});
}

},

xhr.open('GET', url, true);
xhr.send();
socketNotificationReceived: function(notification, payload) {

self.scheduleUpdate(self.updateInterval);
if (notification === 'MMM_NEST_STATUS_DATA') {
// broadcast Nest data update
this.sendNotification('MMM_NEST_STATUS_UPDATE', payload);
// process the data
this.processNestData(payload);
this.scheduleUpdate(this.updateInterval);
} else if (notification === 'MMM_NEST_STATUS_DATA_ERROR') {
this.errMsg = 'Nest API Error: ' + payload;
this.updateDom(this.config.animationSpeed);
}

},

processNestData: function(data) {
Expand Down Expand Up @@ -688,7 +683,7 @@ Module.register('mmm-nest-status', {

var self = this;
setTimeout(function() {
self.updateNests();
self.getData();
}, nextLoad);
}

Expand Down
36 changes: 36 additions & 0 deletions node_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var NodeHelper = require('node_helper');
var request = require('request');

module.exports = NodeHelper.create({

start: function() {
console.log('Starting node_helper for module [' + this.name + ']');
},

socketNotificationReceived: function(notification, payload) {

if (notification === 'MMM_NEST_STATUS_GET') {

var token = payload.token;
var url = 'https://developer-api.nest.com/?auth=' + token;
var self = this;

request(url, {method: 'GET'}, function(err, res, body) {

if ((err) || (res.statusCode !== 200)) {
self.sendSocketNotification('MMM_NEST_STATUS_DATA_ERROR', err);
} else {
if (body === {}) {
self.sendSocketNotification('MMM_NEST_STATUS_DATA_ERROR', 'Token works, but no data was received.<br>Make sure you are using the master account for your Nest.');
} else {
var data = JSON.parse(body);
self.sendSocketNotification('MMM_NEST_STATUS_DATA', data);
}
}

});

}
}

});

0 comments on commit 761a7c4

Please sign in to comment.