using System.Text.Json; using System.Text.Json.Nodes; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers; /// /// Shared millisecond helpers for the AB forms: the driver factories bind *Ms integers /// while the forms edit whole seconds, so a sub-second stored value must survive an edit of an /// unrelated field. /// internal static class DriverConfigMs { /// Reads an integer-millisecond key at from , or null. /// The driver config JSON, which may be null or blank. /// The dot-separated key path to read. /// The integer value, or null when the key is absent or not an integer. public static int? Read(string? json, string path) { if (string.IsNullOrWhiteSpace(json)) return null; try { JsonNode? cur = JsonNode.Parse(json); foreach (var seg in path.Split('.')) { if (cur is not JsonObject o) return null; cur = o.FirstOrDefault(kv => string.Equals(kv.Key, seg, StringComparison.OrdinalIgnoreCase)).Value; } return cur is JsonValue v && v.TryGetValue(out var ms) ? ms : null; } catch (JsonException) { return null; } } /// Whole seconds for display (truncating, like the earlier forms). /// The duration to convert. /// The whole seconds in . public static int ToSeconds(TimeSpan t) => (int)t.TotalSeconds; /// /// The duration to save: the loaded value when the operator left the seconds field unchanged /// (keeps 500 ms from collapsing to 0 s), else the edited whole seconds. /// /// The whole seconds currently shown in the form. /// The duration originally loaded from config, if any. /// The duration to persist. public static TimeSpan Save(int seconds, TimeSpan? loaded) => loaded is { } l && ToSeconds(l) == seconds ? l : TimeSpan.FromSeconds(seconds); }