From 1a9150c482fa792eb4d8041f5961b91f71dfac8c Mon Sep 17 00:00:00 2001 From: "SmallChi(Koike)" <564952747@qq.com> Date: Thu, 21 May 2020 14:41:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0809=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E6=B6=88=E6=81=AFId=E5=86=B2=E7=AA=81?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8=E4=B8=80=E4=B8=AA=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E6=9D=A5=E6=8E=A7=E5=88=B6=E6=98=AF=E5=90=A6=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E5=8E=9F=E6=9C=89=E7=9A=84=E6=B6=88=E6=81=AFId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Internal/JT809BusinessTypeFactory.cs | 45 ++++++++++++++++--- .../Internal/JT809SubBusinessTypeFactory.cs | 21 ++++++++- src/JT809.Protocol/JT809.Protocol.csproj | 2 +- src/JT809.Protocol/JT809Bodies.cs | 5 +++ src/JT809.Protocol/JT809SubBodies.cs | 5 +++ 5 files changed, 68 insertions(+), 10 deletions(-) diff --git a/src/JT809.Protocol/Internal/JT809BusinessTypeFactory.cs b/src/JT809.Protocol/Internal/JT809BusinessTypeFactory.cs index be74f03..659ef1c 100644 --- a/src/JT809.Protocol/Internal/JT809BusinessTypeFactory.cs +++ b/src/JT809.Protocol/Internal/JT809BusinessTypeFactory.cs @@ -33,10 +33,12 @@ namespace JT808.Protocol.Internal var instance = Activator.CreateInstance(type); ushort msgId = 0; JT809Version version; + bool replaceInternalMsgId; try { msgId = (ushort)type.GetProperty(nameof(JT809Bodies.MsgId)).GetValue(instance); version = (JT809Version)type.GetProperty(nameof(JT809Bodies.Version)).GetValue(instance); + replaceInternalMsgId = (bool)type.GetProperty(nameof(JT809Bodies.ReplaceInternalMsgId)).GetValue(instance); } catch (Exception ex) { @@ -44,13 +46,20 @@ namespace JT808.Protocol.Internal } if (Map_2019.ContainsKey(msgId)) { - if (version == JT809Version.JTT2019) + if (replaceInternalMsgId) { Map_2019[msgId] = instance; } else { - throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + if (version == JT809Version.JTT2019) + { + Map_2019[msgId] = instance; + } + else + { + throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + } } } else @@ -61,7 +70,14 @@ namespace JT808.Protocol.Internal { if (version != JT809Version.JTT2019) { - throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + if (replaceInternalMsgId) + { + Map[msgId] = instance; + } + else + { + throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + } } } else @@ -77,11 +93,19 @@ namespace JT808.Protocol.Internal var instance = Activator.CreateInstance(type); var msgId = (ushort)type.GetProperty(nameof(JT809Bodies.MsgId)).GetValue(instance); JT809Version version = (JT809Version)type.GetProperty(nameof(JT809Bodies.Version)).GetValue(instance); + bool replaceInternalMsgId = (bool)type.GetProperty(nameof(JT809Bodies.ReplaceInternalMsgId)).GetValue(instance); if (Map.ContainsKey(msgId)) { - if (version != JT809Version.JTT2019) + if (replaceInternalMsgId) { - throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + Map[msgId] = instance; + } + else + { + if (version != JT809Version.JTT2019) + { + throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + } } } else @@ -90,13 +114,20 @@ namespace JT808.Protocol.Internal } if (Map_2019.ContainsKey(msgId)) { - if (version == JT809Version.JTT2019) + if (replaceInternalMsgId) { Map_2019[msgId] = instance; } else { - throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + if (version == JT809Version.JTT2019) + { + Map_2019[msgId] = instance; + } + else + { + throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + } } } else diff --git a/src/JT809.Protocol/Internal/JT809SubBusinessTypeFactory.cs b/src/JT809.Protocol/Internal/JT809SubBusinessTypeFactory.cs index 07feac2..ea94eae 100644 --- a/src/JT809.Protocol/Internal/JT809SubBusinessTypeFactory.cs +++ b/src/JT809.Protocol/Internal/JT809SubBusinessTypeFactory.cs @@ -27,9 +27,11 @@ namespace JT808.Protocol.Internal { var instance = Activator.CreateInstance(type); ushort msgId = 0; + bool replaceInternalSubMsgId; try { msgId = (ushort)type.GetProperty(nameof(JT809SubBodies.SubMsgId)).GetValue(instance); + replaceInternalSubMsgId = (bool)type.GetProperty(nameof(JT809SubBodies.ReplaceInternalSubMsgId)).GetValue(instance); } catch (Exception ex) { @@ -37,7 +39,14 @@ namespace JT808.Protocol.Internal } if (Map.ContainsKey(msgId)) { - throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + if (replaceInternalSubMsgId) + { + Map[msgId] = instance; + } + else + { + throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + } } else { @@ -56,9 +65,17 @@ namespace JT808.Protocol.Internal Type type = typeof(TJT809SubBodies); var instance = Activator.CreateInstance(type); var msgId = (ushort)type.GetProperty(nameof(JT809SubBodies.SubMsgId)).GetValue(instance); + bool replaceInternalSubMsgId = (bool)type.GetProperty(nameof(JT809SubBodies.ReplaceInternalSubMsgId)).GetValue(instance); if (Map.ContainsKey(msgId)) { - throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + if (replaceInternalSubMsgId) + { + Map[msgId] = instance; + } + else + { + throw new ArgumentException($"{type.FullName} {msgId} An element with the same key already exists."); + } } else { diff --git a/src/JT809.Protocol/JT809.Protocol.csproj b/src/JT809.Protocol/JT809.Protocol.csproj index 494b645..80f6975 100644 --- a/src/JT809.Protocol/JT809.Protocol.csproj +++ b/src/JT809.Protocol/JT809.Protocol.csproj @@ -14,7 +14,7 @@ https://github.com/SmallChi/JT809 https://github.com/SmallChi/JT809/blob/master/LICENSE false - 2.1.4-preview2 + 2.1.4-preview3 LICENSE diff --git a/src/JT809.Protocol/JT809Bodies.cs b/src/JT809.Protocol/JT809Bodies.cs index 1255764..7c8f5f2 100644 --- a/src/JT809.Protocol/JT809Bodies.cs +++ b/src/JT809.Protocol/JT809Bodies.cs @@ -15,5 +15,10 @@ namespace JT809.Protocol public abstract ushort MsgId { get; } public abstract JT809_LinkType LinkType { get; } public abstract string Description { get; } + /// + /// 当外部注册的扩展809的消息Id跟国标的消息Id冲突时可以使用该字段替换国标的消息Id + /// 默认是false,不替换 + /// + public virtual bool ReplaceInternalMsgId { get; } = false; } } diff --git a/src/JT809.Protocol/JT809SubBodies.cs b/src/JT809.Protocol/JT809SubBodies.cs index 543593e..6b284b8 100644 --- a/src/JT809.Protocol/JT809SubBodies.cs +++ b/src/JT809.Protocol/JT809SubBodies.cs @@ -17,5 +17,10 @@ namespace JT809.Protocol public abstract ushort SubMsgId { get; } public abstract string Description { get; } + /// + /// 当外部注册的扩展809的消息Id跟国标的消息Id冲突时可以使用该字段替换国标的消息Id + /// 默认是false,不替换 + /// + public virtual bool ReplaceInternalSubMsgId { get; } = false; } }