import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
import org.eclipse.milo.opcua.stack.core.types.structures.MonitoredItemCreateRequest;
import org.eclipse.milo.opcua.stack.core.types.structures.MonitoredItemNotification;
import org.eclipse.milo.opcua.stack.core.types.structures.MonitoringMode;
import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringParameters;
import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringFilter;
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
import org.eclipse.milo.opcua.stack.core.types.enumerated.ClientMonitoredItemCreateResult;
import org.eclipse.milo.opcua.stack.core.types.enumerated.ClientMonitoredItemCreateRequest;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class OpcUaClientExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// 创建OPC UA客户端,需要服务器URL
String endpointUrl = "opc.tcp://example.com:4840";
OpcUaClient client = OpcUaClient.create(endpointUrl);
// 连接客户端
client.connect().get();
// 创建订阅
UaSubscription subscription = client.getSubscriptionManager().createSubscription(1000.0).get();
// 创建监控项请求,监控特定节点ID
NodeId nodeId = NodeId.parse("ns=2;s=Demo.Sensor1:Temperature");
MonitoredItemCreateRequest monitoredItemCreateRequest = new MonitoredItemCreateRequest(
MonitoringMode.Reporting,
nodeId,
(MonitoringFilter) null,
new MonitoringParameters(
client.getServer().getServerStatus().getServiceLevel(),
true
)
);
评论已关闭