Skip to content

Enertiv updates #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ node-hue-api/node_modules/*
ExpressIntro/node_modules/*
SerialToBrowser/node_modules/*
EnertivClient/cred.js
EnertivClient/node_modules/*
EnertivClient/requestVersion/cred.js
EnertivClient/requestVersion/node_modules/*
Binary file added EnertivClient/.DS_Store
Binary file not shown.
95 changes: 95 additions & 0 deletions EnertivClient/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
var express = require('express');
var client = require('./client.js');
var app = express();
var c = new client;


// Examples using Enertiv node module with Express
// See https://api.enertiv.com/docs/#!/api/ for available endpoints

// Start our server
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('app listening at http://%s:%s', host, port);
});


/*
*
* Important Info
*
* Must hit '/login' first to authenticate
* - Follow setup in 'client.js'
* Most API endpoints use client info (client/location uuid)
* So, use '/client' to save that info for later use
*
*/


// A couple boxes to push our API responses into
var clientData = {};
var topData = [];
var energyData = [];


// Hit this first to authenticate
app.get('/login', function (req,res){
var data = c.login(function (data){
console.log("YOU ARE AUTHENTICATED");
res.end();
});
});


// Returns client information, saves the important bits for later
app.get('/client', function (req,res){
var apiClient = c.apiCall('/api/client/', function (apiClient){
var clientInfo = JSON.parse(apiClient);
clientData.uuid = clientInfo[0].id;
clientData.locationID = clientInfo[0].locations[0];
res.send(clientData);
});

});



// Example showing top 10 energy consumers
// Uses the client uuid we saved from '/client'
app.get('/getTop', function (req,res){
// Set the date range you want to examine
var start = encodeURIComponent('2016-01-01 00:00:00Z');
var end = encodeURIComponent('2016-02-01 00:00:00Z');
var client = clientData.uuid;

var top10 = c.apiCall('/api/client/'+client+'/top_consumers/?count=10&fromTime='+start+'&toTime='+end, function (top10){
console.log(JSON.parse(top10));
topData.push(JSON.parse(top10));
res.end();
});
});


// Get energy and cost data by location
// Uses the location uuid we saved from '/client'
app.get('/getEnergy', function (req,res){
var location = clientData.locationID;
var startdate = encodeURIComponent('2015-01-01 00:00:00Z');
var enddate = encodeURIComponent('2015-09-01 00:00:00Z')
var interval = 'month';

var energy = c.apiCall('/api/location/'+location+'/data/?fromTime='+startdate+'&toTime='+enddate+'&interval='+interval+'&cost=true', function (energy){
console.log(JSON.parse(energy));
energyData.push(JSON.parse(energy));
res.end()
});
});

app.get('/top10', function (req,res){
res.send(topData);
});

app.get('/energy', function (req,res){
res.send(energyData);
});
111 changes: 59 additions & 52 deletions EnertivClient/client.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*
HTTP/HTTPS request example to Enertiv
HTTP/HTTPS request example to Enertiv
Does basic Oauth2 exchange to get access token
then makes first API request with the token.
This is a quick-and-dirty solution, and should be improved upon.

You'll need to add a file, cred.js, to the same directory as this file,
with the following in it:

Expand All @@ -15,27 +14,17 @@
}
module.exports = creds;

to call it from the command line:
node client.js

TODO:
* Simplify with async.js or q.js or an oauth2 library

created 25 Feb 2015
updated 20 Nov 2015
by Tom Igoe
created 25 Feb 2015 by Tom Igoe
updated 06 Jan 2016 by John Farrell
*/


var https = require('https');
var querystring = require('querystring');
var cred = require('./cred.js');

var clientData;

