浏览代码

1.修改0200报警及状态位分析器

2.增加Demo8及修改文档
3.修改CI的dotnet版本
tags/v2.2.6
smallchi(Koike) 5 年前
父节点
当前提交
e82db098e6
共有 5 个文件被更改,包括 154 次插入2 次删除
  1. +1
    -1
      .travis.yml
  2. +6
    -0
      README.md
  3. +38
    -1
      src/JT808.Protocol.Test/MessageBody/JT808_0x0200Test.cs
  4. +88
    -0
      src/JT808.Protocol.Test/Simples/Demo8.cs
  5. +21
    -0
      src/JT808.Protocol/MessageBody/JT808_0x0200.cs

+ 1
- 1
.travis.yml 查看文件

@@ -1,6 +1,6 @@
language: csharp language: csharp
solution: JT808.Protocol.sln solution: JT808.Protocol.sln
dotnet: 3.1.100
dotnet: 3.1.101
os: linux os: linux
mono: none mono: none
dist: trusty2 dist: trusty2


+ 6
- 0
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) [可以参考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安装 ## NuGet安装


| Package Name | Version | Downloads | | Package Name | Version | Downloads |


+ 38
- 1
src/JT808.Protocol.Test/MessageBody/JT808_0x0200Test.cs 查看文件

@@ -251,7 +251,12 @@ namespace JT808.Protocol.Test.MessageBody
jT808Package.Bodies = jT808UploadLocationRequest; jT808Package.Bodies = jT808UploadLocationRequest;
var hex = JT808Serializer.Serialize(jT808Package).ToHexString(); 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] [Fact]
public void Test5() 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(100, ((JT808_0x0200_0x01)jT808UploadLocationRequest.JT808LocationAttachData[JT808Constants.JT808_0x0200_0x01]).Mileage);
Assert.Equal(55, ((JT808_0x0200_0x02)jT808UploadLocationRequest.JT808LocationAttachData[JT808Constants.JT808_0x0200_0x02]).Oil); 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<byte, JT808_0x0200_BodyBase>()
};
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<JT808_0x0200>(bodys);
}


[Fact] [Fact]
public void Test_all_attcahids() public void Test_all_attcahids()


+ 88
- 0
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<DT1Demo8>();
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<DT1Demo8>,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);
}
}
}
}

+ 21
- 0
src/JT808.Protocol/MessageBody/JT808_0x0200.cs 查看文件

@@ -6,6 +6,7 @@ using JT808.Protocol.Interfaces;
using JT808.Protocol.MessagePack; using JT808.Protocol.MessagePack;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text.Json; using System.Text.Json;


namespace JT808.Protocol.MessageBody namespace JT808.Protocol.MessageBody
@@ -208,8 +209,28 @@ namespace JT808.Protocol.MessageBody
JT808_0x0200 value = new JT808_0x0200(); JT808_0x0200 value = new JT808_0x0200();
value.AlarmFlag = reader.ReadUInt32(); value.AlarmFlag = reader.ReadUInt32();
writer.WriteNumber($"[{value.AlarmFlag.ReadNumber()}]报警标志", value.AlarmFlag); writer.WriteNumber($"[{value.AlarmFlag.ReadNumber()}]报警标志", value.AlarmFlag);
var alarmFlags = JT808EnumExtensions.GetEnumTypes<JT808Alarm>((int)value.AlarmFlag, 32);
if (alarmFlags.Any())
{
writer.WriteStartArray("报警标志集合");
foreach(var item in alarmFlags)
{
writer.WriteStringValue(item.ToString());
}
writer.WriteEndArray();
}
value.StatusFlag = reader.ReadUInt32(); value.StatusFlag = reader.ReadUInt32();
writer.WriteNumber($"[{value.StatusFlag.ReadNumber()}]状态位标志", value.StatusFlag); writer.WriteNumber($"[{value.StatusFlag.ReadNumber()}]状态位标志", value.StatusFlag);
var status = JT808EnumExtensions.GetEnumTypes<JT808Status>((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) if (((value.StatusFlag >> 28) & 1) == 1)
{ //南纬 268435456 0x10000000 { //南纬 268435456 0x10000000
value.Lat = (int)reader.ReadUInt32(); value.Lat = (int)reader.ReadUInt32();


正在加载...
取消
保存