using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace VeederRoot_ATG_Console.MessageEntity.Incoming
{
///
/// i|IA01,Probe Type and Serial Number
///
public class QueryInTankDiagnosticReportResponse : 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;
}
}
public IEnumerable Probes
{
get
{
if (base.FunctionCode.Item1 == MessageFormat.Display)
return null;
// skip 10 bytes of current date and time
var rawDataBody = base.DataFieldAndOptionalCheckSumAndETX.Skip(10)
//and exclude tail of -> && + 4 bytes check sum + ETX <- which is total 7 bytes.
.Take(base.DataFieldAndOptionalCheckSumAndETX.Count - 10 - 7);
var result = new List();
int elapsed = 0;
while (true)
{
var buffer = rawDataBody.Skip(elapsed);
if (!buffer.Any()) break;
result.Add(new ProbeInfo(buffer.Take(27).ToArray()));
elapsed += 27;
}
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")
+ ", TankNumber: " + (this?.TankNumberInFunctionCode ?? -1)
+ " with Diameters list(tankNumber:probeLength:probeSerialNo.)-> "
+ (this.Probes != null && this.Probes.Any() ?
this.Probes.Select(p => p.TankNumber + ":" + p.ProbeLength + ":" + p.ProbeSerialNumber).Aggregate((acc, n) => acc + ", " + n)
: "");
}
public class ProbeInfo
{
public enum ProbeType
{
CAP0 = 1,
CAP1 = 2,
MAG1 = 3,
}
private byte[] bytes;
public ProbeInfo(byte[] bytes)
{
if (bytes == null || bytes.Length != 27)
throw new ArgumentException("must not null and length must equals 27");
this.bytes = bytes;
}
public int TankNumber
{
get
{
var r = int.Parse(Encoding.ASCII.GetString(this.bytes.Take(2).ToArray()));
return r;
}
}
public char? ProductCode
{
get
{
return
Encoding.ASCII.GetString(
this.bytes.Skip(2).Take(1).ToArray())[0];
}
}
public ProbeType Type
{
get
{
var _ = Enum.Parse(typeof(ProbeType),
Encoding.ASCII.GetString(this.bytes.Skip(3).Take(2).ToArray()));
return (ProbeType)_;
}
}
public string CircuitCode
{
get
{
return Encoding.ASCII.GetString(this.bytes.Skip(5).Take(4).ToArray());
}
}
public double ProbeLength
{
get
{
var asciiEncodedBytes = Util.ConvertHexBcdStrToBytes(this.bytes.Skip(9).Take(8).ToArray());
var dataValue =
Util.ConvertIEEEWith4BytesToDouble(asciiEncodedBytes.ToArray());
return dataValue;
}
}
public string ProbeSerialNumber
{
get
{
return Encoding.ASCII.GetString(this.bytes.Skip(17).Take(6).ToArray());
}
}
public string ProbeDateCode
{
get
{
return Encoding.ASCII.GetString(this.bytes.Skip(23).Take(4).ToArray());
}
}
}
}
}