123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- 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
- {
- /// <summary>
- /// Gets the default singleton instance of type MessageTemplateLookup.
- /// </summary>
- public static MessageTemplateLookup Default { get; } = new MessageTemplateLookup();
- /// <summary>
- /// Dictionary that holds the commands and types
- /// </summary>
- private static readonly Dictionary<string, string> messageCodeToTypeStrDict = new Dictionary<string, string>();
- 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");
- }
- /// <summary>
- /// Create a message entity based on input whole message raw bytes which is: 1bytes Len + 1 byte cmd + variable length
- /// parameter+1byte check code
- /// </summary>
- /// <param name="bytes">whole message raw bytes</param>
- /// <returns>new created message entity</returns>
- 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));
- }
- }
- }
- }
|