using System; using System.Collections.Generic; using System.Text; using System.Xml; using Wayne.ForecourtControl; using Wayne.Lib.IO; namespace LSForecourtSimulatorImpl { /// /// Hardcoded temporary solution! /// public class FuelModeAndPriceModeConfig : IForecourtConfiguration { private readonly IFileSupport fileSupport; private XmlDocument xmlDocument; public FuelModeAndPriceModeConfig(IFileSupport fileSupport) { this.fileSupport = fileSupport; ReadConfig(@"c:\Wayne\Config\FuelModeConfig\FuelModeConfig.xml"); } public FuelModeAndPriceModeConfig(string configFilePath, IFileSupport fileSupport) { this.fileSupport = fileSupport; ReadConfig(configFilePath); } private void ReadConfig(string filePath) { xmlDocument = new XmlDocument(); fileSupport.LoadXml(xmlDocument, filePath, Encoding.UTF8); } #region Implementation of IForecourtConfiguration public IEnumerable Pumps { get; private set; } public IEnumerable Tanks { get; private set; } public IEnumerable TankSuctions { get; private set; } public IEnumerable Products { get; private set; } public IEnumerable Grades { get; private set; } public IEnumerable GetPumpGrades(IDeviceIndex pump) { throw new NotImplementedException(); } public IEnumerable TankMonitors { get; private set; } public IEnumerable PricePoles { get; private set; } public IEnumerable Prices { get; private set; } /// /// Gets a fuel mode, given the fuelling type and the price group /// /// The fuelling type /// The price group public int GetFuelMode(FuellingType fuellingType, PriceGroup priceGroup) { try { var configNode = xmlDocument.SelectSingleNode( string.Concat("/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType[@t='", (int)fuellingType, "']/PriceGroup[@g='", (int)priceGroup) + "']"); var fm = XmlConvert.ToInt32(configNode.InnerText); return fm; } catch (Exception) { return 1; } } /// /// Gets a post-paid fuel mode, given the price group and the authorized in advance flag /// /// The price group /// Authorized in advance flag public int GetFuelMode(PriceGroup priceGroup, bool authInAdvance) { try { var authType = authInAdvance ? "AuthInAdvance" : "NormalAuth"; var configNode = xmlDocument.SelectSingleNode( string.Concat("/FuelModeConfig/FuelMode_For_PreAuthorize/" + authType + "/PriceGroup_", (int)priceGroup)); var fm = Convert.ToInt32(configNode.InnerText); return fm; } catch (Exception) { return 1; } } /// /// Gets a fuel mode, given the fuelling type /// /// The fuelling type public int GetFuelMode(FuellingType fuellingType) { try { var configNode = xmlDocument.SelectSingleNode( string.Concat("/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType[@t='", (int)fuellingType) + "']/PriceGroup"); var fm = Convert.ToInt32(configNode.InnerText); return fm; } catch (Exception) { return 1; } } /// /// Gets the default fuel mode for the given price group /// /// The price group public int GetDefaultFuelMode(PriceGroup priceGroup) { try { var configNode = xmlDocument.SelectSingleNode(string.Concat("/FuelModeConfig/DefaultFuelModeTable/PriceGroup_", (int)priceGroup)); var fm = Convert.ToInt32(configNode.InnerText); return fm; } catch (Exception) { return 1; } } /// /// Gets a collection of fuel modes, given the price group /// /// The price group public IEnumerable GetFuelModes(PriceGroup priceGroup) { try { var configNodeList = xmlDocument.SelectNodes( string.Concat( "/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType/PriceGroup[@g='", (int)priceGroup) + "']"); var fmList = new List(); foreach (XmlNode xn in configNodeList) { fmList.Add(Convert.ToInt32(xn.InnerText)); } return (fmList); } catch (Exception) { return new List(); } } /// /// Gets the price group related with the given fuel mode /// /// The fuel mode public PriceGroup GetPriceGroup(int fuelMode, FuellingType fuellingType) { try { var configNodeList = xmlDocument.SelectNodes( string.Concat("/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType[@t='", (int)fuellingType, "']/PriceGroup")); foreach (XmlNode xn in configNodeList) { var fm = Convert.ToInt32(xn.InnerText); if (fm == fuelMode) { var pgv = Convert.ToInt32(xn.Attributes["g"].Value); var pg = (PriceGroup)pgv; return pg; // found first matching fuel mode } } return PriceGroup.Unknown; } catch (Exception) { return PriceGroup.Unknown; } return PriceGroup.Unknown; } /// /// Gets the price groups related with the given fuel mode /// /// The fuel mode public IEnumerable GetPriceGroups(int fuelMode) { try { XmlNodeList configNodeList = xmlDocument.SelectNodes( "/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType/PriceGroup"); IList pgList = new List(); foreach (XmlNode xn in configNodeList) { int fm = Convert.ToInt32(xn.InnerText); if (fm == fuelMode) { var pg = Convert.ToInt32(xn.Attributes["g"].Value); pgList.Add(pg); } } return pgList; } catch (Exception) { return new List(); } } /// /// Gets the fuelling type related with the given fuel mode and price group /// /// fuel mode /// price group public FuellingType GetFuellingType(int fuelMode, PriceGroup priceGroup) { try { var configNodeList = xmlDocument.SelectNodes("/FuelModeConfig/FuellingType_PriceGroup_FuelMode_Mapping/FuellingType"); foreach (XmlNode xn in configNodeList) { foreach (XmlElement cn in xn.ChildNodes) { int fm = Convert.ToInt32(cn.InnerText); var pg = Convert.ToInt32(cn.Attributes["g"].Value); if (fm == fuelMode && pg == (int)priceGroup) { var ft = (FuellingType)Convert.ToInt32(xn.Attributes["t"].Value); return ft; } } } return FuellingType.Unknown; } catch (Exception) { return FuellingType.Unknown; } } public bool WriteFuelPrice(int productId, int fuelMode, decimal newPrice) { return false; } public event AsyncCompletedEventHandler OnConfigurationChange; #endregion } }