CommBuildService.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 static com.ruoyi.sim.constant.CommConst.*;
  12. /**
  13. * 万能询问报文
  14. * AA000103B100000000E0BE55
  15. */
  16. @Service
  17. public class CommBuildService {
  18. private static final Logger l = LoggerFactory.getLogger(CommBuildService.class);
  19. /**
  20. * CRC-16(Modbus)
  21. * <p>
  22. * https://www.23bei.com/tool/59.html
  23. */
  24. public static boolean CRC_16_MODBUS = true;
  25. @Autowired
  26. private SnowflakeIdService idService;
  27. /**
  28. * 读取设备序列号
  29. *
  30. * @param simNum sim.sim_num
  31. */
  32. public SimMsg buildSendMsgReadSimType(final String simNum) {
  33. return buildSendMsg(simNum, CMD_READ_TYPE, CMD_ID_GET_SN);
  34. }
  35. /**
  36. * 询问设备类型和序列号
  37. *
  38. * @return
  39. */
  40. public SimMsg buildSendMsgWhichSim() {
  41. return buildSendMsg(CommConst.BLANK_SIM_NUM, CMD_READ_TYPE, CMD_ID_GET_SN);
  42. }
  43. /**
  44. * 故障下发
  45. *
  46. * @param simNum sim.sim_num
  47. * @param bindHardwareMsg fault.bind_hardware_msg
  48. */
  49. public SimMsg buildSendMsgWriteFault(final String simNum, final String bindHardwareMsg) {
  50. return buildSendMsg(simNum, CommConst.CMD_SET_FAULT, bindHardwareMsg);
  51. }
  52. /**
  53. * 读取故障
  54. *
  55. * @param simNum sim.sim_num
  56. * @param bindHardwareMsg fault.bind_hardware_msg
  57. */
  58. public SimMsg buildSendMsgReadFaultResistance(final String simNum, final String bindHardwareMsg) {
  59. return buildSendMsg(simNum, CMD_READ_FAULT_RESISTANCE, bindHardwareMsg);
  60. }
  61. /**
  62. * 清除故障
  63. *
  64. * @param simNum sim.sim_num
  65. * @param bindHardwareMsg fault.bind_hardware_msg
  66. */
  67. public SimMsg buildSendMsgClearFault(final String simNum, final String bindHardwareMsg) {
  68. return buildSendMsg(simNum, CMD_CLEAR_FAULT, bindHardwareMsg);
  69. }
  70. /**
  71. * 填充内容为空。
  72. *
  73. * @param simNum
  74. * @param cmd
  75. * @param cmdId
  76. * @return
  77. */
  78. public SimMsg buildSendMsg(final String simNum, final String cmd, final String cmdId) {
  79. return buildSendMsg(simNum, ORN_SEND, cmd, cmdId, CommConst.CMD_DATA_PLACE_HOLDER);
  80. }
  81. /**
  82. * 生成发送指令基本方法。
  83. *
  84. * @param simNum
  85. * @param orn
  86. * @param cmd
  87. * @param cmdId
  88. * @param data
  89. * @return
  90. */
  91. public SimMsg buildSendMsg(final String simNum, final String orn, final String cmd, final String cmdId, final String data) {
  92. SimMsg smS = new SimMsg();
  93. smS.setSimMsgId(idService.nextId());
  94. if (StringUtils.isEmpty(simNum) || StringUtils.isEmpty(cmd) || StringUtils.isEmpty(cmdId) || StringUtils.isEmpty(data)) {
  95. throw new IllegalArgumentException("buildSendMsg error 01");
  96. }
  97. if (simNum.length() != LENGTH_2) {
  98. throw new IllegalArgumentException("buildSendMsg error 02");
  99. }
  100. if (cmd.length() != LENGTH_2) {
  101. throw new IllegalArgumentException("buildSendMsg error 03");
  102. }
  103. if (cmdId.length() != LENGTH_2) {
  104. throw new IllegalArgumentException("buildSendMsg error 04");
  105. }
  106. if (data.length() != LENGTH_8) {
  107. throw new IllegalArgumentException("buildSendMsg error 05");
  108. }
  109. StringBuffer m = new StringBuffer();
  110. m.append(PREFIX);
  111. m.append(simNum);
  112. m.append(orn);
  113. m.append(cmd);
  114. m.append(cmdId);
  115. m.append(data);
  116. // 追加CRC校验
  117. if (CRC_16_MODBUS) {
  118. String crcContent = m.toString();
  119. byte[] sendByteContent = CommSendService.hexStrToByteArrs(crcContent);
  120. // l.info("sendByteContent = {}", CommSendService.bytesToHexV2(sendByteContent));
  121. byte[] sendByteCrc = CRC16Modbus.calculateCRC(sendByteContent);
  122. // l.info("sendByteCrc = {}", CommSendService.bytesToHexV2(sendByteCrc));
  123. String crc = CommSendService.bytesToHexV2(sendByteCrc);
  124. // l.debug("crc: {}", crc.toUpperCase());
  125. m.append(crc.toUpperCase());
  126. }
  127. m.append(SUFFIX);
  128. final String mFinal = m.toString();
  129. if (mFinal.length() != LENGTH_24) {
  130. throw new IllegalArgumentException("buildSendMsg error 06");
  131. }
  132. smS.setSendMsg(mFinal);
  133. return smS;
  134. }
  135. public AjaxResult buildSendMsgAR(final String simNum, final String orn, final String cmd, final String cmdId, final String data) {
  136. final SimMsg sm = buildSendMsg(simNum, orn, cmd, cmdId, data);
  137. return AjaxResult.success(sm);
  138. }
  139. }