Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  var tjs = require('teslajs');
  2.  
  3.  var username = process.env.TESLA_EMAIL;
  4.  var password = process.env.TESLA_PASS;
  5.  
  6.  exports.handler = async (event) => {
  7.      var asleep = true;
  8.      var options;
  9.  
  10.      //Login
  11.      await tjs.loginAsync(username, password).then( function(result) {
  12.          if (result.error) {
  13.              console.log(JSON.stringify(result.error));
  14.              process.exit(1);
  15.          }
  16.  
  17.          var token = JSON.stringify(result.authToken);
  18.  
  19.          if (token)
  20.              console.log("Login Succesful!");
  21.  
  22.          options = {
  23.              authToken: result.authToken
  24.          };
  25.      },
  26.      function(result) {
  27.          console.error("Login Failed!");
  28.          process.exit(1);
  29.      });
  30.  
  31.      //Get all vehicle info
  32.      await tjs.vehiclesAsync(options).then( function(vehicle) {
  33.          console.log("Vehicle " + vehicle.vin + " is: " + vehicle.state);
  34.          options.vehicleID = vehicle.id_s;
  35.      },
  36.      function(vehicle) {
  37.          console.error("Vehicles Failed!");
  38.          process.exit(1);
  39.      });
  40.  
  41.      //Send wake up command to vehicle
  42.      await tjs.wakeUpAsync(options).then( function (result) {
  43.              console.log("WakeUp command: " + "Succeeded");
  44.              console.log("Vehicle state: " + result.state);
  45.      },
  46.      function(result) {
  47.          console.error("WakeUp Failed!");
  48.          process.exit(1);
  49.      });
  50.  
  51.      //Wait for vehicle to wake up
  52.      var num_it = 0;
  53.      while(asleep && num_it < 50) {
  54.          await tjs.vehicleAsync(options).then( function(vehicle) {
  55.              console.log("Vehicle " + vehicle.vin + " is: " + vehicle.state);
  56.              
  57.              if(vehicle.state == "online")
  58.              {
  59.                  asleep = false;
  60.              }
  61.          },
  62.          function(vehicle) {
  63.              console.error("Vehicle state Failed!");
  64.              process.exit(1);
  65.          });
  66.  
  67.          await new Promise(resolve=>{ setTimeout(resolve,1000) });
  68.      }
  69.      
  70.      if(asleep)
  71.      {
  72.          console.error("Failed to wake vehicle!");
  73.      }
  74.      else
  75.      {
  76.          if (event.clickType != "DOUBLE") {
  77.              await tjs.doorUnlockAsync(options).then( function(result) {
  78.                  console.log("The doors are now: " + "UNLOCKED");
  79.              },
  80.              function(result) {
  81.                  console.error("DoorUnlock Failed!");
  82.                  process.exit(1);
  83.              });
  84.          }
  85.          else {
  86.              await tjs.openChargePortAsync(options).then( function(result) {
  87.                  console.log("The charge port is now: " + "OPEN");
  88.              },
  89.              function(result) {
  90.                  console.error("OpenChargePort Failed!");
  91.                  process.exit(1);
  92.              });
  93.          }
  94.      }
  95.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement