using System.Collections; using System.Text.Json; using LmxFakeProxy.Grpc; namespace LmxFakeProxy; public class TagMapper { private readonly string _prefix; public TagMapper(string prefix) { _prefix = prefix; } public string ToOpcNodeId(string lmxTag) => $"{_prefix}{lmxTag}"; public static object ParseWriteValue(string value) { if (double.TryParse(value, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var d)) return d; if (bool.TryParse(value, out var b)) return b; return value; } public static string MapQuality(uint statusCode) { if (statusCode == 0) return "Good"; if ((statusCode & 0x80000000) != 0) return "Bad"; return "Uncertain"; } public static string FormatValue(object? value) { if (value is null) return string.Empty; if (value is Array or IList) return JsonSerializer.Serialize(value); return value.ToString() ?? string.Empty; } public static VtqMessage ToVtqMessage(string tag, object? value, DateTime timestampUtc, uint statusCode) { return new VtqMessage { Tag = tag, Value = FormatValue(value), TimestampUtcTicks = timestampUtc.Ticks, Quality = MapQuality(statusCode) }; } }