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 nodeslist=opcuaConfController.mylist(opcuaConfParam); clientHandler.connect(); if (nodeslist != null && nodeslist.size() > 0) { for (OpcuaConfResult opcuaConfResult : nodeslist) { List 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("骚货,我启动成功了!"); } }