MessageBase.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Edge.Core.Parser.BinaryParser.MessageEntity;
  2. using Edge.Core.Parser.BinaryParser.Attributes;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Edge.Core.Parser.BinaryParser.Util;
  9. namespace Wayne_Pump_Dart.MessageEntity
  10. {
  11. public enum ControlCharacter
  12. {
  13. POLL = 0x20,
  14. DATA = 0x30,
  15. ACK = 0xC0,
  16. NAK = 0x50,
  17. EOT = 0x70,
  18. AckPoll = 0xE0,
  19. }
  20. public class MessageBase : MessageTemplateBase
  21. {
  22. /// <summary>
  23. /// physical pump side address, must range from 0x50 to 0x6F
  24. /// </summary>
  25. [Format(1, EncodingType.BIN, -9999)]
  26. [Range(0x50, 0x6F, "Wayne Dart pump address must be range from {1} to {2}, but actual is {0}")]
  27. public byte Adrs { get; set; }
  28. [Format(1, EncodingType.BIN, -9950)]
  29. public byte CtrlCharAndBlockSeqNoRaw { get; set; }
  30. /// <summary>
  31. /// high 4 bits of ControlCharacterAndBlockSeqNumberRaw.
  32. /// </summary>
  33. public ControlCharacter ControlCharacter
  34. {
  35. get
  36. {
  37. var r = Enum.ToObject(typeof(ControlCharacter),
  38. CtrlCharAndBlockSeqNoRaw & 0xF0);
  39. return (ControlCharacter)r;
  40. }
  41. set
  42. {
  43. this.CtrlCharAndBlockSeqNoRaw =
  44. this.CtrlCharAndBlockSeqNoRaw.SetBit(4, 7, (int)value >> 4);
  45. }
  46. }
  47. /// <summary>
  48. /// from low 4 bits of ControlCharacterAndBlockSeqNumberRaw, range from 0x00 to 0x0F.
  49. /// </summary>
  50. public byte BlockSeqNumber
  51. {
  52. get
  53. {
  54. return (byte)(CtrlCharAndBlockSeqNoRaw & 0x0F);
  55. }
  56. set
  57. {
  58. this.CtrlCharAndBlockSeqNoRaw =
  59. this.CtrlCharAndBlockSeqNoRaw.SetBit(0, 3, (int)value);
  60. }
  61. }
  62. [EnumerableFormat("%cascade", -9920)]
  63. public List<TransactionData> TransactionDatas { get; set; }
  64. //[EnumerableFormat(4, 9999)]
  65. //public virtual List<byte> CRC_ETX_SF { get; set; }
  66. public override string ToLogString()
  67. {
  68. if (this.ControlCharacter == ControlCharacter.EOT)
  69. return "EOT(Adrs: 0x" + this.Adrs.ToString("X").PadLeft(2, '0') + ", SeqNo: 0x" + this.BlockSeqNumber.ToString("X").PadLeft(2, '0') + ")";
  70. else if (this.ControlCharacter == ControlCharacter.ACK)
  71. return "ACK(Adrs: 0x" + this.Adrs.ToString("X").PadLeft(2, '0') + ", SeqNo: 0x" + this.BlockSeqNumber.ToString("X").PadLeft(2, '0') + ")";
  72. else
  73. return base.ToLogString();
  74. }
  75. }
  76. public class TransactionData : IFormattable
  77. {
  78. [Format(1, EncodingType.BIN, -9999)]
  79. public byte TransactionNumber { get; set; }
  80. [Format(1, EncodingType.BIN, -9990)]
  81. public int Length { get; set; }
  82. [EnumerableFormat("Length", -9950)]
  83. public List<byte> RawData { get; set; }
  84. public string ToString(string format, IFormatProvider formatProvider)
  85. {
  86. /* these TransactioNumber have different meanings for
  87. request and response, here the implementation is only
  88. for response(from pump to FC) */
  89. // Pump status
  90. if (this.TransactionNumber == 1)
  91. {
  92. switch (this.RawData.First())
  93. {
  94. case 0: return "PUMP NOT PROGRAMMED";
  95. case 1: return "RESET";
  96. case 2: return "AUTHORIZED";
  97. case 4: return "FILLING";
  98. case 5: return "FILLING COMPLETED";
  99. case 6: return "MAX AMOUNT/VOLUME REACHED";
  100. case 7: return "SWITCHED OFF";
  101. case 8: return "SUSPENDED";
  102. }
  103. }
  104. // Filled volume and amount
  105. else if (this.TransactionNumber == 2)
  106. {
  107. return "Filled volume and amount";
  108. }
  109. // Nozzle status and filling price
  110. else if (this.TransactionNumber == 3)
  111. {
  112. return "Nozzle status and filling price";
  113. }
  114. // Total Counters
  115. else if (this.TransactionNumber == 0x65)
  116. {
  117. return "Total Counters";
  118. }
  119. return "";
  120. }
  121. }
  122. }