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.

71 lines
3.1 KiB

  1. using JT808.Protocol.Interfaces;
  2. using JT808.Protocol.MessageBody;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. namespace JT808.Protocol.Internal
  10. {
  11. class JT808_0x0200_Factory : IJT808_0x0200_Factory
  12. {
  13. public IDictionary<byte, object> Map { get; set; }
  14. public JT808_0x0200_Factory()
  15. {
  16. Map = new Dictionary<byte, object>();
  17. Map.Add(JT808Constants.JT808_0x0200_0x01, new JT808_0x0200_0x01());
  18. Map.Add(JT808Constants.JT808_0x0200_0x02, new JT808_0x0200_0x02());
  19. Map.Add(JT808Constants.JT808_0x0200_0x03, new JT808_0x0200_0x03());
  20. Map.Add(JT808Constants.JT808_0x0200_0x04, new JT808_0x0200_0x04());
  21. Map.Add(JT808Constants.JT808_0x0200_0x05, new JT808_0x0200_0x05());
  22. Map.Add(JT808Constants.JT808_0x0200_0x06, new JT808_0x0200_0x06());
  23. Map.Add(JT808Constants.JT808_0x0200_0x07, new JT808_0x0200_0x07());
  24. Map.Add(JT808Constants.JT808_0x0200_0x11, new JT808_0x0200_0x11());
  25. Map.Add(JT808Constants.JT808_0x0200_0x12, new JT808_0x0200_0x12());
  26. Map.Add(JT808Constants.JT808_0x0200_0x13, new JT808_0x0200_0x13());
  27. Map.Add(JT808Constants.JT808_0x0200_0x25, new JT808_0x0200_0x25());
  28. Map.Add(JT808Constants.JT808_0x0200_0x2A, new JT808_0x0200_0x2A());
  29. Map.Add(JT808Constants.JT808_0x0200_0x2B, new JT808_0x0200_0x2B());
  30. Map.Add(JT808Constants.JT808_0x0200_0x30, new JT808_0x0200_0x30());
  31. Map.Add(JT808Constants.JT808_0x0200_0x31, new JT808_0x0200_0x31());
  32. }
  33. public IJT808_0x0200_Factory SetMap<TJT808_0x0200_Body>() where TJT808_0x0200_Body : JT808_0x0200_BodyBase
  34. {
  35. Type type = typeof(TJT808_0x0200_Body);
  36. var instance = Activator.CreateInstance(type);
  37. var attachInfoId = (byte)type.GetProperty(nameof(JT808_0x0200_BodyBase.AttachInfoId)).GetValue(instance);
  38. if (Map.ContainsKey(attachInfoId))
  39. {
  40. throw new ArgumentException($"{type.FullName} {attachInfoId} An element with the same key already exists.");
  41. }
  42. else
  43. {
  44. Map.Add(attachInfoId, instance);
  45. }
  46. return this;
  47. }
  48. public void Register(Assembly externalAssembly)
  49. {
  50. var types = externalAssembly.GetTypes().Where(w => w.GetInterface(nameof(JT808_0x0200_BodyBase)) == typeof(JT808_0x0200_BodyBase)).ToList();
  51. foreach (var type in types)
  52. {
  53. var instance = Activator.CreateInstance(type);
  54. var attachid = (byte)type.GetProperty(nameof(JT808_0x0200_BodyBase.AttachInfoId)).GetValue(instance);
  55. if (Map.ContainsKey(attachid))
  56. {
  57. throw new ArgumentException($"{type.FullName} {attachid} An element with the same key already exists.");
  58. }
  59. else
  60. {
  61. Map.Add(attachid, instance);
  62. }
  63. }
  64. }
  65. }
  66. }