Basic C# library for interacting with Tesla's Owner API, based on https://tesla-api.timdorr.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.8 KiB
141 lines
4.8 KiB
4 years ago
|
using System;
|
||
|
using System.Net;
|
||
|
using System.IO;
|
||
|
using System.Text;
|
||
|
using Newtonsoft.Json;
|
||
|
|
||
|
namespace Tesla {
|
||
|
|
||
|
public class Client
|
||
|
{
|
||
|
public string baseURL = "https://owner-api.teslamotors.com/api/1";
|
||
|
protected string token;
|
||
|
public string vehicle_id { get; set; }
|
||
|
private RootDataResponse dataResponse;
|
||
|
private DateTime lastRefresh;
|
||
|
|
||
|
public Vehicle[] ListVehicles()
|
||
|
{
|
||
|
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(baseURL + "/vehicles");
|
||
|
string responseString = processRequest(myHttpWebRequest);
|
||
|
VehicleListResponse response = JsonConvert.DeserializeObject<VehicleListResponse>(responseString);
|
||
|
return response.response.ToArray();
|
||
|
}
|
||
|
|
||
|
public DataResponse WakeVehicle()
|
||
|
{
|
||
|
|
||
|
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(baseURL + $"/vehicles/{vehicle_id}/wake_up");
|
||
|
myHttpWebRequest.Method = "POST";
|
||
|
string responseString = processRequest(myHttpWebRequest);
|
||
|
RootDataResponse response = JsonConvert.DeserializeObject<RootDataResponse>(responseString);
|
||
|
return response.response;
|
||
|
}
|
||
|
|
||
|
public CommandResponse RunCommand(string command, string parms = "")
|
||
|
{
|
||
|
ASCIIEncoding encoding = new ASCIIEncoding ();
|
||
|
byte[] byte1 = encoding.GetBytes (parms);
|
||
|
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(baseURL + $"/vehicles/{vehicle_id}/command/{command}");
|
||
|
myHttpWebRequest.Method = "POST";
|
||
|
myHttpWebRequest.ContentLength = byte1.Length;
|
||
|
Stream newStream = myHttpWebRequest.GetRequestStream ();
|
||
|
newStream.Write (byte1, 0, byte1.Length);
|
||
|
|
||
|
string responseString = processRequest(myHttpWebRequest);
|
||
|
APIResponseRoot response = JsonConvert.DeserializeObject<APIResponseRoot>(responseString);
|
||
|
return response.response;
|
||
|
}
|
||
|
|
||
|
|
||
|
public ChargeState GetChargeState()
|
||
|
{
|
||
|
return GetVehicleData().response.charge_state;
|
||
|
}
|
||
|
|
||
|
public ClimateState GetClimateState()
|
||
|
{
|
||
|
return GetVehicleData().response.climate_state;
|
||
|
}
|
||
|
|
||
|
public DriveState GetDriveState()
|
||
|
{
|
||
|
return GetVehicleData().response.drive_state;
|
||
|
}
|
||
|
public GuiSettings GetGuiSettings()
|
||
|
{
|
||
|
return GetVehicleData().response.gui_settings;
|
||
|
}
|
||
|
public VehicleState GetVehicleState()
|
||
|
{
|
||
|
return GetVehicleData().response.vehicle_state;
|
||
|
}
|
||
|
public VehicleConfig GetVehicleConfig()
|
||
|
{
|
||
|
return GetVehicleData().response.vehicle_config;
|
||
|
}
|
||
|
public bool GetMobileEnabled()
|
||
|
{
|
||
|
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(baseURL + $"/vehicles/{vehicle_id}/mobile_enabled");
|
||
|
string responseString = processRequest(myHttpWebRequest);
|
||
|
return responseString.Contains("true");
|
||
|
}
|
||
|
|
||
|
public RootChargingSites GetChargingSites()
|
||
|
{
|
||
|
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(baseURL + $"/vehicles/{vehicle_id}/nearby_charging_sites");
|
||
|
string responseString = processRequest(myHttpWebRequest);
|
||
|
RootChargingSites response = JsonConvert.DeserializeObject<RootChargingSites>(responseString);
|
||
|
return response;
|
||
|
}
|
||
|
|
||
|
public RootDataResponse GetVehicleData()
|
||
|
{
|
||
|
RootDataResponse response = this.dataResponse;
|
||
|
if (lastRefresh <= DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(5.0)))
|
||
|
{
|
||
|
return UpdateVehicleData();
|
||
|
}
|
||
|
|
||
|
return response;
|
||
|
}
|
||
|
public RootDataResponse UpdateVehicleData()
|
||
|
{
|
||
|
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(baseURL + $"/vehicles/{vehicle_id}/vehicle_data");
|
||
|
string responseString = processRequest(myHttpWebRequest);
|
||
|
RootDataResponse response = JsonConvert.DeserializeObject<RootDataResponse>(responseString);
|
||
|
this.dataResponse = response;
|
||
|
this.lastRefresh = DateTime.UtcNow;
|
||
|
return response;
|
||
|
}
|
||
|
protected string processRequest(HttpWebRequest myHttpWebRequest)
|
||
|
{
|
||
|
HttpWebResponse myHttpWebResponse = null;
|
||
|
addHeaders(myHttpWebRequest);
|
||
|
try
|
||
|
{
|
||
|
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
|
||
|
Stream stream = myHttpWebResponse.GetResponseStream();
|
||
|
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
|
||
|
String responseString = reader.ReadToEnd();
|
||
|
myHttpWebResponse.Close();
|
||
|
return responseString;
|
||
|
}
|
||
|
catch (System.Net.WebException e)
|
||
|
{
|
||
|
Console.WriteLine(e);
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
public void addHeaders(HttpWebRequest httpWebRequest)
|
||
|
{
|
||
|
httpWebRequest.Headers["Authorization"] = token;
|
||
|
httpWebRequest.Headers["User-Agent"] = "Tesla.NET";
|
||
|
httpWebRequest.Headers["Content-Type"] = "application/json";
|
||
|
}
|
||
|
public void setToken(string t)
|
||
|
{
|
||
|
this.token = t;
|
||
|
}
|
||
|
}
|
||
|
}
|