using System; using System.Collections.Generic; using System.Globalization; using System.Net.Http; using System.Text.Json; namespace Adaptation.FileHandlers.OpenInsightMetrologyViewer; public class TestMe { private static List GetURLCollection(string barcodeServerIP) { List results = new(); int weekOfYear; string checkURL; DateTime dateTime; string weekDirectory; string weekOfYearPadded; string lastURL = string.Empty; Calendar calendar = new CultureInfo("en-US").Calendar; for (int i = 1; i < 3; i++) { if (i == 1) dateTime = DateTime.Now; else dateTime = DateTime.Now.AddHours(-4); weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday) - 1; weekOfYearPadded = weekOfYear.ToString("00"); weekDirectory = $"{dateTime:yyyy}_Week_{weekOfYearPadded}/{dateTime:yyyy-MM-dd}"; checkURL = string.Concat("http://", barcodeServerIP, '/', weekDirectory); if (i == 1 || checkURL != lastURL) { results.Add(string.Concat(checkURL, "/A")); results.Add(string.Concat(checkURL, "/B")); } lastURL = checkURL; } return results; } private static List GetURLPossible(HttpClient httpClient, List urlCollection, JsonSerializerOptions propertyNameCaseInsensitiveJsonSerializerOptions) { List results = new(); string json; NginxFileSystem[] nginxFileSystemCollection; DateTime minimumDateTime = DateTime.Now.AddHours(-4); string nginxFormat = "ddd, dd MMM yyyy HH:mm:ss zzz"; foreach (string url in urlCollection) { try { json = httpClient.GetStringAsync(url).Result; nginxFileSystemCollection = JsonSerializer.Deserialize(json, propertyNameCaseInsensitiveJsonSerializerOptions); foreach (NginxFileSystem nginxFileSystem in nginxFileSystemCollection) { if (!DateTime.TryParseExact(nginxFileSystem.MTime.Replace("GMT", "+00:00"), nginxFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime)) continue; if (dateTime < minimumDateTime) continue; results.Add(string.Concat(url, '/', nginxFileSystem.Name)); } } catch { } } return results; } private static List<(string, BarcodeRecord)> GetBarcodePossible(HttpClient httpClient, JsonSerializerOptions propertyNameCaseInsensitiveJsonSerializerOptions, List possibleURLCollection) { List<(string, BarcodeRecord)> results = new(); string json; BarcodeRecord barcodeRecord; foreach (string possibleURL in possibleURLCollection) { try { json = httpClient.GetStringAsync(possibleURL).Result; barcodeRecord = JsonSerializer.Deserialize(json, propertyNameCaseInsensitiveJsonSerializerOptions); results.Add(new(possibleURL, barcodeRecord)); } catch { } } return results; } public static string GetBarcode(string barcodeServerIP) { string result = string.Empty; using HttpClient httpClient = new(); List urlCollection = GetURLCollection(barcodeServerIP); JsonSerializerOptions propertyNameCaseInsensitiveJsonSerializerOptions = new() { PropertyNameCaseInsensitive = true }; List possibleURLCollection = GetURLPossible(httpClient, urlCollection, propertyNameCaseInsensitiveJsonSerializerOptions); List<(string, BarcodeRecord)> possibleBarcodeCollection = GetBarcodePossible(httpClient, propertyNameCaseInsensitiveJsonSerializerOptions, possibleURLCollection); foreach ((string url, BarcodeRecord barcodeRecord) in possibleBarcodeCollection) { if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(barcodeRecord.Barcode)) continue; } return result; } }