QueryInTankDiagnosticReportResponse.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. namespace VeederRoot_ATG_Console.MessageEntity.Incoming
  7. {
  8. /// <summary>
  9. /// i|IA01,Probe Type and Serial Number
  10. /// </summary>
  11. public class QueryInTankDiagnosticReportResponse : IncomingMessageBase
  12. {
  13. public DateTime? CurrentDateAndTime
  14. {
  15. get
  16. {
  17. if (base.FunctionCode.Item1 == MessageFormat.Display)
  18. return null;
  19. return DateTime.ParseExact(
  20. Encoding.ASCII.GetString(base.DataFieldAndOptionalCheckSumAndETX.Take(10).ToArray()),
  21. "yyMMddHHmm", CultureInfo.InvariantCulture);
  22. }
  23. }
  24. public int? TankNumberInFunctionCode
  25. {
  26. get
  27. {
  28. if (base.FunctionCode.Item1 == MessageFormat.Display)
  29. return null;
  30. var r = int.Parse(base.FunctionCode.Item3);
  31. return r;
  32. }
  33. }
  34. public IEnumerable<ProbeInfo> Probes
  35. {
  36. get
  37. {
  38. if (base.FunctionCode.Item1 == MessageFormat.Display)
  39. return null;
  40. // skip 10 bytes of current date and time
  41. var rawDataBody = base.DataFieldAndOptionalCheckSumAndETX.Skip(10)
  42. //and exclude tail of -> && + 4 bytes check sum + ETX <- which is total 7 bytes.
  43. .Take(base.DataFieldAndOptionalCheckSumAndETX.Count - 10 - 7);
  44. var result = new List<ProbeInfo>();
  45. int elapsed = 0;
  46. while (true)
  47. {
  48. var buffer = rawDataBody.Skip(elapsed);
  49. if (!buffer.Any()) break;
  50. result.Add(new ProbeInfo(buffer.Take(27).ToArray()));
  51. elapsed += 27;
  52. }
  53. return result;
  54. }
  55. }
  56. public override string ToLogString()
  57. {
  58. if (base.FunctionCode.Item1 == MessageFormat.Display)
  59. return Encoding.ASCII.GetString(base.DataFieldAndOptionalCheckSumAndETX.ToArray());
  60. return "Current DateTime: " + this.CurrentDateAndTime.Value.ToString("yyyy-MM-dd HH:mm")
  61. + ", TankNumber: " + (this?.TankNumberInFunctionCode ?? -1)
  62. + " with Diameters list(tankNumber:probeLength:probeSerialNo.)-> "
  63. + (this.Probes != null && this.Probes.Any() ?
  64. this.Probes.Select(p => p.TankNumber + ":" + p.ProbeLength + ":" + p.ProbeSerialNumber).Aggregate((acc, n) => acc + ", " + n)
  65. : "");
  66. }
  67. public class ProbeInfo
  68. {
  69. public enum ProbeType
  70. {
  71. CAP0 = 1,
  72. CAP1 = 2,
  73. MAG1 = 3,
  74. }
  75. private byte[] bytes;
  76. public ProbeInfo(byte[] bytes)
  77. {
  78. if (bytes == null || bytes.Length != 27)
  79. throw new ArgumentException("must not null and length must equals 27");
  80. this.bytes = bytes;
  81. }
  82. public int TankNumber
  83. {
  84. get
  85. {
  86. var r = int.Parse(Encoding.ASCII.GetString(this.bytes.Take(2).ToArray()));
  87. return r;
  88. }
  89. }
  90. public char? ProductCode
  91. {
  92. get
  93. {
  94. return
  95. Encoding.ASCII.GetString(
  96. this.bytes.Skip(2).Take(1).ToArray())[0];
  97. }
  98. }
  99. public ProbeType Type
  100. {
  101. get
  102. {
  103. var _ = Enum.Parse(typeof(ProbeType),
  104. Encoding.ASCII.GetString(this.bytes.Skip(3).Take(2).ToArray()));
  105. return (ProbeType)_;
  106. }
  107. }
  108. public string CircuitCode
  109. {
  110. get
  111. {
  112. return Encoding.ASCII.GetString(this.bytes.Skip(5).Take(4).ToArray());
  113. }
  114. }
  115. public double ProbeLength
  116. {
  117. get
  118. {
  119. var asciiEncodedBytes = Util.ConvertHexBcdStrToBytes(this.bytes.Skip(9).Take(8).ToArray());
  120. var dataValue =
  121. Util.ConvertIEEEWith4BytesToDouble(asciiEncodedBytes.ToArray());
  122. return dataValue;
  123. }
  124. }
  125. public string ProbeSerialNumber
  126. {
  127. get
  128. {
  129. return Encoding.ASCII.GetString(this.bytes.Skip(17).Take(6).ToArray());
  130. }
  131. }
  132. public string ProbeDateCode
  133. {
  134. get
  135. {
  136. return Encoding.ASCII.GetString(this.bytes.Skip(23).Take(4).ToArray());
  137. }
  138. }
  139. }
  140. }
  141. }