ForecourtXml.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using Dfs.WayneChina.IMisPlus.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Xml.Linq;
  8. namespace Dfs.WayneChina.IMisPlus
  9. {
  10. /// <summary>
  11. /// Manages the xml settings part of the legacy iFuel forecourt configuration.
  12. /// </summary>
  13. public class ForecourtXml
  14. {
  15. #region Fields
  16. private XDocument xdoc;
  17. #endregion
  18. #region Properties
  19. public ForecourtConfig ForecourtConfig { get; set; }
  20. #endregion
  21. public bool Load(string fileName)
  22. {
  23. xdoc = XDocument.Load(fileName);
  24. return ParseConfig();
  25. }
  26. public bool Parse(string xml)
  27. {
  28. xdoc = XDocument.Parse(xml);
  29. return ParseConfig();
  30. }
  31. private bool ParseConfig()
  32. {
  33. var configRoot = xdoc.Document.Element("ForecourtConfig");
  34. //Parse <ForecourtConfig/>
  35. ForecourtConfig = new ForecourtConfig();
  36. ForecourtConfig.CurrentVersion = configRoot.Attribute("Version").Value;
  37. Console.WriteLine(configRoot.Attribute("Version").Value);
  38. //Parse <TerminalConfig/>
  39. var terminalConfig = configRoot.Descendants("TerminalConfig").First();
  40. var terminals = from term in terminalConfig.Descendants("Terminal")
  41. select new Terminal
  42. {
  43. Id = Convert.ToByte(term.Attribute("Id").Value),
  44. PayMode = Convert.ToByte(term.Attribute("PayMode").Value),
  45. ProtocolType = Convert.ToByte(term.Attribute("ProtocolType").Value),
  46. Address = term.Attribute("Address").Value
  47. };
  48. ForecourtConfig.Terminals = terminals.ToList();
  49. foreach (var item in ForecourtConfig.Terminals)
  50. {
  51. Console.WriteLine("Terminal Id: {0}, Pay mode: {1}, Protocol type: {2}, Address: {3}",
  52. item.Id, item.PayMode, item.ProtocolType, item.Address);
  53. }
  54. //Parse <DispenserConfig/>
  55. var dispenserConfig = configRoot.Descendants("DispenserConfig").First();
  56. var disps = from disp in dispenserConfig.Descendants("Dispenser")
  57. select new Dispenser
  58. {
  59. DispNo = disp.Attribute("DispNo").Value,
  60. DispType = disp.Attribute("DispType").Value,
  61. Manufacturer = disp.Attribute("Manufacturer").Value,
  62. ProtocolType = Convert.ToByte(disp.Attribute("Protocol").Value),
  63. Address = disp.Attribute("Address").Value,
  64. Life = Convert.ToUInt16(disp.Attribute("Life").Value),
  65. State = disp.Attribute("State").Value,
  66. ManufacturingNo = disp.Attribute("ManufacturingNo").Value,
  67. DateOfManufacture = GetFormattedDate(disp.Attribute("DateOfManufacture").Value)
  68. };
  69. ForecourtConfig.Dispensers = disps.ToList();
  70. foreach (var item in ForecourtConfig.Dispensers)
  71. {
  72. Console.WriteLine("Dispenser number: {0}, type: {1}, manufacturer: {2}, protocol type: {3}, address: {4}, life: {5}, state: {6}, date of manufacture: {7} nozzle count: {8}",
  73. item.DispNo, item.DispType, item.Manufacturer, item.ProtocolType, item.Address, item.Life, item.State, item.DateOfManufacture, item.NozzleCount);
  74. }
  75. //Parse <PumpConfig/>
  76. var pumpConfig = configRoot.Descendants("PumpConfig").First();
  77. var pumps = from pump in pumpConfig.Descendants("Pump")
  78. select new Pump
  79. {
  80. Id = Convert.ToByte(pump.Attribute("Id").Value),
  81. PosId = Convert.ToByte(pump.Attribute("PosId").Value),
  82. DispType = Convert.ToByte(pump.Attribute("DispType").Value),
  83. DispTypeName = pump.Attribute("DispTypeName").Value,
  84. TankId = Convert.ToByte(pump.Attribute("TankId").Value),
  85. AppProtocol = Convert.ToUInt16(pump.Attribute("AppProtocol").Value),
  86. ProtocolType = Convert.ToByte(pump.Attribute("ProtocolType").Value),
  87. Port = pump.Attribute("Port").Value,
  88. SerialPort = Convert.ToUInt16(pump.Attribute("SerialPort").Value),
  89. Node = Convert.ToUInt16(pump.Attribute("Node").Value),
  90. SubAddress = Convert.ToByte(pump.Attribute("SubAddress").Value),
  91. Mode = Convert.ToByte(pump.Attribute("Mode").Value),
  92. PayStackSize = Convert.ToByte(pump.Attribute("PayStack").Value),
  93. CheckOutMode = Convert.ToByte(pump.Attribute("CheckOutMode").Value),
  94. MaxAmount = Convert.ToUInt32(pump.Attribute("MaxAmount").Value),
  95. MaxVolume = Convert.ToUInt32(pump.Attribute("MaxVolume").Value),
  96. Nozzles = (from noz in pump.Descendants("Nozzle")
  97. select new Nozzle
  98. {
  99. Id = Convert.ToByte(noz.Attribute("Id").Value),
  100. PhysicalId = Convert.ToByte(noz.Attribute("PhysicalId").Value),
  101. DispNo = noz.Attribute("DispNo").Value,
  102. SiteNozzleNo = Convert.ToUInt16(noz.Attribute("SiteNozzleNo").Value),
  103. TankId = Convert.ToByte(noz.Attribute("TankId").Value),
  104. ProductCode = noz.Attribute("ProductCode").Value,
  105. State = noz.Attribute("State").Value,
  106. VehicleIdReaderAddress = noz.Attribute("VehicleIdReaderAddress").Value
  107. }).ToList()
  108. };
  109. ForecourtConfig.Pumps = pumps.ToList();
  110. foreach (var p in ForecourtConfig.Pumps)
  111. {
  112. Console.WriteLine("Pump Id: {0}", p.Id);
  113. foreach (var n in p.Nozzles)
  114. {
  115. Console.WriteLine("Nozzle Id: {0}", n.Id);
  116. }
  117. }
  118. //Parse <TankConfig/>
  119. var tankConfig = configRoot.Descendants("TankConfig").First();
  120. var tanks = from tank in tankConfig.Descendants("Tank")
  121. select new Tank
  122. {
  123. Id = Convert.ToByte(tank.Attribute("TankId")),
  124. };
  125. foreach (var t in tanks)
  126. {
  127. Console.WriteLine("Tank Id: {0}, Serial number: {1}", t.Id, "");
  128. }
  129. //Parse <FuelConfig/>
  130. var fuelConfig = configRoot.Descendants("FuelConfig").First();
  131. var fuels = from fuel in fuelConfig.Descendants("Fuel")
  132. select new Fuel
  133. {
  134. Id = Convert.ToInt32(fuel.Attribute("Id").Value),
  135. ProductCode = fuel.Attribute("ProductCode").Value,
  136. ClassNo = fuel.Attribute("ClassNo").Value,
  137. Name = fuel.Attribute("Name").Value,
  138. Price = Convert.ToInt32(fuel.Attribute("Price").Value),
  139. OldPrice = Convert.ToInt32(fuel.Attribute("OldPrice").Value),
  140. UnitMeasure = fuel.Attribute("UnitMeasure").Value,
  141. Storage = Convert.ToInt32(fuel.Attribute("Storage").Value),
  142. TaxPer = Convert.ToInt32(fuel.Attribute("TaxPer").Value),
  143. Density = fuel.Attribute("Density").Value
  144. };
  145. ForecourtConfig.Fuels = fuels.ToList();
  146. return true;
  147. }
  148. public bool AddNozzle(Nozzle nozzle)
  149. {
  150. return true;
  151. }
  152. public bool AddPump(Pump pump)
  153. {
  154. if (pump == null)
  155. return false;
  156. if (ForecourtConfig.Pumps.Any(p => p.Id == pump.Id))
  157. return false;
  158. ForecourtConfig.Pumps.Add(pump);
  159. return true;
  160. }
  161. private DateTime GetFormattedDate(string dateString)
  162. {
  163. DateTime dateOfManufacture;
  164. string format = "yyyy-MM-dd";
  165. bool result =
  166. DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateOfManufacture);
  167. return result ? dateOfManufacture : DateTime.MinValue;
  168. }
  169. }
  170. }