diff --git a/src/JT808.Protocol.Test/MessageBody/JT808_0x8105Test.cs b/src/JT808.Protocol.Test/MessageBody/JT808_0x8105Test.cs index 6a2a5b3..ea3c5a5 100644 --- a/src/JT808.Protocol.Test/MessageBody/JT808_0x8105Test.cs +++ b/src/JT808.Protocol.Test/MessageBody/JT808_0x8105Test.cs @@ -6,108 +6,119 @@ using static JT808.Protocol.MessageBody.JT808_0x8105; namespace JT808.Protocol.Test.MessageBody { - //todo:JT808_0x8105Test public class JT808_0x8105Test { - //JT808Serializer JT808Serializer = new JT808Serializer(); - //[Fact] - //public void Test1() - //{ - // JT808Package jT808Package = new JT808Package - // { - // Header = new JT808Header - // { - // MsgId = Enums.JT808MsgId.终端控制.ToUInt16Value(), - // ManualMsgNum = 1, - // TerminalPhoneNo = "12345678900", - // }, - // Bodies = new JT808_0x8105 - // { - // CommandWord = 1, - // //CommandValue = new CommandParams - // //{ - // // ConnectionControl = 1, - // // DialPointName = "TKName", - // // DialUserName = "TK", - // // DialPwd = "TK123", - // // FirmwareVersion = "1.0", - // // HardwareVersion = "2.0", - // // ConnectTimeLimit = 60, - // // MakerId = "12345", - // // MonitoringPlatformAuthenticationCode = "code", - // // ServerUrl = "www.baidu.com", - // // TCPPort = 8806, - // // UDPPort = 3306, - // // URL = "www.TK.com" - // //} - // } - // }; - // var hex = JT808Serializer.Serialize(jT808Package).ToHexString(); - // Assert.Equal("7E8105004B012345678900000101313B544B4E616D653B544B3B544B3132333B7777772E62616964752E636F6D3B383830363B333330363B31323334353B636F64653B322E303B312E303B7777772E544B2E636F6D3B3630227E", hex); - //} + JT808Serializer JT808Serializer = new JT808Serializer(); + [Fact] + public void Test1() + { + JT808Package jT808Package = new JT808Package + { + Header = new JT808Header + { + MsgId = Enums.JT808MsgId.终端控制.ToUInt16Value(), + ManualMsgNum = 1, + TerminalPhoneNo = "12345678900", + }, + Bodies = new JT808_0x8105 + { + CommandWord = 1, + CommandParameters=new System.Collections.Generic.List + { + new JT808_0x8105.ConnectionControlCommandParameter{ Value=1}, + new JT808_0x8105.DialPointNameCommandParameter{ Value="TKName"}, + new JT808_0x8105.DialUserNameCommandParameter{ Value="TK"}, + new JT808_0x8105.DialPwdCommandParameter{ Value="TK123"}, + new JT808_0x8105.FirmwareVersionCommandParameter{ Value="1.0"}, + new JT808_0x8105.HardwareVersionCommandParameter{ Value="2.0"}, + new JT808_0x8105.ConnectTimeLimitCommandParameter{ Value=60}, + new JT808_0x8105.MakerIdCommandParameter{ Value="12345"}, + new JT808_0x8105.MonitorPlatformAuthCodeCommandParameter{ Value="code"}, + new JT808_0x8105.ServerUrlCommandParameter{ Value="www.baidu.com"}, + new JT808_0x8105.TcpPortCommandParameter{ Value=8806}, + new JT808_0x8105.UdpPortCommandParameter{ Value=3306}, + new JT808_0x8105.UrlCommandParameter{ Value= "www.TK.com"} + } + } + }; + var hex = JT808Serializer.Serialize(jT808Package).ToHexString(); + Assert.Equal("7E81050048012345678900000101013B544B4E616D653B544B3B544B3132333B7777772E62616964752E636F6D3B22663B0CEA3B31323334353B636F64653B322E303B312E303B7777772E544B2E636F6D3B003C3BB27E", hex); + } - //[Fact] - //public void Test1_1() - //{ - // var bytes = "7E8105004B012345678900000101313B544B4E616D653B544B3B544B3132333B7777772E62616964752E636F6D3B383830363B333330363B31323334353B636F64653B322E303B312E303B7777772E544B2E636F6D3B3630227E".ToHexBytes(); - // JT808Package jT808Package = JT808Serializer.Deserialize(bytes); - // Assert.Equal(Enums.JT808MsgId.终端控制.ToUInt16Value(), jT808Package.Header.MsgId); - // Assert.Equal(1, jT808Package.Header.MsgNum); - // Assert.Equal("12345678900", jT808Package.Header.TerminalPhoneNo); + [Fact] + public void Test1_1() + { + var bytes = "7E81050048012345678900000101013B544B4E616D653B544B3B544B3132333B7777772E62616964752E636F6D3B22663B0CEA3B31323334353B636F64653B322E303B312E303B7777772E544B2E636F6D3B003C3BB27E".ToHexBytes(); + JT808Package jT808Package = JT808Serializer.Deserialize(bytes); + Assert.Equal(Enums.JT808MsgId.终端控制.ToUInt16Value(), jT808Package.Header.MsgId); + Assert.Equal(1, jT808Package.Header.MsgNum); + Assert.Equal("12345678900", jT808Package.Header.TerminalPhoneNo); + var JT808_0x8105 = (JT808_0x8105)jT808Package.Bodies; + Assert.Equal(1, JT808_0x8105.CommandWord); + Assert.Equal("www.TK.com", JT808_0x8105.CommandParameters.GetCommandParameter().Value); + Assert.Equal(3306u, JT808_0x8105.CommandParameters.GetCommandParameter().Value.Value); + Assert.Equal(8806, JT808_0x8105.CommandParameters.GetCommandParameter().Value.Value); + Assert.Equal("www.baidu.com", JT808_0x8105.CommandParameters.GetCommandParameter().Value); + Assert.Equal("code", JT808_0x8105.CommandParameters.GetCommandParameter().Value); + Assert.Equal("12345", JT808_0x8105.CommandParameters.GetCommandParameter().Value); + Assert.Equal("2.0", JT808_0x8105.CommandParameters.GetCommandParameter().Value); + Assert.Equal("1.0", JT808_0x8105.CommandParameters.GetCommandParameter().Value); + Assert.Equal("TK", JT808_0x8105.CommandParameters.GetCommandParameter().Value); + Assert.Equal("TK123",JT808_0x8105.CommandParameters.GetCommandParameter().Value); + Assert.Equal("TKName", JT808_0x8105.CommandParameters.GetCommandParameter().Value); + Assert.Equal(1, JT808_0x8105.CommandParameters.GetCommandParameter().Value.Value); + Assert.Equal(60, JT808_0x8105.CommandParameters.GetCommandParameter().Value.Value); + } - // var JT808_0x8105 = (JT808_0x8105)jT808Package.Bodies; - // Assert.Equal(1, JT808_0x8105.CommandWord); - // Assert.Equal("www.TK.com", JT808_0x8105.CommandValue.URL); - // Assert.Equal(3306, (int)JT808_0x8105.CommandValue.UDPPort); - // Assert.Equal(8806, (int)JT808_0x8105.CommandValue.TCPPort); - // Assert.Equal("www.baidu.com", JT808_0x8105.CommandValue.ServerUrl); - // Assert.Equal("code", JT808_0x8105.CommandValue.MonitoringPlatformAuthenticationCode); - // Assert.Equal("12345", JT808_0x8105.CommandValue.MakerId); - // Assert.Equal("2.0", JT808_0x8105.CommandValue.HardwareVersion); - // Assert.Equal("1.0", JT808_0x8105.CommandValue.FirmwareVersion); - // Assert.Equal("TK", JT808_0x8105.CommandValue.DialUserName); - // Assert.Equal("TK123", JT808_0x8105.CommandValue.DialPwd); - // Assert.Equal("TKName", JT808_0x8105.CommandValue.DialPointName); - // Assert.Equal(1, (byte)JT808_0x8105.CommandValue.ConnectionControl); - // Assert.Equal(60, (UInt16)JT808_0x8105.CommandValue.ConnectTimeLimit); - //} + [Fact] + public void Test1_2() + { + var bytes = "7E81050048012345678900000101013B544B4E616D653B544B3B544B3132333B7777772E62616964752E636F6D3B22663B0CEA3B31323334353B636F64653B322E303B312E303B7777772E544B2E636F6D3B003C3BB27E".ToHexBytes(); + string json = JT808Serializer.Analyze(bytes); + } - //[Fact] - //public void Test1_2() - //{ - // var bytes = "7E8105004B012345678900000101313B544B4E616D653B544B3B544B3132333B7777772E62616964752E636F6D3B383830363B333330363B31323334353B636F64653B322E303B312E303B7777772E544B2E636F6D3B3630227E".ToHexBytes(); - // string json = JT808Serializer.Analyze(bytes); - //} + [Fact] + public void Test1_3() + { + var bytes = "7E81050048012345678900000101013B544B4E616D653B544B3B544B3132333B7777772E62616964752E636F6D3B22663B0CEA3B31323334353B636F64653B322E303B312E303B7777772E544B2E636F6D3B003C3BB27E".ToHexBytes(); + JT808Package jT808Package = JT808Serializer.Deserialize(bytes); + Assert.Equal(Enums.JT808MsgId.终端控制.ToUInt16Value(), jT808Package.Header.MsgId); + Assert.Equal(1, jT808Package.Header.MsgNum); + Assert.Equal("12345678900", jT808Package.Header.TerminalPhoneNo); + var JT808_0x8105 = (JT808_0x8105)jT808Package.Bodies; + Assert.Equal(1, JT808_0x8105.CommandWord); + var valueString = JT808_0x8105.CommandParameters.GetCommandParameter().ToValueString(); + } - //[Fact] - //public void Test2() - //{ - // JT808Package jT808Package = new JT808Package(); - // jT808Package.Header = new JT808Header - // { - // MsgId = Enums.JT808MsgId.终端控制.ToUInt16Value(), - // ManualMsgNum = 1, - // TerminalPhoneNo = "12345678900", - // }; - // jT808Package.Bodies = new JT808_0x8105 - // { - // CommandWord = 5 - // }; - // var hex = JT808Serializer.Serialize(jT808Package).ToHexString(); - // Assert.Equal("7E81050001012345678900000105087E", hex); - //} + [Fact] + public void Test2() + { + JT808Package jT808Package = new JT808Package(); + jT808Package.Header = new JT808Header + { + MsgId = Enums.JT808MsgId.终端控制.ToUInt16Value(), + ManualMsgNum = 1, + TerminalPhoneNo = "12345678900", + }; + jT808Package.Bodies = new JT808_0x8105 + { + CommandWord = 5 + }; + var hex = JT808Serializer.Serialize(jT808Package).ToHexString(); + Assert.Equal("7E81050001012345678900000105087E", hex); + } - //[Fact] - //public void Test2_1() - //{ - // var bytes = "7E81050001012345678900000105087E".ToHexBytes(); - // JT808Package jT808Package = JT808Serializer.Deserialize(bytes); - // Assert.Equal(Enums.JT808MsgId.终端控制.ToUInt16Value(), jT808Package.Header.MsgId); - // Assert.Equal(1, jT808Package.Header.MsgNum); - // Assert.Equal("12345678900", jT808Package.Header.TerminalPhoneNo); + [Fact] + public void Test2_1() + { + var bytes = "7E81050001012345678900000105087E".ToHexBytes(); + JT808Package jT808Package = JT808Serializer.Deserialize(bytes); + Assert.Equal(Enums.JT808MsgId.终端控制.ToUInt16Value(), jT808Package.Header.MsgId); + Assert.Equal(1, jT808Package.Header.MsgNum); + Assert.Equal("12345678900", jT808Package.Header.TerminalPhoneNo); - // var JT808_0x8105 = (JT808_0x8105)jT808Package.Bodies; - // Assert.Equal(5, JT808_0x8105.CommandWord); - //} + var JT808_0x8105 = (JT808_0x8105)jT808Package.Bodies; + Assert.Equal(5, JT808_0x8105.CommandWord); + } } } diff --git a/src/JT808.Protocol/Extensions/JT808_0x8105_CommandParameterExtensions.cs b/src/JT808.Protocol/Extensions/JT808_0x8105_CommandParameterExtensions.cs new file mode 100644 index 0000000..010e08f --- /dev/null +++ b/src/JT808.Protocol/Extensions/JT808_0x8105_CommandParameterExtensions.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using static JT808.Protocol.MessageBody.JT808_0x8105; + +namespace JT808.Protocol.Extensions +{ + /// + /// 终端控制 命令参数扩展 + /// + public static class JT808_0x8105_CommandParameterExtensions + { + /// + /// 创建标准命令参数 + /// + /// + /// + public static ICommandParameter CreateCommandParameter(in int order) + { + ICommandParameter commandParameter = default; + switch (order) + { + case 0: + commandParameter = new ConnectionControlCommandParameter(); + break; + case 1: + commandParameter = new DialPointNameCommandParameter(); + break; + case 2: + commandParameter = new DialUserNameCommandParameter(); + break; + case 3: + commandParameter = new DialPwdCommandParameter(); + break; + case 4: + commandParameter = new ServerUrlCommandParameter(); + break; + case 5: + commandParameter = new TcpPortCommandParameter(); + break; + case 6: + commandParameter = new UdpPortCommandParameter(); + break; + case 7: + commandParameter = new MakerIdCommandParameter(); + break; + case 8: + commandParameter = new MonitorPlatformAuthCodeCommandParameter(); + break; + case 9: + commandParameter = new HardwareVersionCommandParameter(); + break; + case 10: + commandParameter = new FirmwareVersionCommandParameter(); + break; + case 11: + commandParameter = new UrlCommandParameter(); + break; + case 12: + commandParameter = new ConnectTimeLimitCommandParameter(); + break; + } + return commandParameter; + } + + /// + /// 获取-连接控制 + /// + /// + /// + public static TICommandParameter GetCommandParameter(this List CommandParameters) + where TICommandParameter : ICommandParameter + { + var value = CommandParameters.FirstOrDefault(f => f.GetType() == typeof(TICommandParameter)); + return (TICommandParameter)value; + } + + /// + /// 获取-连接控制 + /// + /// + /// + public static string ToValueString(this ICommandParameter commandParameter) + { + var name = nameof(ICommandParameterValue.Value); + var property = commandParameter.GetType().GetProperty(name); + if (property == null) return default; + var value= property.GetValue(commandParameter); + if (value == null) return default; + return value.ToString(); + } + } +} diff --git a/src/JT808.Protocol/JT808.Protocol.xml b/src/JT808.Protocol/JT808.Protocol.xml index 020dc6b..a1f9d9c 100644 --- a/src/JT808.Protocol/JT808.Protocol.xml +++ b/src/JT808.Protocol/JT808.Protocol.xml @@ -3767,6 +3767,32 @@ + + + 终端控制 命令参数扩展 + + + + + 创建标准命令参数 + + + + + + + 获取-连接控制 + + + + + + + 获取-连接控制 + + + + JT808配置扩展 @@ -15326,9 +15352,14 @@ - + + + 命令参数值 + + + - 将命令值转为描述 + 对应参数值 @@ -15353,7 +15384,7 @@ 连接控制 - + 连接控制 0:切换到指定监管平台服务器,连接到该服务器后即进入应急状态,此状态下仅有下发控制指令的监管平台可发送包括短信在内的控制指令; @@ -15372,12 +15403,6 @@ - - - - - - 拨号点名称 @@ -15393,7 +15418,7 @@ 拨号点名称 - + 拨号点名称 一般为服务器 APN,无线通信拨号访问点,若网络制式为 CDMA,则该值为 PPP 连接拨号号码 @@ -15411,12 +15436,6 @@ - - - - - - 拨号用户名 @@ -15433,7 +15452,7 @@ 拨号用户名 - + 拨号用户名 服务器无线通信拨号用户名 @@ -15451,12 +15470,6 @@ - - - - - - 拨号密码 @@ -15473,7 +15486,7 @@ 拨号密码 - + 拨号密码 服务器无线通信拨号密码 @@ -15491,12 +15504,6 @@ - - - - - - 服务器地址;IP 或域名 @@ -15512,7 +15519,7 @@ 服务器地址 - + 服务器地址 IP或域名 @@ -15529,12 +15536,6 @@ - - - - - - Tcp端口 @@ -15550,7 +15551,7 @@ 连接控制 - + Tcp端口 @@ -15567,12 +15568,6 @@ - - - - - - Udp端口 @@ -15588,7 +15583,7 @@ 连接控制 - + Udp端口 @@ -15605,12 +15600,6 @@ - - - - - - 制造商ID @@ -15626,7 +15615,7 @@ 服务器地址 - + 制造商ID @@ -15643,12 +15632,6 @@ - - - - - - 监管平台鉴权码 @@ -15665,7 +15648,7 @@ 监管平台鉴权码 - + 监管平台鉴权码 @@ -15682,12 +15665,6 @@ - - - - - - 硬件版本 @@ -15704,7 +15681,7 @@ 硬件版本 - + 硬件版本 终端的硬件版本号,由制造商自定 @@ -15722,12 +15699,6 @@ - - - - - - 固件版本 @@ -15744,7 +15715,7 @@ 固件版本 - + 固件版本 终端的硬件版本号,由制造商自定 @@ -15762,15 +15733,9 @@ - - - - - - - URL地址完整URL地址 + URL的完整地址 @@ -15780,12 +15745,12 @@ - URL地址完整URL地址 + URL的完整地址 - + - URL地址完整URL地址 + URL的完整地址 @@ -15800,12 +15765,6 @@ - - - - - - 连接到指定服务器时限 @@ -15821,7 +15780,7 @@ 连接控制 - + 连接到指定服务器时限 单位:分(min),值非 0 后的有效期截止前,终端应连回原地址。 @@ -15840,12 +15799,6 @@ - - - - - - 查询指定终端参数 diff --git a/src/JT808.Protocol/MessageBody/JT808_0x8105.cs b/src/JT808.Protocol/MessageBody/JT808_0x8105.cs index f922cab..b3cad24 100644 --- a/src/JT808.Protocol/MessageBody/JT808_0x8105.cs +++ b/src/JT808.Protocol/MessageBody/JT808_0x8105.cs @@ -72,17 +72,18 @@ namespace JT808.Protocol.MessageBody while (true) { var index = commandValueBuffer.IndexOf(CommandParameterSeparatorValue); - if (index <= 0) break; - if (index == 1) + if (commandValueBuffer.Length <= 0) break; + if (index == 0) { commandParameters.Add(null); + commandValueBuffer = commandValueBuffer.Slice(index+1); } else { var value = commandValueBuffer.Slice(0, index); commandParameters.Add(value.ToArray()); + commandValueBuffer = commandValueBuffer.Slice(index+1); } - commandValueBuffer = commandValueBuffer.Slice(index); } for (int i = 0; i < commandParameters.Count; i++) { @@ -99,13 +100,13 @@ namespace JT808.Protocol.MessageBody } else { - UnknownCommandParameters.Add(i, cmd); + jT808_0x8105.UnknownCommandParameters.Add(i, cmd); } } else { //读取标准的命令参数 - ICommandParameter commandParameter = CommandParameterFactory(i); + ICommandParameter commandParameter = JT808_0x8105_CommandParameterExtensions.CreateCommandParameter(i); if (commandParameter != null) { commandParameter.ToValue(cmd); @@ -190,17 +191,18 @@ namespace JT808.Protocol.MessageBody while (true) { var index = commandValueBuffer.IndexOf(CommandParameterSeparatorValue); - if (index <= 0) break; - if (index == 1) + if (commandValueBuffer.Length <= 0) break; + if (index == 0) { commandParameters.Add(null); + commandValueBuffer = commandValueBuffer.Slice(index + 1); } else { var value = commandValueBuffer.Slice(0, index); commandParameters.Add(value.ToArray()); + commandValueBuffer = commandValueBuffer.Slice(index + 1); } - commandValueBuffer = commandValueBuffer.Slice(index); } writer.WriteStartObject("命令参数对象"); for (int i = 0; i < commandParameters.Count; i++) @@ -214,7 +216,7 @@ namespace JT808.Protocol.MessageBody { var commandParameter = (ICommandParameter)Activator.CreateInstance(type); commandParameter.ToValue(cmd); - writer.WriteString($"[{cmd.ToHexString()}]{commandParameter.CommandName}", commandParameter.ToDescription()); + writer.WriteString($"[{cmd.ToHexString()}]{commandParameter.CommandName}", commandParameter.ToValueString()); } else { @@ -224,194 +226,17 @@ namespace JT808.Protocol.MessageBody else { //读取标准的命令参数 - ICommandParameter commandParameter = CommandParameterFactory(i); + ICommandParameter commandParameter = JT808_0x8105_CommandParameterExtensions.CreateCommandParameter(i); if (commandParameter != null) { commandParameter.ToValue(cmd); - writer.WriteString($"[{cmd.ToHexString()}]{commandParameter.CommandName}", commandParameter.ToDescription()); + writer.WriteString($"[{cmd.ToHexString()}]{commandParameter.CommandName}", commandParameter.ToValueString()); } } } writer.WriteEndObject(); } } - - - //if (value.CommandWord == 1 || value.CommandWord == 2) - //{ - // value.CommandValue = new CommandParams(); - // var commandValueBuffer = reader.ReadVirtualArray(reader.ReadCurrentRemainContentLength()).ToArray(); - // string commandValue=reader.ReadRemainStringContent(); - // writer.WriteString($"[{ commandValueBuffer.ToHexString()}]命令参数", commandValue); - // writer.WriteStartObject("命令参数对象"); - // var values = commandValue.Split(';'); - // if (!string.IsNullOrEmpty(values[0])) - // { - // var connectionControl = byte.Parse(values[0]); - // writer.WriteNumber("连接控制", connectionControl); - // } - // else - // { - // writer.WriteNull("连接控制"); - // } - // if (!string.IsNullOrEmpty(values[1])) - // { - // var dialPointName = values[1]; - // writer.WriteString("拨号点名称", dialPointName); - // } - // else - // { - // writer.WriteNull("拨号点名称"); - // } - // if (!string.IsNullOrEmpty(values[2])) - // { - // var dialUserName = values[2]; - // writer.WriteString("拨号用户名", dialUserName); - // } - // else - // { - // writer.WriteNull("拨号用户名"); - // } - // if (!string.IsNullOrEmpty(values[3])) - // { - // var dialPwd = values[3]; - // writer.WriteString("拨号密码", dialPwd); - // } - // else - // { - // writer.WriteNull("拨号密码"); - // } - // if (!string.IsNullOrEmpty(values[4])) - // { - // var serverUrl = values[4]; - // writer.WriteString("服务器地址", serverUrl); - // } - // else - // { - // writer.WriteNull("服务器地址"); - // } - // if (!string.IsNullOrEmpty(values[5])) - // { - // var tcpPort = ushort.Parse(values[5]); - // writer.WriteNumber("TCP端口", tcpPort); - // } - // else - // { - // writer.WriteNull("TCP端口"); - // } - // if (!string.IsNullOrEmpty(values[6])) - // { - // var udpPort = ushort.Parse(values[6]); - // writer.WriteNumber("UDP端口", udpPort); - // } - // else - // { - // writer.WriteNull("UDP端口"); - // } - // if (!string.IsNullOrEmpty(values[7])) - // { - // var manufacturerCode = long.Parse(values[7]); - // writer.WriteNumber("制造商ID", manufacturerCode); - // } - // else - // { - // writer.WriteNull("制造商ID"); - // } - // if (!string.IsNullOrEmpty(values[8])) - // { - // var monitoringPlatformAuthenticationCode = values[8]; - // writer.WriteString("监管平台鉴权码", monitoringPlatformAuthenticationCode); - // } - // else - // { - // writer.WriteNull("监管平台鉴权码"); - // } - // if (!string.IsNullOrEmpty(values[9])) - // { - // var hardwareVersion = values[9]; - // writer.WriteString("硬件版本号", hardwareVersion); - // } - // else - // { - // writer.WriteNull("硬件版本号"); - // } - // if (!string.IsNullOrEmpty(values[10])) - // { - // var firmwareVersion = values[10]; - // writer.WriteString("固件版本号", firmwareVersion); - // } - // else - // { - // writer.WriteNull("固件版本号"); - // } - // if (!string.IsNullOrEmpty(values[11])) - // { - // var url = values[11]; - // writer.WriteString("URL地址", url); - // } - // else - // { - // writer.WriteNull("URL地址"); - // } - // if (!string.IsNullOrEmpty(values[12])) - // { - // var connectTimeLimit = ushort.Parse(values[12]); - // writer.WriteNumber("连接到指定服务器时限", connectTimeLimit); - // } - // else - // { - // writer.WriteNull("连接到指定服务器时限"); - // } - // writer.WriteEndObject(); - //} - } - - private ICommandParameter CommandParameterFactory(in int order) - { - ICommandParameter commandParameter=default; - switch (order) - { - case 0: - commandParameter = new ConnectionControlCommandParameter(); - break; - case 1: - commandParameter = new DialPointNameCommandParameter(); - break; - case 2: - commandParameter = new DialUserNameCommandParameter(); - break; - case 3: - commandParameter = new DialPwdCommandParameter(); - break; - case 4: - commandParameter = new ServerUrlCommandParameter(); - break; - case 5: - commandParameter = new TcpPortCommandParameter(); - break; - case 6: - commandParameter = new UdpPortCommandParameter(); - break; - case 7: - commandParameter = new MakerIdCommandParameter(); - break; - case 8: - commandParameter = new MonitorPlatformAuthCodeCommandParameter(); - break; - case 9: - commandParameter = new HardwareVersionCommandParameter(); - break; - case 10: - commandParameter = new FirmwareVersionCommandParameter(); - break; - case 11: - commandParameter = new UrlCommandParameter(); - break; - case 12: - commandParameter = new ConnectTimeLimitCommandParameter(); - break; - } - return commandParameter; } #region 命令参数 @@ -438,10 +263,16 @@ namespace JT808.Protocol.MessageBody /// /// void ToValue(byte[] bytes); + } + /// + /// 命令参数值 + /// + public interface ICommandParameterValue + { /// - /// 将命令值转为描述 + /// 对应参数值 /// - string ToDescription(); + TValue Value { get; set; } } /// /// 自定义命令参数接口 @@ -455,7 +286,7 @@ namespace JT808.Protocol.MessageBody /// 0:切换到指定监管平台服务器,连接到该服务器后即进入应急状态,此状态下仅有下发控制指令的监管平台可发送包括短信在内的控制指令; /// 1:切换回原缺省监控平台服务器,并恢复正常状态。 /// - public class ConnectionControlCommandParameter : ICommandParameter + public class ConnectionControlCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 0 @@ -470,15 +301,15 @@ namespace JT808.Protocol.MessageBody /// 0:切换到指定监管平台服务器,连接到该服务器后即进入应急状态,此状态下仅有下发控制指令的监管平台可发送包括短信在内的控制指令; /// 1:切换回原缺省监控平台服务器,并恢复正常状态。 /// - public byte? ConnectionControl { get; set; } + public byte? Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (ConnectionControl.HasValue) return default; - return new byte[1] { ConnectionControl.Value }; + if (!Value.HasValue) return default; + return new byte[1] { Value.Value }; } /// /// @@ -488,22 +319,14 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - ConnectionControl = bytes[0]; + Value = bytes[0]; } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{(ConnectionControl.HasValue ? "" : ConnectionControl.Value)}"; - } } /// /// 拨号点名称 /// - public class DialPointNameCommandParameter : ICommandParameter + public class DialPointNameCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 1 @@ -517,15 +340,15 @@ namespace JT808.Protocol.MessageBody /// 拨号点名称 /// 一般为服务器 APN,无线通信拨号访问点,若网络制式为 CDMA,则该值为 PPP 连接拨号号码 /// - public string DialPointName { get; set; } + public string Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (string.IsNullOrEmpty(DialPointName)) return default; - return JT808Constants.Encoding.GetBytes(DialPointName); + if (string.IsNullOrEmpty(Value)) return default; + return JT808Constants.Encoding.GetBytes(Value); } /// /// @@ -535,23 +358,15 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - DialPointName = JT808Constants.Encoding.GetString(bytes); + Value = JT808Constants.Encoding.GetString(bytes); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{DialPointName ?? ""}"; - } } /// /// 拨号用户名 /// 服务器无线通信拨号用户名 /// - public class DialUserNameCommandParameter : ICommandParameter + public class DialUserNameCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 2 @@ -565,15 +380,15 @@ namespace JT808.Protocol.MessageBody /// 拨号用户名 /// 服务器无线通信拨号用户名 /// - public string DialUserName { get; set; } + public string Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (string.IsNullOrEmpty(DialUserName)) return default; - return JT808Constants.Encoding.GetBytes(DialUserName); + if (string.IsNullOrEmpty(Value)) return default; + return JT808Constants.Encoding.GetBytes(Value); } /// /// @@ -583,23 +398,15 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - DialUserName = JT808Constants.Encoding.GetString(bytes); + Value = JT808Constants.Encoding.GetString(bytes); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{DialUserName ?? ""}"; - } } /// /// 拨号密码 /// 服务器无线通信拨号密码 /// - public class DialPwdCommandParameter : ICommandParameter + public class DialPwdCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 3 @@ -613,15 +420,15 @@ namespace JT808.Protocol.MessageBody /// 拨号密码 /// 服务器无线通信拨号密码 /// - public string DialPwd { get; set; } + public string Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (string.IsNullOrEmpty(DialPwd)) return default; - return JT808Constants.Encoding.GetBytes(DialPwd); + if (string.IsNullOrEmpty(Value)) return default; + return JT808Constants.Encoding.GetBytes(Value); } /// /// @@ -631,22 +438,14 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - DialPwd = JT808Constants.Encoding.GetString(bytes); + Value = JT808Constants.Encoding.GetString(bytes); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{DialPwd ?? ""}"; - } } /// /// 服务器地址;IP 或域名 /// - public class ServerUrlCommandParameter : ICommandParameter + public class ServerUrlCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 4 @@ -659,15 +458,15 @@ namespace JT808.Protocol.MessageBody /// /// 服务器地址 IP或域名 /// - public string ServerUrl { get; set; } + public string Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (string.IsNullOrEmpty(ServerUrl)) return default; - return JT808Constants.Encoding.GetBytes(ServerUrl); + if (string.IsNullOrEmpty(Value)) return default; + return JT808Constants.Encoding.GetBytes(Value); } /// /// @@ -677,22 +476,14 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - ServerUrl = JT808Constants.Encoding.GetString(bytes); + Value = JT808Constants.Encoding.GetString(bytes); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{ServerUrl ?? ""}"; - } } /// /// Tcp端口 /// - public class TcpPortCommandParameter : ICommandParameter + public class TcpPortCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 5 @@ -705,16 +496,16 @@ namespace JT808.Protocol.MessageBody /// /// Tcp端口 /// - public ushort? TcpPort { get; set; } + public ushort? Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (TcpPort.HasValue) return default; + if (!Value.HasValue) return default; var value = new byte[2]; - BinaryPrimitives.WriteUInt16BigEndian(value, TcpPort.Value); + BinaryPrimitives.WriteUInt16BigEndian(value, Value.Value); return value; } /// @@ -725,22 +516,14 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - TcpPort = BinaryPrimitives.ReadUInt16BigEndian(bytes); + Value = BinaryPrimitives.ReadUInt16BigEndian(bytes); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{(TcpPort.HasValue ? "" : TcpPort.Value)}"; - } } /// /// Udp端口 /// - public class UdpPortCommandParameter : ICommandParameter + public class UdpPortCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 6 @@ -753,16 +536,16 @@ namespace JT808.Protocol.MessageBody /// /// Udp端口 /// - public ushort? UdpPort { get; set; } + public ushort? Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (UdpPort.HasValue) return default; + if (!Value.HasValue) return default; var value = new byte[2]; - BinaryPrimitives.WriteUInt16BigEndian(value, UdpPort.Value); + BinaryPrimitives.WriteUInt16BigEndian(value, Value.Value); return value; } /// @@ -773,22 +556,14 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - UdpPort = BinaryPrimitives.ReadUInt16BigEndian(bytes); + Value = BinaryPrimitives.ReadUInt16BigEndian(bytes); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{(UdpPort.HasValue ? "" : UdpPort.Value)}"; - } } /// /// 制造商ID /// - public class MakerIdCommandParameter : ICommandParameter + public class MakerIdCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 7 @@ -801,15 +576,15 @@ namespace JT808.Protocol.MessageBody /// /// 制造商ID /// - public string MakerId { get; set; } + public string Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (string.IsNullOrEmpty(MakerId)) return default; - return JT808Constants.Encoding.GetBytes(MakerId); + if (string.IsNullOrEmpty(Value)) return default; + return JT808Constants.Encoding.GetBytes(Value.PadRight(5, '\0')); } /// /// @@ -819,23 +594,15 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - MakerId = JT808Constants.Encoding.GetString(bytes); + Value = JT808Constants.Encoding.GetString(bytes).Trim('\0'); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{MakerId ?? ""}"; - } } /// /// 监管平台鉴权码 /// 监管平台下发的鉴权码,仅用于终端连接到监管平台之后的鉴权,终端连接回原监控平台还用原鉴权码 /// - public class MonitorPlatformAuthCodeCommandParameter : ICommandParameter + public class MonitorPlatformAuthCodeCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 8 @@ -848,15 +615,15 @@ namespace JT808.Protocol.MessageBody /// /// 监管平台鉴权码 /// - public string MonitorPlatformAuthCode { get; set; } + public string Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (string.IsNullOrEmpty(MonitorPlatformAuthCode)) return default; - return JT808Constants.Encoding.GetBytes(MonitorPlatformAuthCode); + if (string.IsNullOrEmpty(Value)) return default; + return JT808Constants.Encoding.GetBytes(Value); } /// /// @@ -866,23 +633,15 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - MonitorPlatformAuthCode = JT808Constants.Encoding.GetString(bytes); + Value = JT808Constants.Encoding.GetString(bytes); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{MonitorPlatformAuthCode ?? ""}"; - } } /// /// 硬件版本 /// 终端的硬件版本号,由制造商自定 /// - public class HardwareVersionCommandParameter : ICommandParameter + public class HardwareVersionCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 9 @@ -896,15 +655,15 @@ namespace JT808.Protocol.MessageBody /// 硬件版本 /// 终端的硬件版本号,由制造商自定 /// - public string HardwareVersion { get; set; } + public string Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (string.IsNullOrEmpty(HardwareVersion)) return default; - return JT808Constants.Encoding.GetBytes(HardwareVersion); + if (string.IsNullOrEmpty(Value)) return default; + return JT808Constants.Encoding.GetBytes(Value); } /// /// @@ -914,23 +673,15 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - HardwareVersion = JT808Constants.Encoding.GetString(bytes); + Value = JT808Constants.Encoding.GetString(bytes); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{HardwareVersion ?? ""}"; - } } /// /// 固件版本 /// 终端的固件版本号,由制造商自定 /// - public class FirmwareVersionCommandParameter : ICommandParameter + public class FirmwareVersionCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 10 @@ -944,15 +695,15 @@ namespace JT808.Protocol.MessageBody /// 固件版本 /// 终端的硬件版本号,由制造商自定 /// - public string FirmwareVersion { get; set; } + public string Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (string.IsNullOrEmpty(FirmwareVersion)) return default; - return JT808Constants.Encoding.GetBytes(FirmwareVersion); + if (string.IsNullOrEmpty(Value)) return default; + return JT808Constants.Encoding.GetBytes(Value); } /// /// @@ -962,43 +713,35 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - FirmwareVersion = JT808Constants.Encoding.GetString(bytes); + Value = JT808Constants.Encoding.GetString(bytes); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{FirmwareVersion ?? ""}"; - } } /// - /// URL地址完整URL地址 + /// URL的完整地址 /// - public class UrlCommandParameter : ICommandParameter + public class UrlCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 11 /// public int Order { get; } = 11; /// - /// URL地址完整URL地址 + /// URL的完整地址 /// - public string CommandName { get; } = "URL地址完整URL地址"; + public string CommandName { get; } = "URL的完整地址"; /// - /// URL地址完整URL地址 + /// URL的完整地址 /// - public string Url { get; set; } + public string Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (string.IsNullOrEmpty(Url)) return default; - return JT808Constants.Encoding.GetBytes(Url); + if (string.IsNullOrEmpty(Value)) return default; + return JT808Constants.Encoding.GetBytes(Value); } /// /// @@ -1008,22 +751,14 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - Url = JT808Constants.Encoding.GetString(bytes); + Value = JT808Constants.Encoding.GetString(bytes); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{Url ?? ""}"; - } } /// /// 连接到指定服务器时限 /// - public class ConnectTimeLimitCommandParameter : ICommandParameter + public class ConnectTimeLimitCommandParameter : ICommandParameter, ICommandParameterValue { /// /// 排序 12 @@ -1038,16 +773,16 @@ namespace JT808.Protocol.MessageBody /// 单位:分(min),值非 0 后的有效期截止前,终端应连回原地址。 /// 若值为 0,则表示一直连接指 定服务器 /// - public ushort? ConnectTimeLimit { get; set; } + public ushort? Value { get; set; } /// /// /// /// public byte[] ToBytes() { - if (ConnectTimeLimit.HasValue) return default; + if (!Value.HasValue) return default; var value = new byte[2]; - BinaryPrimitives.WriteUInt16BigEndian(value, ConnectTimeLimit.Value); + BinaryPrimitives.WriteUInt16BigEndian(value, Value.Value); return value; } /// @@ -1058,17 +793,9 @@ namespace JT808.Protocol.MessageBody { if (bytes != null && bytes.Length > 0) { - ConnectTimeLimit = BinaryPrimitives.ReadUInt16BigEndian(bytes); + Value = BinaryPrimitives.ReadUInt16BigEndian(bytes); } } - /// - /// - /// - /// - public string ToDescription() - { - return $"{CommandName}:{(ConnectTimeLimit.HasValue ? "" : ConnectTimeLimit.Value)}"; - } } #endregion }