springboot連接訂閱OPCUA數(shù)據(jù)的實(shí)現(xiàn)
1、安裝依賴
<dependency>
<groupId>org.eclipse.milo</groupId>
<artifactId>sdk-client</artifactId>
<version>0.6.14</version>
</dependency>
2、新建OpcUaConfig配置類
package org.example.opcua.opc_config;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@Configuration
public class OpcUaConfig {
@Bean
public OpcUaClient createClient() throws Exception {
String endPointUrl = "opc.tcp://127.0.0.1:49320";
Path securityTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "security");
Files.createDirectories(securityTempDir);
if (!Files.exists(securityTempDir)) {
throw new Exception("unable to create security dir: " + securityTempDir);
}
OpcUaClient opcUaClient = OpcUaClient.create(endPointUrl,
endpoints ->
endpoints.stream()
.filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri()))
.findFirst(),
configBuilder ->
configBuilder
.setApplicationName(LocalizedText.english("eclipse milo opc-ua client"))
.setApplicationUri("urn:eclipse:milo:examples:client")
//訪問(wèn)方式
.setIdentityProvider(new AnonymousProvider())
.setRequestTimeout(UInteger.valueOf(500))
.build()
);
opcUaClient.connect().get();
// Thread.sleep(2000); // 線程休眠一下再返回對(duì)象,給創(chuàng)建過(guò)程一個(gè)時(shí)間。
return opcUaClient;
}
}
3、新建OpcService用于訂閱OPCUA數(shù)據(jù)
package org.example.opcua.opc_service;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription;
import org.eclipse.milo.opcua.stack.core.AttributeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode;
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
import org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest;
import org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters;
import org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static com.google.common.collect.Lists.newArrayList;
import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint;
@Service
@Slf4j
public class OpcService implements ApplicationRunner {
@Resource
private OpcUaClient opcUaClient;
private static final AtomicInteger atomic = new AtomicInteger();
//單個(gè)訂閱
@SneakyThrows
public void subscribe(OpcUaClient client) {
client
.getSubscriptionManager()
.createSubscription(1000.0)
.thenAccept(t -> {
//節(jié)點(diǎn)
NodeId nodeId = new NodeId(2,"accesstest2.equip1.test");
ReadValueId readValueId = new ReadValueId(nodeId, AttributeId.Value.uid(), null, null);
//創(chuàng)建監(jiān)控的參數(shù)
MonitoringParameters parameters = new MonitoringParameters(UInteger.valueOf(atomic.getAndIncrement()), 1000.0, null, UInteger.valueOf(10), true);
//創(chuàng)建監(jiān)控項(xiàng)請(qǐng)求
//該請(qǐng)求最后用于創(chuàng)建訂閱。
MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(readValueId, MonitoringMode.Reporting, parameters);
List<MonitoredItemCreateRequest> requests = new ArrayList<>();
requests.add(request);
//創(chuàng)建監(jiān)控項(xiàng),并且注冊(cè)變量值改變時(shí)候的回調(diào)函數(shù)。
t.createMonitoredItems(
TimestampsToReturn.Both,
requests,
(item, id) -> item.setValueConsumer((it, val) -> {
System.out.println("nodeid :" + it.getReadValueId().getNodeId());
System.out.println("value :" + val.getValue().getValue());
})
);
}).get();
}
/**
* 批量訂閱
* @throws Exception
*/
public void createSubscription() throws Exception {
// 獲取OPC UA服務(wù)器的數(shù)據(jù)
//創(chuàng)建監(jiān)控項(xiàng)請(qǐng)求
//創(chuàng)建發(fā)布間隔1000ms的訂閱對(duì)象
UaSubscription subscription = opcUaClient.getSubscriptionManager().createSubscription(1000.0).get();
// 你所需要訂閱的key
List<String> key = new ArrayList<>();
key.add("accesstest2.equip1.test");
key.add("accesstest2.equip1.test1");
key.add("accesstest2.equip1.test2");
for (int i = 0; i < key.size(); i++) {
String node = key.get(i);
//創(chuàng)建訂閱的變量
NodeId nodeId = new NodeId(2, node);
ReadValueId readValueId = new ReadValueId(nodeId, AttributeId.Value.uid(), null, null);
//創(chuàng)建監(jiān)控的參數(shù)
MonitoringParameters parameters = new MonitoringParameters(
uint(1 + i), // 為了保證唯一性,否則key值一致
0.0, // sampling interval
null, // filter, null means use default
uint(10), // queue size
true // discard oldest
);
MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(readValueId, MonitoringMode.Reporting, parameters);
//創(chuàng)建監(jiān)控項(xiàng),并且注冊(cè)變量值改變時(shí)候的回調(diào)函數(shù)。
List<UaMonitoredItem> items = subscription.createMonitoredItems(
TimestampsToReturn.Both,
newArrayList(request),
(item, id) -> {
item.setValueConsumer((is, value) -> {
String nodeName = item.getReadValueId().getNodeId().getIdentifier().toString();
String nodeValue = value.getValue().getValue().toString();
System.out.println("訂閱");
System.out.println(nodeName);
System.out.println(nodeValue);
});
}).get();
}
}
@Override
public void run(ApplicationArguments args){
try {
//創(chuàng)建發(fā)布間隔1000ms的訂閱對(duì)象
System.out.println("執(zhí)行訂閱");
this.createSubscription();
}catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
}
到此這篇關(guān)于springboot連接訂閱OPCUA數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot連接訂閱OPCUA內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot整合netty-mqtt-client實(shí)現(xiàn)Mqtt消息的訂閱和發(fā)布示例
- SpringBoot中使用MQTT實(shí)現(xiàn)消息的訂閱和發(fā)布(示例代碼)
- SpringBoot?Redis?發(fā)布訂閱模式(Pub/Sub)的具體使用
- springboot基于Redis發(fā)布訂閱集群下WebSocket的解決方案
- Springboot Websocket Stomp 消息訂閱推送
- SpringBoot+Redis實(shí)現(xiàn)消息的發(fā)布與訂閱的示例代碼
- SpringBoot 集成MQTT實(shí)現(xiàn)消息訂閱的詳細(xì)代碼
- SpringBoot整合Redis實(shí)現(xiàn)消息發(fā)布與訂閱的示例代碼
相關(guān)文章
Spring Boot集成MyBatis訪問(wèn)數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Spring Boot集成MyBatis訪問(wèn)數(shù)據(jù)庫(kù)的方法,需要的朋友可以參考下2017-04-04
javaWeb項(xiàng)目部署到阿里云服務(wù)Linux系統(tǒng)的詳細(xì)步驟
這篇文章主要介紹了javaWeb項(xiàng)目部署到阿里云服務(wù)Linux系統(tǒng),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
解決JAVA8 Collectors.toMap value為null報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了解決JAVA8 Collectors.toMap value為null報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
IDEA工程運(yùn)行時(shí)總是報(bào)xx程序包不存在實(shí)際上包已導(dǎo)入(問(wèn)題分析及解決方案)
這篇文章主要介紹了IDEA工程運(yùn)行時(shí),總是報(bào)xx程序包不存在,實(shí)際上包已導(dǎo)入,本文給大家分享問(wèn)題分析及解決方案,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-08-08
Java swing讀取txt文件實(shí)現(xiàn)學(xué)生考試系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java swing讀取txt文件實(shí)現(xiàn)學(xué)生考試系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
詳解Java8如何使用Lambda表達(dá)式進(jìn)行比較
Lambda表達(dá)式,也可稱為閉包,是java8的新特性,作用是取代大部分內(nèi)部類,優(yōu)化java代碼結(jié)構(gòu),讓代碼變得更加簡(jiǎn)潔緊湊。本文將利用Lambda表達(dá)式進(jìn)行排序比較,需要的可以參考一下2022-01-01
Spring Cloud 網(wǎng)關(guān)服務(wù) zuul 動(dòng)態(tài)路由的實(shí)現(xiàn)方法
網(wǎng)關(guān)服務(wù)是流量的唯一入口。不能隨便停服務(wù)。所以動(dòng)態(tài)路由就顯得尤為必要。這篇文章主要介紹了Spring Cloud 網(wǎng)關(guān)服務(wù) zuul 三 動(dòng)態(tài)路由的相關(guān)知識(shí),需要的朋友可以參考下2019-10-10
Java中Process類的使用與注意事項(xiàng)說(shuō)明
這篇文章主要介紹了Java中Process類的使用與注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

