From 47950164319ff6f6b2161eddb8290c7f433cfbb5 Mon Sep 17 00:00:00 2001 From: Andrew Whewell Date: Sun, 25 Jun 2023 15:05:57 +0100 Subject: [PATCH] Show credits remaining inline with fetch indicator, fix issue with anonymous access --- .../CommandRunner_Rebroadcast.cs | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/opensky-to-basestation/CommandRunner_Rebroadcast.cs b/opensky-to-basestation/CommandRunner_Rebroadcast.cs index 87d8aae..1fe0397 100644 --- a/opensky-to-basestation/CommandRunner_Rebroadcast.cs +++ b/opensky-to-basestation/CommandRunner_Rebroadcast.cs @@ -13,6 +13,7 @@ using System.IO; using System.Linq; using System.Net.Http; +using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using System.Timers; @@ -79,7 +80,14 @@ private void NetworkListener_Error(Task task) private void StartCallingOpenSkyApi() { _HttpClient = new HttpClient(); - _HttpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(Options.UserName + ":" + Options.Password))); + if(!String.IsNullOrEmpty(Options.UserName) && !String.IsNullOrEmpty(Options.Password)) { + _HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( + "Basic", + Convert.ToBase64String( + Encoding.ASCII.GetBytes($"{Options.UserName}:{Options.Password}") + ) + ); + } _RequestUrl = BuildRequestUrl(); _CallOpenSkyTimer = new Timer() { AutoReset = false, @@ -185,12 +193,13 @@ private async Task FetchFromOpenSky() if(!response.IsSuccessStatusCode) { Console.WriteLine($"[{DateTime.Now}] Warning: OpenSky fetch failed, status {(int)response.StatusCode} {response.StatusCode}"); } else { - string resHeaderStr = response.Headers.ToString(); - string xRateLimitStr = resHeaderStr.Substring(resHeaderStr.IndexOf("X-Rate-Limit-Remaining")); - Console.WriteLine(xRateLimitStr); // X-Rate-Limit-Remaining; + var xRateLimit = response.Headers.TryGetValues("X-Rate-Limit-Remaining", out var values) + ? String.Join(", ", values) + : ""; + var jsonText = await response.Content.ReadAsStringAsync(); if(jsonText != null) { - ShowOpenSkyStateFetched(); + ShowOpenSkyStateFetched($"Credits remaining: {xRateLimit}"); SaveJsonToDiagnosticFile(jsonText); var allStateVectors = JsonConvert.DeserializeObject(jsonText); @@ -231,8 +240,9 @@ private void SaveJsonToDiagnosticFile(string jsonText) } private int _IndicatorPhase; + private int _PreviousSuffixLength; - private void ShowOpenSkyStateFetched() + private void ShowOpenSkyStateFetched(string suffixText) { var ch = '\0'; switch(_IndicatorPhase) { @@ -241,9 +251,16 @@ private void ShowOpenSkyStateFetched() case 2: ch = '-'; break; case 3: ch = '\\'; break; } - Console.Write(ch); - Console.CursorLeft--; _IndicatorPhase = ++_IndicatorPhase % 4; + + suffixText ??= ""; + var clearPreviousSuffix = suffixText.Length >= _PreviousSuffixLength + ? "" + : new String(' ', _PreviousSuffixLength - suffixText.Length); + _PreviousSuffixLength = suffixText.Length; + + Console.Write($"{ch} {suffixText}{clearPreviousSuffix}"); + Console.SetCursorPosition(0, Console.CursorTop); } } }