Parcourir la source

增加809外部注册消息Id冲突,使用一个属性来控制是否覆盖原有的消息Id

tags/v2.2.0
SmallChi(Koike) il y a 5 ans
Parent
révision
1a9150c482
5 fichiers modifiés avec 68 ajouts et 10 suppressions
  1. +38
    -7
      src/JT809.Protocol/Internal/JT809BusinessTypeFactory.cs
  2. +19
    -2
      src/JT809.Protocol/Internal/JT809SubBusinessTypeFactory.cs
  3. +1
    -1
      src/JT809.Protocol/JT809.Protocol.csproj
  4. +5
    -0
      src/JT809.Protocol/JT809Bodies.cs
  5. +5
    -0
      src/JT809.Protocol/JT809SubBodies.cs

+ 38
- 7
src/JT809.Protocol/Internal/JT809BusinessTypeFactory.cs Voir le fichier

@@ -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


+ 19
- 2
src/JT809.Protocol/Internal/JT809SubBusinessTypeFactory.cs Voir le fichier

@@ -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
{


+ 1
- 1
src/JT809.Protocol/JT809.Protocol.csproj Voir le fichier

@@ -14,7 +14,7 @@
<PackageProjectUrl>https://github.com/SmallChi/JT809</PackageProjectUrl>
<license>https://github.com/SmallChi/JT809/blob/master/LICENSE</license>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>2.1.4-preview2</Version>
<Version>2.1.4-preview3</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>


+ 5
- 0
src/JT809.Protocol/JT809Bodies.cs Voir le fichier

@@ -15,5 +15,10 @@ namespace JT809.Protocol
public abstract ushort MsgId { get; }
public abstract JT809_LinkType LinkType { get; }
public abstract string Description { get; }
/// <summary>
/// 当外部注册的扩展809的消息Id跟国标的消息Id冲突时可以使用该字段替换国标的消息Id
/// 默认是false,不替换
/// </summary>
public virtual bool ReplaceInternalMsgId { get; } = false;
}
}

+ 5
- 0
src/JT809.Protocol/JT809SubBodies.cs Voir le fichier

@@ -17,5 +17,10 @@ namespace JT809.Protocol
public abstract ushort SubMsgId { get; }

public abstract string Description { get; }
/// <summary>
/// 当外部注册的扩展809的消息Id跟国标的消息Id冲突时可以使用该字段替换国标的消息Id
/// 默认是false,不替换
/// </summary>
public virtual bool ReplaceInternalSubMsgId { get; } = false;
}
}

Chargement…
Annuler
Enregistrer