/// <summary> /// 向客户端发送应答 /// </summary> /// <param name="context"></param> /// <param name="resp"></param> private void DoResponse(IChannelHandlerContext context, PlatformResp resp) { // 构建消息体属性对象 JT808MessageBodyAttr attr = new JT808MessageBodyAttr(); attr.EncryptionType = 0; byte[] body = resp.Encode(); attr.MsgBodyLength = body.Length; attr.IsSplit = false; JT808MessageHead head = new JT808MessageHead(); head.MessageId = resp.GetMessageId(); head.MessageSerial = GetFlowId(context.Channel); head.TerminalId = resp.TerminalId; head.MsgBodyAttr = attr; // 构建JT/T808协议消息对象 JT808Message message = new JT808Message(); message.MsgHeader = head; message.MsgBody = body; byte[] reply = message.Encode(); context.WriteAndFlushAsync(Unpooled.CopiedBuffer(reply)); //ResponseData response = new ResponseData(); //response.CmdID = message.MsgHeader.MessageId; //response.CmdSerialNo = message.MsgHeader.MessageSerial; //response.MsgBody = reply; //response.TerminalID = resp.TerminalId; //response.Time = response.GetTimeStamp(); //context.WriteAndFlushAsync(response); }
public void processMessageData(JT808Message req) { JT808MessageHead header = req.MsgHeader; // 1. 终端心跳-消息体为空 ==> 平台通用应答 if (header.MessageId == JT808Constant.MSG_ID_TERMINAL_HEART_BEAT) { logger.Info("<<<<<[终端心跳],phone={0},flowid={1}", header.TerminalId, header.MessageSerial); ProcessCommonResponse(req); }//2. 终端注册 ==> 终端注册应答 else if (header.MessageId == JT808Constant.MSG_ID_TERMINAL_REGISTER) { ProcessTerminalRegist(req); logger.Info(">>>>>[终端注册],终端ID={0},流水号={1}", header.TerminalId, header.MessageSerial); }//3.终端鉴权 ==> 平台通用应答 else if (header.MessageId == JT808Constant.MSG_ID_TERMINAL_AUTHENTICATION) { logger.Info(">>>>>[终端鉴权],终端ID={0},流水号={1}", header.TerminalId, header.MessageSerial); ProcessCommonResponse(req); }//4.终端注销(终端注销数据消息体为空) ==> 平台通用应答 else if (header.MessageId == JT808Constant.MSG_ID_TERMINAL_LOG_OUT) { logger.Info(">>>>>[终端注销],终端ID={0},流水号={1}", header.TerminalId, header.MessageSerial); }//5.位置信息汇报 ==> 平台通用应答 else if (header.MessageId == JT808Constant.MSG_ID_TERMINAL_LOCATION_INFO_UPLOAD) { ProcessLocationInfo(req); logger.Info(">>>>>[位置信息汇报],终端ID={0},流水号={1}", header.TerminalId, header.MessageSerial); }// 其他情况 else { logger.Info(">>>>>[其他情况],终端ID={0},流水号={1},消息ID={2}", header.TerminalId, header.MessageSerial, header.MessageId); } }
/// <summary> /// 平台通用应答 /// </summary> /// <param name="req"></param> private void ProcessCommonResponse(JT808Message req) { try { //logger.Debug("平台通用应答信息:{0}", JsonConvert.SerializeObject(req)); JT808MessageHead reqHeader = req.MsgHeader; CommonPlatformResp respMsgBody = new CommonPlatformResp(); CommonPlatformResp resp = new CommonPlatformResp(); resp.TerminalId = req.MsgHeader.TerminalId; resp.ReplySerial = req.MsgHeader.MessageSerial; resp.ReplyId = req.MsgHeader.MessageId; resp.ReplyCode = MessageResult.Success; DoResponse(req.channel, resp); } catch (Exception e) { logger.Error(e, "<<<<<[平台通用应答信息]处理错误>>>>>,{0}", e.Message); } }
/// <summary> /// 解码字节数组为JT808Message /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static JT808Message Decode(byte[] bytes) { // Encoding.GetEncoding("GBK").GetBytes(value); ByteBuffer buffer = ByteBuffer.Wrap(bytes); byte headFlag = buffer.Get(); buffer.Position = buffer.Capacity - 1; byte tailFlag = buffer.Get(); // --> Check head flag 检查消息头/尾 if (headFlag != FLAG_BYTE) { throw new Exception( "Parameter: buffer head flag byte is not " + FLAG_BYTE); } if (tailFlag != FLAG_BYTE) { throw new Exception( "Parameter: buffer tail flag byte is not " + FLAG_BYTE); } buffer.Position = 1; buffer.Limit = buffer.Capacity - 1; byte[] dataWithoutFlag = new byte[buffer.Capacity - 2]; buffer.Read(dataWithoutFlag); // unescape - 反转义 byte[] dataAfterUnescape = Unescape(dataWithoutFlag); // Validate checkCode - 验证校验码 byte checkCode = dataAfterUnescape[dataAfterUnescape.Length - 1]; byte[] dataToCheck = new byte[dataAfterUnescape.Length - 1]; Array.Copy(dataAfterUnescape, 0, dataToCheck, 0, dataAfterUnescape.Length - 1); byte expectCheckCode = CheckCode(dataToCheck); if (checkCode != expectCheckCode) { throw new Exception( "Parameter: buffer check failed, expect-check-code=" + expectCheckCode + ", actually-check-code=" + checkCode); } ByteBuffer dataAfterUnescapeBuffer = ByteBuffer.Wrap(dataAfterUnescape); JT808Message message = new JT808Message(); // Decode head ----------------------------------- Begin // 开始头部解码 JT808MessageHead head = new JT808MessageHead(); // Message ID - 消息ID head.MessageId = dataAfterUnescapeBuffer.GetShort(); // Message body attributes - 消息体属性 head.MsgBodyAttr = Decode(dataAfterUnescapeBuffer.GetShort()); // Terminal SimNo - 终端手机号 byte[] mobileBytes = new byte[6]; dataAfterUnescapeBuffer.Read(mobileBytes); head.TerminalId = Bcd6_2mobile(mobileBytes); head.MessageSerial = dataAfterUnescapeBuffer.GetShort(); if (head.MsgBodyAttr.IsSplit) { MessageSplitInfo splitInfo = new MessageSplitInfo(); splitInfo.Packages = dataAfterUnescapeBuffer.GetShort(); splitInfo.PackageNo = dataAfterUnescapeBuffer.GetShort(); head.SplitInfo = splitInfo; } message.MsgHeader = head; // Decode head ------------------------------------ End // 解码头部结束 // Message body // 消息体 int bodyLength = head.MsgBodyAttr.MsgBodyLength; byte[] body = new byte[bodyLength]; dataAfterUnescapeBuffer.Read(body); message.MsgBody = body; // Check Code - 校验码 message.CheckCode = checkCode; return(message); }