From 1a2b07f01ba4616fd9e894dddf474b56d020158c Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 07 四月 2025 15:18:51 +0800
Subject: [PATCH] 整理版本

---
 pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpDownCommandObj.java |  117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 117 insertions(+), 0 deletions(-)

diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpDownCommandObj.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpDownCommandObj.java
new file mode 100644
index 0000000..3a76be7
--- /dev/null
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpDownCommandObj.java
@@ -0,0 +1,117 @@
+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, this.result.downCode, this.result.downCodeName);
+			return removeNodeFromCach ;
+		}
+		if(this.sendedTimes >= this.result.maxSendTimes){
+			//鍙戦�佹鏁拌揪鍒版渶澶у��
+			if(now - this.lastSendStamp >= ServerProperties.cacheWaitResultTimeout){
+				//瓒呮椂
+				removeNodeFromCach = true ;
+	     		//璁板綍鐘舵��
+	     		RtuStatusDealer.commandFailure(this.result.rtuAddr, this.result.downCode, this.result.downCodeName);
+			}
+			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.offLineCacheTimeout){
+					//缂撳瓨瓒呮椂
+					removeNodeFromCach = true ;
+				}
+			}
+			return removeNodeFromCach ;
+		}
+		
+		if(tcpSe.lastUpDataTime != null){
+			//璇存槑涓婅鏁版嵁鏃跺埢鏈夋晥锛屽嵆姝ゆ椂绂讳笂琛屾暟鎹椂鍒诲お杩戯紝杩樹笉鑳戒笅琛屾暟鎹紝寤惰繜涓�涓�
+			return false ;
+		}
+
+		boolean nowSend = false ;
+		if(this.result.countdown4Send > 0){
+			this.result.countdown4Send -= 1 ;
+		}else{
+			Long lastDownComTime = tcpSe.lastDownComTime ;
+			if(this.result.isQuickSend){
+				//鍦ㄥ懡浠ゆ瀯寤烘椂锛屽彧鏈夊湪浣庡姛鑰楁儏鍐典笅锛屾墠浼氭妸isQuickSend浠樺�间负true
+				//鏈敹鍒板懡浠ょ粨鏋滐紝鏈揪鍒版渶澶у彂閫佹鏁帮紝RTU鍦ㄧ嚎锛岀涓婅鏁版嵁鏃跺埢涓嶈繎锛屽綋鍓嶆槸閫熷彂鍛戒护锛屾弧瓒冲彂閫佸懡浠ゆ潯浠讹紝鎵ц鍙戦�佸懡浠�
+				nowSend = true ;
+			}else if(lastDownComTime == null){
+				//鏈敹鍒板懡浠ょ粨鏋滐紝鏈揪鍒版渶澶у彂閫佹鏁帮紝RTU鍦ㄧ嚎锛岀涓婅鏁版嵁鏃跺埢涓嶈繎锛屽綋鍓峈TU涓婄嚎浠ユ潵鏈浘涓嬪彂杩囧懡浠わ紝婊¤冻鍙戦�佸懡浠ゆ潯浠讹紝鎵ц鍙戦�佸懡浠�
+				nowSend = true ;
+			}else if(this.result.isFastCom && (now - lastDownComTime) >= ServerProperties.fastCommandSendInterval){
+				//鏈敹鍒板懡浠ょ粨鏋滐紝鏈揪鍒版渶澶у彂閫佹鏁帮紝RTU鍦ㄧ嚎锛岀涓婅鏁版嵁鏃跺埢涓嶈繎锛孯TU涓婄嚎浠ユ潵鏇句笅鍙戣繃鍛戒护锛屽綋鍓嶅懡浠ゆ槸蹇�熷懡浠や笖瓒呰繃鍛戒护涓嬪彂闂撮殧锛屾弧瓒冲彂閫佸懡浠ゆ潯浠讹紝鎵ц鍙戦�佸懡浠�
+				nowSend = true ;
+			}else if(!this.result.isFastCom && (now - lastDownComTime) >= ServerProperties.commandSendInterval){
+				//鏈敹鍒板懡浠ょ粨鏋滐紝鏈揪鍒版渶澶у彂閫佹鏁帮紝RTU鍦ㄧ嚎锛岀涓婅鏁版嵁鏃跺埢涓嶈繎锛孯TU涓婄嚎浠ユ潵鏇句笅鍙戣繃鍛戒护锛屽綋鍓嶅懡浠ら潪蹇�熷懡浠わ紝RTU涓嬪彂鍛戒护闂撮殧瓒呰繃鍛戒护涓嬪彂闂撮殧锛屾弧瓒冲彂閫佸懡浠ゆ潯浠讹紝鎵ц鍙戦�佸懡浠�
+				nowSend = true ;
+			}
+		}
+
+		if(nowSend){
+			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.downCodeName==null?"":("锛�" + this.result.downCodeName + "锛�")) + "锛�" + this.result.downBufHex);
+			//璁板綍鐘舵��
+			RtuStatusDealer.downData(this.result.rtuAddr, this.result.downCode, this.result.downCodeName, this.result.downBuffer.length);
+			
+			log.info("涓嬭RTU锛堝湴鍧�=" + this.result.rtuAddr + "锛夊懡浠鍔熻兘鐮�=" + this.result.downCode + (this.result.downCodeName==null?"":("锛�" + this.result.downCodeName + "锛�")) + "] "  + this.result.downBufHex ) ;
+		}	
+		
+		return removeNodeFromCach ;
+	}
+}

--
Gitblit v1.8.0