@eiannone/tesla-api 1.7.0 → 1.8.1

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.
Files changed (2) hide show
  1. package/TeslaStream.js +18 -9
  2. package/package.json +2 -2
package/TeslaStream.js CHANGED
@@ -10,6 +10,7 @@ export default class TeslaStream extends EventEmitter {
10
10
  static DEFAULT_COLUMNS = ['elevation', 'est_heading', 'est_lat', 'est_lng', 'odometer', 'power', 'shift_state', 'speed', 'soc'];
11
11
 
12
12
  constructor(logger = null) {
13
+ super();
13
14
  this.log = logger || this.#internalLog;
14
15
  this.ws = null;
15
16
  this.state = CLOSED;
@@ -30,6 +31,10 @@ export default class TeslaStream extends EventEmitter {
30
31
  return this.state == CLOSED;
31
32
  }
32
33
 
34
+ isClosing() {
35
+ return this.state == CLOSING;
36
+ }
37
+
33
38
  #internalLog(msg, level = 'debug') {
34
39
  console.log("[%s] %s", level, msg);
35
40
  }
@@ -115,12 +120,16 @@ export default class TeslaStream extends EventEmitter {
115
120
  if (columns != null) this.columns = columns;
116
121
  let shiftStatePos = this.columns.indexOf('shift_state');
117
122
 
118
- if (this.ws != null && this.ws.readyState != WebSocket.CLOSED) return;
123
+ if (this.ws != null) {
124
+ if (this.state == CLOSING) this.reconnect = true;
125
+ if (this.ws.readyState != WebSocket.CLOSED) return;
126
+ }
119
127
  this.state = CONNECTING;
120
128
  this.log("Connecting to websocket...");
121
129
  this.ws = new WebSocket("wss://streaming.vn.teslamotors.com/streaming/", {
122
130
  perMessageDeflate: false,
123
- handshakeTimeout: 6000
131
+ handshakeTimeout: 6000,
132
+ skipUTF8Validation: true
124
133
  });
125
134
  if (this.checkTimeout != null) clearTimeout(this.checkTimeout);
126
135
  this.checkTimeout = setTimeout(this.#timeout.bind(this), this.#expBackOffMs(this.timeouts, 10, 30));
@@ -129,11 +138,11 @@ export default class TeslaStream extends EventEmitter {
129
138
  this.log("Websocket open.");
130
139
  this.#subscribe(this.tag, token);
131
140
  });
132
- this.ws.on('message', (data) => {
141
+ this.ws.on('message', (message) => {
133
142
  if (this.checkTimeout != null) clearTimeout(this.checkTimeout);
134
143
  this.checkTimeout = setTimeout(this.#timeout.bind(this), 15000);
135
-
136
- let d = JSON.parse(data);
144
+
145
+ let d = JSON.parse(message);
137
146
  if (d.msg_type == 'control:hello') {
138
147
  this.log("Hello response received.");
139
148
  this.state = CONNECTED;
@@ -161,16 +170,16 @@ export default class TeslaStream extends EventEmitter {
161
170
  break;
162
171
  }
163
172
  } else {
164
- this.log("Unknown message: " + data, "error");
165
- this.emit('error', data);
173
+ this.log("Unknown message: " + message, "error");
174
+ this.emit('error', message);
166
175
  }
167
176
  });
168
- this.ws.on('error', (error) => {
177
+ this.ws.on('error', error => {
169
178
  const errMsg = (error instanceof Error)? error.message : error;
170
179
  this.log("Websocket error: " + errMsg, "error");
171
180
  });
172
181
  this.ws.on('close', (code, reason) => {
173
- this.log("Websocket closed ("+code + (reason? ': '+reason : '')+").");
182
+ this.log("Websocket closed ("+ code + (reason? ': ' + reason : '') + ").");
174
183
  if (code == 1006 && this.state != CLOSING) this.reconnect = true; // Abnormal close
175
184
  if (this.checkTimeout != null) clearTimeout(this.checkTimeout);
176
185
  this.ws = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eiannone/tesla-api",
3
- "version": "1.7.0",
3
+ "version": "1.8.1",
4
4
  "description": "Nodejs Tesla API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -8,6 +8,6 @@
8
8
  "license": "MIT",
9
9
  "repository": "eiannone/tesla-api",
10
10
  "dependencies": {
11
- "ws": "^7.4.2"
11
+ "ws": "^8.2.3"
12
12
  }
13
13
  }