Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var tjs = require('teslajs');
- var username = process.env.TESLA_EMAIL;
- var password = process.env.TESLA_PASS;
- exports.handler = async (event) => {
- var asleep = true;
- var options;
- //Login
- await tjs.loginAsync(username, password).then( function(result) {
- if (result.error) {
- console.log(JSON.stringify(result.error));
- process.exit(1);
- }
- var token = JSON.stringify(result.authToken);
- if (token)
- console.log("Login Succesful!");
- options = {
- authToken: result.authToken
- };
- },
- function(result) {
- console.error("Login Failed!");
- process.exit(1);
- });
- //Get all vehicle info
- await tjs.vehiclesAsync(options).then( function(vehicle) {
- console.log("Vehicle " + vehicle.vin + " is: " + vehicle.state);
- options.vehicleID = vehicle.id_s;
- },
- function(vehicle) {
- console.error("Vehicles Failed!");
- process.exit(1);
- });
- //Send wake up command to vehicle
- await tjs.wakeUpAsync(options).then( function (result) {
- console.log("WakeUp command: " + "Succeeded");
- console.log("Vehicle state: " + result.state);
- },
- function(result) {
- console.error("WakeUp Failed!");
- process.exit(1);
- });
- //Wait for vehicle to wake up
- var num_it = 0;
- while(asleep && num_it < 50) {
- await tjs.vehicleAsync(options).then( function(vehicle) {
- console.log("Vehicle " + vehicle.vin + " is: " + vehicle.state);
- if(vehicle.state == "online")
- {
- asleep = false;
- }
- },
- function(vehicle) {
- console.error("Vehicle state Failed!");
- process.exit(1);
- });
- await new Promise(resolve=>{ setTimeout(resolve,1000) });
- }
- if(asleep)
- {
- console.error("Failed to wake vehicle!");
- }
- else
- {
- if (event.clickType != "DOUBLE") {
- await tjs.doorUnlockAsync(options).then( function(result) {
- console.log("The doors are now: " + "UNLOCKED");
- },
- function(result) {
- console.error("DoorUnlock Failed!");
- process.exit(1);
- });
- }
- else {
- await tjs.openChargePortAsync(options).then( function(result) {
- console.log("The charge port is now: " + "OPEN");
- },
- function(result) {
- console.error("OpenChargePort Failed!");
- process.exit(1);
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement