1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.dy.rtuMw.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.rtuMw.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 ;
    }
}