package com.dy.aceMw.server.forTcp; 
 | 
  
 | 
import org.apache.logging.log4j.LogManager; 
 | 
import org.apache.logging.log4j.Logger; 
 | 
  
 | 
import com.dy.common.queue.NodeObj; 
 | 
import com.dy.common.mw.protocol.MidResultToRtu; 
 | 
import com.dy.aceMw.server.ServerProperties; 
 | 
  
 | 
public class TcpDownCommandObj implements NodeObj{ 
 | 
     
 | 
    private static Logger log = LogManager.getLogger(TcpDownCommandObj.class.getName()); 
 | 
     
 | 
    public MidResultToRtu result ;//下行命令 
 | 
    public Long cachTime ;//缓存时刻 
 | 
    public Long lastSendStamp ;//上次发送时刻 
 | 
    public byte sendedTimes ;//已经发送次数 
 | 
    public boolean onceReceivedResult ;//已经收到命令应答 
 | 
     
 | 
    public TcpDownCommandObj(MidResultToRtu result){ 
 | 
        this.result = result ; 
 | 
        this.cachTime = System.currentTimeMillis() ; 
 | 
        this.lastSendStamp = 0L ; 
 | 
        this.sendedTimes = 0 ; 
 | 
        this.onceReceivedResult = false ; 
 | 
    } 
 | 
     
 | 
    /** 
 | 
     * 自己处理自己 
 | 
     * @param now 
 | 
     * @return 
 | 
     */ 
 | 
    public boolean dealSelf(Long now){ 
 | 
        boolean removeNodeFromCach = false ; 
 | 
        if(this.onceReceivedResult){ 
 | 
            //已经收到命令结果 
 | 
            removeNodeFromCach = true ; 
 | 
             //记录状态 
 | 
             RtuStatusDealer.commandSuccess(this.result.rtuAddr); 
 | 
            return removeNodeFromCach ; 
 | 
        } 
 | 
        if(this.sendedTimes >= this.result.maxSendTimes){ 
 | 
            //发送次数达到最大值 
 | 
            if(now - this.lastSendStamp >= ServerProperties.cachWaitResultTimeout){ 
 | 
                //超时 
 | 
                removeNodeFromCach = true ; 
 | 
                 //记录状态 
 | 
                 RtuStatusDealer.commandFailure(this.result.rtuAddr); 
 | 
            } 
 | 
            return removeNodeFromCach ; 
 | 
        } 
 | 
         
 | 
        TcpSession tcpSe = TcpSessionCache.getTcpSession(this.result.rtuAddr) ; 
 | 
        Boolean flag = TcpSessionCache.isConnect(this.result.rtuAddr) ; 
 | 
        if(tcpSe == null || flag == null || !flag.booleanValue()){ 
 | 
            //未曾上线或不在线 
 | 
            if(!this.result.isCachForOffLine){ 
 | 
                //不在线命令不缓存 
 | 
                removeNodeFromCach = true ; 
 | 
            }else{ 
 | 
                //不在线命令缓存 
 | 
                if(now - this.cachTime >= ServerProperties.offLineCachTimeout){ 
 | 
                    //缓存超时 
 | 
                    removeNodeFromCach = true ; 
 | 
                } 
 | 
            } 
 | 
            return removeNodeFromCach ; 
 | 
        } 
 | 
         
 | 
        if(tcpSe.lastUpDataTime != null){ 
 | 
            //说明上行数据时刻有效,即此时离上行数据时刻太近,还不能下行数据,延迟一下 
 | 
            return false ; 
 | 
        } 
 | 
  
 | 
        Long lastSendStamp = tcpSe.lastDownComTime ; 
 | 
        if(this.result.isQuickSend || lastSendStamp == null || (now - lastSendStamp >= ServerProperties.commandSendInterval)){ 
 | 
            //未收到命令结果,未达到最大发送次数,RTU在线,速发命令或超过命令下发间隔,以上满足发送命令条件,执行发送命令 
 | 
            tcpSe.ioSession.write(this.result.downBuffer) ; 
 | 
            tcpSe.lastDownComTime = now ; 
 | 
            if(!this.result.hasResponse){ 
 | 
                //无应答 
 | 
                removeNodeFromCach = true ; 
 | 
            } 
 | 
             
 | 
            this.sendedTimes++ ; 
 | 
            this.lastSendStamp = now ; 
 | 
             
 | 
            //记录日志 
 | 
            RtuLogDealer.log(this.result.rtuAddr, "下行数据 " + this.result.downCode + ":" + this.result.downBufHex); 
 | 
            //记录状态 
 | 
            RtuStatusDealer.downData(this.result.rtuAddr, this.result.downBuffer.length); 
 | 
             
 | 
            log.info("下行RTU(地址=" + this.result.rtuAddr + ")命令(功能码=" + this.result.downCode + ") "  + this.result.downBufHex ) ; 
 | 
        }     
 | 
         
 | 
        return removeNodeFromCach ; 
 | 
    } 
 | 
} 
 |