Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

331 рядки
14 KiB

  1. using JT808.Protocol.Enums;
  2. using JT808.Protocol.Interfaces;
  3. using JT808.Protocol.MessageBody;
  4. using JT808.Protocol.Extensions;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. using System.Text;
  9. using Xunit;
  10. using JT808.Protocol.Formatters;
  11. using JT808.Protocol.MessagePack;
  12. using Newtonsoft.Json.Linq;
  13. using Newtonsoft.Json;
  14. using JT808.Protocol.Internal;
  15. namespace JT808.Protocol.Test.Simples
  16. {
  17. public class Demo4
  18. {
  19. public Demo4()
  20. {
  21. }
  22. private readonly Dictionary<string, DeviceType> cache = new Dictionary<string, DeviceType>
  23. {
  24. { "123456789012",DeviceType.DT1 },
  25. { "123456789013",DeviceType.DT2 }
  26. };
  27. /// <summary>
  28. /// 处理多设备多协议附加信息Id冲突
  29. /// </summary>
  30. [Fact]
  31. public void Test1()
  32. {
  33. IJT808Config jT808Config = new DefaultGlobalConfig();
  34. jT808Config.JT808_0X0200_Custom_Factory.SetMap<JT808_0x0200_DT1_0x81>();
  35. jT808Config.FormatterFactory.SetMap<JT808_0x0200_DT1_0x81>();
  36. JT808Serializer demo5JT808Serializer = new JT808Serializer(jT808Config);
  37. JT808Package jT808Package = JT808MsgId._0x0200.Create("123456789012",
  38. new JT808_0x0200
  39. {
  40. AlarmFlag = 1,
  41. Altitude = 40,
  42. GPSTime = DateTime.Parse("2018-12-20 20:10:10"),
  43. Lat = 12222222,
  44. Lng = 132444444,
  45. Speed = 60,
  46. Direction = 0,
  47. StatusFlag = 2,
  48. CustomLocationAttachData = new Dictionary<byte, JT808_0x0200_CustomBodyBase>
  49. {
  50. {0x81,new JT808_0x0200_DT1_0x81 {
  51. Age=15,
  52. Gender=1,
  53. UserName="smallchi"
  54. } }
  55. }
  56. });
  57. byte[] data = demo5JT808Serializer.Serialize(jT808Package);
  58. var jT808PackageResult = demo5JT808Serializer.Deserialize<JT808Package>(data);
  59. JT808_0x0200 jT808_0X0200 = jT808PackageResult.Bodies as JT808_0x0200;
  60. var attach = DeviceTypeFactory.Create(cache[jT808PackageResult.Header.TerminalPhoneNo], jT808_0X0200.CustomLocationAttachData);
  61. var extJson = attach.ExtData.Data.ToString(Formatting.None);
  62. var attachinfo81 = (JT808_0x0200_DT1_0x81)attach.JT808CustomLocationAttachData[0x81];
  63. Assert.Equal((uint)15, attachinfo81.Age);
  64. Assert.Equal(1, attachinfo81.Gender);
  65. Assert.Equal("smallchi", attachinfo81.UserName);
  66. }
  67. }
  68. public interface IExtData
  69. {
  70. JObject Data { get; set; }
  71. }
  72. public interface IExtDataProcessor
  73. {
  74. void Processor(IExtData extData);
  75. }
  76. public class JT808_0x0200_DT1_0x81_ExtDataProcessor : IExtDataProcessor
  77. {
  78. private readonly JT808_0x0200_DT1_0x81 jT808_0X0200_DT1_0X81;
  79. public JT808_0x0200_DT1_0x81_ExtDataProcessor(JT808_0x0200_DT1_0x81 jT808_0X0200_DT1_0X81)
  80. {
  81. this.jT808_0X0200_DT1_0X81 = jT808_0X0200_DT1_0X81;
  82. }
  83. public void Processor(IExtData extData)
  84. {
  85. extData.Data.Add(nameof(JT808_0x0200_DT1_0x81.Age), jT808_0X0200_DT1_0X81.Age);
  86. extData.Data.Add(nameof(JT808_0x0200_DT1_0x81.UserName), jT808_0X0200_DT1_0X81.UserName);
  87. extData.Data.Add(nameof(JT808_0x0200_DT1_0x81.Gender), jT808_0X0200_DT1_0X81.Gender);
  88. }
  89. }
  90. public class JT808_0x0200_DT1_0x82_ExtDataProcessor : IExtDataProcessor
  91. {
  92. private readonly JT808_0x0200_DT1_0x82 jT808_0X0200_DT1_0X82;
  93. public JT808_0x0200_DT1_0x82_ExtDataProcessor(JT808_0x0200_DT1_0x82 jT808_0X0200_DT1_0X82)
  94. {
  95. this.jT808_0X0200_DT1_0X82 = jT808_0X0200_DT1_0X82;
  96. }
  97. public void Processor(IExtData extData)
  98. {
  99. extData.Data.Add(nameof(JT808_0x0200_DT1_0x82.Gender1), jT808_0X0200_DT1_0X82.Gender1);
  100. }
  101. }
  102. public class JT808_0x0200_DT2_0x81_ExtDataProcessor : IExtDataProcessor
  103. {
  104. private readonly JT808_0x0200_DT2_0x81 jT808_0X0200_DT2_0X81;
  105. public JT808_0x0200_DT2_0x81_ExtDataProcessor(JT808_0x0200_DT2_0x81 jT808_0X0200_DT2_0X81)
  106. {
  107. this.jT808_0X0200_DT2_0X81 = jT808_0X0200_DT2_0X81;
  108. }
  109. public void Processor(IExtData extData)
  110. {
  111. extData.Data.Add(nameof(JT808_0x0200_DT2_0x81.Age), jT808_0X0200_DT2_0X81.Age);
  112. extData.Data.Add(nameof(JT808_0x0200_DT2_0x81.Gender), jT808_0X0200_DT2_0X81.Gender);
  113. }
  114. }
  115. public class DeviceTypeFactory
  116. {
  117. public static DeviceTypeBase Create(DeviceType deviceType, Dictionary<byte, JT808_0x0200_CustomBodyBase> jT808CustomLocationAttachData)
  118. {
  119. return deviceType switch
  120. {
  121. DeviceType.DT1 => new DeviceType1(jT808CustomLocationAttachData),
  122. DeviceType.DT2 => new DeviceType2(jT808CustomLocationAttachData),
  123. _ => default,
  124. };
  125. }
  126. }
  127. public enum DeviceType
  128. {
  129. DT1 = 1,
  130. DT2 = 2
  131. }
  132. public abstract class DeviceTypeBase
  133. {
  134. protected JT808Serializer JT808Serializer;
  135. protected class DefaultExtDataImpl : IExtData
  136. {
  137. public JObject Data { get; set; } = new JObject();
  138. }
  139. public virtual IExtData ExtData { get; protected set; } = new DefaultExtDataImpl();
  140. public abstract Dictionary<byte, JT808_0x0200_CustomBodyBase> JT808CustomLocationAttachData { get; protected set; }
  141. protected DeviceTypeBase(Dictionary<byte, JT808_0x0200_CustomBodyBase> jT808CustomLocationAttachData)
  142. {
  143. Execute(jT808CustomLocationAttachData);
  144. }
  145. protected abstract void Execute(Dictionary<byte, JT808_0x0200_CustomBodyBase> jT808CustomLocationAttachData);
  146. }
  147. public class DeviceType1 : DeviceTypeBase
  148. {
  149. private const byte dt1_0x81 = 0x81;
  150. private const byte dt1_0x82 = 0x82;
  151. public DeviceType1(Dictionary<byte, JT808_0x0200_CustomBodyBase> jT808CustomLocationAttachData) : base(jT808CustomLocationAttachData)
  152. {
  153. IJT808Config jT808Config = new DefaultGlobalConfig();
  154. jT808Config.JT808_0X0200_Custom_Factory.SetMap<JT808_0x0200_DT1_0x81>();
  155. jT808Config.FormatterFactory.SetMap<JT808_0x0200_DT1_0x81>();
  156. jT808Config.JT808_0X0200_Custom_Factory.SetMap<JT808_0x0200_DT1_0x82>();
  157. jT808Config.FormatterFactory.SetMap<JT808_0x0200_DT1_0x82>();
  158. JT808Serializer = new JT808Serializer(jT808Config);
  159. }
  160. public override Dictionary<byte, JT808_0x0200_CustomBodyBase> JT808CustomLocationAttachData { get; protected set; }
  161. protected override void Execute(Dictionary<byte, JT808_0x0200_CustomBodyBase> jT808CustomLocationAttachData)
  162. {
  163. JT808CustomLocationAttachData = new Dictionary<byte, JT808_0x0200_CustomBodyBase>();
  164. foreach (var item in jT808CustomLocationAttachData)
  165. {
  166. try
  167. {
  168. switch (item.Key)
  169. {
  170. case dt1_0x81:
  171. var info81 = (JT808_0x0200_DT1_0x81)item.Value;
  172. IExtDataProcessor extDataProcessor81 = new JT808_0x0200_DT1_0x81_ExtDataProcessor(info81);
  173. extDataProcessor81.Processor(ExtData);
  174. JT808CustomLocationAttachData.Add(dt1_0x81, info81);
  175. break;
  176. case dt1_0x82:
  177. var info82 = (JT808_0x0200_DT1_0x82)item.Value;
  178. IExtDataProcessor extDataProcessor82 = new JT808_0x0200_DT1_0x82_ExtDataProcessor(info82);
  179. extDataProcessor82.Processor(ExtData);
  180. JT808CustomLocationAttachData.Add(dt1_0x82, info82);
  181. break;
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. }
  187. }
  188. }
  189. }
  190. public class DeviceType2 : DeviceTypeBase
  191. {
  192. public DeviceType2(Dictionary<byte, JT808_0x0200_CustomBodyBase> jT808CustomLocationAttachData) : base(jT808CustomLocationAttachData)
  193. {
  194. IJT808Config jT808Config = new DefaultGlobalConfig();
  195. jT808Config.JT808_0X0200_Custom_Factory.SetMap<JT808_0x0200_DT2_0x81>();
  196. jT808Config.FormatterFactory.SetMap<JT808_0x0200_DT2_0x81>();
  197. JT808Serializer = new JT808Serializer(jT808Config);
  198. }
  199. public override Dictionary<byte, JT808_0x0200_CustomBodyBase> JT808CustomLocationAttachData { get; protected set; }
  200. private const byte dt2_0x81 = 0x81;
  201. protected override void Execute(Dictionary<byte, JT808_0x0200_CustomBodyBase> jT808CustomLocationAttachOriginalData)
  202. {
  203. JT808CustomLocationAttachData = new Dictionary<byte, JT808_0x0200_CustomBodyBase>();
  204. foreach (var item in jT808CustomLocationAttachOriginalData)
  205. {
  206. try
  207. {
  208. switch (item.Key)
  209. {
  210. case dt2_0x81:
  211. var info81 = (JT808_0x0200_DT2_0x81)item.Value;
  212. IExtDataProcessor extDataProcessor = new JT808_0x0200_DT2_0x81_ExtDataProcessor(info81);
  213. extDataProcessor.Processor(ExtData);
  214. JT808CustomLocationAttachData.Add(dt2_0x81, info81);
  215. break;
  216. }
  217. }
  218. catch (Exception)
  219. {
  220. }
  221. }
  222. }
  223. }
  224. /// <summary>
  225. /// 设备类型1-对应消息协议0x81
  226. /// </summary>
  227. public class JT808_0x0200_DT1_0x81 : JT808MessagePackFormatter<JT808_0x0200_DT1_0x81>, JT808_0x0200_CustomBodyBase
  228. {
  229. public byte AttachInfoId { get; set; } = 0x81;
  230. public byte AttachInfoLength { get; set; } = 13;
  231. public uint Age { get; set; }
  232. public byte Gender { get; set; }
  233. public string UserName { get; set; }
  234. public override JT808_0x0200_DT1_0x81 Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
  235. {
  236. JT808_0x0200_DT1_0x81 jT808_0X0200_DT1_0X81 = new JT808_0x0200_DT1_0x81();
  237. jT808_0X0200_DT1_0X81.AttachInfoId = reader.ReadByte();
  238. jT808_0X0200_DT1_0X81.AttachInfoLength = reader.ReadByte();
  239. jT808_0X0200_DT1_0X81.Age = reader.ReadUInt32();
  240. jT808_0X0200_DT1_0X81.Gender = reader.ReadByte();
  241. jT808_0X0200_DT1_0X81.UserName = reader.ReadRemainStringContent();
  242. return jT808_0X0200_DT1_0X81;
  243. }
  244. public override void Serialize(ref JT808MessagePackWriter writer, JT808_0x0200_DT1_0x81 value, IJT808Config config)
  245. {
  246. writer.WriteByte(value.AttachInfoId);
  247. writer.WriteByte(value.AttachInfoLength);
  248. writer.WriteUInt32(value.Age);
  249. writer.WriteByte(value.Gender);
  250. writer.WriteString(value.UserName);
  251. }
  252. }
  253. /// <summary>
  254. /// 设备类型1-对应消息协议0x82
  255. /// </summary>
  256. public class JT808_0x0200_DT1_0x82 : JT808MessagePackFormatter<JT808_0x0200_DT1_0x82>, JT808_0x0200_CustomBodyBase
  257. {
  258. public byte AttachInfoId { get; set; } = 0x82;
  259. public byte AttachInfoLength { get; set; } = 1;
  260. public byte Gender1 { get; set; }
  261. public override JT808_0x0200_DT1_0x82 Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
  262. {
  263. JT808_0x0200_DT1_0x82 jT808_0X0200_DT1_0X82 = new JT808_0x0200_DT1_0x82();
  264. jT808_0X0200_DT1_0X82.AttachInfoId = reader.ReadByte();
  265. jT808_0X0200_DT1_0X82.AttachInfoLength = reader.ReadByte();
  266. jT808_0X0200_DT1_0X82.Gender1 = reader.ReadByte();
  267. return jT808_0X0200_DT1_0X82;
  268. }
  269. public override void Serialize(ref JT808MessagePackWriter writer, JT808_0x0200_DT1_0x82 value, IJT808Config config)
  270. {
  271. writer.WriteByte(value.AttachInfoId);
  272. writer.WriteByte(value.AttachInfoLength);
  273. writer.WriteByte(value.Gender1);
  274. }
  275. }
  276. /// <summary>
  277. /// 设备类型2-对应消息协议0x81
  278. /// </summary>
  279. public class JT808_0x0200_DT2_0x81 : JT808MessagePackFormatter<JT808_0x0200_DT2_0x81>, JT808_0x0200_CustomBodyBase
  280. {
  281. public byte AttachInfoId { get; set; } = 0x81;
  282. public byte AttachInfoLength { get; set; } = 7;
  283. public uint Age { get; set; }
  284. public byte Gender { get; set; }
  285. public ushort MsgNum { get; set; }
  286. public override JT808_0x0200_DT2_0x81 Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
  287. {
  288. JT808_0x0200_DT2_0x81 jT808_0X0200_DT2_0X81 = new JT808_0x0200_DT2_0x81();
  289. jT808_0X0200_DT2_0X81.AttachInfoId = reader.ReadByte();
  290. jT808_0X0200_DT2_0X81.AttachInfoLength = reader.ReadByte();
  291. jT808_0X0200_DT2_0X81.Age = reader.ReadUInt32();
  292. jT808_0X0200_DT2_0X81.Gender = reader.ReadByte();
  293. jT808_0X0200_DT2_0X81.MsgNum = reader.ReadUInt16();
  294. return jT808_0X0200_DT2_0X81;
  295. }
  296. public override void Serialize(ref JT808MessagePackWriter writer, JT808_0x0200_DT2_0x81 value, IJT808Config config)
  297. {
  298. writer.WriteByte(value.AttachInfoId);
  299. writer.WriteByte(value.AttachInfoLength);
  300. writer.WriteUInt32(value.Age);
  301. writer.WriteByte(value.Gender);
  302. writer.WriteUInt16(value.MsgNum);
  303. }
  304. }
  305. }