@eiannone/tesla-api 1.11.0 → 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/TeslaApi.js +59 -63
- package/package.json +1 -1
package/TeslaApi.js
CHANGED
|
@@ -129,46 +129,47 @@ class TeslaApi {
|
|
|
129
129
|
return this.#apiCall(vid + "/command/" + command, "POST", params);
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
132
|
+
async #oauthCall(params, bearer_token) {
|
|
133
|
+
return new Promise((resolve, reject) => {
|
|
134
|
+
const postData = JSON.stringify(params);
|
|
135
|
+
const req = request(BASE_URL + '/oauth/token', {
|
|
136
|
+
headers: {
|
|
137
|
+
'user-agent': "TeslaEma",
|
|
138
|
+
'Authorization': "Bearer " + bearer_token,
|
|
139
|
+
'Content-Type': 'application/json',
|
|
140
|
+
'Content-Length': postData.length
|
|
141
|
+
},
|
|
142
|
+
timeout: 30000,
|
|
143
|
+
method: 'POST'
|
|
144
|
+
}, res => {
|
|
145
|
+
if (res.statusCode > 199 && res.statusCode < 300) {
|
|
146
|
+
res.setEncoding('utf8');
|
|
147
|
+
let rawData = '';
|
|
148
|
+
res.on('data', chunk => { rawData += chunk; });
|
|
149
|
+
res.on('end', () => {
|
|
150
|
+
try {
|
|
151
|
+
resolve(JSON.parse(rawData));
|
|
152
|
+
} catch(err) {
|
|
153
|
+
reject(new ApiError(err));
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
} else {
|
|
157
|
+
let errMsg = res.statusMessage + " ("+res.statusCode+")";
|
|
158
|
+
reject(new ApiError(errMsg, this.#decodeStatus(res.statusCode)));
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
req.on('error', e => {
|
|
162
|
+
// Error code examples:
|
|
163
|
+
// - EAI_AGAIN (DNS lookup timeout)
|
|
164
|
+
// - ECONNRESET
|
|
165
|
+
// - ECONNREFUSED
|
|
166
|
+
// - ENOTFOUND
|
|
167
|
+
reject(new ApiError(e.message + " ("+e.code+")", ApiError.NETWORK));
|
|
168
|
+
});
|
|
169
|
+
req.write(postData);
|
|
170
|
+
req.end();
|
|
171
|
+
});
|
|
172
|
+
}
|
|
172
173
|
|
|
173
174
|
async #oauthCall2(params) {
|
|
174
175
|
return new Promise((resolve, reject) => {
|
|
@@ -222,30 +223,25 @@ class TeslaApi {
|
|
|
222
223
|
refresh_token,
|
|
223
224
|
scope: 'openid email offline_access'
|
|
224
225
|
};
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
226
|
+
try {
|
|
227
|
+
let resp = await this.#oauthCall2(payLoad);
|
|
228
|
+
this.refresh_token = resp.refresh_token;
|
|
229
|
+
let oauth = await this.#oauthCall({
|
|
230
|
+
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
|
231
|
+
client_id: '81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384',
|
|
232
|
+
client_secret: 'c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3'
|
|
233
|
+
}, resp.access_token);
|
|
234
|
+
this.token = oauth.access_token;
|
|
235
|
+
if (typeof this.cb_refreshToken == 'function') {
|
|
236
|
+
this.cb_refreshToken(this.token, this.refresh_token);
|
|
237
|
+
}
|
|
238
|
+
return oauth;
|
|
239
|
+
}
|
|
240
|
+
catch(error) {
|
|
241
|
+
if (error instanceof Error) error.message += " - Unable to refresh Token";
|
|
242
|
+
throw error;
|
|
243
|
+
}
|
|
238
244
|
}
|
|
239
|
-
|
|
240
|
-
// async getTokens(email, password) {
|
|
241
|
-
// return this.#oauthCall({
|
|
242
|
-
// 'grant_type': "password",
|
|
243
|
-
// 'email': email,
|
|
244
|
-
// 'password': password,
|
|
245
|
-
// 'client_id': "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384",
|
|
246
|
-
// 'client_secret': "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3"
|
|
247
|
-
// });
|
|
248
|
-
// }
|
|
249
245
|
}
|
|
250
246
|
|
|
251
247
|
export { ApiError, TeslaApi }
|