MessageTemplateLookup.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Common.Config;
  2. using Parser.BinaryParser;
  3. using Parser.BinaryParser.MessageEntity;
  4. using Parser.BinaryParser.Util;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace HengShan_Pump_NonIC
  12. {
  13. public class MessageTemplateLookup : IMessageTemplateLookup
  14. {
  15. private static readonly MessageTemplateLookup defaultInstance = new MessageTemplateLookup();
  16. /// <summary>
  17. /// Gets the default singleton instance of type MessageTemplateLookup.
  18. /// </summary>
  19. public static MessageTemplateLookup Default => defaultInstance;
  20. /// <summary>
  21. /// resolve from the config
  22. /// </summary>
  23. private static readonly IEnumerable<MappingEntry> lookup
  24. = MessageLookupConfigurationSectionHandler.GetConfig().DeviceTypes.Cast<DeviceType>().First(d => d.DeviceTypeString == "hengShan_Pump_NonIC").Cast<MappingEntry>();
  25. /// <summary>
  26. /// Create a message entity based on input whole message raw bytes which is: 2bytes Len + 1 byte AppId + 1 byte SSK
  27. /// + variable length message code + variable length message body
  28. /// </summary>
  29. /// <param name="bytes">whole message raw bytes</param>
  30. /// <returns>new created message entity</returns>
  31. public MessageTemplateBase GetMessageTemplateByRawBytes(byte[] bytes)
  32. {
  33. try
  34. {
  35. string debug = bytes.ToHexLogString();//be convenient to see log
  36. var type = GetMessageTemplateTypeByRawBytes(bytes);
  37. var targetInstance = (MessageTemplateBase)Activator.CreateInstance(type);
  38. return targetInstance;
  39. }
  40. catch (Exception ex)
  41. {
  42. throw new ArgumentException("exception message = " + ex.Message);
  43. }
  44. }
  45. /// <summary>
  46. /// Create a message entity based on input whole message raw bytes which is: 2bytes Len + 1 byte AppId + 1 byte SSK
  47. /// + variable length message code + variable length message body
  48. /// </summary>
  49. /// <param name="bytes">whole message raw bytes</param>
  50. /// <returns>new created message entity</returns>
  51. private Type GetMessageTemplateTypeByRawBytes(byte[] bytes)
  52. {
  53. string debug = bytes.Select(s => s.ToString("X").PadLeft(2, '0')).Aggregate((acc, n) => acc + " " + n);//be convenient to see log
  54. //trace.TraceInformation("in the method GetMessageEntityTypeByRawBytes, debug=" + debug);
  55. // from protocol definition, the msg body started at 7th byte, and the 7th bytes is always the msg type code.
  56. var msgHandleCode = bytes.Skip(2).First();
  57. var found = lookup.FirstOrDefault(a => a.CodeString == ("0x" + msgHandleCode.ToString("X").PadLeft(2, '0')));
  58. if (found != null)
  59. {
  60. var t = Type.GetType(found.TypeRawString);
  61. return t;
  62. }
  63. else
  64. {
  65. throw new ArgumentException("Can't find correlated message entity type for incoming raw message bytes: " + bytes.Select(s => s.ToString("X").PadLeft(2, '0')).Aggregate((n, acc) => n + " " + acc));
  66. }
  67. }
  68. }
  69. }