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