@eiannone/tesla-api 1.8.0 → 1.10.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 CHANGED
@@ -47,10 +47,17 @@ class TeslaApi {
47
47
  }
48
48
  }
49
49
 
50
- async #apiCall(path, method = 'GET') {
51
- return new Promise((resolve, reject) => {
50
+ async #apiCall(path, method = 'GET', params = undefined) {
51
+ return new Promise((resolve, reject) => {
52
+ const postData = (typeof params != 'undefined')? JSON.stringify(params) : '';
53
+ let headers = { 'user-agent': "TeslaEma", 'Authorization': "Bearer " + this.token };
54
+ if (postData.length > 0) {
55
+ headers['Content-Type'] = 'application/json';
56
+ headers['Content-Length'] = postData.length;
57
+ }
58
+
52
59
  const req = request(BASE_URL + "/api/1/vehicles/" + path, {
53
- headers: { 'user-agent': "TeslaEma", 'Authorization': "Bearer " + this.token },
60
+ headers: headers,
54
61
  timeout: this.timeout,
55
62
  method: method
56
63
  }, res => {
@@ -89,6 +96,7 @@ class TeslaApi {
89
96
  // - ENOTFOUND
90
97
  reject(new ApiError(e.message + " ("+e.code+")", ApiError.NETWORK));
91
98
  });
99
+ if (postData.length > 0) req.write(postData);
92
100
  req.end();
93
101
  });
94
102
  }
@@ -116,19 +124,19 @@ class TeslaApi {
116
124
  return this.#apiCall(vid + "/wake_up", "POST");
117
125
  }
118
126
 
119
- async command(command, vehicle_id = null) {
127
+ async command(command, params = undefined, vehicle_id = null) {
120
128
  const vid = (vehicle_id == null)? this.vid : vehicle_id;
121
- return this.#apiCall(vid + "/command/" + command, "POST");
129
+ return this.#apiCall(vid + "/command/" + command, "POST", params);
122
130
  }
123
131
 
124
132
  async #oauthCall(params) {
125
- const post_data = JSON.stringify(params);
126
133
  return new Promise((resolve, reject) => {
134
+ const postData = JSON.stringify(params);
127
135
  const req = request(BASE_URL + '/oauth/token', {
128
136
  headers: {
129
137
  'user-agent': "TeslaEma",
130
138
  'Content-Type': 'application/json',
131
- 'Content-Length': post_data.length
139
+ 'Content-Length': postData.length
132
140
  },
133
141
  timeout: 30000,
134
142
  method: 'POST'
@@ -157,7 +165,7 @@ class TeslaApi {
157
165
  // - ENOTFOUND
158
166
  reject(new ApiError(e.message + " ("+e.code+")", ApiError.NETWORK));
159
167
  });
160
- req.write(post_data);
168
+ req.write(postData);
161
169
  req.end();
162
170
  });s
163
171
  }
package/TeslaStream.js CHANGED
@@ -104,12 +104,12 @@ export default class TeslaStream extends EventEmitter {
104
104
  clearTimeout(this.checkTimeout);
105
105
  if (this.disconnects % 10 == 0) {
106
106
  this.log("Too many disconnects!", "warn");
107
- this.emit('too-many-diconnects');
107
+ this.emit('too-many-disconnects');
108
108
  }
109
109
  else {
110
110
  const ms = (this.lastShiftState != null && this.lastShiftState != "")?
111
111
  this.#expBackOffMs(this.disconnects, 0, 8, 1.3) :
112
- this.#expBackOffMs(this.disconnects, minDelay, 60); // Teslamate uses min 15, max 30
112
+ this.#expBackOffMs(this.disconnects, minDelay, 120); // Teslamate uses min 15, max 30
113
113
  this.log("Waiting for " + Math.round(ms / 1000) + " sec...");
114
114
  this.checkTimeout = setTimeout(_ => { this.#subscribe(tag, token); }, ms);
115
115
  }
@@ -138,15 +138,10 @@ export default class TeslaStream extends EventEmitter {
138
138
  this.log("Websocket open.");
139
139
  this.#subscribe(this.tag, token);
140
140
  });
141
- this.ws.on('message', (data, isBinary) => {
142
- if (isBinary) {
143
- this.log("Binary data received. Ignored.");
144
- return;
145
- }
141
+ this.ws.on('message', (message) => {
146
142
  if (this.checkTimeout != null) clearTimeout(this.checkTimeout);
147
143
  this.checkTimeout = setTimeout(this.#timeout.bind(this), 15000);
148
-
149
- const message = data.toString();
144
+
150
145
  let d = JSON.parse(message);
151
146
  if (d.msg_type == 'control:hello') {
152
147
  this.log("Hello response received.");
@@ -163,12 +158,13 @@ export default class TeslaStream extends EventEmitter {
163
158
  this.#disconnectResubscribe(tag, token, "Vehicle disconnected", resubscribeDelay);
164
159
  break;
165
160
  case "vehicle_error":
161
+ if (d.value == 'Vehicle is offline') this.emit('offline');
166
162
  this.#disconnectResubscribe(tag, token, "Vehicle error: " + d.value, resubscribeDelay);
167
163
  break;
168
164
  case "client_error":
169
165
  this.log("Client error: " + d.value, "error");
170
166
  this.emit('error', d.value);
171
- this.#reconnect();
167
+ if (d.value != "Can't validate token. ") this.#reconnect();
172
168
  break;
173
169
  default:
174
170
  this.log("Stream API error ["+d.error_type+"]: " + data, "error");
@@ -184,7 +180,7 @@ export default class TeslaStream extends EventEmitter {
184
180
  this.log("Websocket error: " + errMsg, "error");
185
181
  });
186
182
  this.ws.on('close', (code, reason) => {
187
- this.log("Websocket closed ("+ code + (reason? ': ' + reason.toString() : '') + ").");
183
+ this.log("Websocket closed ("+ code + ((reason != '')? `: ${reason}` : '') + ").");
188
184
  if (code == 1006 && this.state != CLOSING) this.reconnect = true; // Abnormal close
189
185
  if (this.checkTimeout != null) clearTimeout(this.checkTimeout);
190
186
  this.ws = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eiannone/tesla-api",
3
- "version": "1.8.0",
3
+ "version": "1.10.0",
4
4
  "description": "Nodejs Tesla API",
5
5
  "type": "module",
6
6
  "main": "index.js",