| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- using Edge.Core.Parser.BinaryParser.Attributes;
- using Edge.Core.Parser.BinaryParser.Util;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ZhongSheng_NonIC_Pump
- {
- /// <summary>
- /// the manufactor of the dispenser send me an updated protocol that only this message updated,
- /// during the my developing.
- /// just in case, keep this old format message entity here.
- /// 加油实时数据命令格式和错误状态信息格式:
- /// (实时信息有时会一条枪有时会多条枪,每条枪用一个信息块表示)
- /// </summary>
- public class PumpInOperationResponse_OldVersion : MessageTemplateBase
- {
- private List<byte> dataRaw;
- private IEnumerable<FuellingDataBlock> fuellingDataBlocks;
- private IEnumerable<PumpStateBlock_OldVersion> pumpStateBlocks;
- /// <summary>
- /// </summary>
- [EnumerableFormat("%cascade", 1, EncodingType = EncodingType.BIN)]
- public List<byte> DataRaw
- {
- get { return this.dataRaw; }
- set
- {
- this.dataRaw = value;
- this.fuellingDataBlocks = this.ParseFuellingDataBlocks();
- this.pumpStateBlocks = this.ParsePumpStateBlocks();
- }
- }
- private IEnumerable<FuellingDataBlock> ParseFuellingDataBlocks()
- {
- if (this.DataRaw == null || !this.DataRaw.Any()) throw new ArgumentException("DataRaw is null or empty");
- List<FuellingDataBlock> results = null;
- int elapsedCount = 0;
- //skip the 信息计数n(1字节)
- var blocks = this.DataRaw.Skip(1);
- while (true)
- {
- var nxt = blocks.Skip(elapsedCount).ToArray();
- if (!nxt.Any()) return results;
- if (nxt.Length < 12)
- return results;
- if (nxt[0] == 0x01)
- {
- if (results == null) results = new List<FuellingDataBlock>();
- elapsedCount += 12;
- var block = new FuellingDataBlock()
- {
- NozzleNumber = nxt[1],
- Amount = nxt.Skip(2).Take(4).ToInt32(),
- Volume = nxt.Skip(6).Take(3).ToInt32(),
- Price = nxt.Skip(9).Take(3).ToInt32()
- };
- results.Add(block);
- }
- else
- elapsedCount += 4;
- }
- return results;
- }
- private IEnumerable<PumpStateBlock_OldVersion> ParsePumpStateBlocks()
- {
- if (this.DataRaw == null || !this.DataRaw.Any()) throw new ArgumentException("DataRaw is null or empty");
- List<PumpStateBlock_OldVersion> results = null;
- int elapsedCount = 0;
- //skip the 信息计数n(1字节)
- var blocks = this.DataRaw.Skip(1);
- while (true)
- {
- var nxt = blocks.Skip(elapsedCount).ToArray();
- if (!nxt.Any()) return results;
- if (nxt.Length < 5)
- return results;
- //throw new ArgumentException("block data should either have length with times of 12 or 4, please check the full DataRaw: 0x" + this.DataRaw.ToHexLogString());
- if (nxt[0] == 0x01)
- {
- elapsedCount += 12;
- }
- else
- {
- if (results == null) results = new List<PumpStateBlock_OldVersion>();
- elapsedCount += 5;
- var block = new PumpStateBlock_OldVersion()
- {
- NozzleNumber = nxt[1],
- PaymentMode = (byte)(nxt.Skip(2).Take(1).ToInt32()),
- State = nxt.Skip(3).Take(2).ToInt32(),
- };
- results.Add(block);
- }
- }
- }
- public IEnumerable<FuellingDataBlock> FuellingDataBlocks => this.fuellingDataBlocks;
- public IEnumerable<PumpStateBlock_OldVersion> PumpStateBlocks => this.pumpStateBlocks;
- //public override string ToLogString()
- //{
- // if(this.PumpStateBlocks!=null&&this.PumpStateBlocks.Any())
- // return "PumpInOperationResponse report a PumpStateBlocks: "+this.PumpStateBlocks.Select(b=>b.NozzleNumber+b.PaymentMode+b.State;
- //}
- }
- public class PumpStateBlock_OldVersion
- {
- /// <summary>
- /// 枪号为以整个加油站为基础的油枪顺序编号
- /// </summary>
- public byte NozzleNumber { get; set; }
- public byte PaymentMode { get; set; }
- public int State { get; set; }
- }
- }
|