/*
set up the options for the login.
fill in your client_id and client_secret here:
*/
// Bring in login information from our cred file
var loginData = querystring.stringify({
'client_id': cred.clientID,
'client_secret': cred.clientSecret,
Expand All @@ -58,51 +47,69 @@ var options = {
}
};

/*
the callback function to be run when the response comes in.
this callback assumes a chunked response, with several 'data'
events and one final 'end' response.
*/
function saveToken(response) {
var result = ''; // string to hold the response
// Module
var enertiv = function(){
var self = this;
var callback;
var accessToken;
// as each chunk comes in, add it to the result string:
response.on('data', function (data) {
result += data;
});

// when the final chunk comes in, print it out:
response.on('end', function () {
result = JSON.parse(result);
accessToken = result.access_token;
getClientInfo('/api/client/', accessToken);
});
}

// Authenticate with Enertiv API
// Login runs callback to saveToken
this.login = function(cb){
callback = cb;
var request = https.request(options, self.saveToken); // start it
request.write(loginData); // add body of POST request
request.end();
};

function getClientInfo(path, token) {
options.path = path;
options.method = 'GET';
options.headers = {
'Authorization': 'Bearer ' + token
}
request = https.get(options, function (response) { // make the API call
var result = '';
// as each chunk comes in, add it to the result string:
// Parse response and save auth token
// Pass that token to further API calls
this.saveToken = function(response){
var result = ''; // String to hold the response
// As each chunk comes in, add it to the result string:
response.on('data', function (data) {
result += data;
});

// when the final chunk comes in, print it out:
// When the final chunk comes in, grab the access token
// Then run our callback function
response.on('end', function () {
result = JSON.parse(result);
clientData = result;
console.log(clientData);
accessToken = result.access_token;
callback();
});
});
};

// Generic function for API calls using access token
this.apiCall = function (path, cb){

callback = cb;
options.method = 'GET'; // Change to a GET request
options.path = path; // Set our path to the argument
options.headers = { // Change authorization header to include our token
'Authorization': 'Bearer ' + accessToken
}

// Make the API call
request = https.get(options, function (response) {
var result = '';
// As each chunk comes in, add it to the result string:
response.on('data', function (data) {
result += data;
});

// When the final chunk comes in, print it out
// Then run our callback function
response.on('end', function () {
callback(result);
});
});
};
}

// make the login request:
var request = https.request(options, saveToken); // start it
request.write(loginData); // add body of POST request
request.end(); // end it
module.exports = enertiv;





16 changes: 16 additions & 0 deletions EnertivClient/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "enertiv_api",
"version": "1.0.0",
"description": "example usage for the Enertiv API",
"main": "client.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.14.1",
"express": "^4.13.3",
"path": "^0.12.7"
}
}
Binary file added EnertivClient/requestVersion/.DS_Store
Binary file not shown.
91 changes: 91 additions & 0 deletions EnertivClient/requestVersion/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var express = require('express');
var client = require('./client.js');
var app = express();
var c = new client;


// Examples using Enertiv node module with Express
// See https://api.enertiv.com/docs/#!/api/ for available endpoints

// Start our server
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('app listening at http://%s:%s', host, port);
});


/*
*
* Important Info
*
* Must hit '/login' first to authenticate
* - Follow setup in 'client.js'
* Most API endpoints use client info (client/location uuid)
* So, use '/client' to save that info for later use
*
*/


// A couple boxes to push our API responses into
var clientData = {};
var topData = [];
var energyData = [];


// Hit this first to authenticate
app.get('/login', function (req,res){
var data = c.login(function (data){
console.log("YOU ARE AUTHENTICATED");
res.send("AUTHENTICATED");
});
});


// Returns client information, saves the important bits for later
app.get('/client', function (req,res){
var apiClient = c.apiCall('/api/client/', function (apiClient){
var clientInfo = JSON.parse(apiClient);
clientData.uuid = clientInfo[0].id;
clientData.locationID = clientInfo[0].locations[0];
console.log(clientInfo[0]);
res.send(clientData);
});

});



// Example showing top 10 energy consumers
// Uses the client uuid we saved from '/client'
app.get('/top10', function (req,res){
// Set the date range you want to examine
var start = encodeURIComponent('2015-01-01 00:00:00Z');
var end = encodeURIComponent('2015-09-01 00:00:00Z');
var client = clientData.uuid;

var top10 = c.apiCall('/api/client/'+client+'/top_consumers/?count=10&fromTime='+start+'&toTime='+end, function (top10){
console.log(JSON.parse(top10));
topData.push(JSON.parse(top10));
res.send(topData);
});
});


// Get energy and cost data by location
// Uses the location uuid we saved from '/client'
app.get('/energy', function (req,res){
var location = clientData.locationID;
var startdate = encodeURIComponent('2015-01-01 00:00:00Z');
var enddate = encodeURIComponent('2015-09-01 00:00:00Z')
var interval = 'month';

var energy = c.apiCall('/api/location/'+location+'/data/?fromTime='+startdate+'&toTime='+enddate+'&interval='+interval+'&cost=true', function (energy){
console.log(JSON.parse(energy));
energyData.push(JSON.parse(energy));
res.send(energyData);
});
});



Loading