MessageTemplateLookup.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 LanTian_Sinopec_PumpIcCardReader
  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. /// </summary>
  20. /// <param name="bytes">whole message raw bytes</param>
  21. /// <returns>new created message entity</returns>
  22. public MessageTemplateBase GetMessageTemplateByRawBytes(byte[] bytes)
  23. {
  24. // from protocol definition, the msg body started at 7th byte, and the 7th bytes is always the msg type code.
  25. var msgHandleCode = bytes.Skip(6).First();
  26. switch (msgHandleCode)
  27. {
  28. //case 0x30:
  29. // return new PumpGenericInquiryCommand();
  30. case 0x31:
  31. return new PumpRealTimeStateEvent();
  32. case 0x32:
  33. if (bytes.Length > 12)
  34. return new PumpNotifyTransactionDoneRequest();
  35. else
  36. return new PcReadTransactionButDoesNotFindResponse();
  37. case 0x33:
  38. return new PumpAskDataDownloadRequest();
  39. case 0x35:
  40. return new PumpInquiryGrayCardRecordRequest();
  41. case 0x36:
  42. return new PumpInquiryBlackAndWhiteListRequest();
  43. case 0x3A:
  44. return new PcReadPumpInfoResponse();
  45. case 0x3B:
  46. return new PumpInternalErrorRequest();
  47. case 0x50:
  48. return new PumpInquiryCreditCardRecordRequest();
  49. }
  50. 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));
  51. }
  52. }
  53. }