|
@@ -1,7 +1,6 @@
|
|
|
package com.ruoyi.sim.service.impl;
|
|
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
-import com.ruoyi.sim.config.SimConfig;
|
|
|
import com.ruoyi.sim.constant.CommConst;
|
|
|
import com.ruoyi.sim.domain.Seat;
|
|
|
import com.ruoyi.sim.domain.Sim;
|
|
@@ -11,11 +10,13 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
import static com.ruoyi.sim.constant.CommConst.RETRY_COUNT_0;
|
|
|
import static com.ruoyi.sim.constant.CommConst.RETRY_COUNT_QUERY_SN_IMPORTANT;
|
|
@@ -26,8 +27,6 @@ public class CommCheckService {
|
|
|
private static final Logger l = LoggerFactory.getLogger(CommCheckService.class);
|
|
|
|
|
|
@Autowired
|
|
|
- private SimConfig config;
|
|
|
- @Autowired
|
|
|
private SeatService seatService;
|
|
|
@Autowired
|
|
|
private SimService simService;
|
|
@@ -37,6 +36,8 @@ public class CommCheckService {
|
|
|
private CommSendService commSendService;
|
|
|
@Autowired
|
|
|
private FailedCountService failedCountService;
|
|
|
+ @Autowired
|
|
|
+ private SocketService socketService;
|
|
|
|
|
|
/**
|
|
|
* 等同于ping命令。
|
|
@@ -91,6 +92,7 @@ public class CommCheckService {
|
|
|
if (pingIsReachable(rs485Ip)) {
|
|
|
return AjaxResult.success();
|
|
|
} else {
|
|
|
+ // 更新SocketState
|
|
|
seatService.updateSocketStateByRs485Ip(rs485Ip, Seat.SocketState.OFFLINE);
|
|
|
return AjaxResult.error("RS485物联网网关[" + rs485Ip + "]无法连接!");
|
|
|
}
|
|
@@ -111,101 +113,83 @@ public class CommCheckService {
|
|
|
* 检查一个座次状况。
|
|
|
* 维护 seat表-socket_state字段;
|
|
|
* 维护 seat表-sim_id字段;
|
|
|
+ * 维护 sim表-Online、Offline状态;
|
|
|
*
|
|
|
- * @param seat
|
|
|
+ * @param seat 座次
|
|
|
* @param important true:重要的场景 开始考试 重试次数不同,也会进行序列号检查。false:不重要场景 定时巡查。
|
|
|
* @return
|
|
|
*/
|
|
|
+ @Transactional
|
|
|
public AjaxResult checkOneSeatState(final Seat seat, final boolean important) {
|
|
|
// check args.
|
|
|
if (seat == null) {
|
|
|
throw new IllegalArgumentException("seat is null");
|
|
|
}
|
|
|
//
|
|
|
+ socketService.tryOpenAll();
|
|
|
+ //
|
|
|
int retryTotalCount;
|
|
|
if (important) {
|
|
|
- AjaxResult ar01 = checkRouterState(config.getRouterIp());
|
|
|
- if (ar01.isError()) {
|
|
|
- return ar01;
|
|
|
- }
|
|
|
- AjaxResult ar02 = checkPingRs485State(seat.getSeatRs485Ip());
|
|
|
- if (ar02.isError()) {
|
|
|
- // 更新SimId
|
|
|
- seatService.updateSimIdBySeatNum(seat.getSeatNum(), Seat.ID_0);
|
|
|
- // 更新SocketState
|
|
|
- seatService.updateSocketStateBySeatNum(seat.getSeatNum(), Seat.SocketState.OFFLINE);
|
|
|
- return ar02;
|
|
|
- } else {
|
|
|
- // Ping通不代表在线
|
|
|
- }
|
|
|
- retryTotalCount = RETRY_COUNT_QUERY_SN_IMPORTANT;
|
|
|
+ retryTotalCount = CommConst.RETRY_COUNT_WHICH_SIM_IMPORTANT;
|
|
|
} else {
|
|
|
- retryTotalCount = RETRY_COUNT_0;
|
|
|
+ retryTotalCount = CommConst.RETRY_COUNT_0;
|
|
|
}
|
|
|
SimMsg smS01 = commBuildService.buildSendMsgWhichSim();
|
|
|
SimMsg smR01 = commSendService.send(smS01, seat, null, retryTotalCount, CommConst.SLEEP_SHORT);
|
|
|
- final String simNum = CommParseUtils.subSimNum(smR01);
|
|
|
- Sim sim = simService.uniqueBySimNum(simNum);
|
|
|
- if (sim == null) {
|
|
|
- return AjaxResult.error("找不到模拟器[" + simNum + "]对应数据。");
|
|
|
- }
|
|
|
- if (Sim.State.DISABLE.equals(sim.getSimState())) {
|
|
|
- l.warn("sim DISABLE,模拟器被禁用,sim = {}", sim);
|
|
|
- return AjaxResult.error("模拟器[" + simNum + "]被禁用");
|
|
|
- }
|
|
|
- if (StringUtils.isBlank(sim.getSimType()) || StringUtils.isBlank(sim.getSimNum())) {
|
|
|
- l.warn("sim error data {}", sim);
|
|
|
- return AjaxResult.error("模拟器数据错误。");// todo:
|
|
|
+ Integer result = smR01.getResult();
|
|
|
+ if (Objects.equals(result, SimMsg.Result.SUCCESS)) {
|
|
|
+ final String simNum = CommParseUtils.subSimNum(smR01);
|
|
|
+ Sim sim = simService.uniqueBySimNum(simNum);
|
|
|
+ if (sim == null) {
|
|
|
+ return AjaxResult.error("找不到模拟器[" + simNum + "]对应数据。");
|
|
|
+ } else {
|
|
|
+ l.info("在座次[{}]上发现模拟器[{}]", seat.getSeatNum(), sim.getSimNum());
|
|
|
+ }
|
|
|
+ // 更新SimId
|
|
|
+ seatService.updateSimIdBySeatNum(seat.getSeatNum(), sim.getSimId());
|
|
|
+ // 更新Sim状态
|
|
|
+ simService.updateSimStateBySimId(sim.getSimId(), Sim.State.ONLINE);
|
|
|
+ return AjaxResult.success("成功,检查一个座次[" + seat.getSeatNum() + "]OK!模拟器[" + sim.getSimNum() + "]在线。");
|
|
|
+ } else if (Objects.equals(result, SimMsg.Result.RECEIVE_CHECK_FAIL)) {
|
|
|
+ return AjaxResult.error("失败,报文回复异常。");
|
|
|
+ } else if (Objects.equals(result, SimMsg.Result.SOCKET_EXCEPTION)) {
|
|
|
+ // 更新SimId
|
|
|
+ seatService.updateSimIdBySeatNum(seat.getSeatNum(), Sim.ID_0);
|
|
|
+ return AjaxResult.success("成功,检查一个座次[" + seat.getSeatNum() + "]OK!未连接模拟器。");
|
|
|
}
|
|
|
- // 更新SimId
|
|
|
- seatService.updateSimIdBySeatNum(seat.getSeatNum(), sim.getSimId());
|
|
|
- return AjaxResult.success("成功,检查一个座次[" + seat.getSeatNum() + "]OK!");
|
|
|
+ return AjaxResult.error("失败");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 默认Seat中已经有CurrentSimId数据
|
|
|
+ * 纯数据库查询,模拟器是否在线。
|
|
|
+ * 模拟器是否被禁用。
|
|
|
*
|
|
|
- * @param seat
|
|
|
- * @param important
|
|
|
+ * @param simId
|
|
|
* @return
|
|
|
*/
|
|
|
- public AjaxResult checkOneSimState(final Seat seat, final boolean important) {
|
|
|
- // check args.
|
|
|
- if (seat == null) {
|
|
|
- throw new IllegalArgumentException("seat is null");
|
|
|
- }
|
|
|
- if (seat.getCurrentSimId() == null || Sim.ID_0.equals(seat.getCurrentSimId())) {
|
|
|
- return AjaxResult.error("模拟器ID不存在!");
|
|
|
- }
|
|
|
- if (!simService.existBySimId(seat.getCurrentSimId())) {
|
|
|
- return AjaxResult.error("模拟器ID[" + seat.getCurrentSimId() + "]不存在!");
|
|
|
- }
|
|
|
- //
|
|
|
- int retryTotalCount;
|
|
|
- if (important) {
|
|
|
- retryTotalCount = RETRY_COUNT_QUERY_SN_IMPORTANT;
|
|
|
- } else {
|
|
|
- retryTotalCount = RETRY_COUNT_0;
|
|
|
- }
|
|
|
- Sim sim = simService.selectSimBySimId(seat.getCurrentSimId());
|
|
|
- SimMsg smS02 = commBuildService.buildSendMsgReadSimType(sim.getSimNum());
|
|
|
- SimMsg smR02 = commSendService.send(smS02, seat, sim, retryTotalCount, CommConst.SLEEP_SHORT);
|
|
|
- if (StringUtils.isNotBlank(smR02.getReceiveMsg())) {
|
|
|
- // 只要返回正确报文,即认为在线。
|
|
|
- simService.updateSimStateBySimId(sim.getSimId(), Sim.State.ONLINE);
|
|
|
- // SocketOldService实现。
|
|
|
- // socketOldService.commFailCountClearOne(sim.getSimId());
|
|
|
- failedCountService.reset0(seat.toSimSocketVo());
|
|
|
- return AjaxResult.success("成功,检查一个模拟器[" + sim.getSimNum() + "]OK!");
|
|
|
+ public AjaxResult checkOneSimOnlineState(final Long simId) {
|
|
|
+ Sim sim = simService.selectSimBySimId(simId);
|
|
|
+ if (sim != null) {
|
|
|
+ switch (sim.getSimState()) {
|
|
|
+ case Sim.State.ONLINE: {
|
|
|
+ return AjaxResult.success("模拟器[" + sim.getSimNum() + "]在线!");
|
|
|
+ }
|
|
|
+ case Sim.State.OFFLINE: {
|
|
|
+ return AjaxResult.success("模拟器[" + sim.getSimNum() + "]离线!");
|
|
|
+ }
|
|
|
+ case Sim.State.DISABLE: {
|
|
|
+ return AjaxResult.error("模拟器[" + sim.getSimNum() + "]禁用!");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- return AjaxResult.error("失败,检查一个模拟器[" + sim.getSimNum() + "]在线状态执行错误!");
|
|
|
+ return AjaxResult.error("模拟器[" + Objects.requireNonNull(sim).getSimNum() + "]XXXX!");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 默认Seat中已经有CurrentSimId数据
|
|
|
* 检查回应报文模拟器类型是否正确。
|
|
|
*
|
|
|
- * @param seat
|
|
|
+ * @param seat 座次
|
|
|
* @param important
|
|
|
* @param targetSimType 期望模拟器目标类型
|
|
|
* @return
|
|
@@ -231,10 +215,10 @@ public class CommCheckService {
|
|
|
retryTotalCount = RETRY_COUNT_0;
|
|
|
}
|
|
|
Sim sim = simService.selectSimBySimId(seat.getCurrentSimId());
|
|
|
- SimMsg smS02 = commBuildService.buildSendMsgReadSimType(sim.getSimNum());
|
|
|
- SimMsg smR02 = commSendService.send(smS02, seat, sim, retryTotalCount, CommConst.SLEEP_SHORT);
|
|
|
- if (StringUtils.isNotBlank(smR02.getReceiveMsg())) {
|
|
|
- final String content = CommParseUtils.subContentData(smR02);
|
|
|
+ SimMsg smS = commBuildService.buildSendMsgReadSimType(sim.getSimNum());
|
|
|
+ SimMsg smR = commSendService.send(smS, seat, sim, retryTotalCount, CommConst.SLEEP_SHORT);
|
|
|
+ if (StringUtils.isNotBlank(smR.getReceiveMsg())) {
|
|
|
+ final String content = CommParseUtils.subContentData(smR);
|
|
|
switch (targetSimType) {
|
|
|
case Sim.TYPE_0001 -> {
|
|
|
if (content.startsWith(CommConst.TYPE_0001_SN_PREFIX) && content.endsWith(sim.getSimNum())) {
|
|
@@ -264,13 +248,54 @@ public class CommCheckService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 默认Seat中已经有CurrentSimId数据
|
|
|
+ *
|
|
|
+ * @param seat
|
|
|
+ * @param important
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public AjaxResult checkOneSimOnlineState(final Seat seat, final boolean important) {
|
|
|
+ // check args.
|
|
|
+ if (seat == null) {
|
|
|
+ throw new IllegalArgumentException("seat is null");
|
|
|
+ }
|
|
|
+ if (seat.getCurrentSimId() == null || Sim.ID_0.equals(seat.getCurrentSimId())) {
|
|
|
+ return AjaxResult.error("模拟器ID不存在!");
|
|
|
+ }
|
|
|
+ if (!simService.existBySimId(seat.getCurrentSimId())) {
|
|
|
+ return AjaxResult.error("模拟器ID[" + seat.getCurrentSimId() + "]不存在!");
|
|
|
+ }
|
|
|
+ //
|
|
|
+ int retryTotalCount;
|
|
|
+ if (important) {
|
|
|
+ retryTotalCount = RETRY_COUNT_QUERY_SN_IMPORTANT;
|
|
|
+ } else {
|
|
|
+ retryTotalCount = RETRY_COUNT_0;
|
|
|
+ }
|
|
|
+ Sim sim = simService.selectSimBySimId(seat.getCurrentSimId());
|
|
|
+ SimMsg smS02 = commBuildService.buildSendMsgReadSimType(sim.getSimNum());
|
|
|
+ SimMsg smR02 = commSendService.send(smS02, seat, sim, retryTotalCount, CommConst.SLEEP_SHORT);
|
|
|
+ if (StringUtils.isNotBlank(smR02.getReceiveMsg())) {
|
|
|
+ // 只要返回正确报文,即认为在线。
|
|
|
+ simService.updateSimStateBySimId(sim.getSimId(), Sim.State.ONLINE);
|
|
|
+ // SocketOldService实现。
|
|
|
+ // socketOldService.commFailCountClearOne(sim.getSimId());
|
|
|
+ failedCountService.reset0(seat.toSimSocketVo());
|
|
|
+ return AjaxResult.success("成功,检查一个模拟器[" + sim.getSimNum() + "]OK!");
|
|
|
+ }
|
|
|
+ return AjaxResult.error("失败,检查一个模拟器[" + sim.getSimNum() + "]在线状态执行错误!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* [定时执行]查找所有没有被手动禁用的座次 和 座次上的模拟器。
|
|
|
*/
|
|
|
- public void checkAllSeatAndSimState() {
|
|
|
+ public AjaxResult checkAllSeatAndSimState() {
|
|
|
List<Seat> list = seatService.listAllEnable();
|
|
|
list.forEach(seat -> {
|
|
|
checkOneSeatState(seat, false);
|
|
|
- checkOneSimState(seat, false);
|
|
|
+ // checkOneSimState(seat, false);
|
|
|
});
|
|
|
+ return AjaxResult.success("检查完毕,成功!");
|
|
|
}
|
|
|
}
|