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
{
///
/// 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.
/// 加油实时数据命令格式和错误状态信息格式:
/// (实时信息有时会一条枪有时会多条枪,每条枪用一个信息块表示)
///
public class PumpInOperationResponse_OldVersion : MessageTemplateBase
{
private List dataRaw;
private IEnumerable fuellingDataBlocks;
private IEnumerable pumpStateBlocks;
///
///
[EnumerableFormat("%cascade", 1, EncodingType = EncodingType.BIN)]
public List DataRaw
{
get { return this.dataRaw; }
set
{
this.dataRaw = value;
this.fuellingDataBlocks = this.ParseFuellingDataBlocks();
this.pumpStateBlocks = this.ParsePumpStateBlocks();
}
}
private IEnumerable ParseFuellingDataBlocks()
{
if (this.DataRaw == null || !this.DataRaw.Any()) throw new ArgumentException("DataRaw is null or empty");
List 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();
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 ParsePumpStateBlocks()
{
if (this.DataRaw == null || !this.DataRaw.Any()) throw new ArgumentException("DataRaw is null or empty");
List 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();
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 FuellingDataBlocks => this.fuellingDataBlocks;
public IEnumerable 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
{
///
/// 枪号为以整个加油站为基础的油枪顺序编号
///
public byte NozzleNumber { get; set; }
public byte PaymentMode { get; set; }
public int State { get; set; }
}
}