@eiannone/tesla-api 1.12.0 → 1.13.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/.vscode/launch.json +17 -0
- package/TeslaApi.js +24 -14
- package/examples/get-id.js +4 -0
- package/package.json +1 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Usare IntelliSense per informazioni sui possibili attributi.
|
|
3
|
+
// Al passaggio del mouse vengono visualizzate le descrizioni degli attributi esistenti.
|
|
4
|
+
// Per altre informazioni, visitare: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"configurations": [
|
|
7
|
+
{
|
|
8
|
+
"type": "pwa-node",
|
|
9
|
+
"request": "launch",
|
|
10
|
+
"name": "Get-id",
|
|
11
|
+
"skipFiles": [
|
|
12
|
+
"<node_internals>/**"
|
|
13
|
+
],
|
|
14
|
+
"program": "${workspaceFolder}\\examples\\get-id.js"
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
}
|
package/TeslaApi.js
CHANGED
|
@@ -21,8 +21,8 @@ class ApiError extends Error {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
class TeslaApi {
|
|
24
|
-
constructor(access_token = null,
|
|
25
|
-
this.vid =
|
|
24
|
+
constructor(access_token = null, id = null, refresh_token = null) {
|
|
25
|
+
this.vid = id;
|
|
26
26
|
this.token = access_token;
|
|
27
27
|
this.refresh_token = refresh_token;
|
|
28
28
|
this.timeout = 10000;
|
|
@@ -47,7 +47,7 @@ class TeslaApi {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
async #apiCall(path, method = 'GET', params = undefined) {
|
|
50
|
+
async #apiCall(path = "", method = 'GET', params = undefined) {
|
|
51
51
|
return new Promise((resolve, reject) => {
|
|
52
52
|
const postData = (typeof params != 'undefined')? JSON.stringify(params) : '';
|
|
53
53
|
let headers = { 'user-agent': "TeslaEma", 'Authorization': "Bearer " + this.token };
|
|
@@ -102,30 +102,30 @@ class TeslaApi {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
async getVehicles() {
|
|
105
|
-
return this.#apiCall(
|
|
105
|
+
return this.#apiCall();
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
async getVehicle(
|
|
109
|
-
return this.#apiCall((
|
|
108
|
+
async getVehicle(id = null) {
|
|
109
|
+
return this.#apiCall((id == null)? this.vid : id);
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
async getVehicleData(
|
|
113
|
-
const vid = (
|
|
112
|
+
async getVehicleData(id = null) {
|
|
113
|
+
const vid = (id == null)? this.vid : id;
|
|
114
114
|
return this.#apiCall(vid + "/vehicle_data");
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
async getChargeState(
|
|
118
|
-
const vid = (
|
|
117
|
+
async getChargeState(id = null) {
|
|
118
|
+
const vid = (id == null)? this.vid : id;
|
|
119
119
|
return this.#apiCall(vid + "/data_request/charge_state");
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
async wakeUp(
|
|
123
|
-
const vid = (
|
|
122
|
+
async wakeUp(id = null) {
|
|
123
|
+
const vid = (id == null)? this.vid : id;
|
|
124
124
|
return this.#apiCall(vid + "/wake_up", "POST");
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
async command(command, params = undefined,
|
|
128
|
-
const vid = (
|
|
127
|
+
async command(command, params = undefined, id = null) {
|
|
128
|
+
const vid = (id == null)? this.vid : id;
|
|
129
129
|
return this.#apiCall(vid + "/command/" + command, "POST", params);
|
|
130
130
|
}
|
|
131
131
|
|
|
@@ -242,6 +242,16 @@ class TeslaApi {
|
|
|
242
242
|
throw error;
|
|
243
243
|
}
|
|
244
244
|
}
|
|
245
|
+
|
|
246
|
+
async getId(vehicle_id) {
|
|
247
|
+
return this.#apiCall().then(vehicles => {
|
|
248
|
+
for (let v = 0; v < vehicles.length; v++) {
|
|
249
|
+
if (!vehicles[v].hasOwnProperty('vehicle_id') || !vehicles[v].hasOwnProperty('id_s')) continue;
|
|
250
|
+
if (vehicles[v].vehicle_id == vehicle_id) return vehicles[v].id_s;
|
|
251
|
+
}
|
|
252
|
+
throw new ApiError("Vehicle not found");
|
|
253
|
+
});
|
|
254
|
+
}
|
|
245
255
|
}
|
|
246
256
|
|
|
247
257
|
export { ApiError, TeslaApi }
|