diff --git a/src/JT808.Protocol.Test/Internal/JT808_0x0200_Custom_FactoryTest.cs b/src/JT808.Protocol.Test/Internal/JT808_0x0200_Custom_FactoryTest.cs index 43c3c20..1cbda6c 100644 --- a/src/JT808.Protocol.Test/Internal/JT808_0x0200_Custom_FactoryTest.cs +++ b/src/JT808.Protocol.Test/Internal/JT808_0x0200_Custom_FactoryTest.cs @@ -12,7 +12,10 @@ namespace JT808.Protocol.Test.Internal [Fact] public void Test1() { - new JT808_0x0200_Custom_Factory().Register(Assembly.GetExecutingAssembly()); + Assert.Throws(()=> + { + new JT808_0x0200_Custom_Factory().Register(Assembly.GetExecutingAssembly()); + }); } } } diff --git a/src/JT808.Protocol.Test/JT808SerializerTest.cs b/src/JT808.Protocol.Test/JT808SerializerTest.cs index 96a4898..dd6d245 100644 --- a/src/JT808.Protocol.Test/JT808SerializerTest.cs +++ b/src/JT808.Protocol.Test/JT808SerializerTest.cs @@ -17,7 +17,6 @@ namespace JT808.Protocol.Test var result = Parallel.For(0, 100, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (i) => { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); JT808Serializer jT808Serializer = new JT808Serializer(jT808Config); }); if (result.IsCompleted) diff --git a/src/JT808.Protocol.Test/MessageBody/JT808_0x0200Test.cs b/src/JT808.Protocol.Test/MessageBody/JT808_0x0200Test.cs index 78f4600..902bb32 100644 --- a/src/JT808.Protocol.Test/MessageBody/JT808_0x0200Test.cs +++ b/src/JT808.Protocol.Test/MessageBody/JT808_0x0200Test.cs @@ -22,7 +22,7 @@ namespace JT808.Protocol.Test.MessageBody public JT808_0x0200Test() { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); + jT808Config.JT808_0X0200_Custom_Factory.SetMap(); JT808Serializer = new JT808Serializer(jT808Config); } [Fact] @@ -204,7 +204,7 @@ namespace JT808.Protocol.Test.MessageBody Assert.Equal((uint)2, jT808UploadLocationRequest.StatusFlag); 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); - var jT808LocationAttachImpl0x06 = JT808Serializer.Deserialize(jT808UploadLocationRequest.JT808CustomLocationAttachOriginalData[0x06]); + var jT808LocationAttachImpl0x06 = (JT808LocationAttachImpl0x06)jT808UploadLocationRequest.JT808CustomLocationAttachData[0x06]; Assert.Equal(18, jT808LocationAttachImpl0x06.Age); Assert.Equal(1, jT808LocationAttachImpl0x06.Gender); Assert.Equal("smallchi", jT808LocationAttachImpl0x06.UserName); @@ -370,6 +370,58 @@ namespace JT808.Protocol.Test.MessageBody var hex = JT808Serializer.Serialize(jT808Package).ToHexString(); Assert.Equal("7E0200005C11223344556622B8000000010000000200BA7F0E07E4F11C0028003C00001807151010100104000000640202003703020038040200011105010000000112060100000001011307000000020022012504000000172A0200F42B04000000F2300102310105167E", hex); //7E0200005C11223344556622B8000000010000000200BA7F0E07E4F11C0028003C00001807151010100104000000640202003703020038040200011105010000000112060100000001011307000000020022012504000000172A0200F42B04000000F2300102310105167E + + //7E + //02 00 + //00 5C + //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 + //03 + // 02 + // 00 38 + //04 + // 02 + // 00 01 + //11 + // 05 + // 01 00 00 00 01 + //12 + // 06 + // 01 00 00 00 01 01 + //13 + // 07 + // 00 00 00 02 00 22 01 + //25 + // 04 + // 00 00 00 17 + //2A + // 02 + // 00 F4 + //2B + // 04 + // 00 00 00 F2 + //30 + // 01 + // 02 + //31 + // 01 + // 05 + //16 + //7E } [Fact] public void LatLngTest1_1() diff --git a/src/JT808.Protocol.Test/MessageBody/JT808_0x0701Test.cs b/src/JT808.Protocol.Test/MessageBody/JT808_0x0701Test.cs index ad9f176..a9e52c9 100644 --- a/src/JT808.Protocol.Test/MessageBody/JT808_0x0701Test.cs +++ b/src/JT808.Protocol.Test/MessageBody/JT808_0x0701Test.cs @@ -15,7 +15,7 @@ namespace JT808.Protocol.Test.MessageBody public JT808_0x0701Test() { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); + jT808Config.FormatterFactory.SetMap(); JT808Serializer = new JT808Serializer(jT808Config); } [Fact] diff --git a/src/JT808.Protocol.Test/MessageBody/JT808_0x0801Test.cs b/src/JT808.Protocol.Test/MessageBody/JT808_0x0801Test.cs index 0a97c64..f654047 100644 --- a/src/JT808.Protocol.Test/MessageBody/JT808_0x0801Test.cs +++ b/src/JT808.Protocol.Test/MessageBody/JT808_0x0801Test.cs @@ -17,7 +17,6 @@ namespace JT808.Protocol.Test.MessageBody public JT808_0x0801Test() { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); jT808Config.SkipCRCCode = true; JT808Serializer = new JT808Serializer(jT808Config); } diff --git a/src/JT808.Protocol.Test/MessageBody/JT808_0x0900Test.cs b/src/JT808.Protocol.Test/MessageBody/JT808_0x0900Test.cs index 57e1d24..b7905cd 100644 --- a/src/JT808.Protocol.Test/MessageBody/JT808_0x0900Test.cs +++ b/src/JT808.Protocol.Test/MessageBody/JT808_0x0900Test.cs @@ -15,8 +15,7 @@ namespace JT808.Protocol.Test.MessageBody public JT808_0x0900Test() { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); - + jT808Config.FormatterFactory.SetMap(); JT808Serializer = new JT808Serializer(jT808Config); } [Fact] diff --git a/src/JT808.Protocol.Test/MessageBody/JT808_0x8103Test.cs b/src/JT808.Protocol.Test/MessageBody/JT808_0x8103Test.cs index 39b475d..ee6eb77 100644 --- a/src/JT808.Protocol.Test/MessageBody/JT808_0x8103Test.cs +++ b/src/JT808.Protocol.Test/MessageBody/JT808_0x8103Test.cs @@ -16,7 +16,7 @@ namespace JT808.Protocol.Test.MessageBody public JT808_0x8103Test() { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); + jT808Config.JT808_0X8103_Custom_Factory.SetMap(); JT808Serializer = new JT808Serializer(jT808Config); } [Fact] diff --git a/src/JT808.Protocol.Test/MessageBody/JT808_0x8900Test.cs b/src/JT808.Protocol.Test/MessageBody/JT808_0x8900Test.cs index 7630e00..e539c1c 100644 --- a/src/JT808.Protocol.Test/MessageBody/JT808_0x8900Test.cs +++ b/src/JT808.Protocol.Test/MessageBody/JT808_0x8900Test.cs @@ -15,7 +15,7 @@ namespace JT808.Protocol.Test.MessageBody public JT808_0x8900Test() { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); + jT808Config.FormatterFactory.SetMap(); JT808Serializer = new JT808Serializer(jT808Config); } [Fact] diff --git a/src/JT808.Protocol.Test/Simples/Demo1.cs b/src/JT808.Protocol.Test/Simples/Demo1.cs index bbd56d0..a030997 100644 --- a/src/JT808.Protocol.Test/Simples/Demo1.cs +++ b/src/JT808.Protocol.Test/Simples/Demo1.cs @@ -16,7 +16,6 @@ namespace JT808.Protocol.Test.Simples public Demo1() { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); JT808Serializer = new JT808Serializer(jT808Config); } diff --git a/src/JT808.Protocol.Test/Simples/Demo2.cs b/src/JT808.Protocol.Test/Simples/Demo2.cs index 2f125e4..3b7821d 100644 --- a/src/JT808.Protocol.Test/Simples/Demo2.cs +++ b/src/JT808.Protocol.Test/Simples/Demo2.cs @@ -17,7 +17,6 @@ namespace JT808.Protocol.Test.Simples public Demo2() { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); JT808Serializer = new JT808Serializer(jT808Config); } diff --git a/src/JT808.Protocol.Test/Simples/Demo3.cs b/src/JT808.Protocol.Test/Simples/Demo3.cs index 619d842..e9e8e39 100644 --- a/src/JT808.Protocol.Test/Simples/Demo3.cs +++ b/src/JT808.Protocol.Test/Simples/Demo3.cs @@ -17,7 +17,6 @@ namespace JT808.Protocol.Test.Simples public Demo3() { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); JT808Serializer = new JT808Serializer(jT808Config); } diff --git a/src/JT808.Protocol.Test/Simples/Demo4.cs b/src/JT808.Protocol.Test/Simples/Demo4.cs index fca4bcf..80c9e3a 100644 --- a/src/JT808.Protocol.Test/Simples/Demo4.cs +++ b/src/JT808.Protocol.Test/Simples/Demo4.cs @@ -18,12 +18,9 @@ namespace JT808.Protocol.Test.Simples { public class Demo4 { - public JT808Serializer JT808Serializer; public Demo4() { - IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); - JT808Serializer = new JT808Serializer(jT808Config); + } private Dictionary cache = new Dictionary { @@ -37,34 +34,35 @@ namespace JT808.Protocol.Test.Simples public void Test1() { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); + jT808Config.JT808_0X0200_Custom_Factory.SetMap(); JT808Serializer demo5JT808Serializer = new JT808Serializer(jT808Config); JT808Package jT808Package = JT808MsgId.位置信息汇报.Create("123456789012", - new JT808_0x0200 - { - AlarmFlag = 1, - Altitude = 40, - GPSTime = DateTime.Parse("2018-12-20 20:10:10"), - Lat = 12222222, - Lng = 132444444, - Speed = 60, - Direction = 0, - StatusFlag = 2, - JT808CustomLocationAttachData = new Dictionary - { - {0x81,new JT808_0x0200_DT1_0x81 { - Age=15, - Gender=1, - UserName="smallchi" - } } - } - }); + new JT808_0x0200 + { + AlarmFlag = 1, + Altitude = 40, + GPSTime = DateTime.Parse("2018-12-20 20:10:10"), + Lat = 12222222, + Lng = 132444444, + Speed = 60, + Direction = 0, + StatusFlag = 2, + JT808CustomLocationAttachData = new Dictionary + { + {0x81,new JT808_0x0200_DT1_0x81 { + Age=15, + Gender=1, + UserName="smallchi" + } } + } + }); byte[] data = demo5JT808Serializer.Serialize(jT808Package); var jT808PackageResult = demo5JT808Serializer.Deserialize(data); JT808_0x0200 jT808_0X0200 = jT808PackageResult.Bodies as JT808_0x0200; - var attach = DeviceTypeFactory.Create(cache[jT808PackageResult.Header.TerminalPhoneNo], jT808_0X0200.JT808CustomLocationAttachOriginalData); + + var attach = DeviceTypeFactory.Create(cache[jT808PackageResult.Header.TerminalPhoneNo], jT808_0X0200.JT808CustomLocationAttachData); var extJson = attach.ExtData.Data.ToString(Formatting.None); var attachinfo81 = (JT808_0x0200_DT1_0x81)attach.JT808CustomLocationAttachData[0x81]; Assert.Equal((uint)15, attachinfo81.Age); @@ -128,14 +126,14 @@ namespace JT808.Protocol.Test.Simples public class DeviceTypeFactory { - public static DeviceTypeBase Create(DeviceType deviceType, Dictionary jT808CustomLocationAttachOriginalData) + public static DeviceTypeBase Create(DeviceType deviceType, Dictionary jT808CustomLocationAttachData) { switch (deviceType) { case DeviceType.DT1: - return new DeviceType1(jT808CustomLocationAttachOriginalData); + return new DeviceType1(jT808CustomLocationAttachData); case DeviceType.DT2: - return new DeviceType2(jT808CustomLocationAttachOriginalData); + return new DeviceType2(jT808CustomLocationAttachData); default: return default; } @@ -157,50 +155,46 @@ namespace JT808.Protocol.Test.Simples } public virtual IExtData ExtData { get; protected set; } = new DefaultExtDataImpl(); public abstract Dictionary JT808CustomLocationAttachData { get; protected set; } - protected DeviceTypeBase(Dictionary jT808CustomLocationAttachOriginalData) + protected DeviceTypeBase(Dictionary jT808CustomLocationAttachData) { - IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); - JT808Serializer = new JT808Serializer(jT808Config); - Execute(jT808CustomLocationAttachOriginalData); + Execute(jT808CustomLocationAttachData); } - protected abstract void Execute(Dictionary jT808CustomLocationAttachOriginalData); + protected abstract void Execute(Dictionary jT808CustomLocationAttachData); } public class DeviceType1 : DeviceTypeBase { private const byte dt1_0x81 = 0x81; private const byte dt1_0x82 = 0x82; - public DeviceType1(Dictionary jT808CustomLocationAttachOriginalData) : base(jT808CustomLocationAttachOriginalData) + public DeviceType1(Dictionary jT808CustomLocationAttachData) : base(jT808CustomLocationAttachData) { + IJT808Config jT808Config = new DefaultGlobalConfig(); + jT808Config.JT808_0X0200_Custom_Factory.SetMap(); + jT808Config.JT808_0X0200_Custom_Factory.SetMap(); + JT808Serializer = new JT808Serializer(jT808Config); } public override Dictionary JT808CustomLocationAttachData { get; protected set; } - protected override void Execute(Dictionary jT808CustomLocationAttachOriginalData) + protected override void Execute(Dictionary jT808CustomLocationAttachData) { JT808CustomLocationAttachData = new Dictionary(); - foreach (var item in jT808CustomLocationAttachOriginalData) + foreach (var item in jT808CustomLocationAttachData) { try { switch (item.Key) { case dt1_0x81: - var info81 = JT808Serializer.Deserialize(item.Value); - if (info81 != null) - { - IExtDataProcessor extDataProcessor = new JT808_0x0200_DT1_0x81_ExtDataProcessor(info81); - extDataProcessor.Processor(ExtData); - JT808CustomLocationAttachData.Add(dt1_0x81, info81); - } + var info81 = (JT808_0x0200_DT1_0x81)item.Value; + IExtDataProcessor extDataProcessor81 = new JT808_0x0200_DT1_0x81_ExtDataProcessor(info81); + extDataProcessor81.Processor(ExtData); + JT808CustomLocationAttachData.Add(dt1_0x81, info81); + break; case dt1_0x82: - var info82 = JT808Serializer.Deserialize(item.Value); - if (info82 != null) - { - IExtDataProcessor extDataProcessor = new JT808_0x0200_DT1_0x82_ExtDataProcessor(info82); - extDataProcessor.Processor(ExtData); - JT808CustomLocationAttachData.Add(dt1_0x82, info82); - } + var info82 = (JT808_0x0200_DT1_0x82)item.Value; + IExtDataProcessor extDataProcessor82 = new JT808_0x0200_DT1_0x82_ExtDataProcessor(info82); + extDataProcessor82.Processor(ExtData); + JT808CustomLocationAttachData.Add(dt1_0x82, info82); break; } } @@ -214,13 +208,16 @@ namespace JT808.Protocol.Test.Simples public class DeviceType2 : DeviceTypeBase { - public DeviceType2(Dictionary jT808CustomLocationAttachOriginalData) : base(jT808CustomLocationAttachOriginalData) + public DeviceType2(Dictionary jT808CustomLocationAttachData) : base(jT808CustomLocationAttachData) { + IJT808Config jT808Config = new DefaultGlobalConfig(); + jT808Config.JT808_0X0200_Custom_Factory.SetMap(); + JT808Serializer = new JT808Serializer(jT808Config); } public override Dictionary JT808CustomLocationAttachData { get; protected set; } private const byte dt2_0x81 = 0x81; - protected override void Execute(Dictionary jT808CustomLocationAttachOriginalData) + protected override void Execute(Dictionary jT808CustomLocationAttachOriginalData) { JT808CustomLocationAttachData = new Dictionary(); foreach (var item in jT808CustomLocationAttachOriginalData) @@ -230,13 +227,10 @@ namespace JT808.Protocol.Test.Simples switch (item.Key) { case dt2_0x81: - var info81 = JT808Serializer.Deserialize(item.Value); - if (info81 != null) - { - IExtDataProcessor extDataProcessor = new JT808_0x0200_DT2_0x81_ExtDataProcessor(info81); - extDataProcessor.Processor(ExtData); - JT808CustomLocationAttachData.Add(dt2_0x81, info81); - } + var info81 = (JT808_0x0200_DT2_0x81)item.Value; + IExtDataProcessor extDataProcessor = new JT808_0x0200_DT2_0x81_ExtDataProcessor(info81); + extDataProcessor.Processor(ExtData); + JT808CustomLocationAttachData.Add(dt2_0x81, info81); break; } } diff --git a/src/JT808.Protocol.Test/Simples/Demo5.cs b/src/JT808.Protocol.Test/Simples/Demo5.cs index b05741a..3e15018 100644 --- a/src/JT808.Protocol.Test/Simples/Demo5.cs +++ b/src/JT808.Protocol.Test/Simples/Demo5.cs @@ -14,7 +14,6 @@ namespace JT808.Protocol.Test.Simples public Demo5() { IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); jT808Config.SkipCRCCode = true; JT808Serializer = new JT808Serializer(jT808Config); } diff --git a/src/JT808.Protocol.Test/Simples/Demo6.cs b/src/JT808.Protocol.Test/Simples/Demo6.cs index 7d6dcc5..facc0f8 100644 --- a/src/JT808.Protocol.Test/Simples/Demo6.cs +++ b/src/JT808.Protocol.Test/Simples/Demo6.cs @@ -16,15 +16,18 @@ namespace JT808.Protocol.Test.Simples { public class Demo6 { - public JT808Serializer JT808Serializer; + public JT808Serializer DT1JT808Serializer; + public JT808Serializer DT2JT808Serializer; public Demo6() { - IJT808Config jT808Config = new DefaultGlobalConfig(); - jT808Config.Register(Assembly.GetExecutingAssembly()); + IJT808Config DT1JT808Config = new DefaultGlobalConfig(); + IJT808Config DT2JT808Config = new DefaultGlobalConfig(); + //根据不同的设备终端号,添加自定义消息Id - jT808Config.MsgIdFactory.CustomSetMap(0x91, "1234567891"); - jT808Config.MsgIdFactory.CustomSetMap(0x91, "1234567892"); - JT808Serializer = new JT808Serializer(jT808Config); + DT1JT808Config.MsgIdFactory.SetMap(); + DT2JT808Config.MsgIdFactory.SetMap(); + DT1JT808Serializer = new JT808Serializer(DT1JT808Config); + DT2JT808Serializer = new JT808Serializer(DT2JT808Config); } /// @@ -56,16 +59,16 @@ namespace JT808.Protocol.Test.Simples dT2Demo6.Age2 = 18; dT2Demo6.Sex2 = 2; dt2Package.Bodies = dT2Demo6; - byte[] dt1Data = JT808Serializer.Serialize(dt1Package); + byte[] dt1Data = DT1JT808Serializer.Serialize(dt1Package); var dt1Hex = dt1Data.ToHexString(); //7E00910003001234567891007D02020012657E - byte[] dt2Data = JT808Serializer.Serialize(dt2Package); + byte[] dt2Data = DT2JT808Serializer.Serialize(dt2Package); var dt2Hex = dt2Data.ToHexString(); //7E00910003001234567892007D02020012667E Assert.Equal("7E00910003001234567891007D02020012657E", dt1Hex); Assert.Equal("7E00910003001234567892007D02020012667E", dt2Hex); - JT808Package dt1Package1 = JT808Serializer.Deserialize(dt1Data); + JT808Package dt1Package1 = DT1JT808Serializer.Deserialize(dt1Data); Assert.Equal(0x91, dt1Package1.Header.MsgId); Assert.Equal(126, dt1Package1.Header.MsgNum); Assert.Equal("1234567891", dt1Package1.Header.TerminalPhoneNo); @@ -73,7 +76,7 @@ namespace JT808.Protocol.Test.Simples Assert.Equal((ushort)18, dt1Bodies.Age1); Assert.Equal(2, dt1Bodies.Sex1); - JT808Package dt2Package1 = JT808Serializer.Deserialize(dt2Data); + JT808Package dt2Package1 = DT2JT808Serializer.Deserialize(dt2Data); Assert.Equal(0x91, dt2Package1.Header.MsgId); Assert.Equal(126, dt2Package1.Header.MsgNum); Assert.Equal("1234567892", dt2Package1.Header.TerminalPhoneNo); diff --git a/src/JT808.Protocol/Formatters/IJT808FormatterFactory.cs b/src/JT808.Protocol/Formatters/IJT808FormatterFactory.cs index b1fc0e3..d9a7d9d 100644 --- a/src/JT808.Protocol/Formatters/IJT808FormatterFactory.cs +++ b/src/JT808.Protocol/Formatters/IJT808FormatterFactory.cs @@ -8,8 +8,8 @@ namespace JT808.Protocol.Formatters { public interface IJT808FormatterFactory: IJT808ExternalRegister { - Dictionary FormatterDict { get;} - IJT808FormatterFactory SetMap() - where TJT808Bodies : JT808Bodies; + IDictionary FormatterDict { get;} + IJT808FormatterFactory SetMap() + where TIJT808Formatter : IJT808Formatter; } } diff --git a/src/JT808.Protocol/Interfaces/IJT808MsgIdFactory.cs b/src/JT808.Protocol/Interfaces/IJT808MsgIdFactory.cs index a21871f..0f200ad 100644 --- a/src/JT808.Protocol/Interfaces/IJT808MsgIdFactory.cs +++ b/src/JT808.Protocol/Interfaces/IJT808MsgIdFactory.cs @@ -1,17 +1,12 @@ using System; +using System.Collections.Generic; namespace JT808.Protocol.Interfaces { - public interface IJT808MsgIdFactory + public interface IJT808MsgIdFactory:IJT808ExternalRegister { - object GetBodiesImplInstanceByMsgId(ushort msgId, string terminalPhoneNo); - IJT808MsgIdFactory SetMap(ushort msgId, string terminalPhoneNo) - where TJT808Bodies : JT808Bodies; - IJT808MsgIdFactory SetMap(ushort msgId, string terminalPhoneNo, Type bodiesImplType); - IJT808MsgIdFactory ReplaceMap(ushort msgId, string terminalPhoneNo) - where TJT808Bodies : JT808Bodies; - IJT808MsgIdFactory CustomSetMap(ushort msgId, string terminalPhoneNo) - where TJT808Bodies : JT808Bodies; - IJT808MsgIdFactory CustomSetMap(ushort msgId, string terminalPhoneNo, Type bodiesImplType); + IDictionary Map { get; } + bool TryGetValue(ushort msgId, out object instance); + IJT808MsgIdFactory SetMap() where TJT808Bodies : JT808Bodies; } } diff --git a/src/JT808.Protocol/Interfaces/IJT808_0x0200_Custom_Factory.cs b/src/JT808.Protocol/Interfaces/IJT808_0x0200_Custom_Factory.cs index 173bcbe..3f8c487 100644 --- a/src/JT808.Protocol/Interfaces/IJT808_0x0200_Custom_Factory.cs +++ b/src/JT808.Protocol/Interfaces/IJT808_0x0200_Custom_Factory.cs @@ -1,4 +1,5 @@ -using System; +using JT808.Protocol.MessageBody; +using System; using System.Collections.Generic; using System.Text; @@ -6,6 +7,7 @@ namespace JT808.Protocol.Interfaces { public interface IJT808_0x0200_Custom_Factory: IJT808ExternalRegister { - HashSet AttachIds { get; } + IDictionary Map { get; } + IJT808_0x0200_Custom_Factory SetMap() where TJT808_0x0200_CustomBody : JT808_0x0200_CustomBodyBase; } } diff --git a/src/JT808.Protocol/Interfaces/IJT808_0x0200_Factory.cs b/src/JT808.Protocol/Interfaces/IJT808_0x0200_Factory.cs index 1aa2b16..43782e0 100644 --- a/src/JT808.Protocol/Interfaces/IJT808_0x0200_Factory.cs +++ b/src/JT808.Protocol/Interfaces/IJT808_0x0200_Factory.cs @@ -1,12 +1,15 @@ -using System; +using JT808.Protocol.MessageBody; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Text; namespace JT808.Protocol.Interfaces { - public interface IJT808_0x0200_Factory - { - IDictionary JT808LocationAttachMethod { get; set; } - } + public interface IJT808_0x0200_Factory: IJT808ExternalRegister + { + IDictionary Map { get; set; } + + IJT808_0x0200_Factory SetMap() where TJT808_0x0200_Body : JT808_0x0200_BodyBase; + } } diff --git a/src/JT808.Protocol/Interfaces/IJT808_0x8103_Custom_Factory.cs b/src/JT808.Protocol/Interfaces/IJT808_0x8103_Custom_Factory.cs index b716b44..a75eead 100644 --- a/src/JT808.Protocol/Interfaces/IJT808_0x8103_Custom_Factory.cs +++ b/src/JT808.Protocol/Interfaces/IJT808_0x8103_Custom_Factory.cs @@ -1,4 +1,5 @@ -using System; +using JT808.Protocol.MessageBody; +using System; using System.Collections.Generic; using System.Text; @@ -6,6 +7,8 @@ namespace JT808.Protocol.Interfaces { public interface IJT808_0x8103_Custom_Factory : IJT808ExternalRegister { - IDictionary ParamMethods { get;} + IDictionary Map { get;} + + IJT808_0x8103_Custom_Factory SetMap() where TJT808_0x8103_CustomBody : JT808_0x8103_CustomBodyBase; } } diff --git a/src/JT808.Protocol/Interfaces/IJT808_0x8103_Factory.cs b/src/JT808.Protocol/Interfaces/IJT808_0x8103_Factory.cs index 90a5be7..b3b983f 100644 --- a/src/JT808.Protocol/Interfaces/IJT808_0x8103_Factory.cs +++ b/src/JT808.Protocol/Interfaces/IJT808_0x8103_Factory.cs @@ -1,12 +1,14 @@ -using System; +using JT808.Protocol.MessageBody; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Text; namespace JT808.Protocol.Interfaces { - public interface IJT808_0x8103_Factory + public interface IJT808_0x8103_Factory: IJT808ExternalRegister { - IDictionary ParamMethods { get; set; } + IDictionary Map { get; } + IJT808_0x8103_Factory SetMap() where TJT808_0x8103_Body : JT808_0x8103_BodyBase; } } diff --git a/src/JT808.Protocol/Internal/JT808FormatterFactory.cs b/src/JT808.Protocol/Internal/JT808FormatterFactory.cs index 7b1c900..6843c99 100644 --- a/src/JT808.Protocol/Internal/JT808FormatterFactory.cs +++ b/src/JT808.Protocol/Internal/JT808FormatterFactory.cs @@ -12,7 +12,7 @@ namespace JT808.Protocol.Internal { internal class JT808FormatterFactory : IJT808FormatterFactory { - public Dictionary FormatterDict { get; } + public IDictionary FormatterDict { get; } public JT808FormatterFactory() { @@ -40,20 +40,19 @@ namespace JT808.Protocol.Internal } } - public IJT808FormatterFactory SetMap() - where TJT808Bodies:JT808Bodies + public void Register(Assembly externalAssembly) + { + Init(externalAssembly); + } + + public IJT808FormatterFactory SetMap() where TIJT808Formatter : IJT808Formatter { - Type type = typeof(TJT808Bodies); + Type type = typeof(TIJT808Formatter); if (!FormatterDict.ContainsKey(type.GUID)) { FormatterDict.Add(type.GUID, Activator.CreateInstance(type)); } return this; } - - public void Register(Assembly externalAssembly) - { - Init(externalAssembly); - } } } diff --git a/src/JT808.Protocol/Internal/JT808MsgIdFactory.cs b/src/JT808.Protocol/Internal/JT808MsgIdFactory.cs index 71b0848..c732519 100644 --- a/src/JT808.Protocol/Internal/JT808MsgIdFactory.cs +++ b/src/JT808.Protocol/Internal/JT808MsgIdFactory.cs @@ -5,130 +5,72 @@ using JT808.Protocol.Formatters; using JT808.Protocol.Interfaces; using System; using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Reflection; namespace JT808.Protocol.Internal { internal class JT808MsgIdFactory: IJT808MsgIdFactory { - private Dictionary map; - - private Dictionary> customMap; + public IDictionary Map { get; } internal JT808MsgIdFactory() { - map = new Dictionary(); - customMap = new Dictionary>(StringComparer.OrdinalIgnoreCase); - InitMap(); + Map = new Dictionary(); + InitMap(Assembly.GetExecutingAssembly()); } - private void InitMap() + private void InitMap(Assembly assembly) { - foreach (var item in Enum.GetNames(typeof(JT808MsgId))) + var types = assembly.GetTypes().Where(w => w.BaseType == typeof(JT808Bodies)).ToList(); + foreach (var type in types) { - JT808MsgId msgId = item.ToEnum(); - if (!map.ContainsKey((ushort)msgId)) + var instance = Activator.CreateInstance(type); + ushort msgId = 0; + try { - JT808BodiesTypeAttribute jT808BodiesTypeAttribute = msgId.GetAttribute(); - if (jT808BodiesTypeAttribute != null) - { - map.Add((ushort)msgId, Activator.CreateInstance(jT808BodiesTypeAttribute.JT808BodiesType)); - } + msgId = (ushort)type.GetProperty(nameof(JT808Bodies.MsgId)).GetValue(instance); } - } - } - - public object GetBodiesImplInstanceByMsgId(ushort msgId, string terminalPhoneNo) - { - //判断有无自定义消息Id类型 - if (customMap.TryGetValue(terminalPhoneNo, out var dict)) - { - if (dict != null) + catch (Exception ex) { - return dict.TryGetValue(msgId, out object bodiesImptType) ? bodiesImptType : null; + continue; + } + if (Map.ContainsKey(msgId)) + { + throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + } + else + { + Map.Add(msgId, instance); } } - return map.TryGetValue(msgId, out dynamic type) ? type : null; - } - - public IJT808MsgIdFactory SetMap(ushort msgId, string terminalPhoneNo) - where TJT808Bodies : JT808Bodies - { - if (!map.ContainsKey(msgId)) - { - map.Add(msgId, Activator.CreateInstance(typeof(TJT808Bodies))); - } - return this; } - public IJT808MsgIdFactory SetMap(ushort msgId, string terminalPhoneNo, Type bodiesImplType) + public bool TryGetValue(ushort msgId, out object instance) { - if (!map.ContainsKey(msgId)) - { - map.Add(msgId, Activator.CreateInstance(bodiesImplType)); - } - return this; + return Map.TryGetValue(msgId, out instance); } - public IJT808MsgIdFactory ReplaceMap(ushort msgId, string terminalPhoneNo) where TJT808Bodies : JT808Bodies + public IJT808MsgIdFactory SetMap() where TJT808Bodies : JT808Bodies { - if (!map.ContainsKey(msgId)) + Type type = typeof(TJT808Bodies); + var instance = Activator.CreateInstance(type); + var msgId = (ushort)type.GetProperty(nameof(JT808Bodies.MsgId)).GetValue(instance); + if (Map.ContainsKey(msgId)) { - map.Add(msgId, Activator.CreateInstance(typeof(TJT808Bodies))); + throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); } else { - map[msgId] = Activator.CreateInstance(typeof(TJT808Bodies)); - } - return this; - } - - public IJT808MsgIdFactory CustomSetMap(ushort msgId, string terminalPhoneNo) where TJT808Bodies : JT808Bodies - { - if (!string.IsNullOrEmpty(terminalPhoneNo)) - { - if (!customMap.TryGetValue(terminalPhoneNo, out var dict)) - { - if (dict == null) - { - Dictionary tmp = new Dictionary(); - tmp.Add(msgId, Activator.CreateInstance(typeof(TJT808Bodies))); - customMap.Add(terminalPhoneNo, tmp); - } - else - { - if (!dict.ContainsKey(msgId)) - { - dict.Add(msgId, Activator.CreateInstance(typeof(TJT808Bodies))); - } - } - } + Map.Add(msgId, instance); } return this; } - public IJT808MsgIdFactory CustomSetMap(ushort msgId, string terminalPhoneNo, Type bodiesImplType) + public void Register(Assembly externalAssembly) { - if (!string.IsNullOrEmpty(terminalPhoneNo)) - { - if (!customMap.TryGetValue(terminalPhoneNo, out var dict)) - { - if (dict == null) - { - Dictionary tmp = new Dictionary(); - tmp.Add(msgId, Activator.CreateInstance(bodiesImplType)); - customMap.Add(terminalPhoneNo, tmp); - return this; - } - else - { - if (!dict.ContainsKey(msgId)) - { - dict.Add(msgId, Activator.CreateInstance(bodiesImplType)); - } - } - } - } - return this; + InitMap(externalAssembly); } } } diff --git a/src/JT808.Protocol/Internal/JT808_0x0200_Custom_Factory.cs b/src/JT808.Protocol/Internal/JT808_0x0200_Custom_Factory.cs index 4ff7f31..398279f 100644 --- a/src/JT808.Protocol/Internal/JT808_0x0200_Custom_Factory.cs +++ b/src/JT808.Protocol/Internal/JT808_0x0200_Custom_Factory.cs @@ -10,11 +10,11 @@ namespace JT808.Protocol.Internal { class JT808_0x0200_Custom_Factory : IJT808_0x0200_Custom_Factory { - public HashSet AttachIds { get; } + public IDictionary Map { get; } public JT808_0x0200_Custom_Factory() { - AttachIds = new HashSet(); + Map = new Dictionary(); } public void Register(Assembly externalAssembly) @@ -22,12 +22,33 @@ namespace JT808.Protocol.Internal var types = externalAssembly.GetTypes().Where(w => w.BaseType == typeof(JT808_0x0200_CustomBodyBase)).ToList(); foreach(var type in types) { - var attachid = (byte)type.GetProperty(nameof(JT808_0x0200_CustomBodyBase.AttachInfoId)).GetValue(Activator.CreateInstance(type)); - if (!AttachIds.Contains(attachid)) + var instance = Activator.CreateInstance(type); + var attachid = (byte)type.GetProperty(nameof(JT808_0x0200_CustomBodyBase.AttachInfoId)).GetValue(instance); + if (Map.ContainsKey(attachid)) { - AttachIds.Add(attachid); + throw new ArgumentException($"{type.FullName} {attachid} An element with the same key already exists."); } + else + { + Map.Add(attachid, instance); + } + } + } + + public IJT808_0x0200_Custom_Factory SetMap() where TJT808_0x0200_CustomBody : JT808_0x0200_CustomBodyBase + { + Type type = typeof(TJT808_0x0200_CustomBody); + var instance = Activator.CreateInstance(type); + var attachInfoId = (byte)type.GetProperty(nameof(JT808_0x0200_CustomBodyBase.AttachInfoId)).GetValue(instance); + if (Map.ContainsKey(attachInfoId)) + { + throw new ArgumentException($"{type.FullName} {attachInfoId} An element with the same key already exists."); + } + else + { + Map.Add(attachInfoId, instance); } + return this; } } } diff --git a/src/JT808.Protocol/Internal/JT808_0x0200_Factory.cs b/src/JT808.Protocol/Internal/JT808_0x0200_Factory.cs index 519a249..f60d230 100644 --- a/src/JT808.Protocol/Internal/JT808_0x0200_Factory.cs +++ b/src/JT808.Protocol/Internal/JT808_0x0200_Factory.cs @@ -3,29 +3,65 @@ using JT808.Protocol.MessageBody; using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Linq; +using System.Reflection; using System.Text; namespace JT808.Protocol.Internal { class JT808_0x0200_Factory : IJT808_0x0200_Factory { - public IDictionary JT808LocationAttachMethod { get ; set; } + public IDictionary Map { get; set; } public JT808_0x0200_Factory() { - JT808LocationAttachMethod = new Dictionary(); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x01, typeof(JT808_0x0200_0x01)); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x02, typeof(JT808_0x0200_0x02)); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x03, typeof(JT808_0x0200_0x03)); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x04, typeof(JT808_0x0200_0x04)); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x11, typeof(JT808_0x0200_0x11)); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x12, typeof(JT808_0x0200_0x12)); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x13, typeof(JT808_0x0200_0x13)); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x25, typeof(JT808_0x0200_0x25)); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x2A, typeof(JT808_0x0200_0x2A)); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x2B, typeof(JT808_0x0200_0x2B)); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x30, typeof(JT808_0x0200_0x30)); - JT808LocationAttachMethod.Add(JT808Constants.JT808_0x0200_0x31, typeof(JT808_0x0200_0x31)); + Map = new Dictionary(); + Map.Add(JT808Constants.JT808_0x0200_0x01, new JT808_0x0200_0x01()); + Map.Add(JT808Constants.JT808_0x0200_0x02, new JT808_0x0200_0x02()); + Map.Add(JT808Constants.JT808_0x0200_0x03, new JT808_0x0200_0x03()); + Map.Add(JT808Constants.JT808_0x0200_0x04, new JT808_0x0200_0x04()); + Map.Add(JT808Constants.JT808_0x0200_0x11, new JT808_0x0200_0x11()); + Map.Add(JT808Constants.JT808_0x0200_0x12, new JT808_0x0200_0x12()); + Map.Add(JT808Constants.JT808_0x0200_0x13, new JT808_0x0200_0x13()); + Map.Add(JT808Constants.JT808_0x0200_0x25, new JT808_0x0200_0x25()); + Map.Add(JT808Constants.JT808_0x0200_0x2A, new JT808_0x0200_0x2A()); + Map.Add(JT808Constants.JT808_0x0200_0x2B, new JT808_0x0200_0x2B()); + Map.Add(JT808Constants.JT808_0x0200_0x30, new JT808_0x0200_0x30()); + Map.Add(JT808Constants.JT808_0x0200_0x31, new JT808_0x0200_0x31()); + } + + public IJT808_0x0200_Factory SetMap() where TJT808_0x0200_Body : JT808_0x0200_BodyBase + { + Type type = typeof(TJT808_0x0200_Body); + var instance = Activator.CreateInstance(type); + var attachInfoId = (byte)type.GetProperty(nameof(JT808_0x0200_BodyBase.AttachInfoId)).GetValue(instance); + if (Map.ContainsKey(attachInfoId)) + { + throw new ArgumentException($"{type.FullName} {attachInfoId} An element with the same key already exists."); + } + else + { + Map.Add(attachInfoId, instance); + } + return this; + } + + public void Register(Assembly externalAssembly) + { + var types = externalAssembly.GetTypes().Where(w => w.BaseType == typeof(JT808_0x0200_BodyBase)).ToList(); + foreach (var type in types) + { + var instance = Activator.CreateInstance(type); + var attachid = (byte)type.GetProperty(nameof(JT808_0x0200_BodyBase.AttachInfoId)).GetValue(instance); + if (Map.ContainsKey(attachid)) + { + throw new ArgumentException($"{type.FullName} {attachid} An element with the same key already exists."); + } + else + { + Map.Add(attachid, instance); + } + } } } } diff --git a/src/JT808.Protocol/Internal/JT808_0x8103_Custom_Factory.cs b/src/JT808.Protocol/Internal/JT808_0x8103_Custom_Factory.cs index 94be0d1..ac45b34 100644 --- a/src/JT808.Protocol/Internal/JT808_0x8103_Custom_Factory.cs +++ b/src/JT808.Protocol/Internal/JT808_0x8103_Custom_Factory.cs @@ -11,22 +11,43 @@ namespace JT808.Protocol.Interfaces { public JT808_0x8103_Custom_Factory() { - ParamMethods = new Dictionary(); + Map = new Dictionary(); } - public IDictionary ParamMethods { get;} + public IDictionary Map { get; } public void Register(Assembly externalAssembly) { var types = externalAssembly.GetTypes().Where(w => w.BaseType == typeof(JT808_0x8103_CustomBodyBase)).ToList(); foreach (var type in types) { - var paramId = (uint)type.GetProperty(nameof(JT808_0x8103_CustomBodyBase.ParamId)).GetValue(Activator.CreateInstance(type)); - if (!ParamMethods.ContainsKey(paramId)) + var instance = Activator.CreateInstance(type); + var paramId = (uint)type.GetProperty(nameof(JT808_0x8103_CustomBodyBase.ParamId)).GetValue(instance); + if (Map.ContainsKey(paramId)) { - ParamMethods.Add(paramId, type); + throw new ArgumentException($"{type.FullName} {paramId} An element with the same key already exists."); } + else + { + Map.Add(paramId, instance); + } + } + } + + public IJT808_0x8103_Custom_Factory SetMap() where TJT808_0x8103_CustomBody : JT808_0x8103_CustomBodyBase + { + Type type = typeof(TJT808_0x8103_CustomBody); + var instance = Activator.CreateInstance(type); + var paramId = (uint)type.GetProperty(nameof(JT808_0x8103_CustomBodyBase.ParamId)).GetValue(instance); + if (Map.ContainsKey(paramId)) + { + throw new ArgumentException($"{type.FullName} {paramId} An element with the same key already exists."); + } + else + { + Map.Add(paramId, instance); } + return this; } } } diff --git a/src/JT808.Protocol/Internal/JT808_0x8103_Factory.cs b/src/JT808.Protocol/Internal/JT808_0x8103_Factory.cs index 0ad67fa..7c2723a 100644 --- a/src/JT808.Protocol/Internal/JT808_0x8103_Factory.cs +++ b/src/JT808.Protocol/Internal/JT808_0x8103_Factory.cs @@ -12,90 +12,132 @@ namespace JT808.Protocol.Interfaces { public JT808_0x8103_Factory() { - ParamMethods = new Dictionary(); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0001, typeof(JT808_0x8103_0x0001)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0002, typeof(JT808_0x8103_0x0002)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0003, typeof(JT808_0x8103_0x0003)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0004, typeof(JT808_0x8103_0x0004)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0005, typeof(JT808_0x8103_0x0005)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0006, typeof(JT808_0x8103_0x0006)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0007, typeof(JT808_0x8103_0x0007)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0010, typeof(JT808_0x8103_0x0010)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0011, typeof(JT808_0x8103_0x0011)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0012, typeof(JT808_0x8103_0x0012)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0013, typeof(JT808_0x8103_0x0013)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0014, typeof(JT808_0x8103_0x0014)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0015, typeof(JT808_0x8103_0x0015)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0016, typeof(JT808_0x8103_0x0016)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0017, typeof(JT808_0x8103_0x0017)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0018, typeof(JT808_0x8103_0x0018)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0019, typeof(JT808_0x8103_0x0019)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x001A, typeof(JT808_0x8103_0x001A)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x001B, typeof(JT808_0x8103_0x001B)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x001C, typeof(JT808_0x8103_0x001C)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x001D, typeof(JT808_0x8103_0x001D)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0020, typeof(JT808_0x8103_0x0020)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0021, typeof(JT808_0x8103_0x0021)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0022, typeof(JT808_0x8103_0x0022)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0027, typeof(JT808_0x8103_0x0027)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0028, typeof(JT808_0x8103_0x0028)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0029, typeof(JT808_0x8103_0x0029)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x002C, typeof(JT808_0x8103_0x002C)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x002D, typeof(JT808_0x8103_0x002D)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x002E, typeof(JT808_0x8103_0x002E)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x002F, typeof(JT808_0x8103_0x002F)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0030, typeof(JT808_0x8103_0x0030)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0031, typeof(JT808_0x8103_0x0031)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0040, typeof(JT808_0x8103_0x0040)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0041, typeof(JT808_0x8103_0x0041)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0042, typeof(JT808_0x8103_0x0042)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0043, typeof(JT808_0x8103_0x0043)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0044, typeof(JT808_0x8103_0x0044)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0045, typeof(JT808_0x8103_0x0045)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0046, typeof(JT808_0x8103_0x0046)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0047, typeof(JT808_0x8103_0x0047)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0048, typeof(JT808_0x8103_0x0048)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0049, typeof(JT808_0x8103_0x0049)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0050, typeof(JT808_0x8103_0x0050)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0051, typeof(JT808_0x8103_0x0051)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0052, typeof(JT808_0x8103_0x0052)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0053, typeof(JT808_0x8103_0x0053)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0054, typeof(JT808_0x8103_0x0054)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0055, typeof(JT808_0x8103_0x0055)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0056, typeof(JT808_0x8103_0x0056)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0057, typeof(JT808_0x8103_0x0057)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0058, typeof(JT808_0x8103_0x0058)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0059, typeof(JT808_0x8103_0x0059)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x005A, typeof(JT808_0x8103_0x005A)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x005B, typeof(JT808_0x8103_0x005B)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x005C, typeof(JT808_0x8103_0x005C)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x005D, typeof(JT808_0x8103_0x005D)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x005E, typeof(JT808_0x8103_0x005E)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0064, typeof(JT808_0x8103_0x0064)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0065, typeof(JT808_0x8103_0x0065)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0070, typeof(JT808_0x8103_0x0070)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0071, typeof(JT808_0x8103_0x0081)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0072, typeof(JT808_0x8103_0x0072)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0073, typeof(JT808_0x8103_0x0073)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0074, typeof(JT808_0x8103_0x0074)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0080, typeof(JT808_0x8103_0x0080)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0081, typeof(JT808_0x8103_0x0081)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0082, typeof(JT808_0x8103_0x0082)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0083, typeof(JT808_0x8103_0x0083)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0084, typeof(JT808_0x8103_0x0084)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0090, typeof(JT808_0x8103_0x0090)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0091, typeof(JT808_0x8103_0x0091)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0092, typeof(JT808_0x8103_0x0092)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0093, typeof(JT808_0x8103_0x0093)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0094, typeof(JT808_0x8103_0x0094)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0095, typeof(JT808_0x8103_0x0095)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0100, typeof(JT808_0x8103_0x0100)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0101, typeof(JT808_0x8103_0x0101)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0102, typeof(JT808_0x8103_0x0102)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0103, typeof(JT808_0x8103_0x0103)); - ParamMethods.Add(JT808Constants.JT808_0x8103_0x0110, typeof(JT808_0x8103_0x0110)); + Map = new Dictionary(); + Map.Add(JT808Constants.JT808_0x8103_0x0001, new JT808_0x8103_0x0001()); + Map.Add(JT808Constants.JT808_0x8103_0x0002, new JT808_0x8103_0x0002()); + Map.Add(JT808Constants.JT808_0x8103_0x0003, new JT808_0x8103_0x0003()); + Map.Add(JT808Constants.JT808_0x8103_0x0004, new JT808_0x8103_0x0004()); + Map.Add(JT808Constants.JT808_0x8103_0x0005, new JT808_0x8103_0x0005()); + Map.Add(JT808Constants.JT808_0x8103_0x0006, new JT808_0x8103_0x0006()); + Map.Add(JT808Constants.JT808_0x8103_0x0007, new JT808_0x8103_0x0007()); + Map.Add(JT808Constants.JT808_0x8103_0x0010, new JT808_0x8103_0x0010()); + Map.Add(JT808Constants.JT808_0x8103_0x0011, new JT808_0x8103_0x0011()); + Map.Add(JT808Constants.JT808_0x8103_0x0012, new JT808_0x8103_0x0012()); + Map.Add(JT808Constants.JT808_0x8103_0x0013, new JT808_0x8103_0x0013()); + Map.Add(JT808Constants.JT808_0x8103_0x0014, new JT808_0x8103_0x0014()); + Map.Add(JT808Constants.JT808_0x8103_0x0015, new JT808_0x8103_0x0015()); + Map.Add(JT808Constants.JT808_0x8103_0x0016, new JT808_0x8103_0x0016()); + Map.Add(JT808Constants.JT808_0x8103_0x0017, new JT808_0x8103_0x0017()); + Map.Add(JT808Constants.JT808_0x8103_0x0018, new JT808_0x8103_0x0018()); + Map.Add(JT808Constants.JT808_0x8103_0x0019, new JT808_0x8103_0x0019()); + Map.Add(JT808Constants.JT808_0x8103_0x001A, new JT808_0x8103_0x001A()); + Map.Add(JT808Constants.JT808_0x8103_0x001B, new JT808_0x8103_0x001B()); + Map.Add(JT808Constants.JT808_0x8103_0x001C, new JT808_0x8103_0x001C()); + Map.Add(JT808Constants.JT808_0x8103_0x001D, new JT808_0x8103_0x001D()); + Map.Add(JT808Constants.JT808_0x8103_0x0020, new JT808_0x8103_0x0020()); + Map.Add(JT808Constants.JT808_0x8103_0x0021, new JT808_0x8103_0x0021()); + Map.Add(JT808Constants.JT808_0x8103_0x0022, new JT808_0x8103_0x0022()); + Map.Add(JT808Constants.JT808_0x8103_0x0027, new JT808_0x8103_0x0027()); + Map.Add(JT808Constants.JT808_0x8103_0x0028, new JT808_0x8103_0x0028()); + Map.Add(JT808Constants.JT808_0x8103_0x0029, new JT808_0x8103_0x0029()); + Map.Add(JT808Constants.JT808_0x8103_0x002C, new JT808_0x8103_0x002C()); + Map.Add(JT808Constants.JT808_0x8103_0x002D, new JT808_0x8103_0x002D()); + Map.Add(JT808Constants.JT808_0x8103_0x002E, new JT808_0x8103_0x002E()); + Map.Add(JT808Constants.JT808_0x8103_0x002F, new JT808_0x8103_0x002F()); + Map.Add(JT808Constants.JT808_0x8103_0x0030, new JT808_0x8103_0x0030()); + Map.Add(JT808Constants.JT808_0x8103_0x0031, new JT808_0x8103_0x0031()); + Map.Add(JT808Constants.JT808_0x8103_0x0040, new JT808_0x8103_0x0040()); + Map.Add(JT808Constants.JT808_0x8103_0x0041, new JT808_0x8103_0x0041()); + Map.Add(JT808Constants.JT808_0x8103_0x0042, new JT808_0x8103_0x0042()); + Map.Add(JT808Constants.JT808_0x8103_0x0043, new JT808_0x8103_0x0043()); + Map.Add(JT808Constants.JT808_0x8103_0x0044, new JT808_0x8103_0x0044()); + Map.Add(JT808Constants.JT808_0x8103_0x0045, new JT808_0x8103_0x0045()); + Map.Add(JT808Constants.JT808_0x8103_0x0046, new JT808_0x8103_0x0046()); + Map.Add(JT808Constants.JT808_0x8103_0x0047, new JT808_0x8103_0x0047()); + Map.Add(JT808Constants.JT808_0x8103_0x0048, new JT808_0x8103_0x0048()); + Map.Add(JT808Constants.JT808_0x8103_0x0049, new JT808_0x8103_0x0049()); + Map.Add(JT808Constants.JT808_0x8103_0x0050, new JT808_0x8103_0x0050()); + Map.Add(JT808Constants.JT808_0x8103_0x0051, new JT808_0x8103_0x0051()); + Map.Add(JT808Constants.JT808_0x8103_0x0052, new JT808_0x8103_0x0052()); + Map.Add(JT808Constants.JT808_0x8103_0x0053, new JT808_0x8103_0x0053()); + Map.Add(JT808Constants.JT808_0x8103_0x0054, new JT808_0x8103_0x0054()); + Map.Add(JT808Constants.JT808_0x8103_0x0055, new JT808_0x8103_0x0055()); + Map.Add(JT808Constants.JT808_0x8103_0x0056, new JT808_0x8103_0x0056()); + Map.Add(JT808Constants.JT808_0x8103_0x0057, new JT808_0x8103_0x0057()); + Map.Add(JT808Constants.JT808_0x8103_0x0058, new JT808_0x8103_0x0058()); + Map.Add(JT808Constants.JT808_0x8103_0x0059, new JT808_0x8103_0x0059()); + Map.Add(JT808Constants.JT808_0x8103_0x005A, new JT808_0x8103_0x005A()); + Map.Add(JT808Constants.JT808_0x8103_0x005B, new JT808_0x8103_0x005B()); + Map.Add(JT808Constants.JT808_0x8103_0x005C, new JT808_0x8103_0x005C()); + Map.Add(JT808Constants.JT808_0x8103_0x005D, new JT808_0x8103_0x005D()); + Map.Add(JT808Constants.JT808_0x8103_0x005E, new JT808_0x8103_0x005E()); + Map.Add(JT808Constants.JT808_0x8103_0x0064, new JT808_0x8103_0x0064()); + Map.Add(JT808Constants.JT808_0x8103_0x0065, new JT808_0x8103_0x0065()); + Map.Add(JT808Constants.JT808_0x8103_0x0070, new JT808_0x8103_0x0070()); + Map.Add(JT808Constants.JT808_0x8103_0x0071, new JT808_0x8103_0x0081()); + Map.Add(JT808Constants.JT808_0x8103_0x0072, new JT808_0x8103_0x0072()); + Map.Add(JT808Constants.JT808_0x8103_0x0073, new JT808_0x8103_0x0073()); + Map.Add(JT808Constants.JT808_0x8103_0x0074, new JT808_0x8103_0x0074()); + Map.Add(JT808Constants.JT808_0x8103_0x0080, new JT808_0x8103_0x0080()); + Map.Add(JT808Constants.JT808_0x8103_0x0081, new JT808_0x8103_0x0081()); + Map.Add(JT808Constants.JT808_0x8103_0x0082, new JT808_0x8103_0x0082()); + Map.Add(JT808Constants.JT808_0x8103_0x0083, new JT808_0x8103_0x0083()); + Map.Add(JT808Constants.JT808_0x8103_0x0084, new JT808_0x8103_0x0084()); + Map.Add(JT808Constants.JT808_0x8103_0x0090, new JT808_0x8103_0x0090()); + Map.Add(JT808Constants.JT808_0x8103_0x0091, new JT808_0x8103_0x0091()); + Map.Add(JT808Constants.JT808_0x8103_0x0092, new JT808_0x8103_0x0092()); + Map.Add(JT808Constants.JT808_0x8103_0x0093, new JT808_0x8103_0x0093()); + Map.Add(JT808Constants.JT808_0x8103_0x0094, new JT808_0x8103_0x0094()); + Map.Add(JT808Constants.JT808_0x8103_0x0095, new JT808_0x8103_0x0095()); + Map.Add(JT808Constants.JT808_0x8103_0x0100, new JT808_0x8103_0x0100()); + Map.Add(JT808Constants.JT808_0x8103_0x0101, new JT808_0x8103_0x0101()); + Map.Add(JT808Constants.JT808_0x8103_0x0102, new JT808_0x8103_0x0102()); + Map.Add(JT808Constants.JT808_0x8103_0x0103, new JT808_0x8103_0x0103()); + Map.Add(JT808Constants.JT808_0x8103_0x0110, new JT808_0x8103_0x0110()); } - public IDictionary ParamMethods { get; set; } + public IDictionary Map { get; } + + public void Register(Assembly externalAssembly) + { + var types = externalAssembly.GetTypes().Where(w => w.BaseType == typeof(JT808_0x8103_BodyBase)).ToList(); + foreach (var type in types) + { + var instance = Activator.CreateInstance(type); + uint paramId = 0; + try + { + paramId = (ushort)type.GetProperty(nameof(JT808_0x8103_BodyBase.ParamId)).GetValue(instance); + } + catch (Exception ex) + { + continue; + } + if (Map.ContainsKey(paramId)) + { + throw new ArgumentException($"{type.FullName} {paramId} An element with the same key already exists."); + } + else + { + Map.Add(paramId, instance); + } + } + } + + public IJT808_0x8103_Factory SetMap() where TJT808_0x8103_Body : JT808_0x8103_BodyBase + { + Type type = typeof(TJT808_0x8103_Body); + var instance = Activator.CreateInstance(type); + var paramId = (uint)type.GetProperty(nameof(JT808_0x8103_BodyBase.ParamId)).GetValue(instance); + if (Map.ContainsKey(paramId)) + { + throw new ArgumentException($"{type.FullName} {paramId} An element with the same key already exists."); + } + else + { + Map.Add(paramId, instance); + } + return this; + } } } diff --git a/src/JT808.Protocol/JT808Package.cs b/src/JT808.Protocol/JT808Package.cs index db9e0cd..d109a83 100644 --- a/src/JT808.Protocol/JT808Package.cs +++ b/src/JT808.Protocol/JT808Package.cs @@ -83,8 +83,7 @@ namespace JT808.Protocol // 4.1.判断有无数据体 if (jT808Package.Header.MessageBodyProperty.DataLength > 0) { - object instance = config.MsgIdFactory.GetBodiesImplInstanceByMsgId(jT808Package.Header.MsgId, jT808Package.Header.TerminalPhoneNo); - if (instance != null) + if(config.MsgIdFactory.TryGetValue(jT808Package.Header.MsgId,out object instance)) { if (jT808Package.Header.MessageBodyProperty.IsPackage) { @@ -132,6 +131,9 @@ namespace JT808.Protocol } } } + + + // 5.读取校验码 jT808Package.CheckCode = reader.ReadByte(); // 6.读取终止位置 diff --git a/src/JT808.Protocol/JT808SplitPackageBodies.cs b/src/JT808.Protocol/JT808SplitPackageBodies.cs index fb05c0c..8a12e54 100644 --- a/src/JT808.Protocol/JT808SplitPackageBodies.cs +++ b/src/JT808.Protocol/JT808SplitPackageBodies.cs @@ -12,7 +12,7 @@ namespace JT808.Protocol public byte[] Data { get; set; } - public override ushort MsgId => throw new System.NotImplementedException(); + public override ushort MsgId => throw new System.NotImplementedException("JT808SplitPackageBodies"); public JT808SplitPackageBodies Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { diff --git a/src/JT808.Protocol/MessageBody/JT808_0x0104.cs b/src/JT808.Protocol/MessageBody/JT808_0x0104.cs index 1e1bf37..f518539 100644 --- a/src/JT808.Protocol/MessageBody/JT808_0x0104.cs +++ b/src/JT808.Protocol/MessageBody/JT808_0x0104.cs @@ -34,17 +34,15 @@ namespace JT808.Protocol.MessageBody for (int i = 0; i < jT808_0x0104.AnswerParamsCount; i++) { var paramId = reader.ReadVirtualUInt32();//参数ID - if (config.JT808_0X8103_Factory.ParamMethods.TryGetValue(paramId, out Type type)) + if (config.JT808_0X8103_Factory.Map.TryGetValue(paramId, out object instance)) { if (jT808_0x0104.ParamList != null) { - jT808_0x0104.ParamList.Add(JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize( - config.GetMessagePackFormatterByType(type), ref reader, config)); + jT808_0x0104.ParamList.Add(JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(instance, ref reader, config)); } else { - jT808_0x0104.ParamList = new List { JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize( - config.GetMessagePackFormatterByType(type), ref reader, config) }; + jT808_0x0104.ParamList = new List { JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(instance, ref reader, config) }; } } } diff --git a/src/JT808.Protocol/MessageBody/JT808_0x0200.cs b/src/JT808.Protocol/MessageBody/JT808_0x0200.cs index 40142f1..8a66279 100644 --- a/src/JT808.Protocol/MessageBody/JT808_0x0200.cs +++ b/src/JT808.Protocol/MessageBody/JT808_0x0200.cs @@ -100,7 +100,7 @@ namespace JT808.Protocol.MessageBody jT808_0X0200.GPSTime = reader.ReadDateTime6(); // 位置附加信息 jT808_0X0200.JT808LocationAttachData = new Dictionary(); - jT808_0X0200.JT808CustomLocationAttachOriginalData = new Dictionary(); + jT808_0X0200.JT808CustomLocationAttachData = new Dictionary(); jT808_0X0200.JT808UnknownLocationAttachOriginalData = new Dictionary(); while (reader.ReadCurrentRemainContentLength() > 0) { @@ -109,17 +109,15 @@ namespace JT808.Protocol.MessageBody ReadOnlySpan attachSpan = reader.GetVirtualReadOnlySpan(2); byte attachId = attachSpan[0]; byte attachLen = attachSpan[1]; - if (config.JT808_0X0200_Factory.JT808LocationAttachMethod.TryGetValue(attachId, out Type jT808LocationAttachType)) + if (config.JT808_0X0200_Factory.Map.TryGetValue(attachId, out object jT808LocationAttachInstance)) { - object attachImplObj = config.GetMessagePackFormatterByType(jT808LocationAttachType); - dynamic attachImpl = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(attachImplObj, ref reader, config); + dynamic attachImpl = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(jT808LocationAttachInstance, ref reader, config); jT808_0X0200.JT808LocationAttachData.Add(attachImpl.AttachInfoId, attachImpl); } - else if (config.JT808_0X0200_Custom_Factory.AttachIds.Contains(attachId)) + else if (config.JT808_0X0200_Custom_Factory.Map.TryGetValue(attachId,out object customAttachInstance)) { - reader.Skip(2); - jT808_0X0200.JT808CustomLocationAttachOriginalData.Add(attachId, reader.ReadArray(reader.ReaderCount - 2, attachLen + 2).ToArray()); - reader.Skip(attachLen); + dynamic attachImpl = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(customAttachInstance, ref reader, config); + jT808_0X0200.JT808CustomLocationAttachData.Add(attachImpl.AttachInfoId, attachImpl); } else { @@ -189,10 +187,9 @@ namespace JT808.Protocol.MessageBody { try { - object attachImplObj = config.GetMessagePackFormatterByType(item.Value.GetType()); - JT808MessagePackFormatterResolverExtensions.JT808DynamicSerialize(attachImplObj, ref writer, item.Value, config); + JT808MessagePackFormatterResolverExtensions.JT808DynamicSerialize(item.Value, ref writer, item.Value, config); } - catch + catch(Exception ex) { } @@ -202,8 +199,7 @@ namespace JT808.Protocol.MessageBody { foreach (var item in value.JT808CustomLocationAttachData) { - object attachImplObj = config.GetMessagePackFormatterByType(item.Value.GetType()); - JT808MessagePackFormatterResolverExtensions.JT808DynamicSerialize(attachImplObj, ref writer, item.Value, config); + JT808MessagePackFormatterResolverExtensions.JT808DynamicSerialize(item.Value, ref writer, item.Value, config); } } } diff --git a/src/JT808.Protocol/MessageBody/JT808_0x0200_0x11.cs b/src/JT808.Protocol/MessageBody/JT808_0x0200_0x11.cs index 629bdcd..e43c8a3 100644 --- a/src/JT808.Protocol/MessageBody/JT808_0x0200_0x11.cs +++ b/src/JT808.Protocol/MessageBody/JT808_0x0200_0x11.cs @@ -20,7 +20,7 @@ namespace JT808.Protocol.MessageBody /// 区域或路段 ID /// 若位置类型为 0,无该字段 /// - public int AreaId { get; set; } + public uint AreaId { get; set; } public override byte AttachInfoId { get; set; } = 0x11; public override byte AttachInfoLength { @@ -46,7 +46,7 @@ namespace JT808.Protocol.MessageBody jT808LocationAttachImpl0x11.JT808PositionType = (JT808PositionType)reader.ReadByte(); if (jT808LocationAttachImpl0x11.JT808PositionType != JT808PositionType.无特定位置) { - jT808LocationAttachImpl0x11.AreaId = reader.ReadInt32(); + jT808LocationAttachImpl0x11.AreaId = reader.ReadUInt32(); } return jT808LocationAttachImpl0x11; } @@ -58,7 +58,7 @@ namespace JT808.Protocol.MessageBody writer.WriteByte((byte)value.JT808PositionType); if (value.JT808PositionType != JT808PositionType.无特定位置) { - writer.WriteInt32(value.AreaId); + writer.WriteUInt32(value.AreaId); } } } diff --git a/src/JT808.Protocol/MessageBody/JT808_0x8103.cs b/src/JT808.Protocol/MessageBody/JT808_0x8103.cs index 3eb6f6c..41e9dc5 100644 --- a/src/JT808.Protocol/MessageBody/JT808_0x8103.cs +++ b/src/JT808.Protocol/MessageBody/JT808_0x8103.cs @@ -49,16 +49,14 @@ namespace JT808.Protocol.MessageBody for (int i = 0; i < paramCount; i++) { var paramId = reader.ReadVirtualUInt32();//参数ID - if (config.JT808_0X8103_Factory.ParamMethods.TryGetValue(paramId, out Type type)) + if (config.JT808_0X8103_Factory.Map.TryGetValue(paramId, out object instance)) { - object attachImplObj = config.GetMessagePackFormatterByType(type); - dynamic attachImpl = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(attachImplObj, ref reader, config); + dynamic attachImpl = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(instance, ref reader, config); jT808_0x8103.ParamList.Add(attachImpl); } - else if (config.JT808_0X8103_Custom_Factory.ParamMethods.TryGetValue(paramId, out Type customType)) + else if (config.JT808_0X8103_Custom_Factory.Map.TryGetValue(paramId, out object customInstance)) { - object attachImplObj = config.GetMessagePackFormatterByType(customType); - dynamic attachImpl = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(attachImplObj, ref reader, config); + dynamic attachImpl = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(customInstance, ref reader, config); jT808_0x8103.CustomParamList.Add(attachImpl); } } @@ -77,15 +75,13 @@ namespace JT808.Protocol.MessageBody { foreach (var item in value.ParamList) { - object attachImplObj = config.GetMessagePackFormatterByType(item.GetType()); - JT808MessagePackFormatterResolverExtensions.JT808DynamicSerialize(attachImplObj, ref writer, item, config); + JT808MessagePackFormatterResolverExtensions.JT808DynamicSerialize(item, ref writer, item, config); } if (value.CustomParamList != null) { foreach (var item in value.CustomParamList) { - object attachImplObj = config.GetMessagePackFormatterByType(item.GetType()); - JT808MessagePackFormatterResolverExtensions.JT808DynamicSerialize(attachImplObj, ref writer, item, config); + JT808MessagePackFormatterResolverExtensions.JT808DynamicSerialize(item, ref writer, item, config); } } } diff --git a/src/JT808.Protocol/MessageBody/JT808_0x8900.cs b/src/JT808.Protocol/MessageBody/JT808_0x8900.cs index 25854e9..8d315e2 100644 --- a/src/JT808.Protocol/MessageBody/JT808_0x8900.cs +++ b/src/JT808.Protocol/MessageBody/JT808_0x8900.cs @@ -37,8 +37,7 @@ namespace JT808.Protocol.MessageBody public void Serialize(ref JT808MessagePackWriter writer, JT808_0x8900 value, IJT808Config config) { writer.WriteByte(value.PassthroughType); - object obj = config.GetMessagePackFormatterByType(value.JT808_0X8900_BodyBase.GetType()); - JT808MessagePackFormatterResolverExtensions.JT808DynamicSerialize(obj, ref writer, value.JT808_0X8900_BodyBase, config); + JT808MessagePackFormatterResolverExtensions.JT808DynamicSerialize(value.JT808_0X8900_BodyBase, ref writer, value.JT808_0X8900_BodyBase, config); } } } diff --git a/src/JT808.Protocol/MessagePack/JT808MessagePackReader.cs b/src/JT808.Protocol/MessagePack/JT808MessagePackReader.cs index 5851cbf..7bfe262 100644 --- a/src/JT808.Protocol/MessagePack/JT808MessagePackReader.cs +++ b/src/JT808.Protocol/MessagePack/JT808MessagePackReader.cs @@ -213,9 +213,7 @@ namespace JT808.Protocol.MessagePack } public string ReadRemainStringContent() { - var readOnlySpan = ReadContent(0); - string value = JT808Constants.Encoding.GetString(readOnlySpan.ToArray()); - return value.Trim('\0'); + return ReadString(ReadCurrentRemainContentLength()); } public string ReadHex(int len) {