Skip to content
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

Adds method to hit the solar 'history' API endpoint. #306

Open
wants to merge 16 commits into
base: master
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
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
"name": "Mark Seminatore",
"url": "https://github.com/mseminatore/TeslaJS"
},
"name": "teslajs",
"version": "4.9.8",
"description": "Full-featured Tesla REST API NodeJS package",
"dependencies": {
"promise": "^8.0.3",
"request": "^2.88.2",
"ws": "^7.2.1"
},
"bugs": {
"url": "https://github.com/mseminatore/TeslaJS/issues"
},
Expand Down
51 changes: 37 additions & 14 deletions src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,7 @@ exports.login = function login(credentials, callback) {
redirect_uri: url.protocol + '//' + url.host + url.pathname
}
});
}).then(function (result) {
return req({
method: 'POST',
url: 'https://owner-api.teslamotors.com/oauth/token',
headers: {
Authorization: 'Bearer ' + result.body.access_token
},
json: true,
body: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
client_id: _0x2dc0[0]
}
});
}).then(function (result) {
}).then(bearerForAccessToken).then(function (result) {
callback(null, result.response, result.body);
}).catch(function (error) {
callback(error);
Expand Down Expand Up @@ -208,6 +195,24 @@ function mfaVerify(transactionId, host, referer, mfaPassCode, mfaDeviceName) {
});
}

function bearerForAccessToken(bearerResult) {
return req({
method: 'POST',
url: 'https://owner-api.teslamotors.com/oauth/token',
headers: {
authorization: 'bearer ' + bearerResult.body.access_token
},
json: true,
body: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
client_id: _0x2dc0[0]
}
}).then(function (result) {
result.body.refresh_token = bearerResult.body.refresh_token;
return result;
});
}

function generateCodeVerifier() {
// Tesla might use something more sophisticated, but in my experience it's a 112-char alphanumeric string so let's just do that
var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
Expand All @@ -228,6 +233,24 @@ function generateCodeChallenge(verifier) {
.replace(/\//g, '_');
}

exports.refresh = function refresh(refresh_token, callback) {
req({
method: 'POST',
url: 'https://auth.tesla.com/oauth2/v3/token',
json: true,
body: {
"grant_type": "refresh_token",
"client_id": "ownerapi",
"refresh_token": refresh_token,
"scope": "openid email offline_access"
}
}).then(bearerForAccessToken).then(function (result) {
callback(null, result.response, result.body);
}).catch(function (error) {
callback(error);
});
}

function req(parameters) {
return new Promise(function (resolve, reject) {
request(parameters, function (error, response, body) {
Expand Down
87 changes: 74 additions & 13 deletions teslajs.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,22 +404,17 @@ exports.refreshToken = function refreshToken(refresh_token, callback) {
return;
}

var req = {
method: 'POST',
url: portalBaseURI + '/oauth/token',
body: {
"grant_type": "refresh_token",
"refresh_token": refresh_token
}
};

log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(req));

request(req, function (error, response, body) {

require('./src/auth').refresh(refresh_token, function (error, response, body) {

log(API_RESPONSE_LEVEL, "\nResponse: " + body);

callback(error, { error: error, response: response, body: JSON.stringify(body), authToken: body.access_token, refreshToken: body.refresh_token });
if (error) {
callback(error)
}
else {
callback(error, { error: error, response: response, body: JSON.stringify(body), authToken: body.access_token, refreshToken: body.refresh_token });
}

log(API_RETURN_LEVEL, "TeslaJS.refreshToken() completed.");
});
Expand Down Expand Up @@ -2040,6 +2035,72 @@ exports.solarStatus = function solarStatus(options, callback) {
exports.solarStatusAsync = Promise.denodeify(exports.solarStatus);



/**
* Return historical data for solar installation
* @function solarHistory
* @param {optionsType} options - options object
* @param {string} period - time period
* @param {string} kind - kind (i.e. 'energy')
* @param {nodeBack} callback - Node-style callback
* @returns {solarStatus} solarHistory JSON data
*/
exports.solarHistory = function solarHistory(options, period, kind, callback) {
log(API_CALL_LEVEL, "TeslaJS.solarHistory()");

// Default Values
callback = callback || function(err, solarHistory) { }; /* do nothing! */
period = period || "day";
kind = kind || "energy";

var req = {
method: "GET",
url: portalBaseURI + "/api/1/energy_sites/" + options.siteId + "/calendar_history?kind="+kind+"&period="+period,
headers: {
Authorization: "Bearer " + options.authToken,
"Content-Type": "application/json; charset=utf-8"
}
};

log(API_REQUEST_LEVEL, "\nRequest: " + JSON.stringify(req));

request(req, function(error, response, body) {
if (error) {
log(API_ERR_LEVEL, error);
return callback(error, null);
}

if (response.statusCode != 200) {
return callback(response.statusMessage, null);
}

log(API_BODY_LEVEL, "\nBody: " + JSON.stringify(body));
log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response));

try {
body = body.response;

callback(null, body);
} catch (e) {
log(API_ERR_LEVEL, "Error parsing solarHistory response");
callback(e, null);
}

log(API_RETURN_LEVEL, "\nGET request: " + "/solarHistory" + " completed.");
});
};

/**
* Return historical data for solar installation
* @function solarHistoryAsync
* @param {optionsType} options - options object
* @param {string} period - time period
* @param {string} kind - kind (i.e. 'energy')
* @param {nodeBack} callback - Node-style callback
* @returns {Promise} solarHistory JSON data
*/
exports.solarHistoryAsync = Promise.denodeify(exports.solarHistory);

/*
//
// [Alpha impl] Not yet supported
Expand Down