177 lines
6.5 KiB
C#
177 lines
6.5 KiB
C#
using System.Text.Json;
|
|
using AutoForge.Application.Features.DisplayUnits;
|
|
using AutoForge.Diagnostics;
|
|
|
|
namespace AutoForge.UI.Features.DisplayUnits;
|
|
|
|
public sealed record DisplayUnitText(string Value, string Unit, string? Notice = null, string? Error = null);
|
|
|
|
public static class DisplayUnitPresentation
|
|
{
|
|
public static string FormatSourceValue(JsonElement sourceValue) =>
|
|
sourceValue.ValueKind switch
|
|
{
|
|
JsonValueKind.Undefined => "undefined",
|
|
JsonValueKind.Null => "null",
|
|
JsonValueKind.String => sourceValue.GetString() ?? string.Empty,
|
|
_ => sourceValue.GetRawText()
|
|
};
|
|
|
|
public static DisplayUnitText Project(
|
|
JsonElement sourceValue,
|
|
string sourceUnit,
|
|
DisplayUnitSelection selection,
|
|
int precision = 3)
|
|
{
|
|
return Project(sourceValue, sourceUnit, selection, SignalQuality.Good, precision);
|
|
}
|
|
|
|
public static DisplayUnitText Project(
|
|
JsonElement sourceValue,
|
|
string sourceUnit,
|
|
DisplayUnitSelection selection,
|
|
SignalQuality quality,
|
|
int precision = 3)
|
|
{
|
|
if (quality == SignalQuality.Unknown)
|
|
{
|
|
return new DisplayUnitText("--", sourceUnit, Notice: "The source quality is Unknown; the current measurement is unavailable.");
|
|
}
|
|
|
|
if (!Enum.IsDefined(quality))
|
|
{
|
|
return new DisplayUnitText("--", sourceUnit, Error: "The source quality is undefined.");
|
|
}
|
|
|
|
if (sourceValue.ValueKind is JsonValueKind.Null or JsonValueKind.Undefined)
|
|
{
|
|
return new DisplayUnitText("--", sourceUnit, Error: "The source measurement is missing or null.");
|
|
}
|
|
|
|
if (sourceUnit == "boolean")
|
|
{
|
|
if (sourceValue.ValueKind is JsonValueKind.True or JsonValueKind.False)
|
|
{
|
|
return new DisplayUnitText(sourceValue.GetRawText(), sourceUnit, GetQualityNotice(quality));
|
|
}
|
|
|
|
return new DisplayUnitText("--", sourceUnit, Error: "The boolean source value has the wrong JSON type.");
|
|
}
|
|
|
|
if (!DisplayUnitConverter.IsKnownNumericUnit(sourceUnit))
|
|
{
|
|
return new DisplayUnitText(
|
|
FormatSourceValue(sourceValue),
|
|
sourceUnit,
|
|
CombineNotices(
|
|
$"Unit '{sourceUnit}' is not in the supported display mapping; showing the exact source value and unit.",
|
|
GetQualityNotice(quality)));
|
|
}
|
|
|
|
if (sourceValue.ValueKind != JsonValueKind.Number ||
|
|
!sourceValue.TryGetDouble(out var number) ||
|
|
!double.IsFinite(number))
|
|
{
|
|
return new DisplayUnitText("--", sourceUnit, Error: "The numeric source value is missing, non-numeric or non-finite.");
|
|
}
|
|
|
|
return ProjectNumber(number, sourceUnit, selection, quality, precision);
|
|
}
|
|
|
|
public static DisplayUnitText Project(
|
|
double? sourceValue,
|
|
string sourceText,
|
|
string sourceUnit,
|
|
DisplayUnitSelection selection,
|
|
int precision = 3) =>
|
|
Project(sourceValue, sourceText, sourceUnit, selection, SignalQuality.Good, precision);
|
|
|
|
public static DisplayUnitText Project(
|
|
double? sourceValue,
|
|
string sourceText,
|
|
string sourceUnit,
|
|
DisplayUnitSelection selection,
|
|
SignalQuality quality,
|
|
int precision = 3)
|
|
{
|
|
if (quality == SignalQuality.Unknown)
|
|
{
|
|
return new DisplayUnitText("--", sourceUnit, Notice: "The source quality is Unknown; the current measurement is unavailable.");
|
|
}
|
|
|
|
if (!Enum.IsDefined(quality))
|
|
{
|
|
return new DisplayUnitText("--", sourceUnit, Error: "The source quality is undefined.");
|
|
}
|
|
|
|
if (sourceUnit == "boolean")
|
|
{
|
|
if (sourceValue is null && bool.TryParse(sourceText, out var boolean))
|
|
{
|
|
return new DisplayUnitText(boolean ? "true" : "false", sourceUnit, GetQualityNotice(quality));
|
|
}
|
|
|
|
return new DisplayUnitText("--", sourceUnit, Error: "The boolean source value is missing or has the wrong type.");
|
|
}
|
|
|
|
if (sourceValue is not { } numericValue)
|
|
{
|
|
if (string.IsNullOrEmpty(sourceText) || StringComparer.OrdinalIgnoreCase.Equals(sourceText, "null"))
|
|
{
|
|
return new DisplayUnitText("--", sourceUnit, Error: "The source measurement is missing or null.");
|
|
}
|
|
|
|
return DisplayUnitConverter.IsKnownNumericUnit(sourceUnit)
|
|
? new DisplayUnitText("--", sourceUnit, Error: "The numeric source value is missing, non-numeric or non-finite.")
|
|
: new DisplayUnitText(
|
|
sourceText,
|
|
sourceUnit,
|
|
CombineNotices(
|
|
$"Unit '{sourceUnit}' is not in the supported display mapping; showing the exact source value and unit.",
|
|
GetQualityNotice(quality)));
|
|
}
|
|
|
|
if (!double.IsFinite(numericValue))
|
|
{
|
|
return new DisplayUnitText("--", sourceUnit, Error: "The numeric source value is non-finite.");
|
|
}
|
|
|
|
if (!DisplayUnitConverter.IsKnownNumericUnit(sourceUnit))
|
|
{
|
|
return new DisplayUnitText(
|
|
sourceText,
|
|
sourceUnit,
|
|
CombineNotices(
|
|
$"Unit '{sourceUnit}' is not in the supported display mapping; showing the exact source value and unit.",
|
|
GetQualityNotice(quality)));
|
|
}
|
|
|
|
return ProjectNumber(numericValue, sourceUnit, selection, quality, precision);
|
|
}
|
|
|
|
private static DisplayUnitText ProjectNumber(
|
|
double value,
|
|
string sourceUnit,
|
|
DisplayUnitSelection selection,
|
|
SignalQuality quality,
|
|
int precision)
|
|
{
|
|
var measurement = DisplayUnitConverter.Project(value, sourceUnit, selection);
|
|
return new DisplayUnitText(
|
|
DisplayUnitConverter.Format(measurement, precision),
|
|
measurement.Unit,
|
|
CombineNotices(measurement.Notice, GetQualityNotice(quality)),
|
|
measurement.Error);
|
|
}
|
|
|
|
private static string? GetQualityNotice(SignalQuality quality) =>
|
|
quality == SignalQuality.Uncertain
|
|
? "The source marked this finite value Uncertain."
|
|
: null;
|
|
|
|
private static string? CombineNotices(string? first, string? second) =>
|
|
first is null ? second :
|
|
second is null ? first :
|
|
$"{first} {second}";
|
|
}
|