@@ -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<JT808_0x8302.Answer>() | |||
{ | |||
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<JT808_0x8302>(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); | |||
} | |||
} | |||
} |
@@ -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<JT808_0x8302.Answer>(); | |||
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); | |||
} | |||
} | |||
} | |||
} | |||
} |
@@ -4837,18 +4837,23 @@ | |||
问题文本,经 GBK 编码,长度为 N | |||
</summary> | |||
</member> | |||
<member name="P:JT808.Protocol.MessageBody.JT808_0x8302.AnswerId"> | |||
<member name="P:JT808.Protocol.MessageBody.JT808_0x8302.Answers"> | |||
<summary> | |||
候选答案列表 | |||
</summary> | |||
</member> | |||
<member name="P:JT808.Protocol.MessageBody.JT808_0x8302.Answer.Id"> | |||
<summary> | |||
答案 ID | |||
</summary> | |||
</member> | |||
<member name="P:JT808.Protocol.MessageBody.JT808_0x8302.AnswerContentLength"> | |||
<member name="P:JT808.Protocol.MessageBody.JT808_0x8302.Answer.ContentLength"> | |||
<summary> | |||
答案内容长度 | |||
答案内容字段字节长度 | |||
</summary> | |||
</member> | |||
<member name="P:JT808.Protocol.MessageBody.JT808_0x8302.AnswerContent"> | |||
<member name="P:JT808.Protocol.MessageBody.JT808_0x8302.Answer.Content"> | |||
<summary> | |||
答案内容 | |||
答案内容,经 GBK 编码 | |||
@@ -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 | |||
/// </summary> | |||
public string Issue { get; set; } | |||
/// <summary> | |||
/// 答案 ID | |||
/// 候选答案列表 | |||
/// </summary> | |||
public byte AnswerId { get; set; } | |||
/// <summary> | |||
/// 答案内容长度 | |||
/// 答案内容字段字节长度 | |||
/// </summary> | |||
public ushort AnswerContentLength { get; set; } | |||
/// <summary> | |||
/// 答案内容 | |||
/// 答案内容,经 GBK 编码 | |||
/// </summary> | |||
public string AnswerContent { get; set; } | |||
public List<Answer> Answers { get; set; } | |||
public class Answer | |||
{ | |||
/// <summary> | |||
/// 答案 ID | |||
/// </summary> | |||
public byte Id { get; set; } | |||
/// <summary> | |||
/// 答案内容长度 | |||
/// 答案内容字段字节长度 | |||
/// </summary> | |||
public ushort ContentLength { get; set; } | |||
/// <summary> | |||
/// 答案内容 | |||
/// 答案内容,经 GBK 编码 | |||
/// </summary> | |||
public string Content { get; set; } | |||
} | |||
} | |||
} |