forked from janhenrik/MMM-Tesla
-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_helper.js
152 lines (133 loc) · 3.88 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
/* Magic Mirror
* Module: MMM-Tesla2
*
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
var request = require('request');
var moment = require('moment');
var accessToken = null;
function getToken(config) {
try {
const token = getTokenInternal(config);
return token;
} catch (err) {
console.log(err);
return "abc";
}
}
async function getTokenInternal(config) {
// Set the configuration settings
const credentials = {
client: {
id: config.client_id,
secret: config.client_secret
},
auth: {
tokenHost: 'https://owner-api.teslamotors.com',
tokenPath: '/oauth/token'
},
http: {
headers: { 'User-Agent': 'MMM-Tesla2' }
}
};
const oauth2 = require('simple-oauth2').create(credentials);
const tokenConfig = {
email: config.email,
password: config.password,
grant_type: 'password',
client_secret: config.client_secret,
client_id: config.client_id
};
try {
var tokenObject = await oauth2.ownerPassword.getToken(tokenConfig);
return tokenObject.access_token;
} catch (error) {
console.log('Access Token Error', error.message);
}
}
module.exports = NodeHelper.create({
start: function() {
this.started = false;
this.config = null;
this.drivestate_data = null;
this.charge_data = null;
this.vehicle_data = null;
},
getData: function() {
var self = this;
const vehicleId = this.config.vehicle_id;
const baseUrl = 'https://owner-api.teslamotors.com/api/1/vehicles/' + vehicleId;
function getChargeDate(token) {
request.get({
url: baseUrl + '/data_request/charge_state',
headers: { 'Authorization': "Bearer " + token, 'Content-type': "application/json; charset=utf-8", 'User-Agent': 'MMM-Tesla2' }
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
self.charge_data = body;
self.sendSocketNotification("CHARGE_DATA", body);
}
})
}
function getDriveStateDate(token) {
request.get({
url: baseUrl + '/data_request/drive_state',
headers: { 'Authorization': "Bearer " + token, 'Content-type': "application/json; charset=utf-8", 'User-Agent': 'MMM-Tesla2' }
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
self.drivestate_data = body;
self.sendSocketNotification("DRIVESTATE_DATA", body);
}
})
}
function getVehicleDate(token) {
request.get({
url: baseUrl,
headers: { 'Authorization': "Bearer " + token, 'Content-type': "application/json; charset=utf-8", 'User-Agent': 'MMM-Tesla2' }
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
self.vehicle_data = body;
self.sendSocketNotification("VEHICLE_DATA", body);
}
})
}
if (accessToken === null ) {
var tempToken = getToken(this.config);
var localToken = tempToken.then(function(accessToken){
return accessToken;
});
localToken.then(function(token) {
accessToken = token;
getChargeDate(token);
getDriveStateDate(token);
getVehicleDate(token);
});
}
else {
getChargeDate(accessToken);
getDriveStateDate(accessToken);
getVehicleDate(accessToken);
}
if(self.charge_data.charging_state === 'Charging') {
setTimeout(function() { self.getData(); }, 1000 * 60 * 5);
}
else {
setTimeout(function() { self.getData(); }, this.config.refreshInterval);
}
},
socketNotificationReceived: function(notification, payload) {
console.log("socketNotificationReceived");
var self = this;
if (notification === 'CONFIG' && self.started == false) {
self.config = payload;
self.sendSocketNotification("STARTED", true);
self.getData();
self.started = true;
} else if (notification == 'CONFIG') {
self.sendSocketNotification("CHARGE_DATA", self.charge_data);
self.sendSocketNotification("DRIVESTATE_DATA", self.drivestate_data);
self.sendSocketNotification("VEHICLE_DATA", self.vehicle_data);
}
}
});