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"); //设置查询条件,订阅状态的所有数据. daOpcuaConfig.setState(Long.valueOf("0")); //设置查询条件,正常状态. 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("OPCUA初始化成功,并完成"+ nodeslist.size() +"个节点订阅"); } catch (Exception e) { e.printStackTrace(); log.info("系统提示,OPCUA订阅失败!"); } } }