소스 검색

调整。

tom 5 달 전
부모
커밋
a17c169a4a

+ 7 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/config/SimDebugConfig.java

@@ -13,4 +13,11 @@ public class SimDebugConfig {
      * 是否运行 连接情况 的 定时任务。默认true todo:
      */
     public static boolean SCHEDULED_CONNECT = true;
+
+
+    public static final String KEY_TCP_LOCAL_PORT = "TCP_LOCAL_PORT";
+    /**
+     * 本地TCP端口
+     */
+    public static Integer TCP_LOCAL_PORT = 32001;
 }

+ 13 - 4
ruoyi-sim/src/main/java/com/ruoyi/sim/controller/ConfigController.java

@@ -4,6 +4,7 @@ import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.sim.config.SimDebugConfig;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.web.bind.annotation.*;
 
 @RestController
@@ -23,8 +24,8 @@ public class ConfigController {
     @GetMapping(value = "/set")
     @ApiOperation("set config")
     public AjaxResult set(@RequestParam final String key, @RequestParam final String value) {
-        if (SimDebugConfig.KEY_CHECK_REPLACE_EMPTY.equals(key.toUpperCase())) {
-            SimDebugConfig.CHECK_REPLACE_EMPTY = Boolean.valueOf(value);
+        if (StringUtils.equalsIgnoreCase(SimDebugConfig.KEY_CHECK_REPLACE_EMPTY, key)) {
+            SimDebugConfig.CHECK_REPLACE_EMPTY = Boolean.parseBoolean(value);
             return AjaxResult.success(SimDebugConfig.CHECK_REPLACE_EMPTY);
         }
         // 不允许关闭定时循环。
@@ -33,24 +34,32 @@ public class ConfigController {
 //            SimDebugConfig.SCHEDULED_CONNECT = Boolean.valueOf(value);
 //            return AjaxResult.success(SimDebugConfig.SCHEDULED_CONNECT);
 //        }
+        if (StringUtils.equalsIgnoreCase(SimDebugConfig.KEY_TCP_LOCAL_PORT, key)) {
+            SimDebugConfig.TCP_LOCAL_PORT = Integer.parseInt(value);
+            return AjaxResult.success(SimDebugConfig.TCP_LOCAL_PORT);
+        }
         return AjaxResult.error("no match key.");
     }
 
     /**
      * http://192.168.1.110:8080/sim/config/get?key=CHECK_REPLACE_EMPTY
      * http://192.168.1.60:8080/sim/config/get?key=SCHEDULED_CONNECT
+     *
      * @param key
      * @return
      */
     @GetMapping(value = "/get")
     @ApiOperation("get config")
     public AjaxResult get(@RequestParam final String key) {
-        if (SimDebugConfig.KEY_CHECK_REPLACE_EMPTY.equals(key.toUpperCase())) {
+        if (StringUtils.equalsIgnoreCase(SimDebugConfig.KEY_CHECK_REPLACE_EMPTY, key)) {
             return AjaxResult.success(SimDebugConfig.CHECK_REPLACE_EMPTY);
         }
-        if (SimDebugConfig.KEY_SCHEDULED_CONNECT.equals(key.toUpperCase())) {
+        if (StringUtils.equalsIgnoreCase(SimDebugConfig.KEY_SCHEDULED_CONNECT, key)) {
             return AjaxResult.success(SimDebugConfig.SCHEDULED_CONNECT);
         }
+        if (StringUtils.equalsIgnoreCase(SimDebugConfig.KEY_TCP_LOCAL_PORT, key)) {
+            return AjaxResult.success(SimDebugConfig.TCP_LOCAL_PORT);
+        }
         return AjaxResult.error("no match key.");
     }
 }

+ 16 - 6
ruoyi-sim/src/main/java/com/ruoyi/sim/controller/HardwareCommDebugController.java

@@ -41,8 +41,7 @@ public class HardwareCommDebugController extends BaseController {
 
     @GetMapping(value = "/debugReadOneFaultResistance/{seatId}/{bindHardwareMsg}")
     @ApiOperation("debug读取一个故障位置数据")
-    public AjaxResult debugReadOneFaultResistance(@PathVariable("seatId") final Long seatId,
-                                                  @PathVariable("bindHardwareMsg") final String bindHardwareMsg) {
+    public AjaxResult debugReadOneFaultResistance(@PathVariable("seatId") final Long seatId, @PathVariable("bindHardwareMsg") final String bindHardwareMsg) {
         return success(commSendService.debugReadOneFaultResistance(seatId, bindHardwareMsg));
     }
 
@@ -60,8 +59,7 @@ public class HardwareCommDebugController extends BaseController {
 
     @GetMapping(value = "/debugClearOneFault/{seatId}/{bindHardwareMsg}")
     @ApiOperation("debug清除一个故障")
-    public AjaxResult debugClearOneFault(@PathVariable("seatId") final Long seatId,
-                                         @PathVariable("bindHardwareMsg") final String bindHardwareMsg) {
+    public AjaxResult debugClearOneFault(@PathVariable("seatId") final Long seatId, @PathVariable("bindHardwareMsg") final String bindHardwareMsg) {
         return success(commSendService.debugClearOneFault(seatId, bindHardwareMsg));
     }
 
@@ -79,8 +77,7 @@ public class HardwareCommDebugController extends BaseController {
 
     @GetMapping(value = "/debugWriteOneFault/{seatId}/{bindHardwareMsg}")
     @ApiOperation("debug下发一个故障")
-    public AjaxResult debugWriteOneFault(@PathVariable("seatId") final Long seatId,
-                                         @PathVariable("bindHardwareMsg") final String bindHardwareMsg) {
+    public AjaxResult debugWriteOneFault(@PathVariable("seatId") final Long seatId, @PathVariable("bindHardwareMsg") final String bindHardwareMsg) {
         return success(commSendService.debugWriteOneFault(seatId, bindHardwareMsg));
     }
 
@@ -104,6 +101,19 @@ public class HardwareCommDebugController extends BaseController {
         return commSendService.debugScanAllSeat();
     }
 
+    /**
+     * @return
+     * @RequestParam final Integer localPort,
+     * @RequestParam final String remoteIp,
+     * @RequestParam final Integer remotePort,
+     * @RequestParam final Boolean checkReplace
+     */
+    @GetMapping(value = "/debugSocketTryConnectRemoteSim/")
+    @ApiOperation("debug尝试连接远程模拟器")
+    public AjaxResult debugSocketTryConnectRemoteSim() {
+        return AjaxResult.success();
+    }
+
     @GetMapping(value = "/buildMsg/")
     @ApiOperation("拼写带CRC16校验的命令")
     public AjaxResult buildSendMsg(@RequestParam final String simNum,

+ 0 - 16
ruoyi-sim/src/main/java/com/ruoyi/sim/domain/vo/SocketWrapCacheVo.java

@@ -30,13 +30,6 @@ public class SocketWrapCacheVo {
      */
     private Long previousSendSleep = 0L;
 
-    /**
-     * 每个Socket都有。
-     * 重试次数。
-     * default 0.
-     */
-    private AtomicInteger failedCount = new AtomicInteger(0);
-
     public SocketWrapCacheVo() {
     }
 
@@ -87,14 +80,6 @@ public class SocketWrapCacheVo {
         this.previousSendSleep = previousSendSleep;
     }
 
-    public AtomicInteger getFailedCount() {
-        return failedCount;
-    }
-
-    public void setFailedCount(AtomicInteger failedCount) {
-        this.failedCount = failedCount;
-    }
-
     @Override
     public String toString() {
         return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
@@ -102,7 +87,6 @@ public class SocketWrapCacheVo {
                 .append("port", port)
                 .append("socket", socket)
                 .append("okTimeMillis", okTimeMillis)
-                .append("failedCount", failedCount)
                 .toString();
     }
 }

+ 22 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommRunningService.java

@@ -0,0 +1,22 @@
+package com.ruoyi.sim.service.impl;
+
+import org.springframework.stereotype.Service;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+@Service
+public class CommRunningService {
+
+    /**
+     * 是否有关键指令运行。
+     */
+    private AtomicBoolean keyIsRunning = new AtomicBoolean(false);
+
+    public boolean getKeyIsRunning() {
+        return keyIsRunning.get();
+    }
+
+    public void setKeyIsRunning(Boolean keyIsRunning) {
+        this.keyIsRunning.set(keyIsRunning);
+    }
+}

+ 4 - 2
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommSendService.java

@@ -470,7 +470,8 @@ public class CommSendService {
      * @return
      * @throws IOException
      */
-    public SimMsg debugWriteOneFault(final Long seatId, final String bindHardwareMsg) {
+    public SimMsg debugWriteOneFault(final Long seatId,
+                                     final String bindHardwareMsg) {
         Seat seat = seatService.selectSeatBySeatId(seatId);
         Sim sim = gggSimBySeatId(seatId);
         SimMsg sm = commBuildService.buildSendMsgWriteFault(sim.getSimNum(), bindHardwareMsg);
@@ -727,7 +728,8 @@ public class CommSendService {
      * @param bindHardwareMsg
      * @return
      */
-    public SimMsg debugReadOneFaultResistance(final Long seatId, final String bindHardwareMsg) {
+    public SimMsg debugReadOneFaultResistance(final Long seatId,
+                                              final String bindHardwareMsg) {
         Seat seat = seatService.selectSeatBySeatId(seatId);
         Sim sim = gggSimBySeatId(seatId);
         SimMsg sm = commBuildService.buildSendMsgReadFaultResistance(sim.getSimNum(), bindHardwareMsg);

+ 3 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommStrategy.java

@@ -5,6 +5,9 @@ import org.springframework.stereotype.Service;
 @Service
 public class CommStrategy {
 
+    /**
+     * 是局域网环境。
+     */
     private boolean isLAN = true;
 
     /**

+ 37 - 18
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/SocketService.java

@@ -2,6 +2,7 @@ package com.ruoyi.sim.service.impl;
 
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.sim.config.SimConfig;
+import com.ruoyi.sim.config.SimDebugConfig;
 import com.ruoyi.sim.domain.Seat;
 import com.ruoyi.sim.domain.vo.SimSocketParamVo;
 import com.ruoyi.sim.domain.vo.SocketWrapCacheVo;
@@ -12,6 +13,7 @@ import org.springframework.security.core.parameters.P;
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
+import java.net.InetAddress;
 import java.net.Socket;
 import java.util.HashMap;
 import java.util.List;
@@ -36,7 +38,7 @@ public class SocketService {
      * 孙总办公室域名
      * nas.yichiot.com
      */
-    public static final String IP_TEST = "221.218.213.170";
+    public static final String IP_TEST = "221.218.215.73";
 
     public static final int PORT_TEST = 8899;
 
@@ -57,6 +59,12 @@ public class SocketService {
      * value:
      */
     private static HashMap<String, SocketWrapCacheVo> cachedMap = new HashMap<>(INIT_SIZE);
+    /**
+     * 每个Socket都有。
+     * 重试次数。
+     * default 0.
+     */
+    private static HashMap<String, AtomicInteger> failedMap = new HashMap<>(INIT_SIZE);
 
     @Autowired
     private SimConfig config;
@@ -116,9 +124,11 @@ public class SocketService {
             if (isNotOk(sspv) || force) {
                 final String key = sspv.toKey();
                 l.info("openSocket cachedSocket is not ok!try new socket ip = {}:{}!forceNew = {}", sspv.getIp(), sspv.getPort(), force);
-                closeOne(sspv);
-                // todo: LOCAL_PORT
+                closeOne(sspv, false);
+                // 不指定本地端口实现。
                 Socket s = new Socket(sspv.getIp(), sspv.getPort());
+                // todo: LOCAL_PORT
+                // Socket s = new Socket(sspv.getIp(), sspv.getPort(), InetAddress.getLocalHost(), SimDebugConfig.TCP_LOCAL_PORT);
                 s.setSoTimeout(SOCKET_TIME_OUT);
                 SocketWrapCacheVo value = new SocketWrapCacheVo(sspv.getIp(), sspv.getPort(), s, System.currentTimeMillis());
                 cachedMap.put(key, value);
@@ -147,6 +157,14 @@ public class SocketService {
         }
     }
 
+    public AjaxResult tryOpenOne(final SimSocketParamVo sspv) {
+        if (!config.isCommGlobal()) {
+            l.warn("isCommGlobal == {} [模拟器通信被禁用!]", config.isCommGlobal());
+            return AjaxResult.error("模拟器通信被禁用!");
+        }
+        return openOne(sspv, false);
+    }
+
     /**
      * todo:部分返回Aj结果。
      *
@@ -165,12 +183,11 @@ public class SocketService {
         return AjaxResult.success("所有Socket,创建成功!");
     }
 
-    public AjaxResult closeOne(final SimSocketParamVo sspv) {
+    public AjaxResult closeOne(final SimSocketParamVo sspv, boolean failedReset) {
         if (!config.isCommGlobal()) {
             l.warn("isCommGlobal == {} [模拟器通信被禁用!]", config.isCommGlobal());
             return AjaxResult.error("模拟器通信被禁用!");
         }
-        String msgOk = "关闭Socket成功!";
         final String key = sspv.toKey();
         try {
             if (cachedMap.containsKey(key)) {
@@ -182,10 +199,12 @@ public class SocketService {
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
-            // failed count reset.
-            failedReset0(sspv);
+            if (failedReset) {
+                // failed count reset.
+                failedReset0(sspv);
+            }
             cachedMap.remove(key);
-            return AjaxResult.success(msgOk);
+            return AjaxResult.success("关闭Socket成功!");
         }
     }
 
@@ -197,7 +216,7 @@ public class SocketService {
         final String msgOk = "关闭所有Socket成功!";
         List<Seat> allSeat = seatService.listAllEnable();
         for (Seat s : allSeat) {
-            closeOne(new SimSocketParamVo(s.getSeatRs485Ip(), s.getSeatRs485Port()));
+            closeOne(new SimSocketParamVo(s.getSeatRs485Ip(), s.getSeatRs485Port()), true);
         }
         return AjaxResult.success(msgOk);
     }
@@ -244,23 +263,23 @@ public class SocketService {
      */
     public boolean failedIsReachedMax(final SimSocketParamVo sspv, final int limit) {
         final String key = sspv.toKey();
-        return (cachedMap.containsKey(key) && cachedMap.get(key).getFailedCount().get() >= limit);
+        return (failedMap.containsKey(key) && failedMap.get(key).get() >= limit);
     }
 
     public int failedPlus1(final SimSocketParamVo sspv) {
         final String key = sspv.toKey();
-        if (!cachedMap.containsKey(key)) {
-            cachedMap.get(key).setFailedCount(new AtomicInteger(SOCKET_FAILED_COUNT_ADD_1));
+        if (!failedMap.containsKey(key)) {
+            failedMap.put(key, new AtomicInteger(SOCKET_FAILED_COUNT_ADD_1));
         } else {
-            cachedMap.get(key).getFailedCount().addAndGet(SOCKET_FAILED_COUNT_ADD_1);
+            failedMap.get(key).addAndGet(SOCKET_FAILED_COUNT_ADD_1);
         }
-        return cachedMap.get(key).getFailedCount().get();
+        return failedMap.get(key).get();
     }
 
     public int failedGet(final SimSocketParamVo sspv) {
         final String key = sspv.toKey();
-        if (cachedMap.containsKey(key)) {
-            return cachedMap.get(key).getFailedCount().get();
+        if (failedMap.containsKey(key)) {
+            return failedMap.get(key).get();
         } else {
             return SOCKET_FAILED_COUNT_0;
         }
@@ -268,8 +287,8 @@ public class SocketService {
 
     public void failedReset0(final SimSocketParamVo sspv) {
         final String key = sspv.toKey();
-        if (cachedMap.containsKey(key)) {
-            cachedMap.get(key).getFailedCount().set(SOCKET_FAILED_COUNT_0);
+        if (failedMap.containsKey(key)) {
+            failedMap.get(key).set(SOCKET_FAILED_COUNT_0);
         } else {
             l.debug("not containsKey SimSocketParamVo sspv:" + sspv);
         }