From 656aa4f953eb52502a6414965e46642c31b6e8ee Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期二, 29 四月 2025 15:33:09 +0800
Subject: [PATCH] 设备会在多种功能码上报数据中携带报警与状态数据,这样数据库保存设备报警与状态数据可能很多,为此修改逻辑,当前实现为一设备一个小时内只保存一条报警与状态数据。

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

diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpSessionCacheBk.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpSessionCacheBk.java
new file mode 100644
index 0000000..be8798f
--- /dev/null
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/forTcp/TcpSessionCacheBk.java
@@ -0,0 +1,187 @@
+package com.dy.rtuMw.server.forTcp;
+
+import com.dy.rtuMw.server.ServerProperties;
+import org.apache.mina.core.session.IoSession;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.*;
+import java.util.Map.Entry;
+
+/**
+ * 鐢℉ashtable瀹炵幇锛屼絾瀹炴祴鍙戠幇Hashtable骞朵笉绾跨▼瀹夊叏
+ */
+public class TcpSessionCacheBk {
+	
+	/**
+	 * 鐢℉ashtable鑰屼笉鐢℉ashMap鍘熷洜锛�
+	 * Hashtable绾跨▼瀹夊叏鐨�
+	 * HashMap绾跨▼涓嶅畨鍏ㄧ殑
+	 * 澶氱嚎绋嬪sessionTable璇诲嚭鎴栧瓨鍏ワ紝鍙兘浜х敓寮傚父
+	 * TcpSessionCache鏄湪澶氱嚎绋嬬幆澧冧笅杩愯
+	 */
+	private static Hashtable<String, TcpSession> sessionTable = new Hashtable<String, TcpSession>() ;
+	
+	/**
+	 * 鍔犲叆鏂扮殑IoSession
+	 * @param rtuAddr
+	 * @param protocolName
+	 * @param ioSession
+	 */
+	public static void putNewTcpSession(String rtuAddr, String protocolName, IoSession ioSession){
+		TcpSession tcpSe = sessionTable.get(rtuAddr) ;
+		if(tcpSe == null){
+			tcpSe = new TcpSession() ;
+			tcpSe.ioSession = ioSession ;
+			sessionTable.put(rtuAddr, tcpSe) ;
+		}else{
+			tcpSe.ioSession = ioSession ;
+		}
+	}
+	
+	/**
+	 * 鏇存柊IoSession瀵瑰簲鐨剅tuAddr
+	 * @param oldRtuAddr
+	 * @param newRtuAddr
+	 * @param protocolName
+	 * @param ioSession
+	 */
+	public static void changeRtuAddr(String oldRtuAddr, String newRtuAddr, String protocolName, IoSession ioSession){
+		if(oldRtuAddr != null && newRtuAddr != null && !oldRtuAddr.equals(newRtuAddr)){
+			TcpSession tcpSe = sessionTable.get(oldRtuAddr) ;
+			if(tcpSe == null){
+				putNewTcpSession(newRtuAddr, protocolName, ioSession) ;
+			}else{
+				sessionTable.remove(oldRtuAddr) ;
+				sessionTable.put(newRtuAddr, tcpSe) ;
+			}
+		}
+	}
+	
+	/**
+	 * 寰楀埌TcpSession
+	 * @param rtuAddr
+	 * @return
+	 */
+	public static TcpSession getTcpSession(String rtuAddr){
+		return sessionTable.get(rtuAddr) ;
+	}
+	
+
+	/**
+	 * 寰楀埌鎵�鏈夊湪绾挎儏鍐�
+	 * @return
+	 */
+	public static HashMap<String, Boolean> allOnLine(){
+		HashMap<String, Boolean> map = new HashMap<String, Boolean>();
+		Iterator<Entry<String, TcpSession>> it = sessionTable.entrySet().iterator() ;
+		Entry<String, TcpSession> entry = null ;
+		while(it.hasNext()){
+			entry = it.next() ;
+			map.put(entry.getKey(), entry.getValue().ioSession.isConnected()) ;
+		}
+		return map ;
+	}
+	/**
+	 * 寰楀埌鎵�鏈塕TU杩炴帴鐘舵�佹儏鍐�
+	 * @return
+	 */
+//	public static List<RtuSessionStatus> allConnectStatus(){
+//		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
+	 * @param rtuAddr
+	 * @return
+	 */
+//	public IoSession getIoSession(String rtuAddr){
+//		TcpSession tcpSe = sessionMap.get(rtuAddr) ;
+//		if(tcpSe != null){
+//			return tcpSe.ioSession ;
+//		}
+//		return null ;
+//	}
+	
+	/**
+	 * 缃戠粶鏄惁杩炴帴
+	 * @param rtuAddr
+	 * @return
+	 */
+	public static Boolean isConnect(String rtuAddr){
+		TcpSession tcpSe = sessionTable.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 = sessionTable.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 cacheUpDataTime(String rtuAddr){
+		TcpSession tcpSe = sessionTable.get(rtuAddr) ;
+		if(tcpSe != null){
+			tcpSe.lastUpDataTime = System.currentTimeMillis() ;
+		}
+	}
+	
+	/**
+	 * 鏇存柊涓婅鏁版嵁鏃跺埢
+	 * 褰撲笂琛屾暟鎹椂鍒诲凡缁忚繃鍘讳竴瀹氭椂闀匡紝涓婅鏁版嵁鏃跺埢娓呯┖
+	 */
+	public static void updateUpDataTime(Long now){
+		Iterator<TcpSession> it = sessionTable.values().iterator() ;
+		TcpSession tcpSe = null ;
+		while(it.hasNext()){
+			tcpSe = it.next() ;
+			if(tcpSe.lastUpDataTime != null){
+				if(now - tcpSe.lastUpDataTime > ServerProperties.lastUpDataTimeLive){
+					tcpSe.lastUpDataTime = null ;
+				}
+			}
+		}
+	}
+
+
+}

--
Gitblit v1.8.0