yantian yue
2023-10-20 4f58ba24b9a7e24dc38aa8eb9ca0b92c83e161ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cn.stylefeng.guns.opcua.init;
 
import cn.stylefeng.guns.opcua.client.ClientHandler;
import cn.stylefeng.guns.opcua.controller.OpcuaConfController;
import cn.stylefeng.guns.opcua.entity.NodeEntity;
import cn.stylefeng.guns.opcua.model.params.OpcuaConfParam;
import cn.stylefeng.guns.opcua.model.result.OpcuaConfResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
 
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
@Component
@Slf4j
public class OpcusConfigInit implements CommandLineRunner {
 
    @Autowired
    private ClientHandler clientHandler;
 
    @Autowired
    private OpcuaConfController opcuaConfController;
 
    @Override
    public void run(String... args) throws Exception {
        try {
            OpcuaConfParam opcuaConfParam=new OpcuaConfParam();
            opcuaConfParam.setSubscribe(1); //设置查询条件,是否订阅状态为1的所有数据.
            List<OpcuaConfResult> nodeslist=opcuaConfController.mylist(opcuaConfParam);
             clientHandler.connect();
            if (nodeslist != null && nodeslist.size() > 0) {
                for (OpcuaConfResult opcuaConfResult : nodeslist) {
                   List<NodeEntity> nodes = Stream.of(opcuaConfResult.getNode())
                            .map(id -> NodeEntity.builder().index(2).identifier(id).build()).collect(Collectors.toList());
                    clientHandler.subscribe(nodes);
                }
                log.info("初始化OPC订阅" + nodeslist.size() + "条!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("骚货,我启动失败了!");
        }
        log.info("骚货,我启动成功了!");
    }
}