123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package com.tidecloud.dataacceptance.codec;
- import java.util.Arrays;
- import javax.xml.bind.DatatypeConverter;
- import com.tidecloud.dataacceptance.common.BitOperator;
- import com.tidecloud.dataacceptance.common.Constants;
- import com.tidecloud.dataacceptance.common.JT808ProtocolUtils;
- import com.tidecloud.dataacceptance.entity.LocationSelfInfoUploadMsg;
- import com.tidecloud.dataacceptance.entity.PackageData;
- import com.tidecloud.dataacceptance.entity.ServerCommonRespMsgBody;
- import com.tidecloud.dataacceptance.entity.Session;
- import com.tidecloud.dataacceptance.entity.TerminalRegisterMsg;
- import com.tidecloud.dataacceptance.entity.TerminalRegisterMsgRespBody;
- /**
- * @author: chudk
- * @date: 2017年11月8日 下午4:09:23
- */
- public class MsgEncoder {
- private BitOperator bitOperator;
- private JT808ProtocolUtils jt808ProtocolUtils;
- public MsgEncoder() {
- this.bitOperator = new BitOperator();
- this.jt808ProtocolUtils = new JT808ProtocolUtils();
- }
- public byte[] encode4TerminalRegisterResp(TerminalRegisterMsg req, TerminalRegisterMsgRespBody respMsgBody,
- int flowId) throws Exception {
- // 消息体字节数组
- byte[] msgBody = null;
- // 鉴权码(STRING) 只有在成功后才有该字段
- if (respMsgBody.getReplyCode() == TerminalRegisterMsgRespBody.success) {
-
- msgBody = this.bitOperator.concatAll(Arrays.asList(//
- bitOperator.integerTo2Bytes(respMsgBody.getReplyFlowId()), // 流水号(2)
- new byte[] { respMsgBody.getReplyCode() }, // 结果
- respMsgBody.getReplyToken().getBytes(Constants.GBK_CHARSET)// 鉴权码(STRING)
- ));
- } else {
- msgBody = this.bitOperator.concatAll(Arrays.asList(//
- bitOperator.integerTo2Bytes(respMsgBody.getReplyFlowId()), // 流水号(2)
- new byte[] { respMsgBody.getReplyCode() }// 错误代码
- ));
- }
- // 消息头
- int msgBodyProps = this.jt808ProtocolUtils.generateMsgBodyProps(msgBody.length, 0b000, false, 0);
- byte[] msgHeader = this.jt808ProtocolUtils.generateMsgHeader(req.getMsgHeader().getTerminalPhone(),
- Constants.SERVER_REGISTER_RESP_ID, msgBody, msgBodyProps, flowId);
-
- byte[] headerAndBody = this.bitOperator.concatAll(msgHeader, msgBody);
-
- // 校验码
- int checkSum = this.bitOperator.getCheckSum4JT808(headerAndBody, 0, headerAndBody.length);
- // 连接并且转义
- return this.doEncode(headerAndBody, checkSum);
- }
- // public byte[] encode4ServerCommonRespMsg(TerminalAuthenticationMsg req,
- // ServerCommonRespMsgBody respMsgBody, int flowId) throws Exception {
- public byte[] encode4ServerCommonRespMsg(PackageData req, ServerCommonRespMsgBody respMsgBody, int flowId)
- throws Exception {
- byte[] msgBody = this.bitOperator.concatAll(Arrays.asList(//
- bitOperator.integerTo2Bytes(respMsgBody.getReplyFlowId()), // 应答流水号
- bitOperator.integerTo2Bytes(respMsgBody.getReplyId()), // 应答ID,对应的终端消息的ID
- new byte[] { respMsgBody.getReplyCode() }// 结果
- ));
- // 消息头
- int msgBodyProps = this.jt808ProtocolUtils.generateMsgBodyProps(msgBody.length, 0b000, false, 0);
- byte[] msgHeader = this.jt808ProtocolUtils.generateMsgHeader(req.getMsgHeader().getTerminalPhone(),
- Constants.SERVER_COMMON_RESP_ID, msgBody, msgBodyProps, flowId);
- byte[] headerAndBody = this.bitOperator.concatAll(msgHeader, msgBody);
- // 校验码
- int checkSum = this.bitOperator.getCheckSum4JT808(headerAndBody, 0, headerAndBody.length);
- // 连接并且转义
- return this.doEncode(headerAndBody, checkSum);
- }
-
- // public byte[] encode4ServerCommonRespMsg(TerminalAuthenticationMsg req,
- // ServerCommonRespMsgBody respMsgBody, int flowId) throws Exception {
- public byte[] encode4ServerRespMsg(LocationSelfInfoUploadMsg req, ServerCommonRespMsgBody respMsgBody, int flowId)
- throws Exception {
- byte[] msgBody = BitOperator.concatAll(Arrays.asList(//
- new byte[]{(byte)0xC8, (byte)0xFF, (byte)0xD0},
- bitOperator.integerTo2Bytes(flowId), // 应答流水号
- new byte[]{(byte)0xff, 0x10}, // 应答ID,对应的终端消息的ID
- new byte[] { respMsgBody.getReplyCode() }// 结果
- ));
- // 消息头
- int msgBodyProps = this.jt808ProtocolUtils.generateMsgBodyProps(msgBody.length, 0b000, false, 0);
- byte[] msgHeader = this.jt808ProtocolUtils.generateMsgHeader(req.getMsgHeader().getTerminalPhone(),
- Constants.SERVER_8900_RESP_ID, msgBody, msgBodyProps, flowId);
- byte[] headerAndBody = BitOperator.concatAll(msgHeader, msgBody);
- // 校验码
- int checkSum = BitOperator.getCheckSum4JT808(headerAndBody, 0, headerAndBody.length);
- // 连接并且转义
- return this.doEncode(headerAndBody, checkSum);
- }
- public byte[] encode4ParamSetting(byte[] msgBodyBytes, Session session) throws Exception {
- // 消息头
- int msgBodyProps = this.jt808ProtocolUtils.generateMsgBodyProps(msgBodyBytes.length, 0b000, false, 0);
- byte[] msgHeader = this.jt808ProtocolUtils.generateMsgHeader(session.getDeviceId(),
- Constants.SERVER_SET_PARAM_ID, msgBodyBytes, msgBodyProps, session.currentFlowId());
- // 连接消息头和消息体
- byte[] headerAndBody = BitOperator.concatAll(msgHeader, msgBodyBytes);
- // 校验码
- int checkSum = BitOperator.getCheckSum4JT808(headerAndBody, 0, headerAndBody.length);
- // 连接并且转义
- return this.doEncode(headerAndBody, checkSum);
- }
-
- public static void main(String[] args) {
- MsgEncoder bitOperator = new MsgEncoder();
-
- byte[] integerTo2Bytes = bitOperator.bitOperator.integerTo2Bytes(65488);
-
- String printHexBinary = DatatypeConverter.printHexBinary(integerTo2Bytes);
-
- System.out.println(printHexBinary);
-
- }
-
- private byte[] doEncode(byte[] headerAndBody, int checkSum) throws Exception {
- byte[] noEscapedBytes = this.bitOperator.concatAll(Arrays.asList(//
- new byte[] { Constants.PKG_DELIMITER }, // 0x7e
- headerAndBody, // 消息头+ 消息体
- bitOperator.integerTo1Bytes(checkSum), // 校验码
- new byte[] { Constants.PKG_DELIMITER }// 0x7e
- ));
- // 转义
- return jt808ProtocolUtils.doEscape4Send(noEscapedBytes, 1, noEscapedBytes.length - 2);
- }
- }
|