OCPPSubProtocol.cs 11 KB

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