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;
}
}