Implement LmxOpcUa server — all 6 phases complete
Full OPC UA server on .NET Framework 4.8 (x86) exposing AVEVA System Platform Galaxy tags via MXAccess. Mirrors Galaxy object hierarchy as OPC UA address space, translating contained-name browse paths to tag-name runtime references. Components implemented: - Configuration: AppConfiguration with 4 sections, validator - Domain: ConnectionState, Quality, Vtq, MxDataTypeMapper, error codes - MxAccess: StaComThread, MxAccessClient (partial classes), MxProxyAdapter using strongly-typed ArchestrA.MxAccess COM interop - Galaxy Repository: SQL queries (hierarchy, attributes, change detection), ChangeDetectionService with auto-rebuild on deploy - OPC UA Server: LmxNodeManager (CustomNodeManager2), LmxOpcUaServer, OpcUaServerHost with programmatic config, SecurityPolicy None - Status Dashboard: HTTP server with HTML/JSON/health endpoints - Integration: Full 14-step startup, graceful shutdown, component wiring 175 tests (174 unit + 1 integration), all passing. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
92
tools/opcuacli-dotnet/Commands/BrowseCommand.cs
Normal file
92
tools/opcuacli-dotnet/Commands/BrowseCommand.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using CliFx;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Infrastructure;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Client;
|
||||
|
||||
namespace OpcUaCli.Commands;
|
||||
|
||||
[Command("browse", Description = "Browse the OPC UA address space")]
|
||||
public class BrowseCommand : ICommand
|
||||
{
|
||||
[CommandOption("url", 'u', Description = "OPC UA server endpoint URL", IsRequired = true)]
|
||||
public string Url { get; init; } = default!;
|
||||
|
||||
[CommandOption("node", 'n', Description = "Node ID to browse (default: Objects folder)")]
|
||||
public string? NodeId { get; init; }
|
||||
|
||||
[CommandOption("depth", 'd', Description = "Maximum browse depth")]
|
||||
public int Depth { get; init; } = 1;
|
||||
|
||||
[CommandOption("recursive", 'r', Description = "Browse recursively (uses --depth as max depth)")]
|
||||
public bool Recursive { get; init; }
|
||||
|
||||
public async ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
using var session = await OpcUaHelper.ConnectAsync(Url);
|
||||
|
||||
var startNode = string.IsNullOrEmpty(NodeId)
|
||||
? ObjectIds.ObjectsFolder
|
||||
: new NodeId(NodeId);
|
||||
|
||||
var maxDepth = Recursive ? Depth : 1;
|
||||
await BrowseNodeAsync(session, console, startNode, maxDepth, 0);
|
||||
}
|
||||
|
||||
private static async Task BrowseNodeAsync(
|
||||
ISession session, IConsole console, NodeId nodeId, int maxDepth, int currentDepth)
|
||||
{
|
||||
var indent = new string(' ', currentDepth * 2);
|
||||
|
||||
var (_, continuationPoint, references) = await session.BrowseAsync(
|
||||
null,
|
||||
null,
|
||||
nodeId,
|
||||
0u,
|
||||
BrowseDirection.Forward,
|
||||
ReferenceTypeIds.HierarchicalReferences,
|
||||
true,
|
||||
(uint)NodeClass.Object | (uint)NodeClass.Variable | (uint)NodeClass.Method);
|
||||
|
||||
if (references == null) return;
|
||||
|
||||
// Handle continuation points for large result sets
|
||||
while (references.Count > 0)
|
||||
{
|
||||
foreach (var reference in references)
|
||||
{
|
||||
var nodeClass = reference.NodeClass;
|
||||
var marker = nodeClass switch
|
||||
{
|
||||
NodeClass.Object => "[Object]",
|
||||
NodeClass.Variable => "[Variable]",
|
||||
NodeClass.Method => "[Method]",
|
||||
_ => $"[{nodeClass}]"
|
||||
};
|
||||
|
||||
await console.Output.WriteLineAsync(
|
||||
$"{indent}{marker} {reference.DisplayName} (NodeId: {reference.NodeId})");
|
||||
|
||||
if (currentDepth + 1 < maxDepth && nodeClass == NodeClass.Object)
|
||||
{
|
||||
var childNodeId = ExpandedNodeId.ToNodeId(
|
||||
reference.NodeId, session.NamespaceUris);
|
||||
await BrowseNodeAsync(session, console, childNodeId, maxDepth, currentDepth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Follow continuation point if present
|
||||
if (continuationPoint != null && continuationPoint.Length > 0)
|
||||
{
|
||||
var (_, nextCp, nextRefs) = await session.BrowseNextAsync(
|
||||
null, false, continuationPoint);
|
||||
continuationPoint = nextCp;
|
||||
references = nextRefs;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
22
tools/opcuacli-dotnet/Commands/ConnectCommand.cs
Normal file
22
tools/opcuacli-dotnet/Commands/ConnectCommand.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using CliFx;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Infrastructure;
|
||||
|
||||
namespace OpcUaCli.Commands;
|
||||
|
||||
[Command("connect", Description = "Test connection to an OPC UA server")]
|
||||
public class ConnectCommand : ICommand
|
||||
{
|
||||
[CommandOption("url", 'u', Description = "OPC UA server endpoint URL", IsRequired = true)]
|
||||
public string Url { get; init; } = default!;
|
||||
|
||||
public async ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
using var session = await OpcUaHelper.ConnectAsync(Url);
|
||||
await console.Output.WriteLineAsync($"Connected to: {session.Endpoint.EndpointUrl}");
|
||||
await console.Output.WriteLineAsync($"Server: {session.Endpoint.Server!.ApplicationName}");
|
||||
await console.Output.WriteLineAsync($"Security Mode: {session.Endpoint.SecurityMode}");
|
||||
await console.Output.WriteLineAsync($"Security Policy: {session.Endpoint.SecurityPolicyUri}");
|
||||
await console.Output.WriteLineAsync("Connection successful.");
|
||||
}
|
||||
}
|
||||
31
tools/opcuacli-dotnet/Commands/ReadCommand.cs
Normal file
31
tools/opcuacli-dotnet/Commands/ReadCommand.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using CliFx;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Infrastructure;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Client;
|
||||
|
||||
namespace OpcUaCli.Commands;
|
||||
|
||||
[Command("read", Description = "Read a value from a node")]
|
||||
public class ReadCommand : ICommand
|
||||
{
|
||||
[CommandOption("url", 'u', Description = "OPC UA server endpoint URL", IsRequired = true)]
|
||||
public string Url { get; init; } = default!;
|
||||
|
||||
[CommandOption("node", 'n', Description = "Node ID (e.g. ns=2;s=MyNode)", IsRequired = true)]
|
||||
public string NodeId { get; init; } = default!;
|
||||
|
||||
public async ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
using var session = await OpcUaHelper.ConnectAsync(Url);
|
||||
|
||||
var node = new NodeId(NodeId);
|
||||
var value = await session.ReadValueAsync(node);
|
||||
|
||||
await console.Output.WriteLineAsync($"Node: {NodeId}");
|
||||
await console.Output.WriteLineAsync($"Value: {value.Value}");
|
||||
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}");
|
||||
}
|
||||
}
|
||||
61
tools/opcuacli-dotnet/Commands/SubscribeCommand.cs
Normal file
61
tools/opcuacli-dotnet/Commands/SubscribeCommand.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using CliFx;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Infrastructure;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Client;
|
||||
|
||||
namespace OpcUaCli.Commands;
|
||||
|
||||
[Command("subscribe", Description = "Monitor a node for value changes")]
|
||||
public class SubscribeCommand : ICommand
|
||||
{
|
||||
[CommandOption("url", 'u', Description = "OPC UA server endpoint URL", IsRequired = true)]
|
||||
public string Url { get; init; } = default!;
|
||||
|
||||
[CommandOption("node", 'n', Description = "Node ID to monitor", IsRequired = true)]
|
||||
public string NodeId { get; init; } = default!;
|
||||
|
||||
[CommandOption("interval", 'i', Description = "Polling interval in milliseconds")]
|
||||
public int Interval { get; init; } = 1000;
|
||||
|
||||
public async ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
using var session = await OpcUaHelper.ConnectAsync(Url);
|
||||
|
||||
var subscription = new Subscription(session.DefaultSubscription)
|
||||
{
|
||||
PublishingInterval = Interval,
|
||||
DisplayName = "CLI Subscription"
|
||||
};
|
||||
|
||||
var item = new MonitoredItem(subscription.DefaultItem)
|
||||
{
|
||||
StartNodeId = new NodeId(NodeId),
|
||||
DisplayName = NodeId,
|
||||
SamplingInterval = Interval
|
||||
};
|
||||
|
||||
item.Notification += (_, e) =>
|
||||
{
|
||||
if (e.NotificationValue is MonitoredItemNotification notification)
|
||||
{
|
||||
console.Output.WriteLine(
|
||||
$"[{notification.Value.SourceTimestamp:O}] {NodeId} = {notification.Value.Value} ({notification.Value.StatusCode})");
|
||||
}
|
||||
};
|
||||
|
||||
subscription.AddItem(item);
|
||||
session.AddSubscription(subscription);
|
||||
await subscription.CreateAsync();
|
||||
|
||||
await console.Output.WriteLineAsync(
|
||||
$"Subscribed to {NodeId} (interval: {Interval}ms). Press Ctrl+C to stop.");
|
||||
|
||||
var ct = console.RegisterCancellationHandler();
|
||||
|
||||
try { await Task.Delay(Timeout.Infinite, ct); }
|
||||
catch (OperationCanceledException) { }
|
||||
|
||||
await console.Output.WriteLineAsync("Unsubscribed.");
|
||||
}
|
||||
}
|
||||
44
tools/opcuacli-dotnet/Commands/WriteCommand.cs
Normal file
44
tools/opcuacli-dotnet/Commands/WriteCommand.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using CliFx;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Infrastructure;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Client;
|
||||
|
||||
namespace OpcUaCli.Commands;
|
||||
|
||||
[Command("write", Description = "Write a value to a node")]
|
||||
public class WriteCommand : ICommand
|
||||
{
|
||||
[CommandOption("url", 'u', Description = "OPC UA server endpoint URL", IsRequired = true)]
|
||||
public string Url { get; init; } = default!;
|
||||
|
||||
[CommandOption("node", 'n', Description = "Node ID (e.g. ns=2;s=MyNode)", IsRequired = true)]
|
||||
public string NodeId { get; init; } = default!;
|
||||
|
||||
[CommandOption("value", 'v', Description = "Value to write", IsRequired = true)]
|
||||
public string Value { get; init; } = default!;
|
||||
|
||||
public async ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
using var session = await OpcUaHelper.ConnectAsync(Url);
|
||||
|
||||
var node = new NodeId(NodeId);
|
||||
var current = await session.ReadValueAsync(node);
|
||||
var typedValue = OpcUaHelper.ConvertValue(Value, current.Value);
|
||||
|
||||
var writeValue = new WriteValue
|
||||
{
|
||||
NodeId = node,
|
||||
AttributeId = Attributes.Value,
|
||||
Value = new DataValue(new Variant(typedValue))
|
||||
};
|
||||
|
||||
var request = new WriteValueCollection { writeValue };
|
||||
var response = await session.WriteAsync(null, request, CancellationToken.None);
|
||||
|
||||
if (StatusCode.IsGood(response.Results[0]))
|
||||
await console.Output.WriteLineAsync($"Write successful: {NodeId} = {typedValue}");
|
||||
else
|
||||
await console.Output.WriteLineAsync($"Write failed: {response.Results[0]}");
|
||||
}
|
||||
}
|
||||
81
tools/opcuacli-dotnet/OpcUaHelper.cs
Normal file
81
tools/opcuacli-dotnet/OpcUaHelper.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Client;
|
||||
using Opc.Ua.Configuration;
|
||||
|
||||
namespace OpcUaCli;
|
||||
|
||||
public static class OpcUaHelper
|
||||
{
|
||||
public static async Task<Session> ConnectAsync(string endpointUrl)
|
||||
{
|
||||
var config = new ApplicationConfiguration
|
||||
{
|
||||
ApplicationName = "OpcUaCli",
|
||||
ApplicationUri = "urn:localhost:OpcUaCli",
|
||||
ApplicationType = ApplicationType.Client,
|
||||
SecurityConfiguration = new SecurityConfiguration
|
||||
{
|
||||
ApplicationCertificate = new CertificateIdentifier
|
||||
{
|
||||
StoreType = CertificateStoreType.Directory,
|
||||
StorePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "OpcUaCli", "pki", "own")
|
||||
},
|
||||
TrustedIssuerCertificates = new CertificateTrustList
|
||||
{
|
||||
StoreType = CertificateStoreType.Directory,
|
||||
StorePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "OpcUaCli", "pki", "issuer")
|
||||
},
|
||||
TrustedPeerCertificates = new CertificateTrustList
|
||||
{
|
||||
StoreType = CertificateStoreType.Directory,
|
||||
StorePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "OpcUaCli", "pki", "trusted")
|
||||
},
|
||||
RejectedCertificateStore = new CertificateTrustList
|
||||
{
|
||||
StoreType = CertificateStoreType.Directory,
|
||||
StorePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "OpcUaCli", "pki", "rejected")
|
||||
},
|
||||
AutoAcceptUntrustedCertificates = true
|
||||
},
|
||||
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
|
||||
};
|
||||
|
||||
#pragma warning disable CS0618 // Sync/obsolete API is fine for a CLI tool
|
||||
await config.Validate(ApplicationType.Client);
|
||||
config.CertificateValidator.CertificateValidation += (_, e) => e.Accept = true;
|
||||
|
||||
var endpoint = CoreClientUtils.SelectEndpoint(config, endpointUrl, false);
|
||||
var endpointConfig = EndpointConfiguration.Create(config);
|
||||
var configuredEndpoint = new ConfiguredEndpoint(null, endpoint, endpointConfig);
|
||||
|
||||
var session = await Session.Create(
|
||||
config,
|
||||
configuredEndpoint,
|
||||
false,
|
||||
"OpcUaCli",
|
||||
60000,
|
||||
null,
|
||||
null);
|
||||
|
||||
return session;
|
||||
#pragma warning restore CS0618
|
||||
}
|
||||
|
||||
public static object ConvertValue(string rawValue, object? currentValue)
|
||||
{
|
||||
return currentValue switch
|
||||
{
|
||||
bool => bool.Parse(rawValue),
|
||||
byte => byte.Parse(rawValue),
|
||||
short => short.Parse(rawValue),
|
||||
ushort => ushort.Parse(rawValue),
|
||||
int => int.Parse(rawValue),
|
||||
uint => uint.Parse(rawValue),
|
||||
long => long.Parse(rawValue),
|
||||
ulong => ulong.Parse(rawValue),
|
||||
float => float.Parse(rawValue),
|
||||
double => double.Parse(rawValue),
|
||||
_ => rawValue
|
||||
};
|
||||
}
|
||||
}
|
||||
8
tools/opcuacli-dotnet/Program.cs
Normal file
8
tools/opcuacli-dotnet/Program.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using CliFx;
|
||||
|
||||
return await new CliApplicationBuilder()
|
||||
.AddCommandsFromThisAssembly()
|
||||
.SetExecutableName("opcuacli-dotnet")
|
||||
.SetDescription("OPC UA CLI - command-line tool for testing OPC UA servers")
|
||||
.Build()
|
||||
.RunAsync(args);
|
||||
123
tools/opcuacli-dotnet/README.md
Normal file
123
tools/opcuacli-dotnet/README.md
Normal file
@@ -0,0 +1,123 @@
|
||||
# OPC UA CLI Tool (.NET)
|
||||
|
||||
Command-line utility for testing OPC UA server functions. Built with the [OPC Foundation UA .NET Standard](https://github.com/OPCFoundation/UA-.NETStandard) client library and [CliFx](https://github.com/Tyrrrz/CliFx).
|
||||
|
||||
- **Runtime**: .NET 10
|
||||
- **OPC UA Client**: OPCFoundation.NetStandard.Opc.Ua.Client
|
||||
|
||||
## Build & Run
|
||||
|
||||
```bash
|
||||
cd tools/opcuacli-dotnet
|
||||
dotnet build
|
||||
dotnet run -- <command> [options]
|
||||
```
|
||||
|
||||
## Commands
|
||||
|
||||
### connect
|
||||
|
||||
Test connection to an OPC UA server:
|
||||
|
||||
```
|
||||
dotnet run -- connect -u opc.tcp://localhost:4840
|
||||
```
|
||||
|
||||
| Flag | Description |
|
||||
|------|-------------|
|
||||
| `-u` | OPC UA server endpoint URL (required) |
|
||||
|
||||
### read
|
||||
|
||||
Read a value from a node:
|
||||
|
||||
```
|
||||
dotnet run -- read -u opc.tcp://localhost:4840 -n "ns=2;s=MyNode"
|
||||
```
|
||||
|
||||
| Flag | Description |
|
||||
|------|-------------|
|
||||
| `-u` | OPC UA server endpoint URL (required) |
|
||||
| `-n` | Node ID to read (required) |
|
||||
|
||||
### write
|
||||
|
||||
Write a value to a node (auto-detects the data type from the current value):
|
||||
|
||||
```
|
||||
dotnet run -- write -u opc.tcp://localhost:4840 -n "ns=2;s=MyNode" -v 42
|
||||
```
|
||||
|
||||
| Flag | Description |
|
||||
|------|-------------|
|
||||
| `-u` | OPC UA server endpoint URL (required) |
|
||||
| `-n` | Node ID to write to (required) |
|
||||
| `-v` | Value to write (required) |
|
||||
|
||||
### subscribe
|
||||
|
||||
Monitor a node for value changes:
|
||||
|
||||
```
|
||||
dotnet run -- subscribe -u opc.tcp://localhost:4840 -n "ns=2;s=MyNode" -i 500
|
||||
```
|
||||
|
||||
| Flag | Description |
|
||||
|------|-------------|
|
||||
| `-u` | OPC UA server endpoint URL (required) |
|
||||
| `-n` | Node ID to monitor (required) |
|
||||
| `-i` | Polling interval in milliseconds (default: 1000) |
|
||||
|
||||
### browse
|
||||
|
||||
Browse the OPC UA address space:
|
||||
|
||||
```bash
|
||||
# Browse top-level Objects folder
|
||||
dotnet run -- browse -u opc.tcp://localhost:4840
|
||||
|
||||
# Browse a specific node
|
||||
dotnet run -- browse -u opc.tcp://localhost:4840 -n "ns=2;s=MyFolder"
|
||||
|
||||
# Browse recursively (depth 3)
|
||||
dotnet run -- browse -u opc.tcp://localhost:4840 -r -d 3
|
||||
```
|
||||
|
||||
| Flag | Description |
|
||||
|------|-------------|
|
||||
| `-u` | OPC UA server endpoint URL (required) |
|
||||
| `-n` | Node ID to browse (default: Objects folder) |
|
||||
| `-d` | Maximum browse depth (default: 1) |
|
||||
| `-r` | Browse recursively using `--depth` as max depth |
|
||||
|
||||
## Example: Testing the LmxOpcUa Server
|
||||
|
||||
```bash
|
||||
cd tools/opcuacli-dotnet
|
||||
|
||||
# Connect to the local OPC UA server
|
||||
dotnet run -- connect -u opc.tcp://localhost:4840
|
||||
|
||||
# Browse the address space
|
||||
dotnet run -- browse -u opc.tcp://localhost:4840 -r -d 3
|
||||
|
||||
# Read a tag value
|
||||
dotnet run -- read -u opc.tcp://localhost:4840 -n "ns=2;s=TestMachine_001.SomeAttribute"
|
||||
|
||||
# Subscribe to live updates
|
||||
dotnet run -- subscribe -u opc.tcp://localhost:4840 -n "ns=2;s=TestMachine_001.SomeAttribute" -i 500
|
||||
```
|
||||
|
||||
## Example: Testing with the OPC PLC Sample Server
|
||||
|
||||
```bash
|
||||
# Start the sample server (from another terminal)
|
||||
cd tools/opcsampleserver/publish
|
||||
dotnet opcplc.dll --pn=50000 --autoaccept --unsecuretransport
|
||||
|
||||
# Browse the sample server
|
||||
dotnet run -- browse -u opc.tcp://localhost:50000 -r -d 2
|
||||
|
||||
# Read a sample node
|
||||
dotnet run -- read -u opc.tcp://localhost:50000 -n "ns=2;s=SlowUInt1"
|
||||
```
|
||||
16
tools/opcuacli-dotnet/opcuacli-dotnet.csproj
Normal file
16
tools/opcuacli-dotnet/opcuacli-dotnet.csproj
Normal file
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<RootNamespace>opcuacli_dotnet</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CliFx" Version="2.3.6" />
|
||||
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Client" Version="1.5.378.106" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user