using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Reflection; using Edge.Core.Parser.BinaryParser; using Edge.Core.Parser.BinaryParser.MessageEntity; using Edge.Core.Parser.BinaryParser.Util; namespace Dfs.WayneChina.HengshanTerminalWrapper { #region To be replaced, probably removed public class Mapping : ConfigurationElement { [ConfigurationProperty("code", IsRequired = true)] public string CodeRawString { get { return this["code"] as string; } } public byte[] Code { get { // it matters that starts with 0x or not, need take care differently if (this.CodeRawString.ToLower().StartsWith("0x")) { return this.CodeRawString.Substring(2).ToBytes(); } else { return this.CodeRawString.ToBytes(); } } } [ConfigurationProperty("type", IsRequired = true)] public string TypeRawString { get { return this["type"] as string; } } public Type Type { get { return Assembly.GetAssembly(typeof(MessageTemplateBase)).GetType(this.TypeRawString); } } [ConfigurationProperty("description", IsRequired = false)] public string Description { get { return this["description"] as string; } } } public class MessageEntityMappingConfig : ConfigurationSection { public static MessageEntityMappingConfig GetConfig() { return (MessageEntityMappingConfig)System.Configuration.ConfigurationManager.GetSection("messageEntityMappings"); } [System.Configuration.ConfigurationProperty("Mappings")] public MappingCollection Mappings { get { object o = this["Mappings"]; return o as MappingCollection; } } } public class PortAndPumpMapping : ConfigurationElement { //[ConfigurationProperty("port",IsRequired = true)] [ConfigurationProperty("port", IsRequired = true)] public string PortRawString { get { return (string)this["port"]; } } [ConfigurationProperty("pumpId", IsRequired = true)] public string PumpIdRawString { get { return (string)this["pumpId"]; } } public string Port { get { return this.PortRawString.Trim(); } } public int PumpId { get { return Convert.ToInt32(this.PumpIdRawString.Trim()); } } } public class PortAndPumpMappingCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new PortAndPumpMapping(); } protected override object GetElementKey(ConfigurationElement element) { string result = null; bool first = true; foreach (var s in ((PortAndPumpMapping)element).PortRawString) { var s1 = s.ToString(); if (first) { first = false; result = s1; continue; } result = result + s1; } return result; } } class PortAndPumpMappingConfig : ConfigurationSection { public static PortAndPumpMappingConfig GetConfig() { return (PortAndPumpMappingConfig)System.Configuration.ConfigurationManager.GetSection("portAndPumpMappings"); } [System.Configuration.ConfigurationProperty("Mappings")] public PortAndPumpMappingCollection Mappings { get { object o = this["Mappings"]; return o as PortAndPumpMappingCollection; } } } public class MappingCollection : ConfigurationElementCollection { //public Mapping this[int index] //{ // get // { // return base.BaseGet(index) as Mapping; // } // set // { // if (base.BaseGet(index) != null) // { // base.BaseRemoveAt(index); // } // this.BaseAdd(index, value); // } //} //public new Mapping this[string responseString] //{ // get { return (Mapping)BaseGet(responseString); } // set // { // if (BaseGet(responseString) != null) // { // BaseRemoveAt(BaseIndexOf(BaseGet(responseString))); // } // BaseAdd(value); // } //} protected override System.Configuration.ConfigurationElement CreateNewElement() { return new Mapping(); } protected override object GetElementKey(System.Configuration.ConfigurationElement element) { return ((Mapping)element).CodeRawString.Select(s => s.ToString()).Aggregate((n, acc) => n + acc); } } #endregion public class MessageTemplateLookup : IMessageTemplateLookup { /// /// Gets the default singleton instance of type MessageTemplateLookup. /// public static MessageTemplateLookup Default { get; } = new MessageTemplateLookup(); /// /// Dictionary that holds the commands and types /// private static readonly Dictionary messageCodeToTypeStrDict = new Dictionary(); static MessageTemplateLookup() { messageCodeToTypeStrDict.Add("0xA7", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.AuthPumpWithGallonRequest, Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xA8", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.AuthPumpWithKiloRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xA9", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.AuthPumpWithAmountRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xA0", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.GetNozzleStatusRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xA1", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.SetFuelPriceRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xA4", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.CancelFullMonitorRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xA5", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.OpenRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xA6", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.CloseRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xAA", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.UnAuthorizePumpRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xAB", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.GetTransactionRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xAC", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.GetAccumulateRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xBA", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.GetVersionRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xB0", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.DisplayClockRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xBE", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.DisplayRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xC0", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.ClearRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xC3", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.GetBacklightStatusRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xE5", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.StartRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xE7", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.ReservePumpWithGallonRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xE8", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.ReservePumpWithKiloRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xE9", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.ReservePumpWithAmountRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xEA", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.RoundingRequest,Dfs.WayneChina.HengshanTerminalWrapper"); messageCodeToTypeStrDict.Add("0xEB", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.RoundUpByVolumeRequest,Dfs.WayneChina.HengshanTerminalWrapper"); } /// /// Create a message entity based on input whole message raw bytes which is: 1bytes Len + 1 byte cmd + variable length /// parameter+1byte check code /// /// whole message raw bytes /// new created message entity public MessageTemplateBase GetMessageTemplateByRawBytes(byte[] bytes) { var msgHandleCode = bytes.Skip(2).First(); var key = "0x" + msgHandleCode.ToString("X").PadLeft(2, '0'); if (messageCodeToTypeStrDict.ContainsKey(key)) { var found = messageCodeToTypeStrDict[key]; var t = Type.GetType(found); var targetInstance = (MessageTemplateBase)Activator.CreateInstance(t); return targetInstance; } else { throw new ArgumentException("Can't find correlated message entity type for incoming raw message bytes: " + bytes.Select(s => s.ToString("X").PadLeft(2, '0')).Aggregate((n, acc) => n + " " + acc)); } } } }