From 05c3f58d6561c89019532bdeb65365f8129504c3 Mon Sep 17 00:00:00 2001
From: wuzeyu <1223318623@qq.com>
Date: 星期三, 16 十月 2024 08:47:45 +0800
Subject: [PATCH] Merge branch 'master' of http://8.140.179.55:20000/r/pipIrr-SV

---
 pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpSessionCache.java |  100 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 92 insertions(+), 8 deletions(-)

diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpSessionCache.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpSessionCache.java
index 8d37adf..55869f8 100644
--- a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpSessionCache.java
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpSessionCache.java
@@ -2,12 +2,11 @@
 
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
+import java.util.*;
 import java.util.Map.Entry;
 
+import com.dy.common.mw.channel.tcp.TcpIoSessionAttrIdIsRtuAddr;
+import com.dy.common.util.DateTime;
 import org.apache.mina.core.session.IoSession;
 
 import com.dy.rtuMw.server.ServerProperties;
@@ -25,7 +24,43 @@
 	 * 2023-12-19瀹炴祴锛屽彂鐜癏ashtable骞朵笉绾跨▼瀹夊叏锛屾墍浠ュ簲鐢ㄤ簡HashMap鍜宻ynchronized
 	 */
 	private static HashMap<String, TcpSession> sessionTable = new HashMap<String, TcpSession>() ;
-	
+
+
+	/**
+	 * 寰楀埌淇℃伅
+	 * @return
+	 */
+	public static Integer[] info(){
+		Integer rtuTotalConnect = 0 ;//宸茬粡杩炴帴杩囦腑闂翠欢鐨凴TU鎬绘暟锛堝寘鎷湪绾夸笌绂荤嚎鐨勶級
+		Integer rtuTotalOnLine = 0 ;//鍦ㄧ嚎RTU鎬绘暟
+		Integer rtuTotalOffLine = 0 ;//绂荤嚎RTU鎬绘暟
+		synchronized (sessionTable){
+			rtuTotalConnect = sessionTable.size() ;
+			Collection<TcpSession> col = sessionTable.values() ;
+			for(TcpSession se : col){
+				if(se.ioSession.isConnected()){
+					rtuTotalOnLine ++ ;
+				}else{
+					rtuTotalOffLine ++ ;
+				}
+			}
+		}
+		return new Integer[] {rtuTotalConnect, rtuTotalOnLine, rtuTotalOffLine} ;
+	}
+
+	/**
+	 * 鍏抽棴鎵�鏈夌綉缁滆繛鎺�
+	 */
+	public static void closeAllSessions(){
+		synchronized (sessionTable){
+			Collection<TcpSession> col = sessionTable.values() ;
+			for(TcpSession se : col){
+				se.ioSession.closeNow() ;
+			}
+			sessionTable.clear();
+		}
+	}
+
 	/**
 	 * 鍔犲叆鏂扮殑IoSession
 	 * @param rtuAddr
@@ -87,6 +122,27 @@
 			return tcpSe.protocolName ;
 		}else{
 			return null ;
+		}
+	}
+
+	/**
+	 * 寰楀埌鎵�鏈夊湪绾夸笌绂荤嚎鏁伴噺缁熻
+	 * @return [0]=鍦ㄧ嚎鏁伴噺锛孾2]涓婄嚎杩囷紝浣嗗綋鍓嶇绾跨殑鏁伴噺
+	 */
+	public static Integer[] allOnLineStateStatistics(){
+		synchronized (sessionTable){
+			Integer[] arr = new Integer[]{0, 0} ;
+			Iterator<Entry<String, TcpSession>> it = sessionTable.entrySet().iterator() ;
+			Entry<String, TcpSession> entry = null ;
+			while(it.hasNext()){
+				entry = it.next() ;
+				if(entry.getValue().ioSession.isConnected()){
+					arr[0]++ ;
+				}else{
+					arr[1]++ ;
+				}
+			}
+			return arr ;
 		}
 	}
 	
@@ -189,27 +245,55 @@
 		TcpSession tcpSe = sessionTable.get(rtuAddr) ;
 		if(tcpSe != null){
 			tcpSe.lastUpDataTime = System.currentTimeMillis() ;
+			tcpSe.lastUpDataTimeForOnlineCtrl = System.currentTimeMillis() ;
 		}
 	}
 	
 	/**
 	 * 鏇存柊涓婅鏁版嵁鏃跺埢
 	 * 褰撲笂琛屾暟鎹椂鍒诲凡缁忚繃鍘讳竴瀹氭椂闀匡紝涓婅鏁版嵁鏃跺埢娓呯┖
+	 * 褰撲竴瀹氭椂闂村唴娌℃湁涓婅鏁版嵁锛屽垯璁や负RTU绂荤嚎
 	 */
-	public static void updateUpDataTime(Long now){
+	public static void updateRtuStatus(Long now){
 		synchronized (sessionTable){
-			Iterator<TcpSession> it = sessionTable.values().iterator() ;
+			Set<Map.Entry<String, TcpSession>> entrySet = sessionTable.entrySet() ;
+			Iterator<Map.Entry<String, TcpSession>> it = entrySet.iterator() ;
+			Map.Entry<String, TcpSession> entry ;
 			TcpSession tcpSe ;
 			while(it.hasNext()){
-				tcpSe = it.next() ;
+				entry = it.next() ;
+				tcpSe = entry.getValue();
 				if(tcpSe.lastUpDataTime != null){
 					if(now - tcpSe.lastUpDataTime > ServerProperties.lastUpDataTimeLive){
 						tcpSe.lastUpDataTime = null ;
 					}
 				}
+				if(tcpSe.lastUpDataTimeForOnlineCtrl != null){
+					if(tcpSe.ioSession != null && tcpSe.ioSession.isConnected()){
+						if(now - tcpSe.lastUpDataTimeForOnlineCtrl > ServerProperties.disconnectedByNoUpDataMinutes){
+							tcpSe.ioSession.closeNow() ;
+							RtuLogDealer.log(entry.getKey(), "鍥犺緝闀挎椂闂存湭鏀朵笂琛屾暟鎹紝璁や负璁惧绂荤嚎");
+						}
+					}
+				}
 			}
 		}
 	}
 
+//	public static void updateRtuStatus(Long now){
+//		synchronized (sessionTable){
+//			Iterator<TcpSession> it = sessionTable.values().iterator() ;
+//			TcpSession tcpSe ;
+//			while(it.hasNext()){
+//				tcpSe = it.next() ;
+//				if(tcpSe.lastUpDataTime != null){
+//					if(now - tcpSe.lastUpDataTime > ServerProperties.lastUpDataTimeLive){
+//						tcpSe.lastUpDataTime = null ;
+//					}
+//				}
+//			}
+//		}
+//	}
+
 
 }

--
Gitblit v1.8.0