@@ -1,5 +1,6 @@ | |||||
using JT808.Protocol.Extensions; | using JT808.Protocol.Extensions; | ||||
using JT808.Protocol.MessageBody; | using JT808.Protocol.MessageBody; | ||||
using System.Collections.Generic; | |||||
using Xunit; | using Xunit; | ||||
namespace JT808.Protocol.Test.MessageBody | namespace JT808.Protocol.Test.MessageBody | ||||
@@ -12,34 +13,51 @@ namespace JT808.Protocol.Test.MessageBody | |||||
{ | { | ||||
JT808_0x8302 jT808_0X8302 = new JT808_0x8302 | JT808_0x8302 jT808_0X8302 = new JT808_0x8302 | ||||
{ | { | ||||
AnswerId = 128, | Answers=new List<JT808_0x8302.Answer>() | ||||
AnswerContent = "123456", | { | ||||
new JT808_0x8302.Answer() | |||||
{ | |||||
Id = 128, | |||||
Content = "123456", | |||||
}, | |||||
new JT808_0x8302.Answer() | |||||
{ | |||||
Id = 127, | |||||
Content = "123457", | |||||
} | |||||
}, | |||||
Flag = 1, | Flag = 1, | ||||
Issue = "sdddaff" | Issue = "sdddaff" | ||||
}; | }; | ||||
var hex = JT808Serializer.Serialize(jT808_0X8302).ToHexString(); | var hex = JT808Serializer.Serialize(jT808_0X8302).ToHexString(); | ||||
//01 | //01 | ||||
//07 | //07 | ||||
//73 64 64 64 61 66 66 | //73 64 64 64 61 66 66 | ||||
//80 | //80 | ||||
//06 00 | //00 06 | ||||
//31 32 33 34 35 36 | //31 32 33 34 35 36 | ||||
//010006646464616666800000313233343536 | //7F | ||||
//010773646464616666800006313233343536 | //00 06 | ||||
Assert.Equal("010773646464616666800006313233343536", hex); | //31 32 33 34 35 37 | ||||
Assert.Equal("0107736464646166668000063132333435367F0006313233343537", hex); | |||||
} | } | ||||
[Fact] | [Fact] | ||||
public void Test1_1() | public void Test1_1() | ||||
{ | { | ||||
byte[] bytes = "010773646464616666800006313233343536".ToHexBytes(); | byte[] bytes = "0107736464646166668000063132333435367F0006313233343537".ToHexBytes(); | ||||
JT808_0x8302 jT808_0X8302 = JT808Serializer.Deserialize<JT808_0x8302>(bytes); | 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(1, jT808_0X8302.Flag); | ||||
Assert.Equal("sdddaff", jT808_0X8302.Issue); | |||||
Assert.Equal(6, jT808_0X8302.AnswerContentLength); | |||||
Assert.Equal(7, jT808_0X8302.IssueContentLength); | 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 JT808.Protocol.Interfaces; | ||||
using System; | using System; | ||||
using JT808.Protocol.MessagePack; | using JT808.Protocol.MessagePack; | ||||
using System.Collections.Generic; | |||||
namespace JT808.Protocol.Formatters.MessageBodyFormatters | namespace JT808.Protocol.Formatters.MessageBodyFormatters | ||||
{ | { | ||||
@@ -14,9 +15,22 @@ namespace JT808.Protocol.Formatters.MessageBodyFormatters | |||||
jT808_0X8302.Flag = reader.ReadByte(); | jT808_0X8302.Flag = reader.ReadByte(); | ||||
jT808_0X8302.IssueContentLength = reader.ReadByte(); | jT808_0X8302.IssueContentLength = reader.ReadByte(); | ||||
jT808_0X8302.Issue = reader.ReadString(jT808_0X8302.IssueContentLength); | jT808_0X8302.Issue = reader.ReadString(jT808_0X8302.IssueContentLength); | ||||
jT808_0X8302.AnswerId = reader.ReadByte(); | jT808_0X8302.Answers = new List<JT808_0x8302.Answer>(); | ||||
jT808_0X8302.AnswerContentLength = reader.ReadUInt16(); | while (reader.ReadCurrentRemainContentLength() > 0) | ||||
jT808_0X8302.AnswerContent = reader.ReadString(jT808_0X8302.AnswerContentLength); | { | ||||
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; | return jT808_0X8302; | ||||
} | } | ||||
@@ -28,11 +42,17 @@ namespace JT808.Protocol.Formatters.MessageBodyFormatters | |||||
writer.WriteString(value.Issue); | writer.WriteString(value.Issue); | ||||
ushort issueLength = (ushort)(writer.GetCurrentPosition() - issuePosition - 1); | ushort issueLength = (ushort)(writer.GetCurrentPosition() - issuePosition - 1); | ||||
writer.WriteByteReturn((byte)issueLength, issuePosition); | writer.WriteByteReturn((byte)issueLength, issuePosition); | ||||
writer.WriteByte(value.AnswerId); | if(value.Answers!=null && value.Answers.Count > 0) | ||||
writer.Skip(2, out int answerPosition); | { | ||||
writer.WriteString(value.AnswerContent); | foreach(var item in value.Answers) | ||||
ushort answerLength = (ushort)(writer.GetCurrentPosition() - answerPosition - 2); | { | ||||
writer.WriteUInt16Return(answerLength, answerPosition); | 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 | 问题文本,经 GBK 编码,长度为 N | ||||
</summary> | </summary> | ||||
</member> | </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> | <summary> | ||||
答案 ID | 答案 ID | ||||
</summary> | </summary> | ||||
</member> | </member> | ||||
<member name="P:JT808.Protocol.MessageBody.JT808_0x8302.AnswerContentLength"> | <member name="P:JT808.Protocol.MessageBody.JT808_0x8302.Answer.ContentLength"> | ||||
<summary> | <summary> | ||||
答案内容长度 | 答案内容长度 | ||||
答案内容字段字节长度 | 答案内容字段字节长度 | ||||
</summary> | </summary> | ||||
</member> | </member> | ||||
<member name="P:JT808.Protocol.MessageBody.JT808_0x8302.AnswerContent"> | <member name="P:JT808.Protocol.MessageBody.JT808_0x8302.Answer.Content"> | ||||
<summary> | <summary> | ||||
答案内容 | 答案内容 | ||||
答案内容,经 GBK 编码 | 答案内容,经 GBK 编码 | ||||
@@ -1,5 +1,6 @@ | |||||
using JT808.Protocol.Attributes; | using JT808.Protocol.Attributes; | ||||
using JT808.Protocol.Formatters.MessageBodyFormatters; | using JT808.Protocol.Formatters.MessageBodyFormatters; | ||||
using System.Collections.Generic; | |||||
namespace JT808.Protocol.MessageBody | namespace JT808.Protocol.MessageBody | ||||
{ | { | ||||
@@ -25,18 +26,26 @@ namespace JT808.Protocol.MessageBody | |||||
/// </summary> | /// </summary> | ||||
public string Issue { get; set; } | public string Issue { get; set; } | ||||
/// <summary> | /// <summary> | ||||
/// 答案 ID | /// 候选答案列表 | ||||
/// </summary> | /// </summary> | ||||
public byte AnswerId { get; set; } | public List<Answer> Answers { get; set; } | ||||
/// <summary> | public class Answer | ||||
/// 答案内容长度 | { | ||||
/// 答案内容字段字节长度 | /// <summary> | ||||
/// </summary> | /// 答案 ID | ||||
public ushort AnswerContentLength { get; set; } | /// </summary> | ||||
/// <summary> | public byte Id { get; set; } | ||||
/// 答案内容 | /// <summary> | ||||
/// 答案内容,经 GBK 编码 | /// 答案内容长度 | ||||
/// </summary> | /// 答案内容字段字节长度 | ||||
public string AnswerContent { get; set; } | /// </summary> | ||||
public ushort ContentLength { get; set; } | |||||
/// <summary> | |||||
/// 答案内容 | |||||
/// 答案内容,经 GBK 编码 | |||||
/// </summary> | |||||
public string Content { get; set; } | |||||
} | |||||
} | } | ||||
} | } |