|
@@ -1,5 +1,6 @@
|
|
|
package com.ruoyi.sim.service.impl;
|
|
|
|
|
|
+import com.ruoyi.common.utils.DateUtils;
|
|
|
import com.ruoyi.sim.config.SimConfig;
|
|
|
import com.ruoyi.sim.domain.*;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -14,7 +15,6 @@ import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.Socket;
|
|
|
-import java.net.SocketTimeoutException;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
@@ -24,12 +24,12 @@ import static com.ruoyi.sim.service.impl.CommSendService.Const.*;
|
|
|
* 硬件通信
|
|
|
* send service.
|
|
|
*/
|
|
|
-@Service
|
|
|
+@Service("commSendService")
|
|
|
public class CommSendService {
|
|
|
|
|
|
interface Const {
|
|
|
|
|
|
- String ROUTER_IP = "127.0.0.1";
|
|
|
+ String ROUTER_IP = "192.168.1.1";
|
|
|
String IP = "123.112.16.165";
|
|
|
int PORT = 8899;
|
|
|
/**
|
|
@@ -95,23 +95,10 @@ public class CommSendService {
|
|
|
private SimConfig sConfig;
|
|
|
|
|
|
/**
|
|
|
- * 初始化方法,项目启动后自动运行。
|
|
|
+ *
|
|
|
*/
|
|
|
public void init() {
|
|
|
- //
|
|
|
- try {
|
|
|
- if (!isReachable(ROUTER_IP)) {
|
|
|
- // todo:ping 不通。
|
|
|
- }
|
|
|
- if (!isReachable(IP)) {
|
|
|
- // todo:ping 不通。
|
|
|
- }
|
|
|
- openSocket();
|
|
|
- //
|
|
|
- checkAllSimState();
|
|
|
- } catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -122,7 +109,8 @@ public class CommSendService {
|
|
|
}
|
|
|
|
|
|
public void readAll() {
|
|
|
- List<RealExam> listRE = realExamService.listAllStatus(RealExam.STATE_ANSWERING);
|
|
|
+ l.info("readAll");
|
|
|
+ List<RealExam> listRE = realExamService.listAllByStatus(RealExam.State.ANSWERING);
|
|
|
listRE.forEach(e -> {
|
|
|
if (e == null) {
|
|
|
return;
|
|
@@ -132,10 +120,11 @@ public class CommSendService {
|
|
|
Sim s = simService.selectSimBySimId(e.getSimId());
|
|
|
Fault f = faultService.selectFaultByFaultId(ref.getFaultId());
|
|
|
if (f != null &&
|
|
|
- f.getFaultType().equals(Fault.TYPE_3) &&
|
|
|
- f.getFaultState().equals(Fault.State.ENABLE)
|
|
|
- )
|
|
|
+ Fault.TYPE_3.equals(f.getFaultType()) &&
|
|
|
+ Fault.State.ENABLE.equals(f.getFaultState())
|
|
|
+ ) {
|
|
|
readOneFaultResistance(s, ref, f);
|
|
|
+ }
|
|
|
});
|
|
|
});
|
|
|
}
|
|
@@ -155,14 +144,53 @@ public class CommSendService {
|
|
|
Fault f = faultService.selectFaultByFaultId(ref.getFaultId());
|
|
|
readOneFaultResistance(s, ref, f);
|
|
|
});
|
|
|
- realExamService.updateOneState(re, RealExam.STATE_SUBMITTED);
|
|
|
+
|
|
|
+ // realExamService.updateOneState(re.getExamId(), RealExam.State.SUBMITTED);
|
|
|
+ {
|
|
|
+ RealExam re1 = realExamService.selectRealExamByExamId(re.getExamId());
|
|
|
+ re.setExamStatus(RealExam.State.SUBMITTED);
|
|
|
+ re.setEndTime(DateUtils.getNowDate());
|
|
|
+ realExamService.updateRealExam(re1);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * todo:????
|
|
|
* 定时任务。
|
|
|
*/
|
|
|
- public void scheduledCheckAllSimState() {
|
|
|
- checkAllSimState();
|
|
|
+ public void scheduledReadSim() {
|
|
|
+ l.info("scheduledReadSim");
|
|
|
+ if (isSocketOk()) {
|
|
|
+ readAll();
|
|
|
+ } else {
|
|
|
+ scheduledConnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo:????
|
|
|
+ * 定时任务。
|
|
|
+ */
|
|
|
+ public void scheduledConnect() {
|
|
|
+ l.info("scheduledConnect");
|
|
|
+ try {
|
|
|
+ if (!isReachable(ROUTER_IP)) {
|
|
|
+ // ping 不通。
|
|
|
+ l.warn("ping not ok");
|
|
|
+ simService.updateAllEnableState(Sim.State.GATEWAY_OFFLINE);
|
|
|
+ }
|
|
|
+ if (!isReachable(IP)) {
|
|
|
+ // todo:ping 不通。
|
|
|
+ l.warn("ping not ok");
|
|
|
+ simService.updateAllEnableState(Sim.State.GATEWAY_OFFLINE);
|
|
|
+ }
|
|
|
+ openSocket();
|
|
|
+ } catch (IOException e) {
|
|
|
+ catchException(e);
|
|
|
+ }
|
|
|
+ if (isSocketOk()) {
|
|
|
+ checkAllSimStateAsync();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -175,9 +203,9 @@ public class CommSendService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- @Async()
|
|
|
+ @Async("tp-comm")
|
|
|
public void checkAllSimStateAsync() {
|
|
|
-
|
|
|
+ checkAllSimState();
|
|
|
}
|
|
|
|
|
|
public void checkOneSimState(Sim s) {
|
|
@@ -186,6 +214,10 @@ public class CommSendService {
|
|
|
if (Objects.isNull(s)) {
|
|
|
return;
|
|
|
}
|
|
|
+ if (Sim.State.DISABLE.equals(s.getSimState())) {
|
|
|
+ l.warn("sim DISABLE getSimId = {}", s.getSimId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
//
|
|
|
try {
|
|
|
SimMsg sm = new SimMsg();
|
|
@@ -195,11 +227,17 @@ public class CommSendService {
|
|
|
sm.setReceiveMsg(receiveMsg);
|
|
|
simReceiveService.checkOneSimState(sm, s);
|
|
|
} catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ catchException(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * static lock
|
|
|
+ * todo:
|
|
|
+ */
|
|
|
+ public static boolean clearListFaultLocked = false;
|
|
|
+
|
|
|
+ /**
|
|
|
* 清除一个考试的,对应的某型号一台模拟器的,所有设备故障。
|
|
|
*
|
|
|
* @param re
|
|
@@ -209,6 +247,16 @@ public class CommSendService {
|
|
|
if (Objects.isNull(re)) {
|
|
|
|
|
|
}
|
|
|
+ // about lock
|
|
|
+ if (clearListFaultLocked) {
|
|
|
+ l.warn("clearListFaultLocked = true");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //
|
|
|
+ clearListFaultLocked = true;
|
|
|
+ {
|
|
|
+ realExamService.updateOneState(re.getExamId(), RealExam.State.SIM_WRITING);
|
|
|
+ }
|
|
|
//
|
|
|
List<RealExamFault> list = realExamFaultService.listAllType2InitStateByExamId(re.getExamId());
|
|
|
list.forEach(ref -> {
|
|
@@ -217,15 +265,17 @@ public class CommSendService {
|
|
|
l.warn("故障{}-Disable", ref.getFaultId());
|
|
|
return;
|
|
|
}
|
|
|
- l.info("f.toString() = " + f.toString());
|
|
|
+ l.info("f.toString() = {}", f);
|
|
|
Sim s = simService.selectSimBySimId(re.getSimId());
|
|
|
- l.info("s.toString() = " + s.toString());
|
|
|
+ l.info("s.toString() = {}", s);
|
|
|
// check
|
|
|
if (Objects.isNull(f)) {
|
|
|
|
|
|
}
|
|
|
clearOneFault(s, ref, f);
|
|
|
});
|
|
|
+ //
|
|
|
+ clearListFaultLocked = false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -267,19 +317,19 @@ public class CommSendService {
|
|
|
sm1.setReceiveMsg(receiveMsg1);
|
|
|
simReceiveService.clearOneFault(sm1, s, reF, f);
|
|
|
// step2
|
|
|
- if (reF != null && realExamFaultService.isState(reF.getRefId(), RealExamFault.REF_STATE_CLEARED)) {
|
|
|
+ if (reF != null && realExamFaultService.isState(reF.getRefId(), RealExamFault.State.CLEARED)) {
|
|
|
writeOneFault(s, reF, f);
|
|
|
}
|
|
|
- } catch (SocketTimeoutException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
} catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ catchException(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public void writeOneFault(Sim s, RealExamFault ref, Fault f) {
|
|
|
try {
|
|
|
+ // todo:ref is null.
|
|
|
+
|
|
|
// 下发故障
|
|
|
SimMsg sm1 = new SimMsg();
|
|
|
String sendMsg1 = buildSendMsgWriteFault(s.getSimNum(), f.getBindHardwareMsg());
|
|
@@ -295,19 +345,32 @@ public class CommSendService {
|
|
|
String receiveMsg2 = send(sendMsg2, s);
|
|
|
sm2.setReceiveMsg(receiveMsg2);
|
|
|
simReceiveService.setFaultQuestionValue(sm2, s, ref, f);
|
|
|
- // 修改关联状态。
|
|
|
- {
|
|
|
+ if (ref != null) {
|
|
|
+ // 修改关联状态。
|
|
|
RealExamFault f1 = realExamFaultService.selectRealExamFaultByRefId(ref.getRefId());
|
|
|
- f1.setRefState(RealExamFault.REF_STATE_LOOP_READ);
|
|
|
+ f1.setRefState(RealExamFault.State.LOOP_READ);
|
|
|
realExamFaultService.updateRealExamFault(f1);
|
|
|
+
|
|
|
+ //
|
|
|
+ {
|
|
|
+ boolean allWritten = realExamFaultService.isAllType2YesStateEqualWritten(f1.getExamId());
|
|
|
+ if (allWritten) {
|
|
|
+ // 如果全部下发完毕 修改RealExam状态
|
|
|
+ l.info("allWritten id = {}", f1.getExamId());
|
|
|
+ realExamService.updateOneState(f1.getExamId(), RealExam.State.SIM_PREPARE_OK);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ l.info("ref is null");
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ catchException(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public void readOneFaultResistance(Sim s, RealExamFault reF, Fault f) {
|
|
|
+ l.info("readOneFaultResistance");
|
|
|
try {
|
|
|
SimMsg sm = new SimMsg();
|
|
|
String sendMsg = buildSendMsgReadFaultResistance(s.getSimNum(), "03");
|
|
@@ -316,7 +379,7 @@ public class CommSendService {
|
|
|
sm.setReceiveMsg(receiveMsg);
|
|
|
simReceiveService.setFaultAnswerValue(sm, s, reF, f);
|
|
|
} catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ catchException(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -335,7 +398,7 @@ public class CommSendService {
|
|
|
send(sendMsg, null);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ catchException(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -423,8 +486,10 @@ public class CommSendService {
|
|
|
public synchronized String send(final String sendMsg, final Sim s) throws IOException {
|
|
|
l.info("sendMsg = " + sendMsg);
|
|
|
String receiveMsg = null;
|
|
|
- if (cachedSocket == null) {
|
|
|
+ if (!isSocketOk()) {
|
|
|
openSocket();
|
|
|
+ l.warn("socket not open!");
|
|
|
+ return "";
|
|
|
}
|
|
|
InputStream is = cachedSocket.getInputStream();
|
|
|
OutputStream os = cachedSocket.getOutputStream();
|
|
@@ -452,12 +517,22 @@ public class CommSendService {
|
|
|
}
|
|
|
|
|
|
public void openSocket() throws IOException {
|
|
|
-
|
|
|
- if (cachedSocket == null) {
|
|
|
+ if (!isSocketOk()) {
|
|
|
+ simService.updateAllEnableState(Sim.State.GATEWAY_OFFLINE);
|
|
|
+ l.info("openSocket cachedSocket is null");
|
|
|
cachedSocket = new Socket(IP, PORT);
|
|
|
- // setSoTimeout
|
|
|
- // cachedSocket.setSoTimeout(1000);
|
|
|
+ // setSoTimeout todo:????
|
|
|
+ // cachedSocket.setSoTimeout(2000);
|
|
|
+ } else {
|
|
|
+ l.info("openSocket cachedSocket is ok");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isSocketOk() {
|
|
|
+ if (cachedSocket != null && cachedSocket.isConnected()) {
|
|
|
+ return true;
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -527,4 +602,19 @@ public class CommSendService {
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ private void catchException(IOException e) {
|
|
|
+ l.warn("catchException", e);
|
|
|
+
|
|
|
+ // e.printStackTrace();
|
|
|
+ // throw new RuntimeException(e);
|
|
|
+ {
|
|
|
+ clearListFaultLocked = false;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ closeSocket();
|
|
|
+ } catch (IOException ex) {
|
|
|
+ throw new RuntimeException(ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|