using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace VeederRoot_ATG_Console.MessageEntity.Incoming
{
///
/// i|I602
///
public class QueryOrSetTankProductLabelResponse : IncomingMessageBase
{
public DateTime? CurrentDateAndTime
{
get
{
if (base.FunctionCode.Item1 == MessageFormat.Display)
return null;
return DateTime.ParseExact(
Encoding.ASCII.GetString(base.DataFieldAndOptionalCheckSumAndETX.Take(10).ToArray()),
"yyMMddHHmm", CultureInfo.InvariantCulture);
}
}
public int? TankNumberInFunctionCode
{
get
{
if (base.FunctionCode.Item1 == MessageFormat.Display)
return null;
var r = int.Parse(base.FunctionCode.Item3);
return r;
}
}
///
/// Get pairs of TankNumber:ProductLabel
///
public IEnumerable> ProductLabels
{
get
{
if (base.FunctionCode.Item1 == MessageFormat.Display)
return null;
var result = new List>();
int elapsed = 10;
while (true)
{
if (base.DataFieldAndOptionalCheckSumAndETX.Skip(elapsed).Count() < 22) break;
var target = base.DataFieldAndOptionalCheckSumAndETX.Skip(elapsed).Take(22).ToArray();
var tankNumber = int.Parse(Encoding.ASCII.GetString(target.Take(2).ToArray()));
var label = Encoding.ASCII.GetString(target.Skip(2).Take(20).ToArray());
elapsed += 22;
result.Add(new Tuple(tankNumber, label));
}
return result;
}
}
public override string ToLogString()
{
if (base.FunctionCode.Item1 == MessageFormat.Display)
return Encoding.ASCII.GetString(base.DataFieldAndOptionalCheckSumAndETX.ToArray());
return "Current DateTime: " + this.CurrentDateAndTime.Value.ToString("yyyy-MM-dd HH:mm")
+ ", TankNumberInFunctionCode: " + this.TankNumberInFunctionCode.Value
+ " with ProductLabel list(tankNumber:ProductLabel)-> "
+ (this.ProductLabels != null && this.ProductLabels.Any() ?
this.ProductLabels.Select(p => p.Item1 + ":" + p.Item2).Aggregate((acc, n) => acc + ", " + n)
: "");
}
}
}