QueryOrSetTankProductLabelResponse.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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|I602
  10. /// </summary>
  11. public class QueryOrSetTankProductLabelResponse : 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. /// <summary>
  35. /// Get pairs of TankNumber:ProductLabel
  36. /// </summary>
  37. public IEnumerable<Tuple<int, string>> ProductLabels
  38. {
  39. get
  40. {
  41. if (base.FunctionCode.Item1 == MessageFormat.Display)
  42. return null;
  43. var result = new List<Tuple<int, string>>();
  44. int elapsed = 10;
  45. while (true)
  46. {
  47. if (base.DataFieldAndOptionalCheckSumAndETX.Skip(elapsed).Count() < 22) break;
  48. var target = base.DataFieldAndOptionalCheckSumAndETX.Skip(elapsed).Take(22).ToArray();
  49. var tankNumber = int.Parse(Encoding.ASCII.GetString(target.Take(2).ToArray()));
  50. var label = Encoding.ASCII.GetString(target.Skip(2).Take(20).ToArray());
  51. elapsed += 22;
  52. result.Add(new Tuple<int, string>(tankNumber, label));
  53. }
  54. return result;
  55. }
  56. }
  57. public override string ToLogString()
  58. {
  59. if (base.FunctionCode.Item1 == MessageFormat.Display)
  60. return Encoding.ASCII.GetString(base.DataFieldAndOptionalCheckSumAndETX.ToArray());
  61. return "Current DateTime: " + this.CurrentDateAndTime.Value.ToString("yyyy-MM-dd HH:mm")
  62. + ", TankNumberInFunctionCode: " + this.TankNumberInFunctionCode.Value
  63. + " with ProductLabel list(tankNumber:ProductLabel)-> "
  64. + (this.ProductLabels != null && this.ProductLabels.Any() ?
  65. this.ProductLabels.Select(p => p.Item1 + ":" + p.Item2).Aggregate((acc, n) => acc + ", " + n)
  66. : "");
  67. }
  68. }
  69. }