|
@@ -1,8 +1,8 @@
|
|
|
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;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -14,11 +14,10 @@ import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.Socket;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
-import static com.ruoyi.sim.service.impl.CommSendService.Const.*;
|
|
|
-
|
|
|
/**
|
|
|
* 硬件通信
|
|
|
* send service.
|
|
@@ -26,52 +25,6 @@ import static com.ruoyi.sim.service.impl.CommSendService.Const.*;
|
|
|
@Service("commSendService")
|
|
|
public class CommSendService {
|
|
|
|
|
|
- interface Const {
|
|
|
-
|
|
|
- /**
|
|
|
- * 报文长度
|
|
|
- */
|
|
|
- int MSG_LENGTH = 20;
|
|
|
-
|
|
|
- String PREFIX = "AA";
|
|
|
-
|
|
|
- String SUFFIX = "55";
|
|
|
-
|
|
|
- /**
|
|
|
- * orientation
|
|
|
- */
|
|
|
- String ORN_SEND = "01";
|
|
|
-
|
|
|
- /**
|
|
|
- * orientation
|
|
|
- */
|
|
|
- String ORN_RECEIVE = "02";
|
|
|
-
|
|
|
- String CMD_DATA_PLACE_HOLDER = "00000000";
|
|
|
-
|
|
|
- /**
|
|
|
- * 故障下发
|
|
|
- */
|
|
|
- String CMD_SET_FAULT = "01";
|
|
|
- /**
|
|
|
- * 故障清清除
|
|
|
- */
|
|
|
- String CMD_CLEAR_FAULT = "02";
|
|
|
- /**
|
|
|
- * 设备类型读取
|
|
|
- */
|
|
|
- String CMD_READ_TYPE = "03";
|
|
|
- /**
|
|
|
- * 状态读取 RESISTANCE电阻值
|
|
|
- */
|
|
|
- String CMD_READ_FAULT_RESISTANCE = "03";
|
|
|
-
|
|
|
- String CMD_ID_GET_SN = "B1";
|
|
|
- int LENGTH_2 = 2;
|
|
|
- int LENGTH_8 = 8;
|
|
|
- int LENGTH_20 = 20;
|
|
|
- }
|
|
|
-
|
|
|
private static final Logger l = LoggerFactory.getLogger(CommSendService.class);
|
|
|
/**
|
|
|
* 缓存的Socket连接。
|
|
@@ -89,13 +42,8 @@ public class CommSendService {
|
|
|
private RealExamFaultService realExamFaultService;
|
|
|
@Autowired
|
|
|
private SimConfig config;
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- */
|
|
|
- public void init() {
|
|
|
-
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ private CommBuildService commBuildService;
|
|
|
|
|
|
/**
|
|
|
* 定时任务。
|
|
@@ -205,7 +153,7 @@ public class CommSendService {
|
|
|
} catch (IOException e) {
|
|
|
catchException(e);
|
|
|
}
|
|
|
- if (isSocketOk()) {
|
|
|
+ if (isSocketOk()) { // todo:????
|
|
|
checkAllSimStateAsync();
|
|
|
}
|
|
|
}
|
|
@@ -238,7 +186,7 @@ public class CommSendService {
|
|
|
//
|
|
|
try {
|
|
|
SimMsg sm = new SimMsg();
|
|
|
- String sendMsg = buildSendMsgReadSimType(s.getSimNum());
|
|
|
+ String sendMsg = commBuildService.buildSendMsgReadSimType(s.getSimNum());
|
|
|
sm.setSendMsg(sendMsg);
|
|
|
String receiveMsg = send(sendMsg, s);
|
|
|
sm.setReceiveMsg(receiveMsg);
|
|
@@ -316,33 +264,65 @@ public class CommSendService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ public SimMsg debugReadSimType(final String simNum) throws IOException {
|
|
|
+ String msg1 = commBuildService.buildSendMsgReadSimType(simNum);
|
|
|
+ String msg2 = send(msg1, null);
|
|
|
+ return new SimMsg(msg1, msg2);
|
|
|
+ }
|
|
|
+
|
|
|
+ public SimMsg debugClearOneFault(final String simNum, final String bindHardwareMsg) throws IOException {
|
|
|
+ String msg1 = commBuildService.buildSendMsgClearFault(simNum, bindHardwareMsg);
|
|
|
+ String msg2 = send(msg1, null);
|
|
|
+ return new SimMsg(msg1, msg2);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<SimMsg> debugClearAllFault(final String simNum, String simType) throws IOException {
|
|
|
+ List<SimMsg> list = new ArrayList<>();
|
|
|
+ String[] array = CommBuildService.CommConst.TYPE_1_BIND_MSG;
|
|
|
+ for (String b : array) {
|
|
|
+ list.add(debugClearOneFault(simNum, b));
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @param s
|
|
|
* @param reF 可以为空,表示不关联考试的。
|
|
|
* @param f
|
|
|
*/
|
|
|
public void clearOneFault(Sim s, RealExamFault reF, Fault f) {
|
|
|
- l.info("故障清除:getSimId = {},fault.getName = {}", s.getSimId(), f.getName());
|
|
|
+ l.info("清除One故障:getSimNum = {},fault.getName = {}", s.getSimNum(), f.getName());
|
|
|
// check todo:
|
|
|
|
|
|
//
|
|
|
try {
|
|
|
// step1
|
|
|
+ String msg1 = commBuildService.buildSendMsgClearFault(s.getSimNum(), f.getBindHardwareMsg());
|
|
|
SimMsg sm1 = new SimMsg();
|
|
|
- String sendMsg1 = buildSendMsgClearFault(s.getSimNum(), f.getBindHardwareMsg());
|
|
|
- sm1.setSendMsg(sendMsg1);
|
|
|
- String receiveMsg1 = send(sendMsg1, s);
|
|
|
- sm1.setReceiveMsg(receiveMsg1);
|
|
|
+ sm1.setSendMsg(msg1);
|
|
|
+ String msg2 = send(msg1, s);
|
|
|
+ sm1.setReceiveMsg(msg2);
|
|
|
simReceiveService.clearOneFault(sm1, s, reF, f);
|
|
|
// step2
|
|
|
- if (reF != null && realExamFaultService.isState(reF.getRefId(), RealExamFault.State.CLEARED)) {
|
|
|
- writeOneFault(s, reF, f);
|
|
|
+ if (reF != null &&
|
|
|
+ realExamFaultService.isState(reF.getRefId(), RealExamFault.State.CLEARED)) {
|
|
|
+ if (reF.getFlag().equals(RealExamFault.Flag.YES)) {
|
|
|
+ writeOneFault(s, reF, f);
|
|
|
+ } else if (reF.getFlag().equals(RealExamFault.Flag.NO)) {
|
|
|
+ RealExamFault f1 = realExamFaultService.selectRealExamFaultByRefId(reF.getRefId());
|
|
|
+ f1.setRefState(RealExamFault.State.LOOP_READ);
|
|
|
+ }
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
catchException(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public SimMsg debugWriteOneFault(final String simNum, final String bindHardwareMsg) throws IOException {
|
|
|
+ String msg1 = commBuildService.buildSendMsgWriteFault(simNum, bindHardwareMsg);
|
|
|
+ String msg2 = send(msg1, null);
|
|
|
+ return new SimMsg(msg1, msg2);
|
|
|
+ }
|
|
|
|
|
|
public void writeOneFault(Sim s, RealExamFault ref, Fault f) {
|
|
|
l.info("下发故障:getSimId = {},fault.getName = {}", s.getSimId(), f.getName());
|
|
@@ -351,7 +331,7 @@ public class CommSendService {
|
|
|
|
|
|
// 下发故障
|
|
|
SimMsg sm1 = new SimMsg();
|
|
|
- String sendMsg1 = buildSendMsgWriteFault(s.getSimNum(), f.getBindHardwareMsg());
|
|
|
+ String sendMsg1 = commBuildService.buildSendMsgWriteFault(s.getSimNum(), f.getBindHardwareMsg());
|
|
|
sm1.setSendMsg(sendMsg1);
|
|
|
String receiveMsg1 = send(sendMsg1, s);
|
|
|
sm1.setReceiveMsg(receiveMsg1);
|
|
@@ -359,7 +339,7 @@ public class CommSendService {
|
|
|
|
|
|
// 读取一次当前电阻代表值作为出题值。
|
|
|
SimMsg sm2 = new SimMsg();
|
|
|
- String sendMsg2 = buildSendMsgReadFaultResistance(s.getSimNum(), f.getBindHardwareMsg());
|
|
|
+ String sendMsg2 = commBuildService.buildSendMsgReadFaultResistance(s.getSimNum(), f.getBindHardwareMsg());
|
|
|
sm2.setSendMsg(sendMsg2);
|
|
|
String receiveMsg2 = send(sendMsg2, s);
|
|
|
sm2.setReceiveMsg(receiveMsg2);
|
|
@@ -376,9 +356,14 @@ public class CommSendService {
|
|
|
if (allWritten) {
|
|
|
// 如果全部下发完毕 修改RealExam状态
|
|
|
l.info("allWritten id = {}", f1.getExamId());
|
|
|
- realExamService.updateOneState(f1.getExamId(), RealExam.State.SIM_PREPARE_OK);
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // todo:
|
|
|
+
|
|
|
+ // todo:单独一个故障,特殊处理
|
|
|
+ realExamService.updateOneState(f1.getExamId(), RealExam.State.SIM_PREPARE_OK);
|
|
|
} else {
|
|
|
l.info("ref is null");
|
|
|
}
|
|
@@ -387,12 +372,17 @@ public class CommSendService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public SimMsg debugReadOneFaultResistance(final String simNum, final String bindHardwareMsg) throws IOException {
|
|
|
+ String msg1 = commBuildService.buildSendMsgReadFaultResistance(simNum, bindHardwareMsg);
|
|
|
+ String msg2 = send(msg1, null);
|
|
|
+ return new SimMsg(msg1, msg2);
|
|
|
+ }
|
|
|
|
|
|
public void readOneFaultResistance(Sim s, RealExamFault reF, Fault f, String refState) {
|
|
|
l.info("readOneFaultResistance");
|
|
|
try {
|
|
|
SimMsg sm = new SimMsg();
|
|
|
- String sendMsg = buildSendMsgReadFaultResistance(s.getSimNum(), f.getBindHardwareMsg());
|
|
|
+ String sendMsg = commBuildService.buildSendMsgReadFaultResistance(s.getSimNum(), f.getBindHardwareMsg());
|
|
|
sm.setSendMsg(sendMsg);
|
|
|
String receiveMsg = send(sendMsg, s);
|
|
|
sm.setReceiveMsg(receiveMsg);
|
|
@@ -411,15 +401,15 @@ public class CommSendService {
|
|
|
public void test() {
|
|
|
try {
|
|
|
{
|
|
|
- String sendMsg = buildSendMsgReadFaultResistance("01", "04");
|
|
|
+ String sendMsg = commBuildService.buildSendMsgReadFaultResistance("01", "04");
|
|
|
send(sendMsg, null);
|
|
|
}
|
|
|
{
|
|
|
- String sendMsg = buildSendMsgReadFaultResistance("01", "05");
|
|
|
+ String sendMsg = commBuildService.buildSendMsgReadFaultResistance("01", "05");
|
|
|
send(sendMsg, null);
|
|
|
}
|
|
|
{
|
|
|
- String sendMsg = buildSendMsgReadFaultResistance("01", "06");
|
|
|
+ String sendMsg = commBuildService.buildSendMsgReadFaultResistance("01", "06");
|
|
|
send(sendMsg, null);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
@@ -428,80 +418,6 @@ public class CommSendService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设备类型读取
|
|
|
- *
|
|
|
- * @param simNum sim.sim_num
|
|
|
- */
|
|
|
- public String buildSendMsgReadSimType(final String simNum) {
|
|
|
- return buildSendMsg(simNum, CMD_READ_TYPE, CMD_ID_GET_SN);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 故障下发
|
|
|
- *
|
|
|
- * @param simNum sim.sim_num
|
|
|
- * @param bindHardwareMsg fault.bind_hardware_msg
|
|
|
- */
|
|
|
- public String buildSendMsgWriteFault(final String simNum, final String bindHardwareMsg) {
|
|
|
- return buildSendMsg(simNum, CMD_SET_FAULT, bindHardwareMsg);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 状态读取
|
|
|
- *
|
|
|
- * @param simNum sim.sim_num
|
|
|
- * @param bindHardwareMsg fault.bind_hardware_msg
|
|
|
- */
|
|
|
- public String buildSendMsgReadFaultResistance(final String simNum, final String bindHardwareMsg) {
|
|
|
- return buildSendMsg(simNum, CMD_READ_FAULT_RESISTANCE, bindHardwareMsg);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 故障清清除
|
|
|
- *
|
|
|
- * @param simNum sim.sim_num
|
|
|
- * @param bindHardwareMsg fault.bind_hardware_msg
|
|
|
- */
|
|
|
- public String buildSendMsgClearFault(final String simNum, final String bindHardwareMsg) {
|
|
|
- return buildSendMsg(simNum, CMD_CLEAR_FAULT, bindHardwareMsg);
|
|
|
- }
|
|
|
-
|
|
|
- public String buildSendMsg(final String simNum, final String cmd, final String cmdId) {
|
|
|
- return buildSendMsg(simNum, cmd, cmdId, CMD_DATA_PLACE_HOLDER);
|
|
|
- }
|
|
|
-
|
|
|
- public String buildSendMsg(final String simNum, final String cmd, final String cmdId, final String data) {
|
|
|
- if (StringUtils.isEmpty(simNum) || StringUtils.isEmpty(cmd) || StringUtils.isEmpty(cmdId) || StringUtils.isEmpty(data)) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg isEmpty");
|
|
|
- }
|
|
|
- if (simNum.length() != LENGTH_2) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg length error");
|
|
|
- }
|
|
|
- if (cmd.length() != LENGTH_2) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg length error");
|
|
|
- }
|
|
|
- if (cmdId.length() != LENGTH_2) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg length error");
|
|
|
- }
|
|
|
- if (data.length() != LENGTH_8) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg length error");
|
|
|
- }
|
|
|
- StringBuffer m = new StringBuffer();
|
|
|
- m.append(PREFIX);
|
|
|
- m.append(simNum);
|
|
|
- m.append(ORN_SEND);
|
|
|
- m.append(cmd);
|
|
|
- m.append(cmdId);
|
|
|
- m.append(data);
|
|
|
- m.append(SUFFIX);
|
|
|
- final String mFinal = m.toString();
|
|
|
- if (mFinal.length() != LENGTH_20) {
|
|
|
- throw new IllegalArgumentException("buildSendMsg length error");
|
|
|
- }
|
|
|
- return mFinal;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* send hex message
|
|
|
*
|
|
|
* @param sendMsg
|
|
@@ -513,13 +429,21 @@ public class CommSendService {
|
|
|
l.warn("isCommGlobal == false [模拟器通信被禁用!]");
|
|
|
return "";
|
|
|
}
|
|
|
- l.info("********sendMsg == [{}]", sendMsg);
|
|
|
+ SimMsg tempSm = new SimMsg();
|
|
|
+ // log.
|
|
|
+ {
|
|
|
+ tempSm.setSendMsg(sendMsg);
|
|
|
+ l.info("********sendMsg == [{}]", tempSm);
|
|
|
+ }
|
|
|
//
|
|
|
{
|
|
|
- try {
|
|
|
- Thread.currentThread().sleep(1000L);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ if (false) {
|
|
|
+ try {
|
|
|
+ // sleep ,追求顺序请求。
|
|
|
+ Thread.sleep(CommBuildService.CommConst.SLEEP);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
//
|
|
@@ -532,6 +456,7 @@ public class CommSendService {
|
|
|
InputStream is = cachedSocket.getInputStream();
|
|
|
OutputStream os = cachedSocket.getOutputStream();
|
|
|
os.write(hexStrToByteArrs(sendMsg));
|
|
|
+ tempSm.setSendTime(DateUtils.getNowDate());
|
|
|
if (s != null) {
|
|
|
simService.updateLastSentTime(s);
|
|
|
}
|
|
@@ -542,8 +467,13 @@ public class CommSendService {
|
|
|
sbHex.append(String.format("%02X", buffer[i]));
|
|
|
}
|
|
|
receiveMsg = sbHex.toString();
|
|
|
- l.info("********receiveMsg = [{}]", receiveMsg);
|
|
|
- if (!checkReceiveMsg(receiveMsg)) {
|
|
|
+ tempSm.setReceiveTime(DateUtils.getNowDate());
|
|
|
+ tempSm.setReceiveMsg(receiveMsg);
|
|
|
+ // log.
|
|
|
+ {
|
|
|
+ l.info("********receiveMsg = [{}]", tempSm);
|
|
|
+ }
|
|
|
+ if (!commBuildService.checkReceiveMsg(receiveMsg)) {
|
|
|
// todo:
|
|
|
l.warn("checkReceiveMsg fail receiveMsg = {}", receiveMsg);
|
|
|
return "";
|
|
@@ -555,13 +485,13 @@ public class CommSendService {
|
|
|
}
|
|
|
|
|
|
public void openSocket() throws IOException {
|
|
|
- l.info(config.toString());
|
|
|
if (!config.isCommGlobal()) {
|
|
|
l.warn("isCommGlobal == false [模拟器通信被禁用!]");
|
|
|
return;
|
|
|
}
|
|
|
if (!isSocketOk()) {
|
|
|
- simService.updateAllEnableState(Sim.State.GATEWAY_OFFLINE);
|
|
|
+ // todo:
|
|
|
+ // simService.updateAllEnableState(Sim.State.GATEWAY_OFFLINE);
|
|
|
l.info("openSocket cachedSocket is null");
|
|
|
cachedSocket = new Socket(config.getRs485Ip(), config.getRs485Port());
|
|
|
// setSoTimeout todo:????
|
|
@@ -571,6 +501,11 @@ public class CommSendService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 连接情况是否ok。
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public boolean isSocketOk() {
|
|
|
if (cachedSocket != null && cachedSocket.isConnected()) {
|
|
|
return true;
|
|
@@ -620,32 +555,6 @@ public class CommSendService {
|
|
|
return ia.isReachable(2000);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * check receiveMsg
|
|
|
- *
|
|
|
- * @param receiveMsg
|
|
|
- * @return
|
|
|
- */
|
|
|
- public boolean checkReceiveMsg(String receiveMsg) {
|
|
|
- if (StringUtils.isEmpty(receiveMsg)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (receiveMsg.length() != CommSendService.Const.LENGTH_20) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- final String orn = StringUtils.substring(receiveMsg, 4, 6);
|
|
|
- if (!CommSendService.Const.ORN_RECEIVE.equals(orn)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (!StringUtils.startsWith(receiveMsg, CommSendService.Const.PREFIX)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (!StringUtils.endsWith(receiveMsg, CommSendService.Const.SUFFIX)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
private void catchException(IOException e) {
|
|
|
l.warn("catchException", e);
|
|
|
|