OCPPSubProtocol.cs 11 KB

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