From 7bd3be957884b617f7fbad2f44c7f3b0d342a254 Mon Sep 17 00:00:00 2001 From: Flo Date: Wed, 10 Jul 2024 14:59:11 +0200 Subject: [PATCH] if (r4.ContainsKey("tokens") && r4["tokens"] != null) --- TeslaLogger/WebHelper.cs | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/TeslaLogger/WebHelper.cs b/TeslaLogger/WebHelper.cs index 7ff5cc007..4541a99d9 100644 --- a/TeslaLogger/WebHelper.cs +++ b/TeslaLogger/WebHelper.cs @@ -692,7 +692,8 @@ public string GetRegion() } HttpClient httpClientTeslaAPI = GetHttpClientTeslaAPI(); - using (var request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://teslalogger.de:4444/api/1/users/region"))) { + using (var request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://teslalogger.de:4444/api/1/users/region"))) + { request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Tesla_token); Tools.DebugLog($"GetRegion #{car.CarInDB} request: {request.RequestUri}"); HttpResponseMessage response = httpClientTeslaAPI.SendAsync(request).Result; @@ -1606,9 +1607,10 @@ public void ResetLastChargingState() internal bool IsCharging(bool justCheck = false, bool noMemcache = false) { - if (car.FleetAPI && justCheck) { - return car.telemetry?.IsCharging ?? false; - } + if (car.FleetAPI && justCheck) + { + return car.telemetry?.IsCharging ?? false; + } string resultContent = ""; try @@ -2474,14 +2476,13 @@ public async Task IsOnline(bool returnOnUnauthorized = false) } string state = r4["state"].ToString(); - string temp_Tesla_Streamingtoken = r4["tokens"][0].ToString(); - - if (temp_Tesla_Streamingtoken != Tesla_Streamingtoken) + if (r4.ContainsKey("tokens") && r4["tokens"] != null) { - Tesla_Streamingtoken = temp_Tesla_Streamingtoken; - //Log("Streamingtoken changed (IsOnline): " + Tools.ObfuscateString(Tesla_Streamingtoken)); - - // can be ignored, is not used at the moment car.Log("Tesla_Streamingtoken changed!"); + string temp_Tesla_Streamingtoken = r4["tokens"][0].ToString(); + if (temp_Tesla_Streamingtoken != Tesla_Streamingtoken) + { + Tesla_Streamingtoken = temp_Tesla_Streamingtoken; + } } try @@ -3258,8 +3259,8 @@ public bool IsDriving(bool justinsertdb = false) if (car.FleetAPI && !justinsertdb) { if (car.telemetry?.Driving == false) - { - return false; + { + return false; } } @@ -4942,7 +4943,7 @@ public async Task GetCommand(string cmd, bool noMemcache = false) { sleep = sleep * 1000; } - else + else { sleep = (random.Next(5000) + 60000) * 1000; } @@ -4958,7 +4959,7 @@ public async Task GetCommand(string cmd, bool noMemcache = false) if (result.Headers.TryGetValues("ratelimit-reset", out var v3)) l += ", ratelimit-reset: " + v3.First(); - l += ", sleep till: "+ DateTime.Now.AddMilliseconds(sleep).ToString(Tools.ciDeDE); + l += ", sleep till: " + DateTime.Now.AddMilliseconds(sleep).ToString(Tools.ciDeDE); l += $", CommandCounter: {commandCounter} Drive: {commandCounterDrive} Charge: {commandCounterCharging} Online: {commandcounterOnline}"; @@ -4969,7 +4970,7 @@ public async Task GetCommand(string cmd, bool noMemcache = false) car.CreateExeptionlessLog("TooManyRequests", l, LogLevel.Warn).Submit(); - Log(l1+l); + Log(l1 + l); car.CurrentJSON.FatalError += " TooManyRequests"; car.CurrentJSON.CreateCurrentJSON(); Thread.Sleep(sleep); @@ -5848,7 +5849,7 @@ internal static bool BranchExists(string branch, out HttpStatusCode statusCode) else if (g.StatusCode == HttpStatusCode.NotFound) { return false; - } + } } catch (Exception ex) {