using Opc.Ua;
namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Helpers;
///
/// Parses node ID strings into OPC UA objects.
/// Supports standard OPC UA format (e.g., "ns=2;s=MyNode", "i=85") and bare numeric IDs.
///
public static class NodeIdParser
{
///
/// Parses a string into a . Returns null if the input is null or empty.
///
/// The node ID string to parse.
/// A parsed , or null if input is null/empty.
/// Thrown when the string cannot be parsed as a valid NodeId.
public static NodeId? Parse(string? nodeIdString)
{
if (string.IsNullOrWhiteSpace(nodeIdString))
return null;
var trimmed = nodeIdString.Trim();
// Standard OPC UA format: ns=X;s=..., ns=X;i=..., ns=X;g=..., ns=X;b=...
// Also: s=..., i=..., g=..., b=... (namespace 0 implied)
if (trimmed.Contains('='))
try
{
return NodeId.Parse(trimmed);
}
catch (Exception ex)
{
throw new FormatException($"Invalid node ID format: '{nodeIdString}'", ex);
}
// Bare numeric: treat as namespace 0, numeric identifier
if (uint.TryParse(trimmed, out var numericId)) return new NodeId(numericId);
throw new FormatException(
$"Invalid node ID format: '{nodeIdString}'. Expected format like 'ns=2;s=MyNode', 'i=85', or a numeric ID.");
}
///
/// Parses a string into a , throwing if the input is null or empty.
///
/// The node ID string to parse.
/// A parsed .
/// Thrown when the input is null or empty.
/// Thrown when the string cannot be parsed as a valid NodeId.
public static NodeId ParseRequired(string? nodeIdString)
{
var result = Parse(nodeIdString);
if (result == null)
throw new ArgumentException("Node ID is required but was not provided.");
return result;
}
}