You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
8.8 KiB

  1. using JT808.Protocol.MessageBody;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. namespace JT808.Protocol.Interfaces
  10. {
  11. class JT808_0x8103_Factory : IJT808_0x8103_Factory
  12. {
  13. public JT808_0x8103_Factory()
  14. {
  15. Map = new Dictionary<uint, object>();
  16. Map.Add(JT808Constants.JT808_0x8103_0x0001, new JT808_0x8103_0x0001());
  17. Map.Add(JT808Constants.JT808_0x8103_0x0002, new JT808_0x8103_0x0002());
  18. Map.Add(JT808Constants.JT808_0x8103_0x0003, new JT808_0x8103_0x0003());
  19. Map.Add(JT808Constants.JT808_0x8103_0x0004, new JT808_0x8103_0x0004());
  20. Map.Add(JT808Constants.JT808_0x8103_0x0005, new JT808_0x8103_0x0005());
  21. Map.Add(JT808Constants.JT808_0x8103_0x0006, new JT808_0x8103_0x0006());
  22. Map.Add(JT808Constants.JT808_0x8103_0x0007, new JT808_0x8103_0x0007());
  23. Map.Add(JT808Constants.JT808_0x8103_0x0010, new JT808_0x8103_0x0010());
  24. Map.Add(JT808Constants.JT808_0x8103_0x0011, new JT808_0x8103_0x0011());
  25. Map.Add(JT808Constants.JT808_0x8103_0x0012, new JT808_0x8103_0x0012());
  26. Map.Add(JT808Constants.JT808_0x8103_0x0013, new JT808_0x8103_0x0013());
  27. Map.Add(JT808Constants.JT808_0x8103_0x0014, new JT808_0x8103_0x0014());
  28. Map.Add(JT808Constants.JT808_0x8103_0x0015, new JT808_0x8103_0x0015());
  29. Map.Add(JT808Constants.JT808_0x8103_0x0016, new JT808_0x8103_0x0016());
  30. Map.Add(JT808Constants.JT808_0x8103_0x0017, new JT808_0x8103_0x0017());
  31. Map.Add(JT808Constants.JT808_0x8103_0x0018, new JT808_0x8103_0x0018());
  32. Map.Add(JT808Constants.JT808_0x8103_0x0019, new JT808_0x8103_0x0019());
  33. Map.Add(JT808Constants.JT808_0x8103_0x001A, new JT808_0x8103_0x001A());
  34. Map.Add(JT808Constants.JT808_0x8103_0x001B, new JT808_0x8103_0x001B());
  35. Map.Add(JT808Constants.JT808_0x8103_0x001C, new JT808_0x8103_0x001C());
  36. Map.Add(JT808Constants.JT808_0x8103_0x001D, new JT808_0x8103_0x001D());
  37. Map.Add(JT808Constants.JT808_0x8103_0x0020, new JT808_0x8103_0x0020());
  38. Map.Add(JT808Constants.JT808_0x8103_0x0021, new JT808_0x8103_0x0021());
  39. Map.Add(JT808Constants.JT808_0x8103_0x0022, new JT808_0x8103_0x0022());
  40. Map.Add(JT808Constants.JT808_0x8103_0x0027, new JT808_0x8103_0x0027());
  41. Map.Add(JT808Constants.JT808_0x8103_0x0028, new JT808_0x8103_0x0028());
  42. Map.Add(JT808Constants.JT808_0x8103_0x0029, new JT808_0x8103_0x0029());
  43. Map.Add(JT808Constants.JT808_0x8103_0x002C, new JT808_0x8103_0x002C());
  44. Map.Add(JT808Constants.JT808_0x8103_0x002D, new JT808_0x8103_0x002D());
  45. Map.Add(JT808Constants.JT808_0x8103_0x002E, new JT808_0x8103_0x002E());
  46. Map.Add(JT808Constants.JT808_0x8103_0x002F, new JT808_0x8103_0x002F());
  47. Map.Add(JT808Constants.JT808_0x8103_0x0030, new JT808_0x8103_0x0030());
  48. Map.Add(JT808Constants.JT808_0x8103_0x0031, new JT808_0x8103_0x0031());
  49. Map.Add(JT808Constants.JT808_0x8103_0x0032, new JT808_0x8103_0x0032());
  50. Map.Add(JT808Constants.JT808_0x8103_0x0040, new JT808_0x8103_0x0040());
  51. Map.Add(JT808Constants.JT808_0x8103_0x0041, new JT808_0x8103_0x0041());
  52. Map.Add(JT808Constants.JT808_0x8103_0x0042, new JT808_0x8103_0x0042());
  53. Map.Add(JT808Constants.JT808_0x8103_0x0043, new JT808_0x8103_0x0043());
  54. Map.Add(JT808Constants.JT808_0x8103_0x0044, new JT808_0x8103_0x0044());
  55. Map.Add(JT808Constants.JT808_0x8103_0x0045, new JT808_0x8103_0x0045());
  56. Map.Add(JT808Constants.JT808_0x8103_0x0046, new JT808_0x8103_0x0046());
  57. Map.Add(JT808Constants.JT808_0x8103_0x0047, new JT808_0x8103_0x0047());
  58. Map.Add(JT808Constants.JT808_0x8103_0x0048, new JT808_0x8103_0x0048());
  59. Map.Add(JT808Constants.JT808_0x8103_0x0049, new JT808_0x8103_0x0049());
  60. Map.Add(JT808Constants.JT808_0x8103_0x0050, new JT808_0x8103_0x0050());
  61. Map.Add(JT808Constants.JT808_0x8103_0x0051, new JT808_0x8103_0x0051());
  62. Map.Add(JT808Constants.JT808_0x8103_0x0052, new JT808_0x8103_0x0052());
  63. Map.Add(JT808Constants.JT808_0x8103_0x0053, new JT808_0x8103_0x0053());
  64. Map.Add(JT808Constants.JT808_0x8103_0x0054, new JT808_0x8103_0x0054());
  65. Map.Add(JT808Constants.JT808_0x8103_0x0055, new JT808_0x8103_0x0055());
  66. Map.Add(JT808Constants.JT808_0x8103_0x0056, new JT808_0x8103_0x0056());
  67. Map.Add(JT808Constants.JT808_0x8103_0x0057, new JT808_0x8103_0x0057());
  68. Map.Add(JT808Constants.JT808_0x8103_0x0058, new JT808_0x8103_0x0058());
  69. Map.Add(JT808Constants.JT808_0x8103_0x0059, new JT808_0x8103_0x0059());
  70. Map.Add(JT808Constants.JT808_0x8103_0x005A, new JT808_0x8103_0x005A());
  71. Map.Add(JT808Constants.JT808_0x8103_0x005B, new JT808_0x8103_0x005B());
  72. Map.Add(JT808Constants.JT808_0x8103_0x005C, new JT808_0x8103_0x005C());
  73. Map.Add(JT808Constants.JT808_0x8103_0x005D, new JT808_0x8103_0x005D());
  74. Map.Add(JT808Constants.JT808_0x8103_0x005E, new JT808_0x8103_0x005E());
  75. Map.Add(JT808Constants.JT808_0x8103_0x0064, new JT808_0x8103_0x0064());
  76. Map.Add(JT808Constants.JT808_0x8103_0x0065, new JT808_0x8103_0x0065());
  77. Map.Add(JT808Constants.JT808_0x8103_0x0070, new JT808_0x8103_0x0070());
  78. Map.Add(JT808Constants.JT808_0x8103_0x0071, new JT808_0x8103_0x0071());
  79. Map.Add(JT808Constants.JT808_0x8103_0x0072, new JT808_0x8103_0x0072());
  80. Map.Add(JT808Constants.JT808_0x8103_0x0073, new JT808_0x8103_0x0073());
  81. Map.Add(JT808Constants.JT808_0x8103_0x0074, new JT808_0x8103_0x0074());
  82. Map.Add(JT808Constants.JT808_0x8103_0x0080, new JT808_0x8103_0x0080());
  83. Map.Add(JT808Constants.JT808_0x8103_0x0081, new JT808_0x8103_0x0081());
  84. Map.Add(JT808Constants.JT808_0x8103_0x0082, new JT808_0x8103_0x0082());
  85. Map.Add(JT808Constants.JT808_0x8103_0x0083, new JT808_0x8103_0x0083());
  86. Map.Add(JT808Constants.JT808_0x8103_0x0084, new JT808_0x8103_0x0084());
  87. Map.Add(JT808Constants.JT808_0x8103_0x0090, new JT808_0x8103_0x0090());
  88. Map.Add(JT808Constants.JT808_0x8103_0x0091, new JT808_0x8103_0x0091());
  89. Map.Add(JT808Constants.JT808_0x8103_0x0092, new JT808_0x8103_0x0092());
  90. Map.Add(JT808Constants.JT808_0x8103_0x0093, new JT808_0x8103_0x0093());
  91. Map.Add(JT808Constants.JT808_0x8103_0x0094, new JT808_0x8103_0x0094());
  92. Map.Add(JT808Constants.JT808_0x8103_0x0095, new JT808_0x8103_0x0095());
  93. Map.Add(JT808Constants.JT808_0x8103_0x0100, new JT808_0x8103_0x0100());
  94. Map.Add(JT808Constants.JT808_0x8103_0x0101, new JT808_0x8103_0x0101());
  95. Map.Add(JT808Constants.JT808_0x8103_0x0102, new JT808_0x8103_0x0102());
  96. Map.Add(JT808Constants.JT808_0x8103_0x0103, new JT808_0x8103_0x0103());
  97. Map.Add(JT808Constants.JT808_0x8103_0x0110, new JT808_0x8103_0x0110());
  98. }
  99. public IDictionary<uint, object> Map { get; }
  100. public void Register(Assembly externalAssembly)
  101. {
  102. var types = externalAssembly.GetTypes().Where(w => w.GetInterface(nameof(JT808_0x8103_BodyBase)) == typeof(JT808_0x8103_BodyBase)).ToList();
  103. foreach (var type in types)
  104. {
  105. var instance = Activator.CreateInstance(type);
  106. uint paramId = 0;
  107. try
  108. {
  109. paramId = (uint)type.GetProperty(nameof(JT808_0x8103_BodyBase.ParamId)).GetValue(instance);
  110. }
  111. catch
  112. {
  113. continue;
  114. }
  115. if (Map.ContainsKey(paramId))
  116. {
  117. throw new ArgumentException($"{type.FullName} {paramId} An element with the same key already exists.");
  118. }
  119. else
  120. {
  121. Map.Add(paramId, instance);
  122. }
  123. }
  124. }
  125. public IJT808_0x8103_Factory SetMap<TJT808_0x8103_Body>() where TJT808_0x8103_Body : JT808_0x8103_BodyBase
  126. {
  127. Type type = typeof(TJT808_0x8103_Body);
  128. var instance = Activator.CreateInstance(type);
  129. var paramId = (uint)type.GetProperty(nameof(JT808_0x8103_BodyBase.ParamId)).GetValue(instance);
  130. if (Map.ContainsKey(paramId))
  131. {
  132. throw new ArgumentException($"{type.FullName} {paramId} An element with the same key already exists.");
  133. }
  134. else
  135. {
  136. Map.Add(paramId, instance);
  137. }
  138. return this;
  139. }
  140. }
  141. }