From db88628e7bb03f334fa65826bd655c27859a9973 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期日, 03 十一月 2024 16:52:04 +0800
Subject: [PATCH] 1、协议版本号增加到协议解析与命令构造中; 2、增加RTU状态记录功能,为RTU远程升级做准备。

---
 pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpSessionCache.java |  158 +++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 105 insertions(+), 53 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 04d21ae..ca59202 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
@@ -1,12 +1,8 @@
 package com.dy.rtuMw.server.forTcp;
 
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
 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;
@@ -23,7 +19,7 @@
 	 *
 	 * 2023-12-19瀹炴祴锛屽彂鐜癏ashtable骞朵笉绾跨▼瀹夊叏锛屾墍浠ュ簲鐢ㄤ簡HashMap鍜宻ynchronized
 	 */
-	private static HashMap<String, TcpSession> sessionTable = new HashMap<String, TcpSession>() ;
+	private static HashMap<String, TcpSession> map = new HashMap<String, TcpSession>() ;
 
 
 	/**
@@ -34,9 +30,9 @@
 		Integer rtuTotalConnect = 0 ;//宸茬粡杩炴帴杩囦腑闂翠欢鐨凴TU鎬绘暟锛堝寘鎷湪绾夸笌绂荤嚎鐨勶級
 		Integer rtuTotalOnLine = 0 ;//鍦ㄧ嚎RTU鎬绘暟
 		Integer rtuTotalOffLine = 0 ;//绂荤嚎RTU鎬绘暟
-		synchronized (sessionTable){
-			rtuTotalConnect = sessionTable.size() ;
-			Collection<TcpSession> col = sessionTable.values() ;
+		synchronized (map){
+			rtuTotalConnect = map.size() ;
+			Collection<TcpSession> col = map.values() ;
 			for(TcpSession se : col){
 				if(se.ioSession.isConnected()){
 					rtuTotalOnLine ++ ;
@@ -52,12 +48,12 @@
 	 * 鍏抽棴鎵�鏈夌綉缁滆繛鎺�
 	 */
 	public static void closeAllSessions(){
-		synchronized (sessionTable){
-			Collection<TcpSession> col = sessionTable.values() ;
+		synchronized (map){
+			Collection<TcpSession> col = map.values() ;
 			for(TcpSession se : col){
 				se.ioSession.closeNow() ;
 			}
-			sessionTable.clear();
+			map.clear();
 		}
 	}
 
@@ -67,14 +63,15 @@
 	 * @param protocolName
 	 * @param ioSession
 	 */
-	public static void putNewTcpSession(String rtuAddr, String protocolName, IoSession ioSession){
-		synchronized (sessionTable){
-			TcpSession tcpSe = sessionTable.get(rtuAddr) ;
+	public static void putNewTcpSession(String rtuAddr, String protocolName, Short protocolVersion, IoSession ioSession){
+		synchronized (map){
+			TcpSession tcpSe = map.get(rtuAddr) ;
 			if(tcpSe == null){
 				tcpSe = new TcpSession() ;
 				tcpSe.protocolName = protocolName ;
+				tcpSe.protocolVersion = protocolVersion ;
 				tcpSe.ioSession = ioSession ;
-				sessionTable.put(rtuAddr, tcpSe) ;
+				map.put(rtuAddr, tcpSe) ;
 			}else{
 				tcpSe.ioSession = ioSession ;
 			}
@@ -86,17 +83,18 @@
 	 * @param oldRtuAddr
 	 * @param newRtuAddr
 	 * @param protocolName
+	 * @param protocolVersion
 	 * @param ioSession
 	 */
-	public static void changeRtuAddr(String oldRtuAddr, String newRtuAddr, String protocolName, IoSession ioSession){
+	public static void changeRtuAddr(String oldRtuAddr, String newRtuAddr, String protocolName, Short protocolVersion, IoSession ioSession){
 		if(oldRtuAddr != null && newRtuAddr != null && !oldRtuAddr.equals(newRtuAddr)){
-			synchronized (sessionTable){
-				TcpSession tcpSe = sessionTable.get(oldRtuAddr) ;
+			synchronized (map){
+				TcpSession tcpSe = map.get(oldRtuAddr) ;
 				if(tcpSe == null){
-					putNewTcpSession(newRtuAddr, protocolName, ioSession) ;
+					putNewTcpSession(newRtuAddr, protocolName, protocolVersion, ioSession) ;
 				}else{
-					sessionTable.remove(oldRtuAddr) ;
-					sessionTable.put(newRtuAddr, tcpSe) ;
+					map.remove(oldRtuAddr) ;
+					map.put(newRtuAddr, tcpSe) ;
 				}
 			}
 		}
@@ -108,14 +106,14 @@
 	 * @return
 	 */
 	public static TcpSession getTcpSession(String rtuAddr){
-		return sessionTable.get(rtuAddr) ;
+		return map.get(rtuAddr) ;
 	}
-	
+
 	/**
 	 * 寰楀埌Tcp閫氫俊鍗忚鍚嶇О
 	 * @param rtuAddr
 	 * @return
-	 */
+
 	public static String getTcpProtocolName(String rtuAddr){
 		TcpSession tcpSe = sessionTable.get(rtuAddr) ;
 		if(tcpSe != null){
@@ -124,15 +122,50 @@
 			return null ;
 		}
 	}
+	 */
+	/**
+	 * 寰楀埌Tcp閫氫俊鍗忚鍚嶇О
+	 * @param rtuAddr
+	 * @return
+	 */
+	public static Object[] getTcpProtocolNameVersion(String rtuAddr){
+		TcpSession tcpSe = map.get(rtuAddr) ;
+		if(tcpSe != null){
+			return new Object[]{tcpSe.protocolName, tcpSe.protocolVersion};
+		}else{
+			return null ;
+		}
+	}
+
+	/**
+	 * 寰楀埌鎵�鏈夊湪绾夸笌绂荤嚎鏁伴噺缁熻
+	 * @return [0]=鍦ㄧ嚎鏁伴噺锛孾2]涓婄嚎杩囷紝浣嗗綋鍓嶇绾跨殑鏁伴噺
+	 */
+	public static Integer[] allOnLineStateStatistics(){
+		synchronized (map){
+			Integer[] arr = new Integer[]{0, 0} ;
+			Iterator<Entry<String, TcpSession>> it = map.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 ;
+		}
+	}
 	
 	/**
 	 * 寰楀埌鎵�鏈夊湪绾挎儏鍐�
 	 * @return
 	 */
 	public static HashMap<String, Boolean> allOnLine(){
-		synchronized (sessionTable){
+		synchronized (map){
 			HashMap<String, Boolean> map = new HashMap<String, Boolean>();
-			Iterator<Entry<String, TcpSession>> it = sessionTable.entrySet().iterator() ;
+			Iterator<Entry<String, TcpSession>> it = TcpSessionCache.map.entrySet().iterator() ;
 			Entry<String, TcpSession> entry = null ;
 			while(it.hasNext()){
 				entry = it.next() ;
@@ -141,35 +174,54 @@
 			return map ;
 		}
 	}
+
+
 	/**
-	 * 寰楀埌鎵�鏈塕TU杩炴帴鐘舵�佹儏鍐�
+	 * 寰楀埌閮ㄥ垎鍦ㄧ嚎鎯呭喌
 	 * @return
 	 */
-	public static List<RtuSessionStatus> allConnectStatus(){
-		synchronized (sessionTable){
-			List<RtuSessionStatus> list = new ArrayList<RtuSessionStatus>();
-			Iterator<Entry<String, TcpSession>> it = sessionTable.entrySet().iterator() ;
-			Entry<String, TcpSession> entry = null ;
-			while(it.hasNext()){
-				entry = it.next() ;
-				RtuSessionStatus vo = new RtuSessionStatus() ;
-				vo.rtuAddr = entry.getKey() ;
-				IoSession se = entry.getValue().ioSession ;
-				vo.onTrueOffLine = se.isConnected() ;
-				InetSocketAddress sa = (InetSocketAddress)se.getRemoteAddress() ;
-				if(sa != null){
-					InetAddress inetAddr = sa.getAddress() ;
-					if(inetAddr != null){
-						vo.ip = inetAddr.getHostAddress() ;
-						vo.port = sa.getPort() ;
-					}
+	public static HashMap<String, Boolean> partOnLine(String[] rtuAddrArrGrp){
+		synchronized (map){
+			HashMap<String, Boolean> map = new HashMap<String, Boolean>();
+			for(String rtuAddr : rtuAddrArrGrp){
+				TcpSession tcpSe = TcpSessionCache.map.get(rtuAddr) ;
+				if(tcpSe != null){
+					map.put(rtuAddr, tcpSe.ioSession.isConnected()) ;
 				}
-				list.add(vo) ;
 			}
-			return list ;
+			return map ;
 		}
 	}
-	
+//
+//	/**
+//	 * 寰楀埌鎵�鏈塕TU杩炴帴鐘舵�佹儏鍐�
+//	 * @return
+//	 */
+//	public static List<RtuSessionStatus> allConnectStatus(){
+//		synchronized (sessionTable){
+//			List<RtuSessionStatus> list = new ArrayList<RtuSessionStatus>();
+//			Iterator<Entry<String, TcpSession>> it = sessionTable.entrySet().iterator() ;
+//			Entry<String, TcpSession> entry = null ;
+//			while(it.hasNext()){
+//				entry = it.next() ;
+//				RtuSessionStatus vo = new RtuSessionStatus() ;
+//				vo.rtuAddr = entry.getKey() ;
+//				IoSession se = entry.getValue().ioSession ;
+//				vo.onTrueOffLine = se.isConnected() ;
+//				InetSocketAddress sa = (InetSocketAddress)se.getRemoteAddress() ;
+//				if(sa != null){
+//					InetAddress inetAddr = sa.getAddress() ;
+//					if(inetAddr != null){
+//						vo.ip = inetAddr.getHostAddress() ;
+//						vo.port = sa.getPort() ;
+//					}
+//				}
+//				list.add(vo) ;
+//			}
+//			return list ;
+//		}
+//	}
+//
 	
 	/**
 	 * 寰楀埌IoSession
@@ -190,7 +242,7 @@
 	 * @return
 	 */
 	public static Boolean isConnect(String rtuAddr){
-		TcpSession tcpSe = sessionTable.get(rtuAddr) ;
+		TcpSession tcpSe = map.get(rtuAddr) ;
 		if(tcpSe != null){
 			return tcpSe.ioSession.isConnected() ;
 		}
@@ -204,7 +256,7 @@
 	 * @throws Exception
 	 */
 	public static void write(String rtuAddr, byte[] data) throws Exception{
-		TcpSession tcpSe = sessionTable.get(rtuAddr) ;
+		TcpSession tcpSe = map.get(rtuAddr) ;
 		if(tcpSe != null){
 			if(tcpSe.ioSession.isConnected()){
 				tcpSe.ioSession.write(data) ;
@@ -221,7 +273,7 @@
 	 * @param rtuAddr
 	 */
 	public static void cacheUpDataTime(String rtuAddr){
-		TcpSession tcpSe = sessionTable.get(rtuAddr) ;
+		TcpSession tcpSe = map.get(rtuAddr) ;
 		if(tcpSe != null){
 			tcpSe.lastUpDataTime = System.currentTimeMillis() ;
 			tcpSe.lastUpDataTimeForOnlineCtrl = System.currentTimeMillis() ;
@@ -234,8 +286,8 @@
 	 * 褰撲竴瀹氭椂闂村唴娌℃湁涓婅鏁版嵁锛屽垯璁や负RTU绂荤嚎
 	 */
 	public static void updateRtuStatus(Long now){
-		synchronized (sessionTable){
-			Set<Map.Entry<String, TcpSession>> entrySet = sessionTable.entrySet() ;
+		synchronized (map){
+			Set<Map.Entry<String, TcpSession>> entrySet = map.entrySet() ;
 			Iterator<Map.Entry<String, TcpSession>> it = entrySet.iterator() ;
 			Map.Entry<String, TcpSession> entry ;
 			TcpSession tcpSe ;

--
Gitblit v1.8.0