最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot連接訂閱OPCUA數(shù)據(jù)的實(shí)現(xiàn)

 更新時(shí)間:2025年07月13日 11:31:30   作者:程序員阿明  
本文主要介紹了springboot連接訂閱OPCUA數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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")
                                //訪問方式
                                .setIdentityProvider(new AnonymousProvider())
                                .setRequestTimeout(UInteger.valueOf(500))
                                .build()
        );
        opcUaClient.connect().get();
//        Thread.sleep(2000); // 線程休眠一下再返回對(duì)象,給創(chuàng)建過程一個(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)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評(píng)論

阳信县| 胶州市| 齐河县| 遂溪县| 富民县| 清水县| 长海县| 通渭县| 同江市| 赤峰市| 大厂| 鄄城县| 林州市| 道孚县| 泾源县| 汝州市| 岑巩县| 元阳县| 招远市| 高雄市| 肇东市| 柞水县| 呼玛县| 昌吉市| 吴川市| 大姚县| 大冶市| 景德镇市| 海丰县| 胶南市| 印江| 壶关县| 凤阳县| 宁河县| 衡阳县| 博白县| 凌源市| 遂溪县| 元江| 永丰县| 永德县|