|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * 2023-12-19实测,发现Hashtable并不线程安全,所以应用了HashMap和synchronized | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private static HashMap<String, TcpSession> sessionTable = new HashMap<String, TcpSession>() ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 得到信息 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static Integer[] info(){ | 
|---|
|  |  |  | Integer rtuTotalConnect = 0 ;//已经连接过中间件的RTU总数(包括在线与离线的) | 
|---|
|  |  |  | 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} ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 关闭所有网络连接 | 
|---|
|  |  |  | 
|---|
|  |  |  | 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 ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 得到所有在线情况 | 
|---|
|  |  |  | 
|---|
|  |  |  | return map ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 得到部分在线情况 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static HashMap<String, Boolean> partOnLine(String[] rtuAddrArrGrp){ | 
|---|
|  |  |  | synchronized (sessionTable){ | 
|---|
|  |  |  | HashMap<String, Boolean> map = new HashMap<String, Boolean>(); | 
|---|
|  |  |  | for(String rtuAddr : rtuAddrArrGrp){ | 
|---|
|  |  |  | TcpSession tcpSe = sessionTable.get(rtuAddr) ; | 
|---|
|  |  |  | if(tcpSe != null){ | 
|---|
|  |  |  | map.put(rtuAddr, tcpSe.ioSession.isConnected()) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return map ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 得到所有RTU连接状态情况 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | 
|---|
|  |  |  | if(tcpSe.ioSession != null && tcpSe.ioSession.isConnected()){ | 
|---|
|  |  |  | if(now - tcpSe.lastUpDataTimeForOnlineCtrl > ServerProperties.disconnectedByNoUpDataMinutes){ | 
|---|
|  |  |  | tcpSe.ioSession.closeNow() ; | 
|---|
|  |  |  | RtuLogDealer.log(entry.getKey(), "因较长时间未收上行数据,认为设备离线"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|