Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage examples #18

Open
ramonsmits opened this issue Oct 26, 2018 · 14 comments
Open

Usage examples #18

ramonsmits opened this issue Oct 26, 2018 · 14 comments

Comments

@ramonsmits
Copy link

ramonsmits commented Oct 26, 2018

Please extend the readme with atleast a simple example explaining how to use the framework. I'm unable to use it.

For example, I tried the following but it doesnt work. Maybe my access token is incorrect but it is also unclear how to obtain an access token in the first place.

            var result = await client.GetVehiclesAsync(TESLA_ACCESS_TOKEN);
            var vehicles = result.Data.Response;
            foreach (var vehicle in vehicles)
            {
                Console.WriteLine(vehicle.DisplayName);
            }
@ramonsmits
Copy link
Author

Ok it seems I found how to use it:

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("MySuperClient/0.1");
var teslaClient = new TeslaClient(TeslaClientBase.DefaultBaseUri, httpClient);

@chrispi68
Copy link

I still don't know how to log in, how to access all the vehicles in my account or how to turn on the heating.

@adamncsu
Copy link

Some basic documentation would be nice.

@Gruski
Copy link

Gruski commented Aug 18, 2019

I agree we need some documentation.

@nlogozzo
Copy link

nlogozzo commented Oct 1, 2019

If someone has figured it out, can you comment an example to get started

@adamncsu
Copy link

adamncsu commented Oct 2, 2019

@nlogozzo Here you go:

const string TESLA_CLIENT_ID = "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384";
const string TESLA_CLIENT_SECRET = "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3";
const string EMAIL = "your@email.com";
const string PASSWORD = "yourpassword";

// authenticate
string accessToken = null;
using (var httpClient = new HttpClient(TeslaClientBase.CreateDefaultHttpClientHandler(), false))
{
    httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("tesla-net/0.6.2");
    using (var auth = new TeslaAuthClient(httpClient))
    {
        var authResult = auth.RequestAccessTokenAsync(TESLA_CLIENT_ID, TESLA_CLIENT_SECRET, EMAIL, PASSWORD).GetAwaiter().GetResult();
        accessToken = authResult.Data.AccessToken;
    }
}

// make api calls
using (var httpClient = new HttpClient(TeslaClientBase.CreateDefaultHttpClientHandler(), false))
{
    httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("tesla-net/0.6.2");
    using (var api = new TeslaClient(httpClient))
    {
        // get vehicle list
        var vehiclesResult = api.GetVehiclesAsync(accessToken).GetAwaiter().GetResult();
        var vehicles = vehiclesResult.Data.Response;

        // check charge status
        var chargeResult = api.GetChargeStateAsync(vehicles.First().Id, accessToken).GetAwaiter().GetResult();
        var chargeState = chargeResult.Data.Response;

        Console.WriteLine($"Charge state: {chargeState.ChargingState}");
        Console.WriteLine($"Battery level: {chargeState.BatteryLevel}%");
        Console.WriteLine($"Estimated range: {chargeState.BatteryRange} mi");
    }
}

The client ID and secret key are apparently subject to change, but I'm not sure how often. You can find this information here: https://tesla-api.timdorr.com/api-basics/authentication

@nlogozzo
Copy link

nlogozzo commented Oct 2, 2019

@adamncsu What is the "tesla-net/0.6.2"? Should that be the version of the api i am using, in my case 0.7.0?

@adamncsu
Copy link

adamncsu commented Oct 2, 2019

@nlogozzo I don’t think it matters. Its just a descriptor for the API to know who’s calling it.

@nlogozzo
Copy link

nlogozzo commented Oct 2, 2019

@adamncsu Okay. Thank You!

@nlogozzo
Copy link

nlogozzo commented Oct 2, 2019

@adamncsu Sorry to bother, I was able to get the basic api setup and able to see some basic vehicle info. However, I can't figure out how to do commands, such as unlock the doors, increase the volume, etc. Can you help?

@adamncsu
Copy link

adamncsu commented Oct 2, 2019

@nlogozzo I don’t think those commands are covered in the scope of this project, but may e @JSkimming could answer. I don’t see it in the code.

@JSkimming
Copy link
Owner

JSkimming commented Oct 2, 2019

@adamncsu Nope, I've only added the APIs I needed, though I'm happy to take PRs :-)

@ramonsmits
Copy link
Author

What is the "tesla-net/0.6.2"? Should that be the version of the api i am using, in my case 0.7.0?

@adamncsu @nlogozzo That is YOUR application identification according to the user-agent spec. So do not use something like "TeslaNET". This is for the server so understand what kind of application is connecting.

@ramonsmits
Copy link
Author

To extend @adamncsu his sample, here one that set the bearer token once. The HttpClient should be reused as much as possible as this will reuse an open connection to the web server.

var httpClient = new HttpClient(TeslaClientBase.CreateDefaultHttpClientHandler(), false);
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);

// authenticate
var auth = new TeslaAuthClient(httpClient);
GC.SuppressFinalize(auth); // Do NOT dispose, this wil also dispose the httpclient. Is a bug in current version!
var authResult = auth.RequestAccessTokenAsync(TESLA_CLIENT_ID, TESLA_CLIENT_SECRET, EMAIL, PASSWORD).GetAwaiter().GetResult();
            
// Set Bearer token in default authorization header
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.Data.AccessToken);

var api = new TeslaClient(httpClient); // Store globally if using a single account
GC.SuppressFinalize(api); // Do NOT dispose, this wil also dispose the HttpClient. Is a bug in current version!

// get vehicle list
var vehiclesResult = api.GetVehiclesAsync().GetAwaiter().GetResult();
var vehicles = vehiclesResult.Data.Response;

// check charge status
var chargeResult = api.GetChargeStateAsync(vehicles.First().Id).GetAwaiter().GetResult();
var chargeState = chargeResult.Data.Response;

const double MilesToKilometersFactor = 1.609344; // I live in Europe, we use the metric system :-)
Console.WriteLine($"Charge state: {chargeState.ChargingState}");
Console.WriteLine($"Battery level: {chargeState.BatteryLevel}%");
Console.WriteLine($"Estimated range: {chargeState.BatteryRange/MilesToKilometersFactor} km");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants