Quellcode durchsuchen

添加失败几次以后,sim标记为离线的方式。

tom vor 4 Monaten
Ursprung
Commit
4857af518d

+ 6 - 1
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommConst.java

@@ -66,5 +66,10 @@ public interface CommConst {
             "06", "07", "08", "09", "0A",
             "0B", "0C", "0D", "0E", "0F"};
 
-    int RETRY_COUNT_CLEAR_FAULT = 2;
+    int RETRY_COUNT_CLEAR_ONE_FAULT = 2;
+    int RETRY_COUNT_WRITE_ONE_FAULT = 2;
+    int RETRY_COUNT_READ_ONE_RESISTANCE = 4;
+    int RETRY_COUNT_0 = 0;
+
+    int OFFLINE_LIMIT = 6;
 }

+ 74 - 41
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommSendService.java

@@ -18,7 +18,9 @@ import java.net.InetAddress;
 import java.net.Socket;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Objects;
 
 import static com.ruoyi.sim.service.impl.CommConst.*;
 
@@ -46,13 +48,11 @@ public class CommSendService {
     @Autowired
     private RealExamFaultService realExamFaultService;
     @Autowired
+    private RealExamCollectionService realExamCollectionService;
+    @Autowired
     private SimConfig config;
     @Autowired
     private CommBuildService commBuildService;
-    /**
-     * 当前考试的模拟器类型。只有考试集合打开后,为实际值。否则为null。
-     */
-    private String currentSimType = null;
 
     /**
      * 定时任务。
@@ -134,8 +134,8 @@ public class CommSendService {
      */
     public void scheduledReadSim() {
         l.info("scheduledReadSim");
-        if (StringUtils.isBlank(currentSimType)) {
-            l.info("currentSimType无效");
+        if (!realExamCollectionService.existOpened()) {
+            l.info("没有open的考试集合");
             return;
         }
         //
@@ -158,9 +158,9 @@ public class CommSendService {
         //  debugClearOneFault(simNum, "02");
 
 
-        boolean bRea = isReachable("123.112.16.165");
-        debugReadSimType(simNum);
-        l.info("bRea:" + bRea);
+        // boolean bRea = isReachable("123.112.16.165");
+        // debugReadSimType(simNum);
+        // l.info("bRea:" + bRea);
 
         l.info("cachedSocket.isConnected():" + cachedSocket.isConnected());
         l.info("cachedSocket.isBound():" + cachedSocket.isBound());
@@ -186,12 +186,12 @@ public class CommSendService {
 
     /**
      * 连接情况 的 定时任务。
-     * 执行频率: 2min debug:10s
+     * 执行频率: 2min debug:30s
      */
     public void scheduledConnect() {
         l.info("scheduled####Connect");
-        if (StringUtils.isBlank(currentSimType)) {
-            l.info("currentSimType无效");
+        if (!realExamCollectionService.existOpened()) {
+            l.info("没有open的考试集合");
             return;
         }
         if (!isReachable(config.getRouterIp())) {
@@ -204,7 +204,8 @@ public class CommSendService {
             simService.updateAllEnableState(Sim.State.GATEWAY_OFFLINE);
             return;
         }
-        openSocket(false, currentSimType);
+
+        openSocket();
         if (isCachedSocketOk()) {
             checkAllSimState();
         }
@@ -225,7 +226,11 @@ public class CommSendService {
         }
         //
         SimMsg smS = commBuildService.buildSendMsgReadSimType(s.getSimNum());
-        SimMsg smR = send(smS, s, 0, SLEEP_SHORT);
+        SimMsg smR = send(smS, s, RETRY_COUNT_0, SLEEP_SHORT);
+        if (StringUtils.isNotBlank(smR.getReceiveMsg())) {
+            l.info("isNotBlank");
+            failCountClearOne(s.getSimId());
+        }
         simReceiveService.checkOneSimState(smR, s);
     }
 
@@ -233,8 +238,11 @@ public class CommSendService {
      * 查找所有没有被手动禁用,并order by sim_num的模拟器列表。检查所有模拟器状态。
      */
     public void checkAllSimState() {
-        List<Sim> list = simService.listAllEnableBySimType(currentSimType);
-        list.forEach(this::checkOneSimState);
+        RealExamCollection ecF = realExamCollectionService.selectRealExamCollectionOpened();
+        if (ecF != null) {
+            List<Sim> list = simService.listAllEnableBySimType(ecF.getSimType());
+            list.forEach(this::checkOneSimState);
+        }
     }
 
     @Async("tp-comm")
@@ -299,7 +307,7 @@ public class CommSendService {
      */
     public SimMsg debugReadSimType(final String simNum) {
         SimMsg sm = commBuildService.buildSendMsgReadSimType(simNum);
-        return send(sm, null, 0, SLEEP_SHORT);
+        return send(sm, null, RETRY_COUNT_0, SLEEP_SHORT);
     }
 
     /**
@@ -311,7 +319,7 @@ public class CommSendService {
      */
     public SimMsg debugClearOneFault(final String simNum, final String bindHardwareMsg) {
         SimMsg sm = commBuildService.buildSendMsgClearFault(simNum, bindHardwareMsg);
-        return send(sm, null, 2, SLEEP_LONG);
+        return send(sm, null, RETRY_COUNT_CLEAR_ONE_FAULT, SLEEP_LONG);
     }
 
     /**
@@ -340,7 +348,7 @@ public class CommSendService {
 
         // step1
         SimMsg smS = commBuildService.buildSendMsgClearFault(s.getSimNum(), f.getBindHardwareMsg());
-        SimMsg smR = send(smS, s, RETRY_COUNT_CLEAR_FAULT, SLEEP_LONG);
+        SimMsg smR = send(smS, s, RETRY_COUNT_CLEAR_ONE_FAULT, SLEEP_LONG);
         simReceiveService.clearOneFault(smR, s, reF, f);
         // step2
         if (reF != null &&
@@ -365,7 +373,7 @@ public class CommSendService {
      */
     public SimMsg debugWriteOneFault(final String simNum, final String bindHardwareMsg) {
         SimMsg sm = commBuildService.buildSendMsgWriteFault(simNum, bindHardwareMsg);
-        return send(sm, null, 2, SLEEP_LONG);
+        return send(sm, null, RETRY_COUNT_WRITE_ONE_FAULT, SLEEP_LONG);
     }
 
     /**
@@ -411,12 +419,12 @@ public class CommSendService {
 
         // 下发故障
         SimMsg smA1 = commBuildService.buildSendMsgWriteFault(s.getSimNum(), f.getBindHardwareMsg());
-        SimMsg smA2 = send(smA1, s, 2, SLEEP_LONG);
+        SimMsg smA2 = send(smA1, s, RETRY_COUNT_WRITE_ONE_FAULT, SLEEP_LONG);
         // todo:
 
         // 读取一次当前电阻代表值作为出题值。
         SimMsg smB1 = commBuildService.buildSendMsgReadFaultResistance(s.getSimNum(), f.getBindHardwareMsg());
-        SimMsg smB2 = send(smB1, s, 2, SLEEP_LONG);
+        SimMsg smB2 = send(smB1, s, RETRY_COUNT_WRITE_ONE_FAULT, SLEEP_LONG);
         simReceiveService.setFaultQuestionValue(smB2, s, ref, f);
         if (ref != null) {
             // 修改关联状态。
@@ -453,7 +461,7 @@ public class CommSendService {
      */
     public SimMsg debugReadOneFaultResistance(final String simNum, final String bindHardwareMsg) {
         SimMsg sm = commBuildService.buildSendMsgReadFaultResistance(simNum, bindHardwareMsg);
-        return send(sm, null, 1, SLEEP_SHORT);
+        return send(sm, null, RETRY_COUNT_0, SLEEP_SHORT);
     }
 
     /**
@@ -476,7 +484,7 @@ public class CommSendService {
         l.info("readOneFaultResistance");
         SimMsg sm1 = commBuildService.buildSendMsgReadFaultResistance(s.getSimNum(), f.getBindHardwareMsg());
         // todo:有所区分。
-        SimMsg sm2 = send(sm1, s, 0, SLEEP_LONG);
+        SimMsg sm2 = send(sm1, s, RETRY_COUNT_READ_ONE_RESISTANCE, SLEEP_LONG);
         simReceiveService.setFaultAnswerValue(sm2, s, reF, f, refState);
     }
 
@@ -512,7 +520,7 @@ public class CommSendService {
                 l.info("####SendMsg#### == [{}]", sm);
             }
             // 如果没有打开socket,顺道打开。正好后面要sleep
-            openSocket(false, currentSimType);
+            openSocket();
             {
                 // sleep ,追求顺序请求。
                 if (sleep > 0) {
@@ -548,14 +556,23 @@ public class CommSendService {
             }
         } catch (InterruptedException | IOException e) {   // SocketTimeoutException
             e.printStackTrace();
-            l.warn("Exception ####进行重试####");
+            // 失败计数
+            boolean limit = failCountAdd1(Objects.requireNonNull(s).getSimId());
+            if (limit) {
+                simService.updateSimStateBySimId(s.getSimId(), Sim.State.SIM_OFFLINE);
+                failCountClearOne(s.getSimId());
+            }
             // 进行重试
             // 先考虑一台模拟器演示。
+            if (sm.getRetryCount() == RETRY_COUNT_0) {
+                l.warn("####RetryTotalCount不重试#### = {}", sm);
+                return sm;
+            }
             if (sm.getRetryCount() < retryTotalCount) {
                 sm.retryCountPlus1();
                 send(sm, s, retryTotalCount, sleep);
             } else {
-                l.warn("####RetryTotalCount#### = {}", sm);
+                l.warn("####RetryTotalCount重试#### = {}", sm);
             }
         }
         return sm;
@@ -576,36 +593,28 @@ public class CommSendService {
     /**
      * openSocket
      *
-     * @param force   是否强制
-     * @param simType 模拟器类型
      * @return
      */
-    public AjaxResult openSocket(final boolean force, final String simType) {
+    public AjaxResult openSocket() {
         // check
         if (!config.isCommGlobal()) {
             l.warn("isCommGlobal == false [模拟器通信被禁用!]");
             return AjaxResult.error("模拟器通信被禁用!");
         }
-        if (!simService.checkSimTypeOk(simType)) {
-            return AjaxResult.error("simType " + simType + " is illegal");
-        }
-        //
-        currentSimType = simType;
         try {
-            //
-            if (force) {
-                closeSocket();
-            }
             if (!isCachedSocketOk()) {
                 l.info("openSocket cachedSocket is not ok!new socket!");
                 cachedSocket = new Socket(config.getRs485Ip(), config.getRs485Port());
                 cachedSocket.setSoTimeout(SOCKET_TIME_OUT);
+                failCountClearAll();
             } else {
                 l.info("openSocket cachedSocket is ok!");
             }
         } catch (IOException e) {
             e.printStackTrace();
             return AjaxResult.error("openSocket IOException");
+        } finally {
+
         }
         return AjaxResult.success();
     }
@@ -630,10 +639,10 @@ public class CommSendService {
             }
         } catch (IOException e) {
             e.printStackTrace();
-            return AjaxResult.error("closeSocket IOException");
+            return AjaxResult.success("closeSocket IOException");
         } finally {
-            currentSimType = null;
             cachedSocket = null;
+            failCountClearAll();
         }
         return AjaxResult.success();
     }
@@ -676,4 +685,28 @@ public class CommSendService {
             return false;
         }
     }
+
+    private HashMap<Long, Integer> map = new HashMap<>();
+
+    /**
+     * @param simId
+     * @return true超过通信失败次数限制
+     */
+    private boolean failCountAdd1(long simId) {
+        l.info("map.containsKey(simId) = {}", map.containsKey(simId));
+        if (map.containsKey(simId)) {
+            map.put(simId, map.get(simId) + 1);
+        } else {
+            map.put(simId, 1);
+        }
+        return (map.get(simId) >= OFFLINE_LIMIT);
+    }
+
+    private void failCountClearOne(long simId) {
+        map.remove(simId);
+    }
+
+    private void failCountClearAll() {
+        map.clear();
+    }
 }

+ 15 - 4
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/RealExamCollectionService.java

@@ -303,15 +303,26 @@ public class RealExamCollectionService extends Ele6RYBaseService {
             updateRealExamCollection(f);
         }
         // 打开socket
-        return commSendService.openSocket(true, f.getSimType());
+        return commSendService.openSocket();
     }
 
+    /**
+     * 是否有一个open的考试集合。
+     *
+     * @return
+     */
     public boolean existOpened() {
-        int countOpened = 0;
+        return (selectRealExamCollectionOpened() != null);
+    }
+
+    public RealExamCollection selectRealExamCollectionOpened() {
         RealExamCollection q = new RealExamCollection();
         q.setExamCollectionState(RealExamCollection.State.OPENED);
-        countOpened = selectRealExamCollectionList(q).size();
-        return countOpened > 0;
+        List<RealExamCollection> list = selectRealExamCollectionList(q);
+        if (list != null && !list.isEmpty()) {
+            return list.get(0);
+        }
+        return null;
     }
 
     /**