fix(adminui): AB forms read/write the factory-bound *Ms duration keys, migrate legacy TimeSpan strings (#576)
Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="ZB.MOM.WW.OtOpcUa.Driver.AbCip.Tests"/>
|
||||
<InternalsVisibleTo Include="ZB.MOM.WW.OtOpcUa.AdminUI.Tests"/>
|
||||
<InternalsVisibleTo Include="ZB.MOM.WW.OtOpcUa.Driver.AbCip.IntegrationTests"/>
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests"/>
|
||||
<InternalsVisibleTo Include="ZB.MOM.WW.OtOpcUa.AdminUI.Tests"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
+58
-7
@@ -16,15 +16,37 @@ internal static class DriverConfigJsonOverlay
|
||||
/// <param name="originalJson">The stored config as loaded; blank or non-object input yields <paramref name="typedJson"/> unchanged.</param>
|
||||
/// <param name="typedJson">The form's own serialised options.</param>
|
||||
/// <param name="ownedPaths">Dot-separated, case-insensitive property paths the form edits (e.g. <c>transport</c>, <c>probe.enabled</c>).</param>
|
||||
public static string Apply(string? originalJson, string typedJson, IEnumerable<string> ownedPaths)
|
||||
/// <param name="timeSpanToMs">
|
||||
/// (TimeSpan-string path, integer-millisecond path) pairs. The typed options serialise durations as
|
||||
/// <c>"00:00:05"</c> but the driver factories bind <c>*Ms</c> integers (#576); each pair is rewritten
|
||||
/// in the typed output (and the legacy TimeSpan path becomes owned, so a stale one is removed).
|
||||
/// </param>
|
||||
public static string Apply(string? originalJson, string typedJson, IEnumerable<string> ownedPaths,
|
||||
IReadOnlyList<(string TimeSpanPath, string MsPath)>? timeSpanToMs = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(originalJson)) return typedJson;
|
||||
JsonObject? original;
|
||||
try { original = JsonNode.Parse(originalJson) as JsonObject; }
|
||||
catch (JsonException) { return typedJson; }
|
||||
if (original is null || JsonNode.Parse(typedJson) is not JsonObject typed) return typedJson;
|
||||
if (JsonNode.Parse(typedJson) is not JsonObject typed) return typedJson;
|
||||
var owned = ownedPaths.ToList();
|
||||
foreach (var (tsPath, msPath) in timeSpanToMs ?? [])
|
||||
{
|
||||
owned.Add(tsPath);
|
||||
owned.Add(msPath);
|
||||
if (Get(typed, tsPath) is JsonValue v && v.TryGetValue<string>(out var text)
|
||||
&& TimeSpan.TryParse(text, System.Globalization.CultureInfo.InvariantCulture, out var ts))
|
||||
{
|
||||
RemovePath(typed, tsPath);
|
||||
Set(typed, msPath, (int)ts.TotalMilliseconds);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var path in ownedPaths)
|
||||
JsonObject? original = null;
|
||||
if (!string.IsNullOrWhiteSpace(originalJson))
|
||||
{
|
||||
try { original = JsonNode.Parse(originalJson) as JsonObject; }
|
||||
catch (JsonException) { /* unusable original: fall through to the typed output */ }
|
||||
}
|
||||
if (original is null) return typed.ToJsonString();
|
||||
|
||||
foreach (var path in owned)
|
||||
{
|
||||
var segments = path.Split('.');
|
||||
JsonObject? from = typed;
|
||||
@@ -50,6 +72,35 @@ internal static class DriverConfigJsonOverlay
|
||||
return original.ToJsonString();
|
||||
}
|
||||
|
||||
private static JsonNode? Get(JsonObject root, string path)
|
||||
{
|
||||
JsonNode? cur = root;
|
||||
foreach (var seg in path.Split('.'))
|
||||
cur = cur is JsonObject o ? Find(o, seg) : null;
|
||||
return cur;
|
||||
}
|
||||
|
||||
private static void RemovePath(JsonObject root, string path)
|
||||
{
|
||||
var segs = path.Split('.');
|
||||
JsonObject? cur = root;
|
||||
for (var i = 0; i < segs.Length - 1 && cur is not null; i++)
|
||||
cur = Find(cur, segs[i]) as JsonObject;
|
||||
if (cur is not null) Remove(cur, segs[^1]);
|
||||
}
|
||||
|
||||
private static void Set(JsonObject root, string path, JsonNode value)
|
||||
{
|
||||
var segs = path.Split('.');
|
||||
var cur = root;
|
||||
for (var i = 0; i < segs.Length - 1; i++)
|
||||
{
|
||||
if (Find(cur, segs[i]) is not JsonObject next) { next = []; cur[segs[i]] = next; }
|
||||
cur = next;
|
||||
}
|
||||
cur[segs[^1]] = value;
|
||||
}
|
||||
|
||||
private static JsonNode? Find(JsonObject o, string key)
|
||||
{
|
||||
foreach (var kv in o)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers;
|
||||
|
||||
/// <summary>
|
||||
/// Shared millisecond helpers for the AB forms (#576): the driver factories bind <c>*Ms</c> integers
|
||||
/// while the forms edit whole seconds, so a sub-second stored value must survive an edit of an
|
||||
/// unrelated field.
|
||||
/// </summary>
|
||||
internal static class DriverConfigMs
|
||||
{
|
||||
/// <summary>Reads an integer-millisecond key at <paramref name="path"/> from <paramref name="json"/>, or null.</summary>
|
||||
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<int>(out var ms) ? ms : null;
|
||||
}
|
||||
catch (JsonException) { return null; }
|
||||
}
|
||||
|
||||
/// <summary>Whole seconds for display (truncating, like the pre-#576 forms).</summary>
|
||||
public static int ToSeconds(TimeSpan t) => (int)t.TotalSeconds;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public static TimeSpan Save(int seconds, TimeSpan? loaded)
|
||||
=> loaded is { } l && ToSeconds(l) == seconds ? l : TimeSpan.FromSeconds(seconds);
|
||||
}
|
||||
+39
-9
@@ -139,9 +139,16 @@
|
||||
/// <summary>Top-level/nested JSON paths this form edits; everything else in the stored config is preserved.</summary>
|
||||
internal static readonly string[] OwnedPaths =
|
||||
[
|
||||
"timeout", "transport", "enableControllerBrowse", "enableDeclarationOnlyUdtGrouping",
|
||||
"enableAlarmProjection", "alarmPollInterval", "maxConcurrentReads", "probeTimeoutSeconds",
|
||||
"probe.enabled", "probe.interval", "probe.timeout", "probe.probeTagPath",
|
||||
"transport", "enableControllerBrowse", "enableDeclarationOnlyUdtGrouping",
|
||||
"enableAlarmProjection", "maxConcurrentReads", "probeTimeoutSeconds",
|
||||
"probe.enabled", "probe.probeTagPath",
|
||||
];
|
||||
|
||||
/// <summary>The factory binds integer-millisecond keys, not the TimeSpan strings the typed options serialise (#576).</summary>
|
||||
internal static readonly (string TimeSpanPath, string MsPath)[] TimeSpanToMs =
|
||||
[
|
||||
("timeout", "timeoutMs"), ("alarmPollInterval", "alarmPollIntervalMs"),
|
||||
("probe.interval", "probe.intervalMs"), ("probe.timeout", "probe.timeoutMs"),
|
||||
];
|
||||
|
||||
// Preserve the driver's existing Devices across a load→save (the device form authors the real endpoints;
|
||||
@@ -154,7 +161,7 @@
|
||||
if (!string.Equals(_lastParsed, DriverConfigJson, StringComparison.Ordinal))
|
||||
{
|
||||
var opts = TryDeserialize(DriverConfigJson) ?? new AbCipDriverOptions();
|
||||
_form = FormModel.FromOptions(opts);
|
||||
_form = FormModel.FromJson(DriverConfigJson, opts);
|
||||
// #577: a brand-new driver (nothing authored yet) must not default the probe ON with no tag path —
|
||||
// that logs an operator warning per driver and runs no probe. An existing config keeps whatever
|
||||
// it stores (an absent "probe" key still means "enabled" to the driver, so don't flip it on edit).
|
||||
@@ -173,7 +180,7 @@
|
||||
|
||||
/// <summary>Serializes options with the form's JSON settings (camelCase, enums as strings).</summary>
|
||||
internal static string Serialize(AbCipDriverOptions options, string? originalJson = null)
|
||||
=> DriverConfigJsonOverlay.Apply(originalJson, JsonSerializer.Serialize(options, _jsonOpts), OwnedPaths);
|
||||
=> DriverConfigJsonOverlay.Apply(originalJson, JsonSerializer.Serialize(options, _jsonOpts), OwnedPaths, TimeSpanToMs);
|
||||
|
||||
private async Task EmitAsync()
|
||||
{
|
||||
@@ -222,8 +229,31 @@
|
||||
// Admin UI probe timeout
|
||||
public int AdminProbeTimeoutSeconds { get; set; } = 5;
|
||||
|
||||
// The loaded durations, so an unchanged seconds field saves the exact stored value (sub-second safe).
|
||||
private TimeSpan? _loadedTimeout, _loadedAlarmPoll, _loadedProbeInterval, _loadedProbeTimeout;
|
||||
|
||||
/// <summary>Loads from the stored JSON: the factory's <c>*Ms</c> keys win; a legacy TimeSpan-string key is the fallback.</summary>
|
||||
public static FormModel FromJson(string? json, AbCipDriverOptions o)
|
||||
{
|
||||
static TimeSpan? Ms(string? j, string path) => DriverConfigMs.Read(j, path) is int ms ? TimeSpan.FromMilliseconds(ms) : null;
|
||||
var f = FromOptions(o);
|
||||
f._loadedTimeout = Ms(json, "timeoutMs") ?? f._loadedTimeout;
|
||||
f._loadedAlarmPoll = Ms(json, "alarmPollIntervalMs") ?? f._loadedAlarmPoll;
|
||||
f._loadedProbeInterval = Ms(json, "probe.intervalMs") ?? f._loadedProbeInterval;
|
||||
f._loadedProbeTimeout = Ms(json, "probe.timeoutMs") ?? f._loadedProbeTimeout;
|
||||
f.TimeoutSeconds = DriverConfigMs.ToSeconds(f._loadedTimeout ?? o.Timeout);
|
||||
f.AlarmPollIntervalSeconds = DriverConfigMs.ToSeconds(f._loadedAlarmPoll ?? o.AlarmPollInterval);
|
||||
f.ProbeIntervalSeconds = DriverConfigMs.ToSeconds(f._loadedProbeInterval ?? o.Probe.Interval);
|
||||
f.ProbeTimeoutSeconds = DriverConfigMs.ToSeconds(f._loadedProbeTimeout ?? o.Probe.Timeout);
|
||||
return f;
|
||||
}
|
||||
|
||||
public static FormModel FromOptions(AbCipDriverOptions o) => new()
|
||||
{
|
||||
_loadedTimeout = o.Timeout,
|
||||
_loadedAlarmPoll = o.AlarmPollInterval,
|
||||
_loadedProbeInterval = o.Probe.Interval,
|
||||
_loadedProbeTimeout = o.Probe.Timeout,
|
||||
TimeoutSeconds = (int)o.Timeout.TotalSeconds,
|
||||
Transport = o.Transport,
|
||||
MaxConcurrentReads = o.MaxConcurrentReads,
|
||||
@@ -245,16 +275,16 @@
|
||||
Probe = new AbCipProbeOptions
|
||||
{
|
||||
Enabled = ProbeEnabled,
|
||||
Interval = TimeSpan.FromSeconds(ProbeIntervalSeconds),
|
||||
Timeout = TimeSpan.FromSeconds(ProbeTimeoutSeconds),
|
||||
Interval = DriverConfigMs.Save(ProbeIntervalSeconds, _loadedProbeInterval),
|
||||
Timeout = DriverConfigMs.Save(ProbeTimeoutSeconds, _loadedProbeTimeout),
|
||||
ProbeTagPath = string.IsNullOrWhiteSpace(ProbeTagPath) ? null : ProbeTagPath,
|
||||
},
|
||||
Timeout = TimeSpan.FromSeconds(TimeoutSeconds),
|
||||
Timeout = DriverConfigMs.Save(TimeoutSeconds, _loadedTimeout),
|
||||
Transport = Transport,
|
||||
MaxConcurrentReads = MaxConcurrentReads,
|
||||
EnableControllerBrowse = EnableControllerBrowse,
|
||||
EnableAlarmProjection = EnableAlarmProjection,
|
||||
AlarmPollInterval = TimeSpan.FromSeconds(AlarmPollIntervalSeconds),
|
||||
AlarmPollInterval = DriverConfigMs.Save(AlarmPollIntervalSeconds, _loadedAlarmPoll),
|
||||
EnableDeclarationOnlyUdtGrouping = EnableDeclarationOnlyUdtGrouping,
|
||||
ProbeTimeoutSeconds = AdminProbeTimeoutSeconds,
|
||||
};
|
||||
|
||||
+32
-7
@@ -97,8 +97,14 @@
|
||||
/// <summary>JSON paths this form edits; everything else in the stored config is preserved.</summary>
|
||||
internal static readonly string[] OwnedPaths =
|
||||
[
|
||||
"timeout", "transport", "probeTimeoutSeconds",
|
||||
"probe.enabled", "probe.interval", "probe.timeout", "probe.probeAddress",
|
||||
"transport", "probeTimeoutSeconds",
|
||||
"probe.enabled", "probe.probeAddress",
|
||||
];
|
||||
|
||||
/// <summary>The factory binds integer-millisecond keys, not the TimeSpan strings the typed options serialise (#576).</summary>
|
||||
internal static readonly (string TimeSpanPath, string MsPath)[] TimeSpanToMs =
|
||||
[
|
||||
("timeout", "timeoutMs"), ("probe.interval", "probe.intervalMs"), ("probe.timeout", "probe.timeoutMs"),
|
||||
];
|
||||
|
||||
// Devices are separate v3 entities — this form doesn't edit them, but preserves the driver's existing
|
||||
@@ -111,7 +117,7 @@
|
||||
if (!string.Equals(_lastParsed, DriverConfigJson, StringComparison.Ordinal))
|
||||
{
|
||||
var opts = TryDeserialize(DriverConfigJson) ?? new AbLegacyDriverOptions();
|
||||
_form = FormModel.FromOptions(opts);
|
||||
_form = FormModel.FromJson(DriverConfigJson, opts);
|
||||
_originalJson = DriverConfigJson;
|
||||
_preservedDevices = opts.Devices;
|
||||
_lastParsed = DriverConfigJson;
|
||||
@@ -125,7 +131,7 @@
|
||||
|
||||
/// <summary>Serializes options with the form's JSON settings (camelCase, enums as strings).</summary>
|
||||
internal static string Serialize(AbLegacyDriverOptions options, string? originalJson = null)
|
||||
=> DriverConfigJsonOverlay.Apply(originalJson, JsonSerializer.Serialize(options, _jsonOpts), OwnedPaths);
|
||||
=> DriverConfigJsonOverlay.Apply(originalJson, JsonSerializer.Serialize(options, _jsonOpts), OwnedPaths, TimeSpanToMs);
|
||||
|
||||
private async Task EmitAsync()
|
||||
{
|
||||
@@ -171,8 +177,27 @@
|
||||
public string? ResilienceConfig { get; set; }
|
||||
public byte[] RowVersion { get; set; } = [];
|
||||
|
||||
private TimeSpan? _loadedTimeout, _loadedProbeInterval, _loadedProbeTimeout;
|
||||
|
||||
/// <summary>Loads from the stored JSON: the factory's <c>*Ms</c> keys win; a legacy TimeSpan-string key is the fallback.</summary>
|
||||
public static FormModel FromJson(string? json, AbLegacyDriverOptions o)
|
||||
{
|
||||
static TimeSpan? Ms(string? j, string path) => DriverConfigMs.Read(j, path) is int ms ? TimeSpan.FromMilliseconds(ms) : null;
|
||||
var f = FromOptions(o);
|
||||
f._loadedTimeout = Ms(json, "timeoutMs") ?? f._loadedTimeout;
|
||||
f._loadedProbeInterval = Ms(json, "probe.intervalMs") ?? f._loadedProbeInterval;
|
||||
f._loadedProbeTimeout = Ms(json, "probe.timeoutMs") ?? f._loadedProbeTimeout;
|
||||
f.TimeoutSeconds = DriverConfigMs.ToSeconds(f._loadedTimeout ?? o.Timeout);
|
||||
f.ProbeIntervalSeconds = DriverConfigMs.ToSeconds(f._loadedProbeInterval ?? o.Probe.Interval);
|
||||
f.ProbeTimeoutSeconds = DriverConfigMs.ToSeconds(f._loadedProbeTimeout ?? o.Probe.Timeout);
|
||||
return f;
|
||||
}
|
||||
|
||||
public static FormModel FromOptions(AbLegacyDriverOptions o) => new()
|
||||
{
|
||||
_loadedTimeout = o.Timeout,
|
||||
_loadedProbeInterval = o.Probe.Interval,
|
||||
_loadedProbeTimeout = o.Probe.Timeout,
|
||||
TimeoutSeconds = (int)o.Timeout.TotalSeconds,
|
||||
Transport = o.Transport,
|
||||
ProbeEnabled = o.Probe.Enabled,
|
||||
@@ -189,11 +214,11 @@
|
||||
Probe = new AbLegacyProbeOptions
|
||||
{
|
||||
Enabled = ProbeEnabled,
|
||||
Interval = TimeSpan.FromSeconds(ProbeIntervalSeconds),
|
||||
Timeout = TimeSpan.FromSeconds(ProbeTimeoutSeconds),
|
||||
Interval = DriverConfigMs.Save(ProbeIntervalSeconds, _loadedProbeInterval),
|
||||
Timeout = DriverConfigMs.Save(ProbeTimeoutSeconds, _loadedProbeTimeout),
|
||||
ProbeAddress = string.IsNullOrWhiteSpace(ProbeAddress) ? null : ProbeAddress,
|
||||
},
|
||||
Timeout = TimeSpan.FromSeconds(TimeoutSeconds),
|
||||
Timeout = DriverConfigMs.Save(TimeoutSeconds, _loadedTimeout),
|
||||
Transport = Transport,
|
||||
ProbeTimeoutSeconds = AdminProbeTimeoutSeconds,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user