在Java中使用Milo可以實現實時通信功能,以下是一個簡單的示例:
<dependency>
<groupId>org.eclipse.milo</groupId>
<artifactId>milo-client-opcua-stack</artifactId>
<version>0.6.6</version>
</dependency>
String endpointUrl = "opc.tcp://localhost:4840";
OpcUaClient client = new OpcUaClient(endpointUrl);
client.connect().get();
NodeId nodeId = new NodeId(2, "HelloWorld/ScalarTypes/Int32");
DataValue value = client.readValue(0, TimestampsToReturn.Both, nodeId).get();
System.out.println("Value: " + value.getValue().getValue().toString());
MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(
new ReadValueId(nodeId, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE),
MonitoringMode.Reporting,
new MonitoringParameters(
UInt32.valueOf(1),
Double.valueOf(0.0),
null,
UInt32.valueOf(10),
true
)
);
client.getSubscriptionManager().createSubscription(1000.0).get();
client.getSubscriptionManager().createMonitoredItems(
TimestampsToReturn.Both,
Collections.singletonList(request),
(item, id) -> System.out.println("Value changed: " + item.getValue().getValue().getValue())
).get();
通過以上步驟,您可以實現基于Java Milo的實時通信功能。更多關于Java Milo的文檔可以在官方網站上找到:https://github.com/eclipse/milo