@eiannone/tesla-api 1.5.3 → 1.6.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/TeslaStream.js +14 -15
- package/package.json +1 -1
package/TeslaStream.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import WebSocket from 'ws';
|
|
2
|
+
import {EventEmitter} from 'events';
|
|
2
3
|
|
|
3
4
|
const CONNECTING = 1;
|
|
4
5
|
const CONNECTED = 2;
|
|
5
6
|
const CLOSING = 3;
|
|
6
7
|
const CLOSED = 4;
|
|
7
8
|
|
|
8
|
-
export default class TeslaStream {
|
|
9
|
+
export default class TeslaStream extends EventEmitter {
|
|
9
10
|
static DEFAULT_COLUMNS = ['elevation', 'est_heading', 'est_lat', 'est_lng', 'odometer', 'power', 'shift_state', 'speed', 'soc'];
|
|
10
11
|
|
|
11
|
-
constructor(logger = null
|
|
12
|
+
constructor(logger = null) {
|
|
12
13
|
this.log = logger || this.#internalLog;
|
|
13
14
|
this.ws = null;
|
|
14
15
|
this.state = CLOSED;
|
|
@@ -16,8 +17,6 @@ export default class TeslaStream {
|
|
|
16
17
|
this.checkTimeout = null;
|
|
17
18
|
this.timeouts = 0;
|
|
18
19
|
this.disconnects = 0;
|
|
19
|
-
this.cbInactive = cb_inactive;
|
|
20
|
-
this.cbDisconnects = cb_disconnects;
|
|
21
20
|
this.reconnect = true;
|
|
22
21
|
this.tag = null;
|
|
23
22
|
this.lastShiftState = null;
|
|
@@ -74,7 +73,7 @@ export default class TeslaStream {
|
|
|
74
73
|
this.log("Stream connection timed out / " + this.timeouts, level);
|
|
75
74
|
if (this.timeouts % 3 == 0) { // Teslamate does it every 5th attempt
|
|
76
75
|
this.log("Stream inactive!");
|
|
77
|
-
|
|
76
|
+
this.emit('inactive');
|
|
78
77
|
}
|
|
79
78
|
this.#reconnect();
|
|
80
79
|
}
|
|
@@ -89,24 +88,24 @@ export default class TeslaStream {
|
|
|
89
88
|
}));
|
|
90
89
|
}
|
|
91
90
|
|
|
92
|
-
#disconnectResubscribe(tag, token, reason) {
|
|
91
|
+
#disconnectResubscribe(tag, token, reason, minDelay) {
|
|
93
92
|
this.disconnects++;
|
|
94
93
|
this.log(reason + ((this.disconnects == 1)? '' : ' / '+this.disconnects), (this.disconnects < 8)? 'debug' : 'info');
|
|
95
94
|
clearTimeout(this.checkTimeout);
|
|
96
95
|
if (this.disconnects % 10 == 0) {
|
|
97
96
|
this.log("Too many disconnects!", "warn");
|
|
98
|
-
|
|
97
|
+
this.emit('too-many-diconnects');
|
|
99
98
|
}
|
|
100
99
|
else {
|
|
101
100
|
const ms = (this.lastShiftState != null && this.lastShiftState != "")?
|
|
102
101
|
this.#expBackOffMs(this.disconnects, 0, 8, 1.3) :
|
|
103
|
-
this.#expBackOffMs(this.disconnects,
|
|
102
|
+
this.#expBackOffMs(this.disconnects, minDelay, 60); // Teslamate uses min 15, max 30
|
|
104
103
|
this.log("Waiting for " + Math.round(ms / 1000) + " sec...");
|
|
105
104
|
this.checkTimeout = setTimeout(_ => { this.#subscribe(tag, token); }, ms);
|
|
106
105
|
}
|
|
107
106
|
}
|
|
108
107
|
|
|
109
|
-
connect(tag, token, columns = null,
|
|
108
|
+
connect(tag, token, columns = null, resubscribeDelay = 30) {
|
|
110
109
|
this.tag = tag.toString();
|
|
111
110
|
if (columns != null) this.columns = columns;
|
|
112
111
|
let shiftStatePos = this.columns.indexOf('shift_state');
|
|
@@ -135,21 +134,21 @@ export default class TeslaStream {
|
|
|
135
134
|
this.state = CONNECTED;
|
|
136
135
|
} else if (d.msg_type == 'data:update') {
|
|
137
136
|
let values = d.value.split(",");
|
|
138
|
-
|
|
137
|
+
this.emit('stream', values);
|
|
139
138
|
this.timeouts = 0;
|
|
140
139
|
this.disconnects = 0;
|
|
141
140
|
if (shiftStatePos > -1) this.lastShiftState = values[shiftStatePos + 1];
|
|
142
141
|
} else if (d.msg_type == 'data:error' && typeof d.error_type != "undefined") {
|
|
143
142
|
switch(d.error_type) {
|
|
144
143
|
case "vehicle_disconnected":
|
|
145
|
-
this.#disconnectResubscribe(tag, token, "Vehicle disconnected");
|
|
144
|
+
this.#disconnectResubscribe(tag, token, "Vehicle disconnected", resubscribeDelay);
|
|
146
145
|
break;
|
|
147
146
|
case "vehicle_error":
|
|
148
|
-
this.#disconnectResubscribe(tag, token, "Vehicle error: " + d.value);
|
|
147
|
+
this.#disconnectResubscribe(tag, token, "Vehicle error: " + d.value, resubscribeDelay);
|
|
149
148
|
break;
|
|
150
149
|
case "client_error":
|
|
151
150
|
this.log("Client error: " + d.value, "error");
|
|
152
|
-
|
|
151
|
+
this.emit('error', d.value);
|
|
153
152
|
this.#reconnect();
|
|
154
153
|
break;
|
|
155
154
|
default:
|
|
@@ -158,7 +157,7 @@ export default class TeslaStream {
|
|
|
158
157
|
}
|
|
159
158
|
} else {
|
|
160
159
|
this.log("Unknown message: " + data, "error");
|
|
161
|
-
|
|
160
|
+
this.emit('error', data);
|
|
162
161
|
}
|
|
163
162
|
});
|
|
164
163
|
this.ws.on('error', (error) => {
|
|
@@ -174,7 +173,7 @@ export default class TeslaStream {
|
|
|
174
173
|
if (this.reconnect) {
|
|
175
174
|
this.checkTimeout = setTimeout(() => {
|
|
176
175
|
this.log("Reconnecting...");
|
|
177
|
-
this.connect(tag, token, columns
|
|
176
|
+
this.connect(tag, token, columns);
|
|
178
177
|
}, 1000);
|
|
179
178
|
}
|
|
180
179
|
});
|