using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using Global_Pump_Fdc.MessageEntity; using Global_Pump_Fdc.MessageEntity.Outgoing; using System.Collections.Specialized; using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump; namespace Global_Pump_Fdc { public class PumpGroupHandler : IEnumerable, IDeviceHandler { static NLog.Logger logger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("PumpHandler"); protected IContext context; protected int pollingInterval; protected List pumpHandlers = new List(); /// /// if the underlying communicator connected to remote device. /// private bool isCommConnected = false; private int amountDecimalDigits; private int volumeDecimalDigits; private int priceDecimalDigits; private int volumeTotalizerDecimalDigits; public PumpGroupHandler(int amountDecimalDigits, int volumeDecimalDigits, int priceDecimalDigits, int volumeTotalizerDecimalDigits, string pumpGroupXmlConfiguration) { this.amountDecimalDigits = amountDecimalDigits; this.volumeDecimalDigits = volumeDecimalDigits; this.priceDecimalDigits = priceDecimalDigits; this.volumeTotalizerDecimalDigits = volumeTotalizerDecimalDigits; //sample of pumpGroupXmlConfiguration, the address is a config value in physical wayne dart //pump mother board, 1 - 32 is the acceptable values. //the reason introduce PumpGroupHandler, because wayne dart pump may run multiple pumps in single //rs485 com port. // // // // // // // // // // // // // // // XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(pumpGroupXmlConfiguration); ////var rootElement = xmlDocument.GetElementsByTagName("PumpGroup").Cast().First(); var pumpElements = xmlDocument.GetElementsByTagName("Pump"); logger.Info("Global Fdc pump group, Will create " + pumpElements.Count + " pump handlers for this Global Fdc Group from local config"); this.CreatePumpHandlers(pumpElements); } protected virtual void CreatePumpHandlers(XmlNodeList pumpElements) { foreach (XmlNode pumpElement in pumpElements) { var pumpId = int.Parse(pumpElement.Attributes["pumpId"].Value); var pumpHandler = new PumpHandler(this, pumpId, amountDecimalDigits, volumeDecimalDigits, priceDecimalDigits, volumeTotalizerDecimalDigits, pumpElement.OuterXml); this.pumpHandlers.Add(pumpHandler); } this.rotateMsgTokens.AddRange(Enumerable.Repeat(0, this.pumpHandlers.Select(s => s.PumpId).Max())); } public IEnumerator GetEnumerator() { return this.pumpHandlers.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return this.pumpHandlers.GetEnumerator(); } /// /// key:value = pumpId:MsgToken /// private List rotateMsgTokens = new List(); /// /// get a valid token which from >=1 and lessOrEqual F. /// NOTE, if ResetMessageTokenToAlign(pumpId) was called, the next token generated here will be 0, which is /// a special value used to align the comm with real pump side, and then if the comm eastablished, we /// should use 1 to F. /// /// /// public byte GetNewMessageToken(int pumpId) { lock (rotateMsgTokens) { //return 0; if (rotateMsgTokens[pumpId - 1] > 0x0E) this.rotateMsgTokens[pumpId - 1] = 0; //var returnValue = this.rotateMsgTokens[pumpId - 1]; //this.rotateMsgTokens[pumpId - 1] += 1; return (byte)(++this.rotateMsgTokens[pumpId - 1]); } } /// /// typically called after a data request was NAKed by pump side, 0 token used for /// re-align the comm from FC to real pump. /// /// public void ResetMessageTokenToAlign(int pumpId) { lock (rotateMsgTokens) this.rotateMsgTokens[pumpId - 1] = -1; } public void Init(IContext context) { this.context = context; this.context.Communicator.OnConnected += (object sender, EventArgs e) => { this.isCommConnected = true; }; this.context.Communicator.OnDisconnected += (object sender, EventArgs e) => { this.isCommConnected = false; }; this.pumpHandlers.ForEach(p => p.Init(this.context)); } public Task Process(IContext context) { var parameters = context.Incoming.Message.Parameters as StringDictionary; if (parameters == null) { logger.Error("Read invalid FCC message" + context.Incoming.Message.Parameters); return Task.CompletedTask; } int pumpId = int.Parse(parameters["PumpID"]); if (pumpId == -1) { foreach (var phr in this.pumpHandlers) { phr.Process(context); } return Task.CompletedTask; } var ph = this.pumpHandlers.FirstOrDefault(p => p.PumpId == pumpId); if (ph == null) { logger.Error("PumpGroupHandler does not contain pumpHandler with physcialId: " + pumpId.ToString("X2")); return Task.CompletedTask; } return ph.Process(context); } } }