|  | @@ -31,10 +31,7 @@ import static com.ruoyi.sim.constant.CommConst.*;
 | 
	
		
			
				|  |  |  public class CommSendService {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private static final Logger l = LoggerFactory.getLogger(CommSendService.class);
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 缓存的Socket连接。
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    private Socket cachedSocket = null;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private CommReceiveService simReceiveService;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
	
		
			
				|  | @@ -48,11 +45,15 @@ public class CommSendService {
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private RealExamCollectionService realExamCollectionService;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  | -    private SimConfig config;
 | 
	
		
			
				|  |  | -    @Autowired
 | 
	
		
			
				|  |  |      private CommBuildService commBuildService;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private DebugFaultService debugFaultService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private SocketOldService socketOldService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    CommReceiveService commReceiveService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private SimConfig config;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 定时任务。
 | 
	
	
		
			
				|  | @@ -120,7 +121,7 @@ public class CommSendService {
 | 
	
		
			
				|  |  |          //
 | 
	
		
			
				|  |  |          // 打开socket
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  | -            AjaxResult ar1 = openSocket();
 | 
	
		
			
				|  |  | +            AjaxResult ar1 = socketOldService.openSocket();
 | 
	
		
			
				|  |  |              if (ar1.isError()) {
 | 
	
		
			
				|  |  |                  return ar1;
 | 
	
		
			
				|  |  |              }
 | 
	
	
		
			
				|  | @@ -196,12 +197,14 @@ public class CommSendService {
 | 
	
		
			
				|  |  |          // debugReadSimType(simNum);
 | 
	
		
			
				|  |  |          // l.info("bRea:" + bRea);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        l.info("cachedSocket.isConnected():" + cachedSocket.isConnected());
 | 
	
		
			
				|  |  | -        l.info("cachedSocket.isBound():" + cachedSocket.isBound());
 | 
	
		
			
				|  |  | -        l.info("cachedSocket.isClosed():" + cachedSocket.isClosed());
 | 
	
		
			
				|  |  | -        l.info("cachedSocket.isInputShutdown():" + cachedSocket.isInputShutdown());
 | 
	
		
			
				|  |  | -        l.info("cachedSocket.isOutputShutdown():" + cachedSocket.isOutputShutdown());
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            Socket s = socketOldService.getCachedSocket();
 | 
	
		
			
				|  |  | +            l.info("cachedSocket.isConnected():" + s.isConnected());
 | 
	
		
			
				|  |  | +            l.info("cachedSocket.isBound():" + s.isBound());
 | 
	
		
			
				|  |  | +            l.info("cachedSocket.isClosed():" + s.isClosed());
 | 
	
		
			
				|  |  | +            l.info("cachedSocket.isInputShutdown():" + s.isInputShutdown());
 | 
	
		
			
				|  |  | +            l.info("cachedSocket.isOutputShutdown():" + s.isOutputShutdown());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  //            debugReadAllFaultResistance(simNum, simType);
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -243,8 +246,8 @@ public class CommSendService {
 | 
	
		
			
				|  |  |              return;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        openSocket();
 | 
	
		
			
				|  |  | -        if (isCachedSocketOk()) {
 | 
	
		
			
				|  |  | +        socketOldService.openSocket();
 | 
	
		
			
				|  |  | +        if (socketOldService.isCachedSocketOk()) {
 | 
	
		
			
				|  |  |              checkAllSimState();
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -302,7 +305,7 @@ public class CommSendService {
 | 
	
		
			
				|  |  |          SimMsg smR = send(smS, s, retryCount, sleep);
 | 
	
		
			
				|  |  |          if (StringUtils.isNotBlank(smR.getReceiveMsg())) {
 | 
	
		
			
				|  |  |              l.info("isNotBlank");
 | 
	
		
			
				|  |  | -            commFailCountClearOne(s.getSimId());
 | 
	
		
			
				|  |  | +            socketOldService.commFailCountClearOne(s.getSimId());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          simReceiveService.checkOneSimState(smR, s);
 | 
	
		
			
				|  |  |          {
 | 
	
	
		
			
				|  | @@ -553,7 +556,7 @@ public class CommSendService {
 | 
	
		
			
				|  |  |          // check sim
 | 
	
		
			
				|  |  |          // 打开socket
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  | -            AjaxResult ar1 = openSocket();
 | 
	
		
			
				|  |  | +            AjaxResult ar1 = socketOldService.openSocket();
 | 
	
		
			
				|  |  |              l.info("ar1 = {}", ar1);
 | 
	
		
			
				|  |  |              if (ar1.isError()) {
 | 
	
		
			
				|  |  |                  return ar1;
 | 
	
	
		
			
				|  | @@ -818,7 +821,7 @@ public class CommSendService {
 | 
	
		
			
				|  |  |                  l.info("####SendMsg#### == [{}]", sm);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              // 如果没有打开socket,顺道打开。正好后面要sleep
 | 
	
		
			
				|  |  | -            openSocket();
 | 
	
		
			
				|  |  | +            socketOldService.openSocket();
 | 
	
		
			
				|  |  |              {
 | 
	
		
			
				|  |  |                  // sleep ,追求顺序请求。
 | 
	
		
			
				|  |  |                  if (sleep > 0 && previousSendSleep != 0L) {
 | 
	
	
		
			
				|  | @@ -826,8 +829,8 @@ public class CommSendService {
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              previousSendSleep = sleep;
 | 
	
		
			
				|  |  | -            InputStream is = cachedSocket.getInputStream();
 | 
	
		
			
				|  |  | -            OutputStream os = cachedSocket.getOutputStream();
 | 
	
		
			
				|  |  | +            InputStream is = socketOldService.getCachedSocket().getInputStream();
 | 
	
		
			
				|  |  | +            OutputStream os = socketOldService.getCachedSocket().getOutputStream();
 | 
	
		
			
				|  |  |              os.write(hexStrToByteArrs(sm.getSendMsg()));
 | 
	
		
			
				|  |  |              sm.setSendTime(DateUtils.getNowDate());
 | 
	
		
			
				|  |  |              if (s != null) {
 | 
	
	
		
			
				|  | @@ -845,10 +848,13 @@ public class CommSendService {
 | 
	
		
			
				|  |  |              {
 | 
	
		
			
				|  |  |                  l.info("####ReceiveMsg#### = [{}]", sm);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -            if (!commBuildService.checkReceiveMsg(sm.getReceiveMsg())) {
 | 
	
		
			
				|  |  | -                // todo:
 | 
	
		
			
				|  |  | -                l.warn("####Fail#### = {}", sm);
 | 
	
		
			
				|  |  | -                return sm;
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                AjaxResult ar =    commReceiveService.checkReceiveMsg(sm.getReceiveMsg());
 | 
	
		
			
				|  |  | +                if (ar.isError()) {
 | 
	
		
			
				|  |  | +                    // todo:
 | 
	
		
			
				|  |  | +                    l.warn("####Fail#### = {}", sm);
 | 
	
		
			
				|  |  | +                    return sm;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              if (s != null) {
 | 
	
		
			
				|  |  |                  simService.updateLastReceivedTime(s);
 | 
	
	
		
			
				|  | @@ -857,10 +863,10 @@ public class CommSendService {
 | 
	
		
			
				|  |  |              e.printStackTrace();
 | 
	
		
			
				|  |  |              // 失败计数
 | 
	
		
			
				|  |  |              l.info("fail sim data = {}", s);
 | 
	
		
			
				|  |  | -            boolean limit = commFailCountAdd1(Objects.requireNonNull(s).getSimId());
 | 
	
		
			
				|  |  | +            boolean limit = socketOldService.commFailCountAdd1(Objects.requireNonNull(s).getSimId());
 | 
	
		
			
				|  |  |              if (limit) {
 | 
	
		
			
				|  |  |                  simService.updateSimStateBySimId(s.getSimId(), Sim.State.OFFLINE);
 | 
	
		
			
				|  |  | -                commFailCountClearOne(s.getSimId());
 | 
	
		
			
				|  |  | +                socketOldService.commFailCountClearOne(s.getSimId());
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              // 先考虑一台模拟器演示。
 | 
	
		
			
				|  |  |              // 进行重试 start
 | 
	
	
		
			
				|  | @@ -893,75 +899,6 @@ public class CommSendService {
 | 
	
		
			
				|  |  |          return null;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * cachedSocket is ok
 | 
	
		
			
				|  |  | -     *
 | 
	
		
			
				|  |  | -     * @return
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    public boolean isCachedSocketOk() {
 | 
	
		
			
				|  |  | -        return (cachedSocket != null &&
 | 
	
		
			
				|  |  | -                cachedSocket.isConnected() &&
 | 
	
		
			
				|  |  | -                cachedSocket.isBound() &&
 | 
	
		
			
				|  |  | -                !cachedSocket.isClosed());
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * openSocket
 | 
	
		
			
				|  |  | -     *
 | 
	
		
			
				|  |  | -     * @return
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    public AjaxResult openSocket() {
 | 
	
		
			
				|  |  | -        // check
 | 
	
		
			
				|  |  | -        if (!config.isCommGlobal()) {
 | 
	
		
			
				|  |  | -            l.warn("isCommGlobal == false [模拟器通信被禁用!]");
 | 
	
		
			
				|  |  | -            return AjaxResult.error("模拟器通信被禁用!");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        try {
 | 
	
		
			
				|  |  | -            if (!isCachedSocketOk()) {
 | 
	
		
			
				|  |  | -                l.info("openSocket cachedSocket is not ok!new socket!");
 | 
	
		
			
				|  |  | -                cachedSocket = new Socket(config.getRs485Ip(), config.getRs485Port());
 | 
	
		
			
				|  |  | -                cachedSocket.setSoTimeout(SOCKET_TIME_OUT);
 | 
	
		
			
				|  |  | -                commFailCountClearAll();
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                l.info("openSocket cachedSocket is ok!");
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -        } catch (IOException e) {
 | 
	
		
			
				|  |  | -            e.printStackTrace();
 | 
	
		
			
				|  |  | -            return AjaxResult.error("开启连接失败!请检查物联网网关连接或配置。");
 | 
	
		
			
				|  |  | -        } finally {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        return AjaxResult.success("开启连接成功!");
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * closeSocket
 | 
	
		
			
				|  |  | -     *
 | 
	
		
			
				|  |  | -     * @return
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    public AjaxResult closeSocket() {
 | 
	
		
			
				|  |  | -        if (!config.isCommGlobal()) {
 | 
	
		
			
				|  |  | -            l.warn("isCommGlobal == false [模拟器通信被禁用!]");
 | 
	
		
			
				|  |  | -            return AjaxResult.error("模拟器通信被禁用!");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        try {
 | 
	
		
			
				|  |  | -            if (cachedSocket != null) {
 | 
	
		
			
				|  |  | -                // todo:判断shutdown
 | 
	
		
			
				|  |  | -                cachedSocket.getInputStream().close();
 | 
	
		
			
				|  |  | -                cachedSocket.getOutputStream().close();
 | 
	
		
			
				|  |  | -                cachedSocket.close();
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                l.info("cachedSocket == null!");
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -        } catch (IOException e) {
 | 
	
		
			
				|  |  | -            e.printStackTrace();
 | 
	
		
			
				|  |  | -            return AjaxResult.success("关闭连接失败!请检查物联网网关连接或配置。");
 | 
	
		
			
				|  |  | -        } finally {
 | 
	
		
			
				|  |  | -            cachedSocket = null;
 | 
	
		
			
				|  |  | -            commFailCountClearAll();
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        return AjaxResult.success("关闭连接成功!");
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 等同于ping命令。
 | 
	
	
		
			
				|  | @@ -984,30 +921,6 @@ public class CommSendService {
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    private HashMap<Long, Integer> map = new HashMap<>();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * @param simId
 | 
	
		
			
				|  |  | -     * @return true超过通信失败次数限制
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    private boolean commFailCountAdd1(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 commFailCountClearOne(long simId) {
 | 
	
		
			
				|  |  | -        map.remove(simId);
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    private void commFailCountClearAll() {
 | 
	
		
			
				|  |  | -        map.clear();
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * https://mvnrepository.com/artifact/com.infiniteautomation/modbus4j/3.0.3
 | 
	
		
			
				|  |  |       */
 |