| package com.dy.rtuMw.server.mqtt; | 
|   | 
| import com.dy.common.mw.protocol4Mqtt.status.DevRunInfo; | 
| import com.dy.rtuMw.server.forTcp.RtuLogDealer; | 
| import com.dy.rtuMw.server.local.localProtocol.RtuOnLineStateStatisticsVo; | 
|   | 
| import java.util.HashMap; | 
| import java.util.Iterator; | 
| import java.util.Map; | 
| import java.util.Set; | 
|   | 
| /** | 
|  * @Author: liurunyu | 
|  * @Date: 2025/6/10 15:00 | 
|  * @Description | 
|  */ | 
| public class DevStatusDealer { | 
|   | 
|     private static Map<String, DevStatus> map = new HashMap<String, DevStatus>() ; | 
|   | 
|     public static HashMap<String, Boolean> allOnLine(){ | 
|         synchronized (map){ | 
|             HashMap<String, Boolean> rsMap = new HashMap<>(); | 
|             Iterator<Map.Entry<String, DevStatus>> it = map.entrySet().iterator() ; | 
|             Map.Entry<String, DevStatus> entry = null ; | 
|             while(it.hasNext()){ | 
|                 entry = it.next() ; | 
|                 rsMap.put(entry.getKey(), entry.getValue().onLine) ; | 
|             } | 
|             return rsMap ; | 
|         } | 
|     } | 
|   | 
|     public static HashMap<String, Boolean> partOnLine(String[] devIds){ | 
|         synchronized (map){ | 
|             HashMap<String, Boolean> rsMap = new HashMap<String, Boolean>(); | 
|             for(String devid : devIds){ | 
|                 DevStatus st = map.get(devid) ; | 
|                 if(st != null){ | 
|                     rsMap.put(devid, st.onLine) ; | 
|                 } | 
|             } | 
|             return rsMap ; | 
|         } | 
|     } | 
|   | 
|     public static Boolean oneOnLine(String devId){ | 
|         synchronized (map){ | 
|             DevStatus st = map.get(devId) ; | 
|             if(st != null){ | 
|                 return st.onLine ; | 
|             } | 
|             return false ; | 
|         } | 
|     } | 
|     /** | 
|      * 统计在线与不在线情况 | 
|      */ | 
|     public static RtuOnLineStateStatisticsVo statisticsOnLine(){ | 
|         RtuOnLineStateStatisticsVo vo = new RtuOnLineStateStatisticsVo() ; | 
|         vo.onLineNum = 0 ; | 
|         vo.offLineNum = 0 ; | 
|         synchronized (map){ | 
|             Iterator<Map.Entry<String, DevStatus>> it = map.entrySet().iterator() ; | 
|             Map.Entry<String, DevStatus> entry = null ; | 
|             while(it.hasNext()){ | 
|                 entry = it.next() ; | 
|                 if((entry.getValue()).onLine != null && (entry.getValue()).onLine.booleanValue()){ | 
|                     vo.onLineNum++ ; | 
|                 }else{ | 
|                     vo.offLineNum++ ; | 
|                 } | 
|             } | 
|         } | 
|         return vo ; | 
|     } | 
|   | 
|     /** | 
|      * 得到全部状态 | 
|      * @return | 
|      */ | 
|     public static Map<String, DevStatus> allStatus(){ | 
|         return map ; | 
|     } | 
|     /** | 
|      * 得到部分状态 | 
|      * @return | 
|      */ | 
|     public static Map<String, DevStatus> someStatus(String[] devIdArrGrp){ | 
|         synchronized (map){ | 
|             Map<String, DevStatus> rsMap = new HashMap<>(); | 
|             for(String devId : devIdArrGrp){ | 
|                 DevStatus status = map.get(devId) ; | 
|                 if(status != null){ | 
|                     rsMap.put(devId, status) ; | 
|                 } | 
|             } | 
|             return rsMap ; | 
|         } | 
|     } | 
|     /** | 
|      * 得到一个RTU的状态 | 
|      * @return | 
|      */ | 
|     public static DevStatus oneStatus(String devId){ | 
|         return map.get(devId) ; | 
|     } | 
|   | 
|     public static void updateOnLineState() { | 
|         if (MqttUnit.confVo != null | 
|                 && MqttUnit.confVo.noSubThenOff != null | 
|                 && MqttUnit.confVo.noSubThenOff.longValue() > 0) { | 
|             Long now = System.currentTimeMillis() ; | 
|             synchronized (map){ | 
|                 Set<Map.Entry<String, DevStatus>> entrySet = map.entrySet() ; | 
|                 Iterator<Map.Entry<String, DevStatus>> it = entrySet.iterator() ; | 
|                 Map.Entry<String, DevStatus> entry ; | 
|                 DevStatus st; | 
|                 while(it.hasNext()){ | 
|                     entry = it.next() ; | 
|                     st = entry.getValue(); | 
|                     if(st.onLine != null && st.onLine.booleanValue() && st.lastUpDataTime != null){ | 
|                         if(now - st.lastUpDataTime > MqttUnit.confVo.noSubThenOff.longValue()){ | 
|                             st.onLine = false ; | 
|                             RtuLogDealer.log4Mqtt(entry.getKey(), "因较长时间未收到上行数据,认为设备离线"); | 
|                         } | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 发送消息后 | 
|      * @param devId | 
|      */ | 
|     public static void afterSendPubMessage(String devId){ | 
|         DevStatus st = map.get(devId); | 
|         if(st != null){ | 
|             st.lastDownComTime = System.currentTimeMillis() ; | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 接收消息后 | 
|      * @param devId | 
|      */ | 
|     public static void afterReceiveSubMessage(String devId){ | 
|         DevStatus st = map.get(devId); | 
|         if(st != null){ | 
|             st.lastUpDataTime = System.currentTimeMillis() ; | 
|         } | 
|     } | 
|   | 
|     public static void onLine(String devId, String protocol){ | 
|         DevStatus vo = map.get(devId) ; | 
|         if(vo == null) { | 
|             vo = new DevStatus(); | 
|             vo.id = devId ; | 
|             vo.protocol = protocol ; | 
|             vo.onLine = true ; | 
|             map.put(devId, vo); | 
|         }else { | 
|             vo.onLine = true ; | 
|         } | 
|     } | 
|   | 
|     public static void offLine(String devId){ | 
|         DevStatus vo = map.get(devId) ; | 
|         if(vo == null) { | 
|             vo = new DevStatus(); | 
|             vo.onLine = false ; | 
|             map.put(devId, vo); | 
|         }else { | 
|             vo.onLine = false ; | 
|         } | 
|     } | 
|   | 
|     public static void setStatus(String devId, DevRunInfo st){ | 
|         DevStatus vo = map.get(devId) ; | 
|         if(vo != null) { | 
|             if(st.stirRunning != null){ | 
|                 vo.stirRunning = st.stirRunning ; | 
|             } | 
|             if(st.injectRunning != null){ | 
|                 vo.injectRunning = st.injectRunning ; | 
|             } | 
|             if(st.irrRunning != null){ | 
|                 vo.irrRunning = st.irrRunning ; | 
|             } | 
|             if(st.alarm != null){ | 
|                 vo.alarm = st.alarm ; | 
|             } | 
|         } | 
|     } | 
| } |