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

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