Skip to content

Commit

Permalink
GetRegion
Browse files Browse the repository at this point in the history
  • Loading branch information
bassmaster187 committed Feb 1, 2024
1 parent ef44e8b commit 7d19b9a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
4 changes: 4 additions & 0 deletions TeslaLogger/Car.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public double WhTR
private static object initCredentialsLock = new object();
private static object _syncRoot = new object();
internal bool FleetAPI = false;
internal string FleetApiRegionURL = "";

[MethodImpl(MethodImplOptions.Synchronized)]
internal TeslaAPIState GetTeslaAPIState() { return teslaAPIState; }
Expand Down Expand Up @@ -389,6 +390,9 @@ private void InitStage3()
ExitCarThread("DBHelper.DBConnectionstring.Length == 0");
}

webhelper.GetRegion();


if (webhelper.GetVehicles() == "NULL")
{
ExitCarThread("wh.GetVehicles() == NULL");
Expand Down
64 changes: 61 additions & 3 deletions TeslaLogger/WebHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public string apiaddress
get
{
if (car.FleetAPI)
return "https://fleet-api.prd.eu.vn.cloud.tesla.com/";
if (String.IsNullOrEmpty(car.FleetApiRegionURL))
return "https://fleet-api.prd.eu.vn.cloud.tesla.com/";
else
return car.FleetApiRegionURL;

else
return "https://owner-api.teslamotors.com/";
}
Expand Down Expand Up @@ -650,6 +654,60 @@ void SetNewAccessToken(string access_token)
}
}

public string GetRegion()
{
try
{
if (!car.FleetAPI)
return "";

var response = GethttpclientTeslaAPI().GetAsync(new Uri("https://teslalogger.de:4444/api/1/users/region")).Result;
string result = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
if (result.Contains("\"error\""))
{
car.Log(result);
return "";
}

dynamic j = JsonConvert.DeserializeObject(result);
dynamic r = j["response"];
String fleeturl = r["fleet_api_base_url"];
if (fleeturl.StartsWith("https:", StringComparison.InvariantCultureIgnoreCase))
{
if (!fleeturl.EndsWith("/"))
fleeturl += "/";

car.FleetApiRegionURL = fleeturl;
car.Log("Fleet URL: " + fleeturl);
return fleeturl;
}

return "";
}
else
{
Log("Error getting Region: " + (int)response.StatusCode + " / " + response.StatusCode.ToString());
return "";
}

}
catch (ThreadAbortException)
{
System.Diagnostics.Debug.WriteLine("Thread Stop!");
}
catch (Exception ex)
{
car.Log(ex.ToString());
car.CreateExceptionlessClient(ex).MarkAsCritical().Submit();
ExceptionlessClient.Default.ProcessQueueAsync();
}

return "";

}

private string UpdateTeslaTokenFromRefreshTokenFromFleetAPI(string refresh_token)
{
try
Expand Down Expand Up @@ -1975,7 +2033,7 @@ internal void GetAllVehicles(out string resultContent, out Newtonsoft.Json.Linq.
string adresse = "https://owner-api.teslamotors.com/api/1/products?orders=true";

if (car.FleetAPI)
adresse = "https://fleet-api.prd.eu.vn.cloud.tesla.com/api/1/vehicles";
adresse = apiaddress + "api/1/vehicles";

Task<HttpResponseMessage> resultTask;
HttpResponseMessage result;
Expand Down Expand Up @@ -2168,7 +2226,7 @@ public async Task<string> IsOnline(bool returnOnUnauthorized = false)
string adresse = "https://owner-api.teslamotors.com/api/1/products?orders=true";

if (car.FleetAPI)
adresse = "https://fleet-api.prd.eu.vn.cloud.tesla.com/api/1/vehicles";
adresse = apiaddress + "api/1/vehicles";

result = await client.GetAsync(adresse);

Expand Down

0 comments on commit 7d19b9a

Please sign in to comment.