105 lines
4.3 KiB
C#

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<string> GetURLCollection(string barcodeServerIP)
{
List<string> 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<string> GetURLPossible(HttpClient httpClient, List<string> urlCollection, JsonSerializerOptions propertyNameCaseInsensitiveJsonSerializerOptions)
{
List<string> 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<NginxFileSystem[]>(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<string> possibleURLCollection)
{
List<(string, BarcodeRecord)> results = new();
string json;
BarcodeRecord barcodeRecord;
foreach (string possibleURL in possibleURLCollection)
{
try
{
json = httpClient.GetStringAsync(possibleURL).Result;
barcodeRecord = JsonSerializer.Deserialize<BarcodeRecord>(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<string> urlCollection = GetURLCollection(barcodeServerIP);
JsonSerializerOptions propertyNameCaseInsensitiveJsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
List<string> 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;
}
}