From e82db098e616a08e9014fa8c0312d4e8079b1ee2 Mon Sep 17 00:00:00 2001 From: "smallchi(Koike)" <564952747@qq.com> Date: Wed, 22 Jan 2020 21:01:53 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B90200=E6=8A=A5=E8=AD=A6?= =?UTF-8?q?=E5=8F=8A=E7=8A=B6=E6=80=81=E4=BD=8D=E5=88=86=E6=9E=90=E5=99=A8?= =?UTF-8?q?=202.=E5=A2=9E=E5=8A=A0Demo8=E5=8F=8A=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=96=87=E6=A1=A3=203.=E4=BF=AE=E6=94=B9CI=E7=9A=84dotnet?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 2 +- README.md | 6 ++ .../MessageBody/JT808_0x0200Test.cs | 39 +++++++- src/JT808.Protocol.Test/Simples/Demo8.cs | 88 +++++++++++++++++++ .../MessageBody/JT808_0x0200.cs | 21 +++++ 5 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 src/JT808.Protocol.Test/Simples/Demo8.cs diff --git a/.travis.yml b/.travis.yml index 6540c65..cb35104 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: csharp solution: JT808.Protocol.sln -dotnet: 3.1.100 +dotnet: 3.1.101 os: linux mono: none dist: trusty2 diff --git a/README.md b/README.md index 39d24ce..f3163d9 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,12 @@ JT808Serializer DT2JT808Serializer = new JT808Serializer(DT2JT808Config); [可以参考Simples的Demo7](https://github.com/SmallChi/JT808/blob/master/src/JT808.Protocol.Test/Simples/Demo7.cs) +### 举个栗子8 + +协议分析器在数据出现异常和纠错的时候也是挺有用的,总不能凭借24K氪金眼去观察数据,那么可以在开发协议的同时就把协议分析器给写好,这样方便技术或者技术支持排查问题,提高效率。 + +[可以参考Simples的Demo8](https://github.com/SmallChi/JT808/blob/master/src/JT808.Protocol.Test/Simples/Demo8.cs) + ## NuGet安装 | Package Name | Version | Downloads | diff --git a/src/JT808.Protocol.Test/MessageBody/JT808_0x0200Test.cs b/src/JT808.Protocol.Test/MessageBody/JT808_0x0200Test.cs index d23ca2b..b04364a 100644 --- a/src/JT808.Protocol.Test/MessageBody/JT808_0x0200Test.cs +++ b/src/JT808.Protocol.Test/MessageBody/JT808_0x0200Test.cs @@ -251,7 +251,12 @@ namespace JT808.Protocol.Test.MessageBody jT808Package.Bodies = jT808UploadLocationRequest; var hex = JT808Serializer.Serialize(jT808Package).ToHexString(); } - + [Fact] + public void Test5_2() + { + byte[] bytes = "7E 02 00 00 26 11 22 33 44 55 66 22 B8 00 00 00 01 00 00 00 02 00 BA 7F 0E 07 E4 F1 1C 00 28 00 3C 00 00 18 07 15 10 10 10 01 04 00 00 00 64 02 02 00 37 57 7E".ToHexBytes(); + string json = JT808Serializer.Analyze(bytes); + } [Fact] public void Test5() { @@ -299,6 +304,38 @@ namespace JT808.Protocol.Test.MessageBody Assert.Equal(100, ((JT808_0x0200_0x01)jT808UploadLocationRequest.JT808LocationAttachData[JT808Constants.JT808_0x0200_0x01]).Mileage); Assert.Equal(55, ((JT808_0x0200_0x02)jT808UploadLocationRequest.JT808LocationAttachData[JT808Constants.JT808_0x0200_0x02]).Oil); } + [Fact] + public void Test6() + { + JT808_0x0200 jT808UploadLocationRequest = new JT808_0x0200 + { + AlarmFlag = 300000, + Altitude = 40, + GPSTime = DateTime.Parse("2018-07-15 10:10:10"), + Lat = 12222222, + Lng = 132444444, + Speed = 60, + Direction = 0, + StatusFlag = 1002222, + JT808LocationAttachData = new Dictionary() + }; + jT808UploadLocationRequest.JT808LocationAttachData.Add(JT808Constants.JT808_0x0200_0x01, new JT808_0x0200_0x01 + { + Mileage = 100 + }); + jT808UploadLocationRequest.JT808LocationAttachData.Add(JT808Constants.JT808_0x0200_0x02, new JT808_0x0200_0x02 + { + Oil = 55 + }); + var hex = JT808Serializer.Serialize(jT808UploadLocationRequest).ToHexString(); + Assert.Equal("000493E0000F4AEE00BA7F0E07E4F11C0028003C000018071510101001040000006402020037", hex); + } + [Fact] + public void Test6_1() + { + byte[] bodys = "000493E0000F4AEE00BA7F0E07E4F11C0028003C000018071510101001040000006402020037".ToHexBytes(); + string json = JT808Serializer.Analyze(bodys); + } [Fact] public void Test_all_attcahids() diff --git a/src/JT808.Protocol.Test/Simples/Demo8.cs b/src/JT808.Protocol.Test/Simples/Demo8.cs new file mode 100644 index 0000000..7e3089a --- /dev/null +++ b/src/JT808.Protocol.Test/Simples/Demo8.cs @@ -0,0 +1,88 @@ +using JT808.Protocol.Enums; +using JT808.Protocol.Interfaces; +using JT808.Protocol.Internal; +using JT808.Protocol.Extensions; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; +using JT808.Protocol.MessageBody; +using JT808.Protocol.Formatters; +using JT808.Protocol.MessagePack; +using System.Text.Json; + +namespace JT808.Protocol.Test.Simples +{ + public class Demo8 + { + public JT808Serializer JT808Serializer; + public Demo8() + { + IJT808Config jT808Config = new DefaultGlobalConfig(); + jT808Config.MsgIdFactory.SetMap(); + JT808Serializer = new JT808Serializer(jT808Config); + } + + [Fact] + public void Test1() + { + JT808Package dt1Package = new JT808Package(); + dt1Package.Header = new JT808Header + { + MsgId = 0x93, + ManualMsgNum = 126, + TerminalPhoneNo = "1234567891" + }; + DT1Demo8 dT1Demo8 = new DT1Demo8(); + dT1Demo8.Age1 = 18; + dT1Demo8.Sex1 = 2; + dt1Package.Bodies = dT1Demo8; + byte[] dt1Data = JT808Serializer.Serialize(dt1Package); + var dt1Hex = dt1Data.ToHexString(); + Assert.Equal("7E00930003001234567891007D02020012677E", dt1Hex); + } + + [Fact] + public void Test2() + { + var data = "7E00930003001234567891007D02020012677E".ToHexBytes(); + string json = JT808Serializer.Analyze(data); + //{"[7E]\u5F00\u59CB":126,"[0093]\u6D88\u606FId":147,"\u6D88\u606F\u4F53\u5C5E\u6027\u5BF9\u8C61":{"[0000000000000011]\u6D88\u606F\u4F53\u5C5E\u6027":3,"[bit15]\u4FDD\u7559":0,"[bit14]\u4FDD\u7559":0,"[bit13]\u662F\u5426\u5206\u5305":false,"[bit10~bit12]\u6570\u636E\u52A0\u5BC6":"None","[bit0~bit9]\u6D88\u606F\u4F53\u957F\u5EA6":3},"[1234567891]\u7EC8\u7AEF\u624B\u673A\u53F7":"1234567891","[007E]\u6D88\u606F\u6D41\u6C34\u53F7":126,"\u6570\u636E\u4F53\u5BF9\u8C61":{"DT1Demo8":"020012","[02]\u6027\u522B":2,"[0012]\u5E74\u9F84":18},"[67]\u6821\u9A8C\u7801":103,"[7E]\u7ED3\u675F":126} + } + + public class DT1Demo8 : JT808Bodies, IJT808MessagePackFormatter,IJT808Analyze + { + public byte Sex1 { get; set; } + + public ushort Age1 { get; set; } + + public override ushort MsgId => 0x93; + + public override string Description => "DT1Demo8"; + + public void Analyze(ref JT808MessagePackReader reader, Utf8JsonWriter writer, IJT808Config config) + { + DT1Demo8 dT1Demo6 = new DT1Demo8(); + dT1Demo6.Sex1 = reader.ReadByte(); + writer.WriteNumber($"[{dT1Demo6.Sex1.ReadNumber()}]性别", dT1Demo6.Sex1); + dT1Demo6.Age1 = reader.ReadUInt16(); + writer.WriteNumber($"[{dT1Demo6.Age1.ReadNumber()}]年龄", dT1Demo6.Age1); + } + + public DT1Demo8 Deserialize(ref JT808MessagePackReader reader, IJT808Config config) + { + DT1Demo8 dT1Demo8 = new DT1Demo8(); + dT1Demo8.Sex1 = reader.ReadByte(); + dT1Demo8.Age1 = reader.ReadUInt16(); + return dT1Demo8; + } + + public void Serialize(ref JT808MessagePackWriter writer, DT1Demo8 value, IJT808Config config) + { + writer.WriteByte(value.Sex1); + writer.WriteUInt16(value.Age1); + } + } + } +} diff --git a/src/JT808.Protocol/MessageBody/JT808_0x0200.cs b/src/JT808.Protocol/MessageBody/JT808_0x0200.cs index 319d1ce..f947bb8 100644 --- a/src/JT808.Protocol/MessageBody/JT808_0x0200.cs +++ b/src/JT808.Protocol/MessageBody/JT808_0x0200.cs @@ -6,6 +6,7 @@ using JT808.Protocol.Interfaces; using JT808.Protocol.MessagePack; using System; using System.Collections.Generic; +using System.Linq; using System.Text.Json; namespace JT808.Protocol.MessageBody @@ -208,8 +209,28 @@ namespace JT808.Protocol.MessageBody JT808_0x0200 value = new JT808_0x0200(); value.AlarmFlag = reader.ReadUInt32(); writer.WriteNumber($"[{value.AlarmFlag.ReadNumber()}]报警标志", value.AlarmFlag); + var alarmFlags = JT808EnumExtensions.GetEnumTypes((int)value.AlarmFlag, 32); + if (alarmFlags.Any()) + { + writer.WriteStartArray("报警标志集合"); + foreach(var item in alarmFlags) + { + writer.WriteStringValue(item.ToString()); + } + writer.WriteEndArray(); + } value.StatusFlag = reader.ReadUInt32(); writer.WriteNumber($"[{value.StatusFlag.ReadNumber()}]状态位标志", value.StatusFlag); + var status = JT808EnumExtensions.GetEnumTypes((int)value.StatusFlag, 32); + if (status.Any()) + { + writer.WriteStartArray("状态标志集合"); + foreach (var item in status) + { + writer.WriteStringValue(item.ToString()); + } + writer.WriteEndArray(); + } if (((value.StatusFlag >> 28) & 1) == 1) { //南纬 268435456 0x10000000 value.Lat = (int)reader.ReadUInt32();