Skip to content

Instantly share code, notes, and snippets.

@roblight
Forked from dirkvm/streamtest.js
Created September 23, 2019 14:16
Show Gist options
  • Save roblight/82a35b3c3a1eb4537134ab5ce8fedaf8 to your computer and use it in GitHub Desktop.
Save roblight/82a35b3c3a1eb4537134ab5ce8fedaf8 to your computer and use it in GitHub Desktop.
websocket based streaming for Tesla
const util = require('util');
const WebSocket = require('ws');
const vehicle_id = ''; // as returned by the /vehicles api call
const token = ''; // tokens[0] as returned by the /vehicles api call
const email = ''; // login e-mail
const stream_columns = [
'speed',
'odometer',
'soc',
'elevation',
'est_heading',
'est_lat',
'est_lng',
'power',
'shift_state',
'range',
'est_range',
// 'heading',
];
let ws = new WebSocket('wss://streaming.vn.teslamotors.com/streaming/', {
followRedirects: true,
});
const msg = {
msg_type: 'data:subscribe',
token: new Buffer.from(email + ':' + token).toString('base64'),
value: stream_columns.join(','),
tag: vehicles_id.toString(),
};
console.log(msg);
ws.on('open', () => {
ws.send(JSON.stringify(msg));
});
ws.on('close', (code, reason) => {
util.log('websocket closed, code=' + code + ', reason=' + reason);
});
ws.on('error', (err) => {
util.log('websocket error: ' + err);
});
ws.on('message', (data) => {
const msg = JSON.parse(data);
util.log(msg);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment