From ea58523b67ad59002a1191c3c564258c5b81f6f2 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期二, 06 五月 2025 17:35:16 +0800
Subject: [PATCH] Revert "1、实现万用token(0000-0000-1234-9876-5); 2、web端单独实现命令结果等待器,并修改相关部分; 3、web端实现透传命令; 4、修改一些不当注释; 5、优化一些代码。"

---
 pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpSessionCache.java |  261 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 261 insertions(+), 0 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
new file mode 100644
index 0000000..144593d
--- /dev/null
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpSessionCache.java
@@ -0,0 +1,261 @@
+package com.dy.rtuMw.server.forTcp;
+
+import java.util.*;
+import java.util.Map.Entry;
+
+import com.dy.common.mw.channel.tcp.TcpIoSessionAttrIdIsRtuAddr;
+import org.apache.mina.core.session.IoSession;
+
+import com.dy.rtuMw.server.ServerProperties;
+
+
+public class TcpSessionCache {
+	
+	/**
+	 * 鐢℉ashtable鑰屼笉鐢℉ashMap鍘熷洜锛�
+	 * Hashtable绾跨▼瀹夊叏鐨�
+	 * HashMap绾跨▼涓嶅畨鍏ㄧ殑
+	 * 澶氱嚎绋嬪sessionTable璇诲嚭鎴栧瓨鍏ワ紝鍙兘浜х敓寮傚父
+	 * TcpSessionCache鏄湪澶氱嚎绋嬬幆澧冧笅杩愯
+	 *
+	 * 2023-12-19瀹炴祴锛屽彂鐜癏ashtable骞朵笉绾跨▼瀹夊叏锛屾墍浠ュ簲鐢ㄤ簡HashMap鍜宻ynchronized
+	 */
+	private static HashMap<String, TcpSession> map = new HashMap<String, TcpSession>() ;
+
+
+	/**
+	 * 寰楀埌淇℃伅
+	 * @return
+	 */
+	public static Integer[] info(){
+		Integer rtuTotalConnect = 0 ;//宸茬粡杩炴帴杩囦腑闂翠欢鐨凴TU鎬绘暟锛堝寘鎷湪绾夸笌绂荤嚎鐨勶級
+		Integer rtuTotalOnLine = 0 ;//鍦ㄧ嚎RTU鎬绘暟
+		Integer rtuTotalOffLine = 0 ;//绂荤嚎RTU鎬绘暟
+		synchronized (map){
+			rtuTotalConnect = map.size() ;
+			Collection<TcpSession> col = map.values() ;
+			for(TcpSession se : col){
+				if(se.ioSession.isConnected()){
+					rtuTotalOnLine ++ ;
+				}else{
+					rtuTotalOffLine ++ ;
+				}
+			}
+		}
+		return new Integer[] {rtuTotalConnect, rtuTotalOnLine, rtuTotalOffLine} ;
+	}
+
+	/**
+	 * 鍏抽棴鎵�鏈夌綉缁滆繛鎺�
+	 */
+	public static void closeAllSessions(){
+		synchronized (map){
+			Collection<TcpSession> col = map.values() ;
+			for(TcpSession se : col){
+				se.ioSession.closeNow() ;
+			}
+			map.clear();
+		}
+	}
+
+	/**
+	 * 鍔犲叆鏂扮殑IoSession
+	 * @param rtuAddr
+	 * @param ioSession
+	 */
+	//public static void putNewTcpSession(String rtuAddr, String protocolName, Short protocolVersion, IoSession ioSession){
+	public static void putNewTcpSession(String rtuAddr, IoSession ioSession){
+		synchronized (map){
+			TcpSession tcpSe = map.get(rtuAddr) ;
+			if(tcpSe == null){
+				tcpSe = new TcpSession() ;
+				//tcpSe.protocolName = protocolName ;
+				//tcpSe.protocolVersion = protocolVersion ;
+				tcpSe.ioSession = ioSession ;
+				map.put(rtuAddr, tcpSe) ;
+			}else{
+				tcpSe.ioSession = ioSession ;
+			}
+		}
+	}
+	
+	/**
+	 * 鏇存柊IoSession瀵瑰簲鐨剅tuAddr
+	 * @param oldRtuAddr
+	 * @param newRtuAddr
+	 * @param ioSession
+	 */
+	//public static void changeRtuAddr(String oldRtuAddr, String newRtuAddr, String protocolName, Short protocolVersion, IoSession ioSession){
+	public static void changeRtuAddr(String oldRtuAddr, String newRtuAddr, IoSession ioSession){
+		if(oldRtuAddr != null && newRtuAddr != null && !oldRtuAddr.equals(newRtuAddr)){
+			synchronized (map){
+				TcpSession tcpSe = map.get(oldRtuAddr) ;
+				if(tcpSe == null){
+					putNewTcpSession(newRtuAddr, ioSession) ;
+				}else{
+					map.remove(oldRtuAddr) ;
+					map.put(newRtuAddr, tcpSe) ;
+				}
+			}
+		}
+	}
+	
+	/**
+	 * 寰楀埌TcpSession
+	 * @param rtuAddr
+	 * @return
+	 */
+	public static TcpSession getTcpSession(String rtuAddr){
+		return map.get(rtuAddr) ;
+	}
+
+	/**
+	 * 寰楀埌Tcp閫氫俊鍗忚鍚嶇О
+	 * @param rtuAddr
+	 * @return
+	 * */
+	public static Object[] getTcpProtocolNameVersion(String rtuAddr){
+		TcpSession tcpSe = map.get(rtuAddr) ;
+		if(tcpSe != null){
+			return new Object[]{
+					tcpSe.ioSession.getAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName),
+					tcpSe.ioSession.getAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolVersion)};
+		}else{
+			return null ;
+		}
+	}
+
+
+	/**
+	 * 寰楀埌鎵�鏈夊湪绾夸笌绂荤嚎鏁伴噺缁熻
+	 * @return [0]=鍦ㄧ嚎鏁伴噺锛孾1]涓婄嚎杩囷紝浣嗗綋鍓嶇绾跨殑鏁伴噺
+	 */
+	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 (map){
+			HashMap<String, Boolean> map = new HashMap<String, Boolean>();
+			Iterator<Entry<String, TcpSession>> it = TcpSessionCache.map.entrySet().iterator() ;
+			Entry<String, TcpSession> entry = null ;
+			while(it.hasNext()){
+				entry = it.next() ;
+				map.put(entry.getKey(), entry.getValue().ioSession.isConnected()) ;
+			}
+			return map ;
+		}
+	}
+
+
+	/**
+	 * 寰楀埌閮ㄥ垎鍦ㄧ嚎鎯呭喌
+	 * @return
+	 */
+	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()) ;
+				}
+			}
+			return map ;
+		}
+	}
+
+	/**
+	 * 缃戠粶鏄惁杩炴帴
+	 * @param rtuAddr
+	 * @return
+	 */
+	public static Boolean isConnect(String rtuAddr){
+		TcpSession tcpSe = map.get(rtuAddr) ;
+		if(tcpSe != null){
+			return tcpSe.ioSession.isConnected() ;
+		}
+		return null ;
+	}
+	
+	/**
+	 * 閫氳繃IoSession杈撳嚭鏁版嵁
+	 * @param rtuAddr
+	 * @param data
+	 * @throws Exception
+	 */
+	public static void write(String rtuAddr, byte[] data) throws Exception{
+		TcpSession tcpSe = map.get(rtuAddr) ;
+		if(tcpSe != null){
+			if(tcpSe.ioSession.isConnected()){
+				tcpSe.ioSession.write(data) ;
+			}else{
+				throw new Exception("Rtu杩炴帴宸茬粡鍏抽棴!") ;
+			}
+		}else{
+			throw new Exception("Rtu鏈浘涓婄嚎!") ;
+		}
+	}
+	
+	/**
+	 * 褰撴湁涓婅鏁版嵁鏃�
+	 * @param rtuAddr
+	 */
+	public static void whenUpData(String rtuAddr){
+		TcpSession tcpSe = map.get(rtuAddr) ;
+		if(tcpSe != null){
+			tcpSe.lastDownComTime = 0L ;//缃�0锛屼娇绛夊緟lastUpDataTimeLive锛堣config.xml閰嶇疆鏂囦欢锛夋椂闀垮悗锛屽嵆鍒讳笅鍙戠紦瀛樹腑鐨勫懡浠�
+			tcpSe.lastUpDataTime = System.currentTimeMillis() ;
+			tcpSe.lastUpDataTimeForOnlineCtrl = System.currentTimeMillis() ;
+		}
+	}
+	
+	/**
+	 * 鏇存柊涓婅鏁版嵁鏃跺埢
+	 * 褰撲笂琛屾暟鎹椂鍒诲凡缁忚繃鍘讳竴瀹氭椂闀匡紝涓婅鏁版嵁鏃跺埢娓呯┖
+	 * 褰撲竴瀹氭椂闂村唴娌℃湁涓婅鏁版嵁锛屽垯璁や负RTU绂荤嚎
+	 */
+	public static void updateRtuStatus(Long now){
+		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 ;
+			while(it.hasNext()){
+				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(), "鍥犺緝闀挎椂闂存湭鏀朵笂琛屾暟鎹紝璁や负璁惧绂荤嚎");
+						}
+					}
+				}
+			}
+		}
+	}
+
+}

--
Gitblit v1.8.0