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