MessageTemplateLookup.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Edge.Core.Parser.BinaryParser;
  2. using Edge.Core.Parser.BinaryParser.MessageEntity;
  3. using Edge.Core.Parser.BinaryParser.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace FuRen_Sinopec_IcCardReader
  11. {
  12. public class MessageTemplateLookup : IMessageTemplateLookup
  13. {
  14. /// <summary>
  15. /// Gets the default singleton instance of type MessageTemplateLookup.
  16. /// </summary>
  17. public static MessageTemplateLookup Default { get; } = new MessageTemplateLookup();
  18. /// <summary>
  19. /// Create a message entity based on input whole message raw bytes which is: 2bytes Len + 1 byte AppId + 1 byte SSK
  20. /// + variable length message code + variable length message body
  21. /// </summary>
  22. /// <param name="bytes">whole message raw bytes</param>
  23. /// <returns>new created message entity</returns>
  24. public MessageTemplateBase GetMessageTemplateByRawBytes(byte[] bytes)
  25. {
  26. // from protocol definition, the msg body started at 7th byte, and the 7th bytes is always the msg type code.
  27. var msgHandleCode = bytes.Skip(6).First();
  28. switch (msgHandleCode)
  29. {
  30. //case 0x30:
  31. // return new PumpGenericInquiryCommand();
  32. case 0x31:
  33. return new PumpStateChangeCommand();
  34. case 0x32:
  35. return new PumpNotifyTransactionDoneCommand();
  36. case 0x3A:
  37. return new FuRen_PcActivelyReadPumpConfigurationInfoResponse();
  38. }
  39. 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));
  40. }
  41. }
  42. }