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

Reduce number of API calls for 2023.38.X #1132

Open
skweee opened this issue Nov 20, 2023 · 3 comments
Open

Reduce number of API calls for 2023.38.X #1132

skweee opened this issue Nov 20, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@skweee
Copy link

skweee commented Nov 20, 2023

Describe the bug
While the fix to support 2023.38.X is working great, it is effectively doubling the amount of calls to the Tesla API.

if (r2.ContainsKey("latitude"))
{
dLatitude = (decimal)r2["latitude"];
dLongitude = (decimal)r2["longitude"];
heading = (int)r2["heading"];
}
else
{
// New API after 2023.38.4
var rc2 = GetCommand("vehicle_data?endpoints=location_data").Result;
if (rc2 == null)
return false;
dynamic jsonResult2 = JsonConvert.DeserializeObject(rc2);
dynamic r2x = jsonResult2["response"]["drive_state"];
if (r2x?.ContainsKey("latitude") == true)
{
dLatitude = (decimal)r2x["latitude"];
dLongitude = (decimal)r2x["longitude"];
heading = (int)r2x["heading"];
}
else
return false;
}

For firmware version 2023.38.X, the Tesla API reply to the first call will never contain the latitude, always forcing Teslalogger to make a second call.

The Tesla API supports getting all relevant information in a single call. To achieve this, all necessary parameters must directly be passed as URL parameters in the first call. TeslaMate shows how this is done and this is also backwards compatible for older firmware versions:
https://github.com/teslamate-org/teslamate/pull/3373/files

Background information
The Tesla API documentation has a section for pricing. While currently the pricing is fixed at $0/week, there is no guarantee that this will continue to be the case. There is also a limit for the amount of API requests, which means that in the future it may be prudent to reduce the number of calls

While it may not seem necessary at the moment, it would be wise to optimize the amount of calls necessary, if not only in order to be a good citizen and to reduce the possibility of potential connectivity issues.

Due to time issues, I can't offer a PR at the moment, but I would like to leave this issue here, so either I can try to fix it in the future or maybe someone else can have a go in the meantime.

@skweee skweee added the bug Something isn't working label Nov 20, 2023
@superfloh247
Copy link
Contributor

I played with this

https://github.com/superfloh247/TeslaLogger/tree/bugfix-1132

but all I get ist 408 timeout, both with an old MCU1 car and an up to date Model Y

@skweee
Copy link
Author

skweee commented Nov 22, 2023

Thanks for giving this a shot. About the 408 - that is usually a sign that either the car is unreachable (car is asleep or no internet connection) or that another client (e.g. the official app) is currently active.

Does the 408 message contain a reason (such as "VEHICLE_UNAVAILABLE")?

@skweee
Copy link
Author

skweee commented Nov 22, 2023

Maybe it makes sense to only request specific parameters while driving (line 2955).

public bool IsDriving(bool justinsertdb = false)
{
string resultContent = "";
try
{
resultContent = GetCommand("vehicle_data").Result;

According to the Tesla API documentation: "This may return cached data if the vehicle is offline."
https://developer.tesla.com/docs/fleet-api#vehicle_data

I am not sure if and how the cache also works when we are passing specific URL parameters. Maybe it makes sense to only address this in driving state for now, where we know that the car is online.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants