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