MessageTemplateLookup.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Edge.Core.Parser.BinaryParser;
  7. using Edge.Core.Parser.BinaryParser.MessageEntity;
  8. using Edge.Core.Parser.BinaryParser.Util;
  9. namespace Dfs.WayneChina.HengshanTerminalWrapper
  10. {
  11. #region To be replaced, probably removed
  12. public class Mapping : ConfigurationElement
  13. {
  14. [ConfigurationProperty("code", IsRequired = true)]
  15. public string CodeRawString
  16. {
  17. get
  18. {
  19. return this["code"] as string;
  20. }
  21. }
  22. public byte[] Code
  23. {
  24. get
  25. {
  26. // it matters that starts with 0x or not, need take care differently
  27. if (this.CodeRawString.ToLower().StartsWith("0x"))
  28. {
  29. return this.CodeRawString.Substring(2).ToBytes();
  30. }
  31. else
  32. {
  33. return this.CodeRawString.ToBytes();
  34. }
  35. }
  36. }
  37. [ConfigurationProperty("type", IsRequired = true)]
  38. public string TypeRawString
  39. {
  40. get
  41. {
  42. return this["type"] as string;
  43. }
  44. }
  45. public Type Type
  46. {
  47. get { return Assembly.GetAssembly(typeof(MessageTemplateBase)).GetType(this.TypeRawString); }
  48. }
  49. [ConfigurationProperty("description", IsRequired = false)]
  50. public string Description
  51. {
  52. get
  53. {
  54. return this["description"] as string;
  55. }
  56. }
  57. }
  58. public class MessageEntityMappingConfig : ConfigurationSection
  59. {
  60. public static MessageEntityMappingConfig GetConfig()
  61. {
  62. return (MessageEntityMappingConfig)System.Configuration.ConfigurationManager.GetSection("messageEntityMappings");
  63. }
  64. [System.Configuration.ConfigurationProperty("Mappings")]
  65. public MappingCollection Mappings
  66. {
  67. get
  68. {
  69. object o = this["Mappings"];
  70. return o as MappingCollection;
  71. }
  72. }
  73. }
  74. public class PortAndPumpMapping : ConfigurationElement
  75. {
  76. //[ConfigurationProperty("port",IsRequired = true)]
  77. [ConfigurationProperty("port", IsRequired = true)]
  78. public string PortRawString
  79. {
  80. get { return (string)this["port"]; }
  81. }
  82. [ConfigurationProperty("pumpId", IsRequired = true)]
  83. public string PumpIdRawString
  84. {
  85. get { return (string)this["pumpId"]; }
  86. }
  87. public string Port
  88. {
  89. get { return this.PortRawString.Trim(); }
  90. }
  91. public int PumpId
  92. {
  93. get { return Convert.ToInt32(this.PumpIdRawString.Trim()); }
  94. }
  95. }
  96. public class PortAndPumpMappingCollection : ConfigurationElementCollection
  97. {
  98. protected override ConfigurationElement CreateNewElement()
  99. {
  100. return new PortAndPumpMapping();
  101. }
  102. protected override object GetElementKey(ConfigurationElement element)
  103. {
  104. string result = null;
  105. bool first = true;
  106. foreach (var s in ((PortAndPumpMapping)element).PortRawString)
  107. {
  108. var s1 = s.ToString();
  109. if (first)
  110. {
  111. first = false;
  112. result = s1;
  113. continue;
  114. }
  115. result = result + s1;
  116. }
  117. return result;
  118. }
  119. }
  120. class PortAndPumpMappingConfig : ConfigurationSection
  121. {
  122. public static PortAndPumpMappingConfig GetConfig()
  123. {
  124. return (PortAndPumpMappingConfig)System.Configuration.ConfigurationManager.GetSection("portAndPumpMappings");
  125. }
  126. [System.Configuration.ConfigurationProperty("Mappings")]
  127. public PortAndPumpMappingCollection Mappings
  128. {
  129. get
  130. {
  131. object o = this["Mappings"];
  132. return o as PortAndPumpMappingCollection;
  133. }
  134. }
  135. }
  136. public class MappingCollection : ConfigurationElementCollection
  137. {
  138. //public Mapping this[int index]
  139. //{
  140. // get
  141. // {
  142. // return base.BaseGet(index) as Mapping;
  143. // }
  144. // set
  145. // {
  146. // if (base.BaseGet(index) != null)
  147. // {
  148. // base.BaseRemoveAt(index);
  149. // }
  150. // this.BaseAdd(index, value);
  151. // }
  152. //}
  153. //public new Mapping this[string responseString]
  154. //{
  155. // get { return (Mapping)BaseGet(responseString); }
  156. // set
  157. // {
  158. // if (BaseGet(responseString) != null)
  159. // {
  160. // BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));
  161. // }
  162. // BaseAdd(value);
  163. // }
  164. //}
  165. protected override System.Configuration.ConfigurationElement CreateNewElement()
  166. {
  167. return new Mapping();
  168. }
  169. protected override object GetElementKey(System.Configuration.ConfigurationElement element)
  170. {
  171. return ((Mapping)element).CodeRawString.Select(s => s.ToString()).Aggregate((n, acc) => n + acc);
  172. }
  173. }
  174. #endregion
  175. public class MessageTemplateLookup : IMessageTemplateLookup
  176. {
  177. /// <summary>
  178. /// Gets the default singleton instance of type MessageTemplateLookup.
  179. /// </summary>
  180. public static MessageTemplateLookup Default { get; } = new MessageTemplateLookup();
  181. /// <summary>
  182. /// Dictionary that holds the commands and types
  183. /// </summary>
  184. private static readonly Dictionary<string, string> messageCodeToTypeStrDict = new Dictionary<string, string>();
  185. static MessageTemplateLookup()
  186. {
  187. messageCodeToTypeStrDict.Add("0xA7", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.AuthPumpWithGallonRequest, Dfs.WayneChina.HengshanTerminalWrapper");
  188. messageCodeToTypeStrDict.Add("0xA8", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.AuthPumpWithKiloRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  189. messageCodeToTypeStrDict.Add("0xA9", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.AuthPumpWithAmountRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  190. messageCodeToTypeStrDict.Add("0xA0", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.GetNozzleStatusRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  191. messageCodeToTypeStrDict.Add("0xA1", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.SetFuelPriceRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  192. messageCodeToTypeStrDict.Add("0xA4", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.CancelFullMonitorRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  193. messageCodeToTypeStrDict.Add("0xA5", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.OpenRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  194. messageCodeToTypeStrDict.Add("0xA6", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.CloseRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  195. messageCodeToTypeStrDict.Add("0xAA", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.UnAuthorizePumpRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  196. messageCodeToTypeStrDict.Add("0xAB", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.GetTransactionRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  197. messageCodeToTypeStrDict.Add("0xAC", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.GetAccumulateRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  198. messageCodeToTypeStrDict.Add("0xBA", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.GetVersionRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  199. messageCodeToTypeStrDict.Add("0xB0", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.DisplayClockRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  200. messageCodeToTypeStrDict.Add("0xBE", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.DisplayRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  201. messageCodeToTypeStrDict.Add("0xC0", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.ClearRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  202. messageCodeToTypeStrDict.Add("0xC3", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.GetBacklightStatusRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  203. messageCodeToTypeStrDict.Add("0xE5", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.StartRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  204. messageCodeToTypeStrDict.Add("0xE7", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.ReservePumpWithGallonRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  205. messageCodeToTypeStrDict.Add("0xE8", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.ReservePumpWithKiloRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  206. messageCodeToTypeStrDict.Add("0xE9", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.ReservePumpWithAmountRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  207. messageCodeToTypeStrDict.Add("0xEA", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.RoundingRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  208. messageCodeToTypeStrDict.Add("0xEB", "Dfs.WayneChina.HengshanTerminalWrapper.MessageEntity.Incoming.RoundUpByVolumeRequest,Dfs.WayneChina.HengshanTerminalWrapper");
  209. }
  210. /// <summary>
  211. /// Create a message entity based on input whole message raw bytes which is: 1bytes Len + 1 byte cmd + variable length
  212. /// parameter+1byte check code
  213. /// </summary>
  214. /// <param name="bytes">whole message raw bytes</param>
  215. /// <returns>new created message entity</returns>
  216. public MessageTemplateBase GetMessageTemplateByRawBytes(byte[] bytes)
  217. {
  218. var msgHandleCode = bytes.Skip(2).First();
  219. var key = "0x" + msgHandleCode.ToString("X").PadLeft(2, '0');
  220. if (messageCodeToTypeStrDict.ContainsKey(key))
  221. {
  222. var found = messageCodeToTypeStrDict[key];
  223. var t = Type.GetType(found);
  224. var targetInstance = (MessageTemplateBase)Activator.CreateInstance(t);
  225. return targetInstance;
  226. }
  227. else
  228. {
  229. 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));
  230. }
  231. }
  232. }
  233. }