refactor: migrate client applications to OPC UA SDK 2.0

This commit is contained in:
Joseph Doherty
2026-11-10 10:08:18 -05:00
parent 9af1a824b3
commit 586ce9422d
21 changed files with 247 additions and 165 deletions
@@ -210,7 +210,7 @@ public class HistoryReadCommand : CommandBase
: StatusCode.IsBad(dv.StatusCode) ? "Bad"
: "Uncertain";
await console.Output.WriteLineAsync(
$"{dv.SourceTimestamp.ToString("O"),-35} {dv.Value?.ToString() ?? "(none)",-15} {status,-10} " +
$"{dv.SourceTimestamp.ToString("O"),-35} {ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers.ValueConverter.ToClrObject(dv.WrappedValue)?.ToString() ?? "(none)",-15} {status,-10} " +
$"{e.UpdateType,-8} {e.UserName ?? "",-20} {e.ModificationTime:O}");
}
@@ -244,7 +244,7 @@ public class HistoryReadCommand : CommandBase
: StatusCode.IsBad(dv.StatusCode) ? "Bad"
: "Uncertain";
await console.Output.WriteLineAsync(
$"{dv.SourceTimestamp.ToString("O"),-35} {dv.Value,-15} {status}");
$"{dv.SourceTimestamp.ToString("O"),-35} {ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers.ValueConverter.ToClrObject(dv.WrappedValue),-15} {status}");
}
await console.Output.WriteLineAsync();
@@ -276,23 +276,25 @@ public class HistoryReadCommand : CommandBase
internal static EventFilter BuildEventFilter(IReadOnlyList<string> fields, NodeId? ofType)
{
var filter = new EventFilter();
var clauses = new List<SimpleAttributeOperand>();
foreach (var field in fields)
{
var type = string.Equals(field, "ConditionName", StringComparison.Ordinal)
? ObjectTypeIds.ConditionType
: ObjectTypeIds.BaseEventType;
filter.SelectClauses.Add(new SimpleAttributeOperand(type, new QualifiedName(field)) { AttributeId = Attributes.Value });
clauses.Add(new SimpleAttributeOperand(type, new QualifiedName(field)) { AttributeId = Attributes.Value });
}
filter.SelectClauses = clauses.ToArrayOf();
if (ofType is not null)
filter.WhereClause.Push(FilterOperator.OfType, new LiteralOperand(new Variant(ofType)));
filter.WhereClause.Push(FilterOperator.OfType, Variant.From(ofType.Value));
return filter;
}
/// <summary>Formats one projected event field for the console: byte strings as hex, times as ISO 8601, null as <c>-</c>.</summary>
/// <param name="field">The field value.</param>
/// <returns>Its display text.</returns>
private static string FormatEventField(Variant field) => field.Value switch
private static string FormatEventField(Variant field) => ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers.ValueConverter.ToClrObject(field) switch
{
null => "-",
byte[] bytes => Convert.ToHexString(bytes),
@@ -89,11 +89,11 @@ public class HistoryUpdateCommand : CommandBase
throw new CommandException(
$"--value and --time must be supplied in pairs ({values.Count} value(s), {times.Count} time(s)).");
var updates = new DataValueCollection();
var updates = new List<DataValue>();
for (var i = 0; i < values.Count; i++)
{
var ts = ParseTime(times[i], "--time");
updates.Add(new DataValue(new Variant(ParseValue(values[i], Type)), StatusCodes.Good, ts, ts));
updates.Add(new DataValue(ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers.ValueConverter.ToVariant(ParseValue(values[i], Type)), StatusCodes.Good, ts, ts));
}
return new UpdateDataDetails
@@ -105,7 +105,7 @@ public class HistoryUpdateCommand : CommandBase
"replace" => PerformUpdateType.Replace,
_ => PerformUpdateType.Update
},
UpdateValues = updates
UpdateValues = updates.ToArrayOf()
};
}
case "delete-raw":
@@ -134,9 +134,9 @@ public class HistoryUpdateCommand : CommandBase
throw new CommandException("--mode delete-at-time takes --time only; --value is not allowed.");
if (times.Count == 0)
throw new CommandException("--mode delete-at-time requires at least one --time.");
var req = new DateTimeCollection();
var req = new List<DateTimeUtc>();
foreach (var t in times) req.Add(ParseTime(t, "--time"));
return new DeleteAtTimeDetails { NodeId = nodeId, ReqTimes = req };
return new DeleteAtTimeDetails { NodeId = nodeId, ReqTimes = req.ToArrayOf() };
}
}
}
@@ -238,8 +238,8 @@ public class HistoryUpdateCommand : CommandBase
/// <returns>The status name, or <c>0xXXXXXXXX</c>.</returns>
private static string StatusName(StatusCode code)
{
var name = StatusCodes.GetBrowseName(code.Code);
if (string.IsNullOrEmpty(name)) name = StatusCodes.GetBrowseName(code.Code & 0xFFFF0000u);
var name = code.SymbolicId;
if (string.IsNullOrEmpty(name)) name = new StatusCode(code.Code & 0xFFFF0000u).SymbolicId;
return string.IsNullOrEmpty(name) ? $"0x{code.Code:X8}" : name;
}
@@ -247,8 +247,8 @@ public class HistoryUpdateCommand : CommandBase
{
return details switch
{
UpdateDataDetails u => u.UpdateValues.Select(v => v.SourceTimestamp.ToString("O")).ToList(),
DeleteAtTimeDetails d => d.ReqTimes.Select(t => t.ToString("O")).ToList(),
UpdateDataDetails u => (u.UpdateValues.ToArray() ?? []).Select(v => v.SourceTimestamp.ToString("O")).ToList(),
DeleteAtTimeDetails d => (d.ReqTimes.ToArray() ?? []).Select(t => t.ToString("O")).ToList(),
DeleteRawModifiedDetails r => [$"{r.StartTime:O}..{r.EndTime:O}"],
_ => []
};
@@ -59,12 +59,12 @@ public class ReadCommand : CommandBase
// not crash with a stack trace — session-level failures still propagate.
await console.Output.WriteLineAsync($"Node: {NodeId}");
await console.Output.WriteLineAsync($"Value: ");
await console.Output.WriteLineAsync($"Status: {(StatusCode)ex.StatusCode} ({StatusCodes.GetBrowseName(ex.StatusCode)})");
await console.Output.WriteLineAsync($"Status: {(StatusCode)ex.StatusCode} ({ex.StatusCode.SymbolicId})");
return;
}
await console.Output.WriteLineAsync($"Node: {NodeId}");
await console.Output.WriteLineAsync($"Value: {value.Value}");
await console.Output.WriteLineAsync($"Value: {ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers.ValueConverter.ToClrObject(value.WrappedValue)}");
await console.Output.WriteLineAsync($"Status: {value.StatusCode}");
await console.Output.WriteLineAsync($"Source Time: {value.SourceTimestamp:O}");
await console.Output.WriteLineAsync($"Server Time: {value.ServerTimestamp:O}");
@@ -123,14 +123,14 @@ public class SubscribeCommand : CommandBase
try
{
var key = e.NodeId.ToString();
lastStatus[key] = (e.Value.StatusCode, DateTime.UtcNow, e.Value.Value);
lastStatus[key] = (e.Value.StatusCode, DateTime.UtcNow, ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers.ValueConverter.ToClrObject(e.Value.WrappedValue));
updateCount.AddOrUpdate(key, 1, (_, v) => v + 1);
if (!StatusCode.IsGood(e.Value.StatusCode))
everBad.TryAdd(key, 0);
if (!Quiet)
{
var line =
$"[{e.Value.SourceTimestamp:O}] {displayNameByNodeId.GetValueOrDefault(key, key)} = {e.Value.Value} ({e.Value.StatusCode})";
$"[{e.Value.SourceTimestamp:O}] {displayNameByNodeId.GetValueOrDefault(key, key)} = {ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers.ValueConverter.ToClrObject(e.Value.WrappedValue)} ({e.Value.StatusCode})";
outputChannel.Writer.TryWrite(line);
}
}
@@ -271,7 +271,7 @@ public class SubscribeCommand : CommandBase
var childPath = $"{parentPath}/{child.DisplayName}";
if (child.NodeClass == "Variable")
{
into.Add((nodeId, childPath));
into.Add((nodeId.Value, childPath));
}
if (child.HasChildren)
{
@@ -54,7 +54,7 @@ public class WriteCommand : CommandBase
// Read current value to determine type for conversion
var currentValue = await service.ReadValueAsync(nodeId, ct);
var typedValue = ValueConverter.ConvertValue(Value, currentValue.Value);
var typedValue = ValueConverter.ConvertValue(Value, ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers.ValueConverter.ToClrObject(currentValue.WrappedValue));
var statusCode = await service.WriteValueAsync(nodeId, typedValue, ct);
@@ -52,6 +52,6 @@ public static class NodeIdParser
var result = Parse(nodeIdString);
if (result == null)
throw new ArgumentException("Node ID is required but was not provided.");
return result;
return result.Value;
}
}