From 795662fdbdd19fceb158330017e80c2a682c888c Mon Sep 17 00:00:00 2001 From: smallchi <564952747@qq.com> Date: Fri, 25 Oct 2019 10:12:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D0x8302=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MessageBody/JT808_0x8302Test.cs | 44 +++++++++++++------ .../JT808_0x8302_Formatter.cs | 36 +++++++++++---- src/JT808.Protocol/JT808.Protocol.xml | 11 +++-- .../MessageBody/JT808_0x8302.cs | 33 +++++++++----- 4 files changed, 88 insertions(+), 36 deletions(-) diff --git a/src/JT808.Protocol.Test/MessageBody/JT808_0x8302Test.cs b/src/JT808.Protocol.Test/MessageBody/JT808_0x8302Test.cs index e9f4990..73bb36f 100644 --- a/src/JT808.Protocol.Test/MessageBody/JT808_0x8302Test.cs +++ b/src/JT808.Protocol.Test/MessageBody/JT808_0x8302Test.cs @@ -1,5 +1,6 @@ using JT808.Protocol.Extensions; using JT808.Protocol.MessageBody; +using System.Collections.Generic; using Xunit; namespace JT808.Protocol.Test.MessageBody @@ -12,34 +13,51 @@ namespace JT808.Protocol.Test.MessageBody { JT808_0x8302 jT808_0X8302 = new JT808_0x8302 { - AnswerId = 128, - AnswerContent = "123456", + Answers=new List() + { + new JT808_0x8302.Answer() + { + Id = 128, + Content = "123456", + }, + new JT808_0x8302.Answer() + { + Id = 127, + Content = "123457", + } + }, Flag = 1, Issue = "sdddaff" }; var hex = JT808Serializer.Serialize(jT808_0X8302).ToHexString(); //01 //07 - //73 64 64 64 61 66 66 + //73 64 64 64 61 66 66 //80 - //06 00 - //31 32 33 34 35 36 - //010006646464616666800000313233343536 - //010773646464616666800006313233343536 - Assert.Equal("010773646464616666800006313233343536", hex); + //00 06 + //31 32 33 34 35 36 + //7F + //00 06 + //31 32 33 34 35 37 + Assert.Equal("0107736464646166668000063132333435367F0006313233343537", hex); } [Fact] public void Test1_1() { - byte[] bytes = "010773646464616666800006313233343536".ToHexBytes(); + byte[] bytes = "0107736464646166668000063132333435367F0006313233343537".ToHexBytes(); JT808_0x8302 jT808_0X8302 = JT808Serializer.Deserialize(bytes); - Assert.Equal(128, jT808_0X8302.AnswerId); - Assert.Equal("123456", jT808_0X8302.AnswerContent); Assert.Equal(1, jT808_0X8302.Flag); - Assert.Equal("sdddaff", jT808_0X8302.Issue); - Assert.Equal(6, jT808_0X8302.AnswerContentLength); Assert.Equal(7, jT808_0X8302.IssueContentLength); + Assert.Equal("sdddaff", jT808_0X8302.Issue); + + Assert.Equal(6, jT808_0X8302.Answers[0].ContentLength); + Assert.Equal(128, jT808_0X8302.Answers[0].Id); + Assert.Equal("123456", jT808_0X8302.Answers[0].Content); + + Assert.Equal(6, jT808_0X8302.Answers[1].ContentLength); + Assert.Equal(127, jT808_0X8302.Answers[1].Id); + Assert.Equal("123457", jT808_0X8302.Answers[1].Content); } } } diff --git a/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x8302_Formatter.cs b/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x8302_Formatter.cs index dead93e..df9320e 100644 --- a/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x8302_Formatter.cs +++ b/src/JT808.Protocol/Formatters/MessageBodyFormatters/JT808_0x8302_Formatter.cs @@ -3,6 +3,7 @@ using JT808.Protocol.MessageBody; using JT808.Protocol.Interfaces; using System; using JT808.Protocol.MessagePack; +using System.Collections.Generic; namespace JT808.Protocol.Formatters.MessageBodyFormatters { @@ -14,9 +15,22 @@ namespace JT808.Protocol.Formatters.MessageBodyFormatters jT808_0X8302.Flag = reader.ReadByte(); jT808_0X8302.IssueContentLength = reader.ReadByte(); jT808_0X8302.Issue = reader.ReadString(jT808_0X8302.IssueContentLength); - jT808_0X8302.AnswerId = reader.ReadByte(); - jT808_0X8302.AnswerContentLength = reader.ReadUInt16(); - jT808_0X8302.AnswerContent = reader.ReadString(jT808_0X8302.AnswerContentLength); + jT808_0X8302.Answers = new List(); + while (reader.ReadCurrentRemainContentLength() > 0) + { + try + { + JT808_0x8302.Answer answer = new JT808_0x8302.Answer(); + answer.Id = reader.ReadByte(); + answer.ContentLength = reader.ReadUInt16(); + answer.Content = reader.ReadString(answer.ContentLength); + jT808_0X8302.Answers.Add(answer); + } + catch (Exception ex) + { + break; + } + } return jT808_0X8302; } @@ -28,11 +42,17 @@ namespace JT808.Protocol.Formatters.MessageBodyFormatters writer.WriteString(value.Issue); ushort issueLength = (ushort)(writer.GetCurrentPosition() - issuePosition - 1); writer.WriteByteReturn((byte)issueLength, issuePosition); - writer.WriteByte(value.AnswerId); - writer.Skip(2, out int answerPosition); - writer.WriteString(value.AnswerContent); - ushort answerLength = (ushort)(writer.GetCurrentPosition() - answerPosition - 2); - writer.WriteUInt16Return(answerLength, answerPosition); + if(value.Answers!=null && value.Answers.Count > 0) + { + foreach(var item in value.Answers) + { + writer.WriteByte(item.Id); + writer.Skip(2, out int answerPosition); + writer.WriteString(item.Content); + ushort answerLength = (ushort)(writer.GetCurrentPosition() - answerPosition - 2); + writer.WriteUInt16Return(answerLength, answerPosition); + } + } } } } diff --git a/src/JT808.Protocol/JT808.Protocol.xml b/src/JT808.Protocol/JT808.Protocol.xml index 53911bc..3c67eb6 100644 --- a/src/JT808.Protocol/JT808.Protocol.xml +++ b/src/JT808.Protocol/JT808.Protocol.xml @@ -4837,18 +4837,23 @@ 问题文本,经 GBK 编码,长度为 N - + + + 候选答案列表 + + + 答案 ID - + 答案内容长度 答案内容字段字节长度 - + 答案内容 答案内容,经 GBK 编码 diff --git a/src/JT808.Protocol/MessageBody/JT808_0x8302.cs b/src/JT808.Protocol/MessageBody/JT808_0x8302.cs index f921f8f..acad82b 100644 --- a/src/JT808.Protocol/MessageBody/JT808_0x8302.cs +++ b/src/JT808.Protocol/MessageBody/JT808_0x8302.cs @@ -1,5 +1,6 @@ using JT808.Protocol.Attributes; using JT808.Protocol.Formatters.MessageBodyFormatters; +using System.Collections.Generic; namespace JT808.Protocol.MessageBody { @@ -25,18 +26,26 @@ namespace JT808.Protocol.MessageBody /// public string Issue { get; set; } /// - /// 答案 ID + /// 候选答案列表 /// - public byte AnswerId { get; set; } - /// - /// 答案内容长度 - /// 答案内容字段字节长度 - /// - public ushort AnswerContentLength { get; set; } - /// - /// 答案内容 - /// 答案内容,经 GBK 编码 - /// - public string AnswerContent { get; set; } + public List Answers { get; set; } + + public class Answer + { + /// + /// 答案 ID + /// + public byte Id { get; set; } + /// + /// 答案内容长度 + /// 答案内容字段字节长度 + /// + public ushort ContentLength { get; set; } + /// + /// 答案内容 + /// 答案内容,经 GBK 编码 + /// + public string Content { get; set; } + } } }