package com.dy.rtuMw.server.tasks; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.dy.common.mw.core.CoreTask; import com.dy.common.mw.protocol.MidResult; import com.dy.common.mw.protocol.Command; import com.dy.common.mw.protocol.Driver; import com.dy.common.mw.protocol.ProtocolCache; import com.dy.rtuMw.server.ServerProperties; import com.dy.rtuMw.server.forTcp.TcpSessionCache; /** * 从web业务系统发向RTU的命令任务 * @author Administrator * */ public class WebDownComTask extends CoreTask { private static Logger log = LogManager.getLogger(WebDownComTask.class.getName()); @Override public Integer execute() { Command com = (Command)this.data ; try { log.info("下发远程命令" + com.getCode() + "的核心任务开始执行"); this.deal(com); } catch (Exception e) { log.error("处理下行命令出错" + (e.getMessage()==null?"!":("," + e.getMessage())) ,e); } return null ; } /** * 处理命令 * @param com 命令 * @throws Exception */ private void deal(Command com) throws Exception{ String rtuAddr = com.getRtuAddr() ; //前面已经判断rtuAddr为空情况,至此其不为空 Driver dri = null ; Object[] objs = TcpSessionCache.getTcpProtocolNameVersion(rtuAddr) ; if(objs == null || objs.length == 0 || objs[0] == null){ //RTU未曾上线 int count = ProtocolCache.driverCount() ; if(count == 1){ //只有一个协议 dri = ProtocolCache.getFirstDriver() ; } }else{ String protocolName = (String)objs[0]; com.protocolVersion = (Short)objs[1]; dri = ProtocolCache.getDriver(protocolName, com.protocolVersion) ; } if(dri == null){ log.error("严重错误,未能得到RTU(" + rtuAddr + ")驱动类实例!"); }else{ MidResult[] actions = dri.createCommand(ServerProperties.isLowPower, com) ; log.info("下发远程命令" + com.getCode() + "由协议驱动构造完成"); if(actions != null){ for(MidResult act : actions){ act.action(); } } } } }