using Dfs.WayneChina.IMisPlus.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Dfs.WayneChina.IMisPlus
{
///
/// Manages the xml settings part of the legacy iFuel forecourt configuration.
///
public class ForecourtXml
{
#region Fields
private XDocument xdoc;
#endregion
#region Properties
public ForecourtConfig ForecourtConfig { get; set; }
#endregion
public bool Load(string fileName)
{
xdoc = XDocument.Load(fileName);
return ParseConfig();
}
public bool Parse(string xml)
{
xdoc = XDocument.Parse(xml);
return ParseConfig();
}
private bool ParseConfig()
{
var configRoot = xdoc.Document.Element("ForecourtConfig");
//Parse
ForecourtConfig = new ForecourtConfig();
ForecourtConfig.CurrentVersion = configRoot.Attribute("Version").Value;
Console.WriteLine(configRoot.Attribute("Version").Value);
//Parse
var terminalConfig = configRoot.Descendants("TerminalConfig").First();
var terminals = from term in terminalConfig.Descendants("Terminal")
select new Terminal
{
Id = Convert.ToByte(term.Attribute("Id").Value),
PayMode = Convert.ToByte(term.Attribute("PayMode").Value),
ProtocolType = Convert.ToByte(term.Attribute("ProtocolType").Value),
Address = term.Attribute("Address").Value
};
ForecourtConfig.Terminals = terminals.ToList();
foreach (var item in ForecourtConfig.Terminals)
{
Console.WriteLine("Terminal Id: {0}, Pay mode: {1}, Protocol type: {2}, Address: {3}",
item.Id, item.PayMode, item.ProtocolType, item.Address);
}
//Parse
var dispenserConfig = configRoot.Descendants("DispenserConfig").First();
var disps = from disp in dispenserConfig.Descendants("Dispenser")
select new Dispenser
{
DispNo = disp.Attribute("DispNo").Value,
DispType = disp.Attribute("DispType").Value,
Manufacturer = disp.Attribute("Manufacturer").Value,
ProtocolType = Convert.ToByte(disp.Attribute("Protocol").Value),
Address = disp.Attribute("Address").Value,
Life = Convert.ToUInt16(disp.Attribute("Life").Value),
State = disp.Attribute("State").Value,
ManufacturingNo = disp.Attribute("ManufacturingNo").Value,
DateOfManufacture = GetFormattedDate(disp.Attribute("DateOfManufacture").Value)
};
ForecourtConfig.Dispensers = disps.ToList();
foreach (var item in ForecourtConfig.Dispensers)
{
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}",
item.DispNo, item.DispType, item.Manufacturer, item.ProtocolType, item.Address, item.Life, item.State, item.DateOfManufacture, item.NozzleCount);
}
//Parse
var pumpConfig = configRoot.Descendants("PumpConfig").First();
var pumps = from pump in pumpConfig.Descendants("Pump")
select new Pump
{
Id = Convert.ToByte(pump.Attribute("Id").Value),
PosId = Convert.ToByte(pump.Attribute("PosId").Value),
DispType = Convert.ToByte(pump.Attribute("DispType").Value),
DispTypeName = pump.Attribute("DispTypeName").Value,
TankId = Convert.ToByte(pump.Attribute("TankId").Value),
AppProtocol = Convert.ToUInt16(pump.Attribute("AppProtocol").Value),
ProtocolType = Convert.ToByte(pump.Attribute("ProtocolType").Value),
Port = pump.Attribute("Port").Value,
SerialPort = Convert.ToUInt16(pump.Attribute("SerialPort").Value),
Node = Convert.ToUInt16(pump.Attribute("Node").Value),
SubAddress = Convert.ToByte(pump.Attribute("SubAddress").Value),
Mode = Convert.ToByte(pump.Attribute("Mode").Value),
PayStackSize = Convert.ToByte(pump.Attribute("PayStack").Value),
CheckOutMode = Convert.ToByte(pump.Attribute("CheckOutMode").Value),
MaxAmount = Convert.ToUInt32(pump.Attribute("MaxAmount").Value),
MaxVolume = Convert.ToUInt32(pump.Attribute("MaxVolume").Value),
Nozzles = (from noz in pump.Descendants("Nozzle")
select new Nozzle
{
Id = Convert.ToByte(noz.Attribute("Id").Value),
PhysicalId = Convert.ToByte(noz.Attribute("PhysicalId").Value),
DispNo = noz.Attribute("DispNo").Value,
SiteNozzleNo = Convert.ToUInt16(noz.Attribute("SiteNozzleNo").Value),
TankId = Convert.ToByte(noz.Attribute("TankId").Value),
ProductCode = noz.Attribute("ProductCode").Value,
State = noz.Attribute("State").Value,
VehicleIdReaderAddress = noz.Attribute("VehicleIdReaderAddress").Value
}).ToList()
};
ForecourtConfig.Pumps = pumps.ToList();
foreach (var p in ForecourtConfig.Pumps)
{
Console.WriteLine("Pump Id: {0}", p.Id);
foreach (var n in p.Nozzles)
{
Console.WriteLine("Nozzle Id: {0}", n.Id);
}
}
//Parse
var tankConfig = configRoot.Descendants("TankConfig").First();
var tanks = from tank in tankConfig.Descendants("Tank")
select new Tank
{
Id = Convert.ToByte(tank.Attribute("TankId")),
};
foreach (var t in tanks)
{
Console.WriteLine("Tank Id: {0}, Serial number: {1}", t.Id, "");
}
//Parse
var fuelConfig = configRoot.Descendants("FuelConfig").First();
var fuels = from fuel in fuelConfig.Descendants("Fuel")
select new Fuel
{
Id = Convert.ToInt32(fuel.Attribute("Id").Value),
ProductCode = fuel.Attribute("ProductCode").Value,
ClassNo = fuel.Attribute("ClassNo").Value,
Name = fuel.Attribute("Name").Value,
Price = Convert.ToInt32(fuel.Attribute("Price").Value),
OldPrice = Convert.ToInt32(fuel.Attribute("OldPrice").Value),
UnitMeasure = fuel.Attribute("UnitMeasure").Value,
Storage = Convert.ToInt32(fuel.Attribute("Storage").Value),
TaxPer = Convert.ToInt32(fuel.Attribute("TaxPer").Value),
Density = fuel.Attribute("Density").Value
};
ForecourtConfig.Fuels = fuels.ToList();
return true;
}
public bool AddNozzle(Nozzle nozzle)
{
return true;
}
public bool AddPump(Pump pump)
{
if (pump == null)
return false;
if (ForecourtConfig.Pumps.Any(p => p.Id == pump.Id))
return false;
ForecourtConfig.Pumps.Add(pump);
return true;
}
private DateTime GetFormattedDate(string dateString)
{
DateTime dateOfManufacture;
string format = "yyyy-MM-dd";
bool result =
DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateOfManufacture);
return result ? dateOfManufacture : DateTime.MinValue;
}
}
}