CommBuildService.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package com.ruoyi.sim.service.impl;
  2. import com.ruoyi.common.core.domain.AjaxResult;
  3. import com.ruoyi.sim.constant.CommConst;
  4. import com.ruoyi.sim.domain.SimMsg;
  5. import com.ruoyi.sim.util.CRC16Modbus;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import static com.ruoyi.sim.constant.CommConst.*;
  13. @Service
  14. public class CommBuildService {
  15. private static final Logger l = LoggerFactory.getLogger(CommBuildService.class);
  16. /**
  17. * CRC-16(Modbus)
  18. * <p>
  19. * https://www.23bei.com/tool/59.html
  20. */
  21. public static boolean CRC_16_MODBUS = true;
  22. @Autowired
  23. private SnowflakeIdService idService;
  24. /**
  25. * 设备类型读取
  26. *
  27. * @param simNum sim.sim_num
  28. */
  29. public SimMsg buildSendMsgReadSimType(final String simNum) {
  30. return buildSendMsg(simNum, CMD_READ_TYPE, CMD_ID_GET_SN);
  31. }
  32. /**
  33. * 故障下发
  34. *
  35. * @param simNum sim.sim_num
  36. * @param bindHardwareMsg fault.bind_hardware_msg
  37. */
  38. public SimMsg buildSendMsgWriteFault(final String simNum, final String bindHardwareMsg) {
  39. return buildSendMsg(simNum, CommConst.CMD_SET_FAULT, bindHardwareMsg);
  40. }
  41. /**
  42. * 状态读取
  43. *
  44. * @param simNum sim.sim_num
  45. * @param bindHardwareMsg fault.bind_hardware_msg
  46. */
  47. public SimMsg buildSendMsgReadFaultResistance(final String simNum, final String bindHardwareMsg) {
  48. return buildSendMsg(simNum, CMD_READ_FAULT_RESISTANCE, bindHardwareMsg);
  49. }
  50. /**
  51. * 故障清清除
  52. *
  53. * @param simNum sim.sim_num
  54. * @param bindHardwareMsg fault.bind_hardware_msg
  55. */
  56. public SimMsg buildSendMsgClearFault(final String simNum, final String bindHardwareMsg) {
  57. return buildSendMsg(simNum, CMD_CLEAR_FAULT, bindHardwareMsg);
  58. }
  59. public SimMsg buildSendMsg(final String simNum, final String cmd, final String cmdId) {
  60. return buildSendMsg(simNum, cmd, cmdId, CMD_DATA_PLACE_HOLDER);
  61. }
  62. public SimMsg buildSendMsg(final String simNum, final String cmd, final String cmdId, final String data) {
  63. SimMsg smS = new SimMsg();
  64. smS.setSimMsgId(idService.nextId());
  65. if (StringUtils.isEmpty(simNum) || StringUtils.isEmpty(cmd) || StringUtils.isEmpty(cmdId) || StringUtils.isEmpty(data)) {
  66. throw new IllegalArgumentException("buildSendMsg error 01");
  67. }
  68. if (simNum.length() != LENGTH_2) {
  69. throw new IllegalArgumentException("buildSendMsg error 02");
  70. }
  71. if (cmd.length() != LENGTH_2) {
  72. throw new IllegalArgumentException("buildSendMsg error 03");
  73. }
  74. if (cmdId.length() != LENGTH_2) {
  75. throw new IllegalArgumentException("buildSendMsg error 04");
  76. }
  77. if (data.length() != LENGTH_8) {
  78. throw new IllegalArgumentException("buildSendMsg error 05");
  79. }
  80. StringBuffer m = new StringBuffer();
  81. m.append(PREFIX);
  82. m.append(simNum);
  83. m.append(ORN_SEND);
  84. m.append(cmd);
  85. m.append(cmdId);
  86. m.append(data);
  87. // 追加CRC校验
  88. if (CRC_16_MODBUS) {
  89. String crcContent = m.toString();
  90. byte[] sendByteContent = CommSendService.hexStrToByteArrs(crcContent);
  91. // l.info("sendByteContent = {}", CommSendService.bytesToHexV2(sendByteContent));
  92. byte[] sendByteCrc = CRC16Modbus.calculateCRC(sendByteContent);
  93. // l.info("sendByteCrc = {}", CommSendService.bytesToHexV2(sendByteCrc));
  94. String crc = CommSendService.bytesToHexV2(sendByteCrc);
  95. // l.debug("crc: {}", crc.toUpperCase());
  96. m.append(crc.toUpperCase());
  97. }
  98. m.append(SUFFIX);
  99. final String mFinal = m.toString();
  100. if (mFinal.length() != LENGTH_24) {
  101. throw new IllegalArgumentException("buildSendMsg error 06");
  102. }
  103. smS.setSendMsg(mFinal);
  104. return smS;
  105. }
  106. public AjaxResult debugBuildSendMsg(@RequestParam final String simNum,
  107. @RequestParam final String cmd,
  108. @RequestParam final String cmdId,
  109. @RequestParam final String data) {
  110. SimMsg sm = buildSendMsg(simNum, cmd, cmdId, data);
  111. return AjaxResult.success(sm);
  112. }
  113. /**
  114. * check receiveMsg
  115. *
  116. * @param receiveMsg
  117. * @return
  118. */
  119. public boolean checkReceiveMsg(String receiveMsg) {
  120. // 不能是empty
  121. if (StringUtils.isEmpty(receiveMsg)) {
  122. return false;
  123. }
  124. // 长度
  125. if (receiveMsg.length() != LENGTH_24) {
  126. return false;
  127. }
  128. // 数据方向
  129. final String orn = StringUtils.substring(receiveMsg, 4, 6);
  130. if (!ORN_RECEIVE.equals(orn)) {
  131. return false;
  132. }
  133. // 前缀
  134. if (!StringUtils.startsWith(receiveMsg, PREFIX)) {
  135. return false;
  136. }
  137. // 后缀
  138. if (!StringUtils.endsWith(receiveMsg, SUFFIX)) {
  139. return false;
  140. }
  141. // 计算CRC16
  142. // todo: receive报文检验错误。
  143. if (false) {
  144. String crcContent = receiveMsg.substring(0, 18);
  145. l.debug("crcContent: {}", crcContent.toUpperCase());
  146. byte[] receiveByteContent = CommSendService.hexStrToByteArrs(crcContent);
  147. byte[] receiveByteCrc = CRC16Modbus.calculateCRC(receiveByteContent);
  148. String crc = CommSendService.bytesToHexV2(receiveByteCrc);
  149. l.debug("crc: {}", crc.toUpperCase());
  150. // if (!receiveMsg.substring(19, 22).equals(crc.toUpperCase())) {
  151. // throw new IllegalArgumentException("checkReceiveMsg length error");
  152. // }
  153. // todo: 比对校验值,不正确。
  154. }
  155. return true;
  156. }
  157. }