FuelModeAndPriceModeConfig.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Xml;
  5. using Wayne.ForecourtControl;
  6. using Wayne.Lib.IO;
  7. namespace LSForecourtSimulatorImpl
  8. {
  9. /// <summary>
  10. /// Hardcoded temporary solution!
  11. /// </summary>
  12. public class FuelModeAndPriceModeConfig : IForecourtConfiguration
  13. {
  14. private readonly IFileSupport fileSupport;
  15. private XmlDocument xmlDocument;
  16. public FuelModeAndPriceModeConfig(IFileSupport fileSupport)
  17. {
  18. this.fileSupport = fileSupport;
  19. ReadConfig(@"c:\Wayne\Config\FuelModeConfig\FuelModeConfig.xml");
  20. }
  21. public FuelModeAndPriceModeConfig(string configFilePath, IFileSupport fileSupport)
  22. {
  23. this.fileSupport = fileSupport;
  24. ReadConfig(configFilePath);
  25. }
  26. private void ReadConfig(string filePath)
  27. {
  28. xmlDocument = new XmlDocument();
  29. fileSupport.LoadXml(xmlDocument, filePath, Encoding.UTF8);
  30. }
  31. #region Implementation of IForecourtConfiguration
  32. public IEnumerable<IPumpConfiguration> Pumps { get; private set; }
  33. public IEnumerable<ITankConfiguration> Tanks { get; private set; }
  34. public IEnumerable<ITankSuctionConfiguration> TankSuctions { get; private set; }
  35. public IEnumerable<IFuelProductConfiguration> Products { get; private set; }
  36. public IEnumerable<IFuelGradeConfiguration> Grades { get; private set; }
  37. public IEnumerable<IFuelGradeConfiguration> GetPumpGrades(IDeviceIndex pump)
  38. {
  39. throw new NotImplementedException();
  40. }
  41. public IEnumerable<ITankMonitorConfiguration> TankMonitors { get; private set; }
  42. public IEnumerable<IPricePoleConfiguration> PricePoles { get; private set; }
  43. public IEnumerable<IFuelPrice> Prices { get; private set; }
  44. /// <summary>
  45. /// Gets a fuel mode, given the fuelling type and the price group
  46. /// </summary>
  47. /// <param name="fuellingType">The fuelling type</param>
  48. /// <param name="priceGroup">The price group</param>
  49. public int GetFuelMode(FuellingType fuellingType, PriceGroup priceGroup)
  50. {
  51. try
  52. {
  53. var configNode =
  54. xmlDocument.SelectSingleNode(
  55. string.Concat("/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType[@t='",
  56. (int)fuellingType, "']/PriceGroup[@g='", (int)priceGroup) + "']");
  57. var fm = XmlConvert.ToInt32(configNode.InnerText);
  58. return fm;
  59. }
  60. catch (Exception)
  61. {
  62. return 1;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets a post-paid fuel mode, given the price group and the authorized in advance flag
  67. /// </summary>
  68. /// <param name="priceGroup">The price group</param>
  69. /// <param name="authInAdvance">Authorized in advance flag</param>
  70. public int GetFuelMode(PriceGroup priceGroup, bool authInAdvance)
  71. {
  72. try
  73. {
  74. var authType = authInAdvance ? "AuthInAdvance" : "NormalAuth";
  75. var configNode =
  76. xmlDocument.SelectSingleNode(
  77. string.Concat("/FuelModeConfig/FuelMode_For_PreAuthorize/" + authType + "/PriceGroup_",
  78. (int)priceGroup));
  79. var fm = Convert.ToInt32(configNode.InnerText);
  80. return fm;
  81. }
  82. catch (Exception)
  83. {
  84. return 1;
  85. }
  86. }
  87. /// <summary>
  88. /// Gets a fuel mode, given the fuelling type
  89. /// </summary>
  90. /// <param name="fuellingType">The fuelling type</param>
  91. public int GetFuelMode(FuellingType fuellingType)
  92. {
  93. try
  94. {
  95. var configNode =
  96. xmlDocument.SelectSingleNode(
  97. string.Concat("/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType[@t='",
  98. (int)fuellingType) + "']/PriceGroup");
  99. var fm = Convert.ToInt32(configNode.InnerText);
  100. return fm;
  101. }
  102. catch (Exception)
  103. {
  104. return 1;
  105. }
  106. }
  107. /// <summary>
  108. /// Gets the default fuel mode for the given price group
  109. /// </summary>
  110. /// <param name="priceGroup">The price group</param>
  111. public int GetDefaultFuelMode(PriceGroup priceGroup)
  112. {
  113. try
  114. {
  115. var configNode =
  116. xmlDocument.SelectSingleNode(string.Concat("/FuelModeConfig/DefaultFuelModeTable/PriceGroup_",
  117. (int)priceGroup));
  118. var fm = Convert.ToInt32(configNode.InnerText);
  119. return fm;
  120. }
  121. catch (Exception)
  122. {
  123. return 1;
  124. }
  125. }
  126. /// <summary>
  127. /// Gets a collection of fuel modes, given the price group
  128. /// </summary>
  129. /// <param name="priceGroup">The price group</param>
  130. public IEnumerable<int> GetFuelModes(PriceGroup priceGroup)
  131. {
  132. try
  133. {
  134. var configNodeList =
  135. xmlDocument.SelectNodes(
  136. string.Concat(
  137. "/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType/PriceGroup[@g='",
  138. (int)priceGroup) + "']");
  139. var fmList = new List<int>();
  140. foreach (XmlNode xn in configNodeList)
  141. {
  142. fmList.Add(Convert.ToInt32(xn.InnerText));
  143. }
  144. return (fmList);
  145. }
  146. catch (Exception)
  147. {
  148. return new List<int>();
  149. }
  150. }
  151. /// <summary>
  152. /// Gets the price group related with the given fuel mode
  153. /// </summary>
  154. /// <param name="fuelMode">The fuel mode</param>
  155. public PriceGroup GetPriceGroup(int fuelMode, FuellingType fuellingType)
  156. {
  157. try
  158. {
  159. var configNodeList =
  160. xmlDocument.SelectNodes(
  161. string.Concat("/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType[@t='", (int)fuellingType, "']/PriceGroup"));
  162. foreach (XmlNode xn in configNodeList)
  163. {
  164. var fm = Convert.ToInt32(xn.InnerText);
  165. if (fm == fuelMode)
  166. {
  167. var pgv = Convert.ToInt32(xn.Attributes["g"].Value);
  168. var pg = (PriceGroup)pgv;
  169. return pg; // found first matching fuel mode
  170. }
  171. }
  172. return PriceGroup.Unknown;
  173. }
  174. catch (Exception)
  175. {
  176. return PriceGroup.Unknown;
  177. }
  178. return PriceGroup.Unknown;
  179. }
  180. /// <summary>
  181. /// Gets the price groups related with the given fuel mode
  182. /// </summary>
  183. /// <param name="fuelMode">The fuel mode</param>
  184. public IEnumerable<int> GetPriceGroups(int fuelMode)
  185. {
  186. try
  187. {
  188. XmlNodeList configNodeList =
  189. xmlDocument.SelectNodes(
  190. "/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType/PriceGroup");
  191. IList<int> pgList = new List<int>();
  192. foreach (XmlNode xn in configNodeList)
  193. {
  194. int fm = Convert.ToInt32(xn.InnerText);
  195. if (fm == fuelMode)
  196. {
  197. var pg = Convert.ToInt32(xn.Attributes["g"].Value);
  198. pgList.Add(pg);
  199. }
  200. }
  201. return pgList;
  202. }
  203. catch (Exception)
  204. {
  205. return new List<int>();
  206. }
  207. }
  208. /// <summary>
  209. /// Gets the fuelling type related with the given fuel mode and price group
  210. /// </summary>
  211. /// <param name="fuelMode">fuel mode</param>
  212. /// <param name="priceGroup">price group</param>
  213. public FuellingType GetFuellingType(int fuelMode, PriceGroup priceGroup)
  214. {
  215. try
  216. {
  217. var configNodeList =
  218. xmlDocument.SelectNodes("/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType");
  219. foreach (XmlNode xn in configNodeList)
  220. {
  221. foreach (XmlElement cn in xn.ChildNodes)
  222. {
  223. int fm = Convert.ToInt32(cn.InnerText);
  224. var pg = Convert.ToInt32(cn.Attributes["g"].Value);
  225. if (fm == fuelMode && pg == (int)priceGroup)
  226. {
  227. var ft = (FuellingType)Convert.ToInt32(xn.Attributes["t"].Value);
  228. return ft;
  229. }
  230. }
  231. }
  232. return FuellingType.Unknown;
  233. }
  234. catch (Exception)
  235. {
  236. return FuellingType.Unknown;
  237. }
  238. }
  239. public bool WriteFuelPrice(int productId, int fuelMode, decimal newPrice)
  240. {
  241. return false;
  242. }
  243. public event AsyncCompletedEventHandler OnConfigurationChange;
  244. #endregion
  245. }
  246. }