package com.jcdm.main.da.opcuaconfig.init; import com.google.common.collect.Lists; import com.jcdm.main.da.opcuaconfig.client.ClientHandler; import com.jcdm.main.da.opcuaconfig.domain.DaOpcuaConfig; import com.jcdm.main.da.opcuaconfig.domain.NodeEntity; import com.jcdm.main.da.opcuaconfig.service.IDaOpcuaConfigService; 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; @Component @Slf4j public class OpcusConfigInit implements CommandLineRunner { @Autowired private ClientHandler clientHandler; @Autowired private IDaOpcuaConfigService daOpcuaConfigService; @Override public void run(String... args) throws Exception { try { DaOpcuaConfig daOpcuaConfig=new DaOpcuaConfig(); daOpcuaConfig.setSubscribe("Y"); //设置查询条件,是否订阅状态为Y的所有数据. List nodeslist=daOpcuaConfigService.selectDaOpcuaConfigList(daOpcuaConfig); clientHandler.connect(); if (nodeslist != null && nodeslist.size() > 0) { List NodeList = Lists.transform(nodeslist, (DaOpcuaConfig) -> { NodeEntity nodeEntity = new NodeEntity(); nodeEntity.setIndex(2); nodeEntity.setIdentifier(DaOpcuaConfig.getNode()); return nodeEntity; }); clientHandler.subscribe(NodeList); log.info("初始化OPC订阅" + nodeslist.size() + "条!"); } log.info("系统提示,OPCUA订阅成功!"); } catch (Exception e) { e.printStackTrace(); log.info("系统提示,OPCUA订阅失败!"); } } }