MessageTemplateLookup.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 ZhongSheng_NonIC_Pump
  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 Edge.Core.Parser.BinaryParser.MessageEntity.MessageTemplateBase GetMessageTemplateByRawBytes(byte[] bytes)
  25. {
  26. var commandCode = bytes.Skip(5).First();
  27. switch (commandCode)
  28. {
  29. case 0x31:
  30. var subCode = bytes.Skip(6).First();
  31. if (subCode == 0)
  32. return new PumpInIdleResponse();
  33. else
  34. return new PumpInOperationResponse();
  35. case 0x32:
  36. return new PumpNotifyTransactionDoneEvent();
  37. case 0x33:
  38. return new ReadPumpAccumulatorResponse();
  39. case 0x34:
  40. return new AckChangePumpPriceResponse();
  41. case 0x35:
  42. return new AckStartPumpResponse();
  43. case 0x36:
  44. return new AckStopPumpResponse();
  45. case 0x37:
  46. return new AckDisplayResponse();
  47. case 0x52:
  48. {
  49. subCode = bytes.Skip(6).First();
  50. if (subCode == 0x01)
  51. return new AckSetPumpConfigResponse();
  52. else if (subCode == 0x02)
  53. return new ReadPumpConfigResponse();
  54. }
  55. throw new ArgumentException("PC 发送参数信息字符串给油机,或者读取参数信息字符串从加油机, 但subcode即不是0x01也不是0x02");
  56. }
  57. 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));
  58. }
  59. }
  60. }