OCPPSubProtocol.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using OCPPServer.Protocol;
  2. using SuperSocket.Common;
  3. using SuperSocket.SocketBase;
  4. using SuperSocket.SocketBase.Logging;
  5. using SuperSocket.SocketBase.Protocol;
  6. using SuperWebSocket;
  7. using SuperWebSocket.Config;
  8. using SuperWebSocket.SubProtocol;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. namespace OCPPServer.SubProtocol
  15. {
  16. public class OCPPSubProtocol : OCPPSubProtocol<ClientData>
  17. {
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="BasicSubProtocol"/> class.
  20. /// </summary>
  21. public OCPPSubProtocol()
  22. : base(Assembly.GetCallingAssembly())
  23. {
  24. }
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="BasicSubProtocol"/> class.
  27. /// </summary>
  28. /// <param name="name">The sub protocol name.</param>
  29. public OCPPSubProtocol(string name)
  30. : base(name, Assembly.GetCallingAssembly())
  31. {
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="BasicSubProtocol"/> class.
  35. /// </summary>
  36. /// <param name="commandAssembly">The command assembly.</param>
  37. public OCPPSubProtocol(Assembly commandAssembly)
  38. : base(commandAssembly)
  39. {
  40. }
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="OCPPSubProtocol"/> class.
  43. /// </summary>
  44. /// <param name="commandAssemblies">The command assemblies.</param>
  45. public OCPPSubProtocol(IEnumerable<Assembly> commandAssemblies)
  46. : base(commandAssemblies)
  47. {
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="OCPPSubProtocol"/> class.
  51. /// </summary>
  52. /// <param name="name">The sub protocol name.</param>
  53. /// <param name="commandAssembly">The command assembly.</param>
  54. public OCPPSubProtocol(string name, Assembly commandAssembly)
  55. : base(name, commandAssembly)
  56. {
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="OCPPSubProtocol"/> class.
  60. /// </summary>
  61. /// <param name="name">The sub protocol name.</param>
  62. /// <param name="commandAssemblies">The command assemblies.</param>
  63. public OCPPSubProtocol(string name, IEnumerable<Assembly> commandAssemblies)
  64. : base(name, commandAssemblies)
  65. {
  66. }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="OCPPSubProtocol"/> class.
  69. /// </summary>
  70. /// <param name="name">The name.</param>
  71. /// <param name="commandAssemblies">The command assemblies.</param>
  72. /// <param name="requestInfoParser">The request info parser.</param>
  73. public OCPPSubProtocol(string name, IEnumerable<Assembly> commandAssemblies, IRequestInfoParser<SubRequestInfo> requestInfoParser)
  74. : base(name, commandAssemblies, requestInfoParser)
  75. {
  76. }
  77. public ILog Getmlog()
  78. {
  79. return getlog();
  80. }
  81. }
  82. public class OCPPSubProtocol<TWebSocketSession> : SubProtocolBase<TWebSocketSession>
  83. where TWebSocketSession : WebSocketSession<TWebSocketSession>, new()
  84. {
  85. /// <summary>
  86. /// Default basic sub protocol name
  87. /// </summary>
  88. public const string DefaultName = "ocpp1.6";//"OCPP1.6";
  89. private List<Assembly> m_CommandAssemblies = new List<Assembly>();
  90. private Dictionary<string, ISubCommand<TWebSocketSession>> m_CommandDict;
  91. private ILog m_Logger;
  92. private SubCommandFilterAttribute[] m_GlobalFilters;
  93. internal static BasicSubProtocol<TWebSocketSession> CreateDefaultSubProtocol()
  94. {
  95. var commandAssembly = typeof(TWebSocketSession).Assembly;
  96. if (commandAssembly == Assembly.GetExecutingAssembly())
  97. commandAssembly = Assembly.GetEntryAssembly();
  98. return new BasicSubProtocol<TWebSocketSession>(DefaultName, commandAssembly);
  99. }
  100. /// <summary>
  101. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with the calling aseembly as command assembly
  102. /// </summary>
  103. public OCPPSubProtocol()
  104. : this(DefaultName, Assembly.GetCallingAssembly())
  105. {
  106. }
  107. /// <summary>
  108. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with the calling aseembly as command assembly
  109. /// </summary>
  110. /// <param name="name">The sub protocol name.</param>
  111. public OCPPSubProtocol(string name)
  112. : this(name, Assembly.GetCallingAssembly())
  113. {
  114. }
  115. /// <summary>
  116. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with command assemblies
  117. /// </summary>
  118. /// <param name="commandAssemblies">The command assemblies.</param>
  119. public OCPPSubProtocol(IEnumerable<Assembly> commandAssemblies)
  120. : this(DefaultName, commandAssemblies, new OCPPSubCommandParser())
  121. {
  122. }
  123. /// <summary>
  124. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with single command assembly.
  125. /// </summary>
  126. /// <param name="commandAssembly">The command assembly.</param>
  127. public OCPPSubProtocol(Assembly commandAssembly)
  128. : this(DefaultName, new List<Assembly> { commandAssembly }, new OCPPSubCommandParser())
  129. {
  130. }
  131. /// <summary>
  132. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with name and single command assembly.
  133. /// </summary>
  134. /// <param name="name">The sub protocol name.</param>
  135. /// <param name="commandAssembly">The command assembly.</param>
  136. public OCPPSubProtocol(string name, Assembly commandAssembly)
  137. : this(name, new List<Assembly> { commandAssembly }, new OCPPSubCommandParser())
  138. {
  139. }
  140. /// <summary>
  141. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class with name and command assemblies.
  142. /// </summary>
  143. /// <param name="name">The sub protocol name.</param>
  144. /// <param name="commandAssemblies">The command assemblies.</param>
  145. public OCPPSubProtocol(string name, IEnumerable<Assembly> commandAssemblies)
  146. : this(name, commandAssemblies, new OCPPSubCommandParser())
  147. {
  148. }
  149. /// <summary>
  150. /// Initializes a new instance of the <see cref="BasicSubProtocol&lt;TWebSocketSession&gt;"/> class.
  151. /// </summary>
  152. /// <param name="name">The name.</param>
  153. /// <param name="commandAssemblies">The command assemblies.</param>
  154. /// <param name="requestInfoParser">The request info parser.</param>
  155. public OCPPSubProtocol(string name, IEnumerable<Assembly> commandAssemblies, IRequestInfoParser<SubRequestInfo> requestInfoParser)
  156. : base(name)
  157. {
  158. //The items in commandAssemblies may be null, so filter here
  159. m_CommandAssemblies.AddRange(commandAssemblies.Where(a => a != null));
  160. SubRequestParser = requestInfoParser;
  161. }
  162. #region ISubProtocol Members
  163. private void DiscoverCommands()
  164. {
  165. var subCommands = new List<ISubCommand<TWebSocketSession>>();
  166. foreach (var assembly in m_CommandAssemblies)
  167. {
  168. subCommands.AddRange(assembly.GetImplementedObjectsByInterface<ISubCommand<TWebSocketSession>>());
  169. }
  170. #if DEBUG
  171. var cmdbuilder = new StringBuilder();
  172. cmdbuilder.AppendLine(string.Format("SubProtocol {0} found the commands below:", this.Name));
  173. foreach (var c in subCommands)
  174. {
  175. cmdbuilder.AppendLine(c.Name);
  176. }
  177. m_Logger.Debug(cmdbuilder.ToString());
  178. #endif
  179. m_CommandDict = new Dictionary<string, ISubCommand<TWebSocketSession>>(subCommands.Count, StringComparer.OrdinalIgnoreCase);
  180. subCommands.ForEach(c =>
  181. {
  182. var fc = c as ISubCommandFilterLoader;
  183. if (fc != null)
  184. fc.LoadSubCommandFilters(m_GlobalFilters);
  185. m_CommandDict.Add(c.Name, c);
  186. }
  187. );
  188. }
  189. private bool ResolveCommmandAssembly(string definition)
  190. {
  191. try
  192. {
  193. var assemblies = AssemblyUtil.GetAssembliesFromString(definition);
  194. if (assemblies.Any())
  195. m_CommandAssemblies.AddRange(assemblies);
  196. return true;
  197. }
  198. catch (Exception e)
  199. {
  200. m_Logger.Error(e);
  201. return false;
  202. }
  203. }
  204. public ILog getlog()
  205. {
  206. return m_Logger;
  207. }
  208. /// <summary>
  209. /// Tries get command from the sub protocol's command inventory.
  210. /// </summary>
  211. /// <param name="name">The name.</param>
  212. /// <param name="command">The command.</param>
  213. /// <returns></returns>
  214. public override bool TryGetCommand(string name, out ISubCommand<TWebSocketSession> command)
  215. {
  216. return m_CommandDict.TryGetValue(name, out command);
  217. }
  218. public override bool Initialize(IAppServer appServer, SubProtocolConfig protocolConfig, ILog logger)
  219. {
  220. m_Logger = logger;
  221. var config = appServer.Config;
  222. m_GlobalFilters = appServer.GetType()
  223. .GetCustomAttributes(true)
  224. .OfType<SubCommandFilterAttribute>()
  225. .Where(a => string.IsNullOrEmpty(a.SubProtocol) || Name.Equals(a.SubProtocol, StringComparison.OrdinalIgnoreCase)).ToArray();
  226. if (Name.Equals(DefaultName, StringComparison.OrdinalIgnoreCase))
  227. {
  228. var commandAssembly = config.Options.GetValue("commandAssembly");
  229. if (!string.IsNullOrEmpty(commandAssembly))
  230. {
  231. if (!ResolveCommmandAssembly(commandAssembly))
  232. return false;
  233. }
  234. }
  235. if (protocolConfig != null && protocolConfig.Commands != null)
  236. {
  237. foreach (var commandConfig in protocolConfig.Commands)
  238. {
  239. var assembly = commandConfig.Options.GetValue("assembly");
  240. if (!string.IsNullOrEmpty(assembly))
  241. {
  242. if (!ResolveCommmandAssembly(assembly))
  243. return false;
  244. }
  245. }
  246. }
  247. //Always discover commands
  248. DiscoverCommands();
  249. return true;
  250. }
  251. #endregion
  252. }
  253. }