PumpInOperationResponse_OldVersion.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Edge.Core.Parser.BinaryParser.Attributes;
  2. using Edge.Core.Parser.BinaryParser.Util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ZhongSheng_NonIC_Pump
  9. {
  10. /// <summary>
  11. /// the manufactor of the dispenser send me an updated protocol that only this message updated,
  12. /// during the my developing.
  13. /// just in case, keep this old format message entity here.
  14. /// 加油实时数据命令格式和错误状态信息格式:
  15. /// (实时信息有时会一条枪有时会多条枪,每条枪用一个信息块表示)
  16. /// </summary>
  17. public class PumpInOperationResponse_OldVersion : MessageTemplateBase
  18. {
  19. private List<byte> dataRaw;
  20. private IEnumerable<FuellingDataBlock> fuellingDataBlocks;
  21. private IEnumerable<PumpStateBlock_OldVersion> pumpStateBlocks;
  22. /// <summary>
  23. /// </summary>
  24. [EnumerableFormat("%cascade", 1, EncodingType = EncodingType.BIN)]
  25. public List<byte> DataRaw
  26. {
  27. get { return this.dataRaw; }
  28. set
  29. {
  30. this.dataRaw = value;
  31. this.fuellingDataBlocks = this.ParseFuellingDataBlocks();
  32. this.pumpStateBlocks = this.ParsePumpStateBlocks();
  33. }
  34. }
  35. private IEnumerable<FuellingDataBlock> ParseFuellingDataBlocks()
  36. {
  37. if (this.DataRaw == null || !this.DataRaw.Any()) throw new ArgumentException("DataRaw is null or empty");
  38. List<FuellingDataBlock> results = null;
  39. int elapsedCount = 0;
  40. //skip the 信息计数n(1字节)
  41. var blocks = this.DataRaw.Skip(1);
  42. while (true)
  43. {
  44. var nxt = blocks.Skip(elapsedCount).ToArray();
  45. if (!nxt.Any()) return results;
  46. if (nxt.Length < 12)
  47. return results;
  48. if (nxt[0] == 0x01)
  49. {
  50. if (results == null) results = new List<FuellingDataBlock>();
  51. elapsedCount += 12;
  52. var block = new FuellingDataBlock()
  53. {
  54. NozzleNumber = nxt[1],
  55. Amount = nxt.Skip(2).Take(4).ToInt32(),
  56. Volume = nxt.Skip(6).Take(3).ToInt32(),
  57. Price = nxt.Skip(9).Take(3).ToInt32()
  58. };
  59. results.Add(block);
  60. }
  61. else
  62. elapsedCount += 4;
  63. }
  64. return results;
  65. }
  66. private IEnumerable<PumpStateBlock_OldVersion> ParsePumpStateBlocks()
  67. {
  68. if (this.DataRaw == null || !this.DataRaw.Any()) throw new ArgumentException("DataRaw is null or empty");
  69. List<PumpStateBlock_OldVersion> results = null;
  70. int elapsedCount = 0;
  71. //skip the 信息计数n(1字节)
  72. var blocks = this.DataRaw.Skip(1);
  73. while (true)
  74. {
  75. var nxt = blocks.Skip(elapsedCount).ToArray();
  76. if (!nxt.Any()) return results;
  77. if (nxt.Length < 5)
  78. return results;
  79. //throw new ArgumentException("block data should either have length with times of 12 or 4, please check the full DataRaw: 0x" + this.DataRaw.ToHexLogString());
  80. if (nxt[0] == 0x01)
  81. {
  82. elapsedCount += 12;
  83. }
  84. else
  85. {
  86. if (results == null) results = new List<PumpStateBlock_OldVersion>();
  87. elapsedCount += 5;
  88. var block = new PumpStateBlock_OldVersion()
  89. {
  90. NozzleNumber = nxt[1],
  91. PaymentMode = (byte)(nxt.Skip(2).Take(1).ToInt32()),
  92. State = nxt.Skip(3).Take(2).ToInt32(),
  93. };
  94. results.Add(block);
  95. }
  96. }
  97. }
  98. public IEnumerable<FuellingDataBlock> FuellingDataBlocks => this.fuellingDataBlocks;
  99. public IEnumerable<PumpStateBlock_OldVersion> PumpStateBlocks => this.pumpStateBlocks;
  100. //public override string ToLogString()
  101. //{
  102. // if(this.PumpStateBlocks!=null&&this.PumpStateBlocks.Any())
  103. // return "PumpInOperationResponse report a PumpStateBlocks: "+this.PumpStateBlocks.Select(b=>b.NozzleNumber+b.PaymentMode+b.State;
  104. //}
  105. }
  106. public class PumpStateBlock_OldVersion
  107. {
  108. /// <summary>
  109. /// 枪号为以整个加油站为基础的油枪顺序编号
  110. /// </summary>
  111. public byte NozzleNumber { get; set; }
  112. public byte PaymentMode { get; set; }
  113. public int State { get; set; }
  114. }
  115. }