using OI.Metrology.Shared.Models; using System.Collections.Specialized; using System.Net; using System.Text.Json; using System.Web; namespace OI.Metrology.Wafer.Counter.Helper; public class ParameterHelper { private static Dictionary GetKeyValuePairs(QueryString queryString) { Dictionary results = []; if (queryString.HasValue) { NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(queryString.Value); foreach (string? key in nameValueCollection.AllKeys) { if (key is null) continue; results.Add(key, nameValueCollection[key]); } } return results; } internal static CharacterizationParameters? GetCharacterizationParameters(QueryString queryString) { CharacterizationParameters? result; Dictionary keyValuePairs = GetKeyValuePairs(queryString); string json = JsonSerializer.Serialize(keyValuePairs, new JsonSerializerOptions() { WriteIndented = true }); result = string.IsNullOrEmpty(json) ? null : JsonSerializer.Deserialize(json, CharacterizationParametersSourceGenerationContext.Default.CharacterizationParameters); return result; } private static string? GetQueryString(Stream stream) { string? result; if (!stream.CanRead) result = null; else { Task task = new StreamReader(stream).ReadToEndAsync(); result = task.Result; } return result; } private static Dictionary GetKeyValuePairs(string? queryString) { Dictionary results = []; if (!string.IsNullOrEmpty(queryString)) { NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(queryString); foreach (string? key in nameValueCollection.AllKeys) { if (key is null) continue; results.Add(key, nameValueCollection[key]); } } return results; } internal static PollValue? GetPollValue(IPAddress? remoteIpAddress, Stream stream) { PollValue? result; string? queryString = GetQueryString(stream); Dictionary keyValuePairs = GetKeyValuePairs(queryString); string json = JsonSerializer.Serialize(keyValuePairs, new JsonSerializerOptions() { WriteIndented = true }); result = string.IsNullOrEmpty(json) ? null : JsonSerializer.Deserialize(json, PollValueSourceGenerationContext.Default.PollValue); if (result is not null) { result = new(null, result.Id, result.Page, queryString, remoteIpAddress is null ? string.Empty : remoteIpAddress.ToString(), result.Time, result.Value); json = JsonSerializer.Serialize(result, PollValueSourceGenerationContext.Default.PollValue); result = new(json, result.Id, result.Page, queryString, remoteIpAddress is null ? string.Empty : remoteIpAddress.ToString(), result.Time, result.Value); } return result; } }