MsgEncoder.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.tidecloud.dataacceptance.codec;
  2. import java.util.Arrays;
  3. import javax.xml.bind.DatatypeConverter;
  4. import com.tidecloud.dataacceptance.common.BitOperator;
  5. import com.tidecloud.dataacceptance.common.Constants;
  6. import com.tidecloud.dataacceptance.common.JT808ProtocolUtils;
  7. import com.tidecloud.dataacceptance.entity.LocationSelfInfoUploadMsg;
  8. import com.tidecloud.dataacceptance.entity.PackageData;
  9. import com.tidecloud.dataacceptance.entity.ServerCommonRespMsgBody;
  10. import com.tidecloud.dataacceptance.entity.Session;
  11. import com.tidecloud.dataacceptance.entity.TerminalRegisterMsg;
  12. import com.tidecloud.dataacceptance.entity.TerminalRegisterMsgRespBody;
  13. /**
  14. * @author: chudk
  15. * @date: 2017年11月8日 下午4:09:23
  16. */
  17. public class MsgEncoder {
  18. private BitOperator bitOperator;
  19. private JT808ProtocolUtils jt808ProtocolUtils;
  20. public MsgEncoder() {
  21. this.bitOperator = new BitOperator();
  22. this.jt808ProtocolUtils = new JT808ProtocolUtils();
  23. }
  24. public byte[] encode4TerminalRegisterResp(TerminalRegisterMsg req, TerminalRegisterMsgRespBody respMsgBody,
  25. int flowId) throws Exception {
  26. // 消息体字节数组
  27. byte[] msgBody = null;
  28. // 鉴权码(STRING) 只有在成功后才有该字段
  29. if (respMsgBody.getReplyCode() == TerminalRegisterMsgRespBody.success) {
  30. msgBody = this.bitOperator.concatAll(Arrays.asList(//
  31. bitOperator.integerTo2Bytes(respMsgBody.getReplyFlowId()), // 流水号(2)
  32. new byte[] { respMsgBody.getReplyCode() }, // 结果
  33. respMsgBody.getReplyToken().getBytes(Constants.GBK_CHARSET)// 鉴权码(STRING)
  34. ));
  35. } else {
  36. msgBody = this.bitOperator.concatAll(Arrays.asList(//
  37. bitOperator.integerTo2Bytes(respMsgBody.getReplyFlowId()), // 流水号(2)
  38. new byte[] { respMsgBody.getReplyCode() }// 错误代码
  39. ));
  40. }
  41. // 消息头
  42. int msgBodyProps = this.jt808ProtocolUtils.generateMsgBodyProps(msgBody.length, 0b000, false, 0);
  43. byte[] msgHeader = this.jt808ProtocolUtils.generateMsgHeader(req.getMsgHeader().getTerminalPhone(),
  44. Constants.SERVER_REGISTER_RESP_ID, msgBody, msgBodyProps, flowId);
  45. byte[] headerAndBody = this.bitOperator.concatAll(msgHeader, msgBody);
  46. // 校验码
  47. int checkSum = this.bitOperator.getCheckSum4JT808(headerAndBody, 0, headerAndBody.length);
  48. // 连接并且转义
  49. return this.doEncode(headerAndBody, checkSum);
  50. }
  51. // public byte[] encode4ServerCommonRespMsg(TerminalAuthenticationMsg req,
  52. // ServerCommonRespMsgBody respMsgBody, int flowId) throws Exception {
  53. public byte[] encode4ServerCommonRespMsg(PackageData req, ServerCommonRespMsgBody respMsgBody, int flowId)
  54. throws Exception {
  55. byte[] msgBody = this.bitOperator.concatAll(Arrays.asList(//
  56. bitOperator.integerTo2Bytes(respMsgBody.getReplyFlowId()), // 应答流水号
  57. bitOperator.integerTo2Bytes(respMsgBody.getReplyId()), // 应答ID,对应的终端消息的ID
  58. new byte[] { respMsgBody.getReplyCode() }// 结果
  59. ));
  60. // 消息头
  61. int msgBodyProps = this.jt808ProtocolUtils.generateMsgBodyProps(msgBody.length, 0b000, false, 0);
  62. byte[] msgHeader = this.jt808ProtocolUtils.generateMsgHeader(req.getMsgHeader().getTerminalPhone(),
  63. Constants.SERVER_COMMON_RESP_ID, msgBody, msgBodyProps, flowId);
  64. byte[] headerAndBody = this.bitOperator.concatAll(msgHeader, msgBody);
  65. // 校验码
  66. int checkSum = this.bitOperator.getCheckSum4JT808(headerAndBody, 0, headerAndBody.length);
  67. // 连接并且转义
  68. return this.doEncode(headerAndBody, checkSum);
  69. }
  70. // public byte[] encode4ServerCommonRespMsg(TerminalAuthenticationMsg req,
  71. // ServerCommonRespMsgBody respMsgBody, int flowId) throws Exception {
  72. public byte[] encode4ServerRespMsg(LocationSelfInfoUploadMsg req, ServerCommonRespMsgBody respMsgBody, int flowId)
  73. throws Exception {
  74. byte[] msgBody = BitOperator.concatAll(Arrays.asList(//
  75. new byte[]{(byte)0xC8, (byte)0xFF, (byte)0xD0},
  76. bitOperator.integerTo2Bytes(flowId), // 应答流水号
  77. new byte[]{(byte)0xff, 0x10}, // 应答ID,对应的终端消息的ID
  78. new byte[] { respMsgBody.getReplyCode() }// 结果
  79. ));
  80. // 消息头
  81. int msgBodyProps = this.jt808ProtocolUtils.generateMsgBodyProps(msgBody.length, 0b000, false, 0);
  82. byte[] msgHeader = this.jt808ProtocolUtils.generateMsgHeader(req.getMsgHeader().getTerminalPhone(),
  83. Constants.SERVER_8900_RESP_ID, msgBody, msgBodyProps, flowId);
  84. byte[] headerAndBody = BitOperator.concatAll(msgHeader, msgBody);
  85. // 校验码
  86. int checkSum = BitOperator.getCheckSum4JT808(headerAndBody, 0, headerAndBody.length);
  87. // 连接并且转义
  88. return this.doEncode(headerAndBody, checkSum);
  89. }
  90. public byte[] encode4ParamSetting(byte[] msgBodyBytes, Session session) throws Exception {
  91. // 消息头
  92. int msgBodyProps = this.jt808ProtocolUtils.generateMsgBodyProps(msgBodyBytes.length, 0b000, false, 0);
  93. byte[] msgHeader = this.jt808ProtocolUtils.generateMsgHeader(session.getDeviceId(),
  94. Constants.SERVER_SET_PARAM_ID, msgBodyBytes, msgBodyProps, session.currentFlowId());
  95. // 连接消息头和消息体
  96. byte[] headerAndBody = BitOperator.concatAll(msgHeader, msgBodyBytes);
  97. // 校验码
  98. int checkSum = BitOperator.getCheckSum4JT808(headerAndBody, 0, headerAndBody.length);
  99. // 连接并且转义
  100. return this.doEncode(headerAndBody, checkSum);
  101. }
  102. public static void main(String[] args) {
  103. MsgEncoder bitOperator = new MsgEncoder();
  104. byte[] integerTo2Bytes = bitOperator.bitOperator.integerTo2Bytes(65488);
  105. String printHexBinary = DatatypeConverter.printHexBinary(integerTo2Bytes);
  106. System.out.println(printHexBinary);
  107. }
  108. private byte[] doEncode(byte[] headerAndBody, int checkSum) throws Exception {
  109. byte[] noEscapedBytes = this.bitOperator.concatAll(Arrays.asList(//
  110. new byte[] { Constants.PKG_DELIMITER }, // 0x7e
  111. headerAndBody, // 消息头+ 消息体
  112. bitOperator.integerTo1Bytes(checkSum), // 校验码
  113. new byte[] { Constants.PKG_DELIMITER }// 0x7e
  114. ));
  115. // 转义
  116. return jt808ProtocolUtils.doEscape4Send(noEscapedBytes, 1, noEscapedBytes.length - 2);
  117. }
  118. }