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.

65 lines
2.4 KiB

  1. using JT808.Protocol.Interfaces;
  2. using JT808.Protocol.MessageBody;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using static JT808.Protocol.MessageBody.JT808_0x8105;
  10. namespace JT808.Protocol.Internal
  11. {
  12. class JT808_0x8105_Cusotm_Factory : IJT808_0x8105_Cusotm_Factory
  13. {
  14. public IDictionary<int, Type> Map { get; }
  15. public JT808_0x8105_Cusotm_Factory()
  16. {
  17. Map = new Dictionary<int, Type>();
  18. }
  19. public void Register(Assembly externalAssembly)
  20. {
  21. var types = externalAssembly.GetTypes().Where(w => w.GetInterface(nameof(ICusotmCommandParameter)) == typeof(ICusotmCommandParameter)).ToList();
  22. foreach(var type in types)
  23. {
  24. var instance = Activator.CreateInstance(type);
  25. var order = (int)type.GetProperty(nameof(ICusotmCommandParameter.Order)).GetValue(instance);
  26. if (order < CommandParameterCount)
  27. {
  28. throw new ArgumentException($"{type.FullName} {order} We're starting at 13 and we're incremying by 1.");
  29. }
  30. if (Map.ContainsKey(order))
  31. {
  32. throw new ArgumentException($"{type.FullName} {order} An element with the same Order already exists.");
  33. }
  34. else
  35. {
  36. Map.Add(order, type);
  37. }
  38. }
  39. }
  40. public IJT808_0x8105_Cusotm_Factory SetMap<TICusotmCommandParameter>() where TICusotmCommandParameter : ICusotmCommandParameter
  41. {
  42. Type type = typeof(TICusotmCommandParameter);
  43. var instance = Activator.CreateInstance(type);
  44. var order = (int)type.GetProperty(nameof(ICusotmCommandParameter.Order)).GetValue(instance);
  45. if(order < CommandParameterCount)
  46. {
  47. throw new ArgumentException($"{type.FullName} Order is {order}. We're starting at 13 and we're incremying by 1.");
  48. }
  49. if (Map.ContainsKey(order))
  50. {
  51. throw new ArgumentException($"{type.FullName} Order is {order}. An element with the same Order already exists.");
  52. }
  53. else
  54. {
  55. Map.Add(order, type);
  56. }
  57. return this;
  58. }
  59. }
  60. }