OCPPWSServer.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 
  2. using EVCB_OCPP.Domain;
  3. using SuperSocket.Common;
  4. using SuperWebSocket;
  5. using SuperWebSocket.SubProtocol;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Security;
  11. using System.Security.Cryptography.X509Certificates;
  12. using System.Text;
  13. namespace OCPPServer.Protocol
  14. {
  15. public class OCPPWSServer : WebSocketServer<ClientData>
  16. {
  17. /// <summary>
  18. /// 可允許連線Clinet數
  19. /// </summary>
  20. public int connectNum { get; set; }
  21. /// <summary>
  22. /// 是否限制連線Clinet數
  23. /// </summary>
  24. public bool beConnectLimit { get; set; }
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="WebSocketServer"/> class.
  27. /// </summary>
  28. /// <param name="subProtocols">The sub protocols.</param>
  29. public OCPPWSServer(IEnumerable<ISubProtocol<ClientData>> subProtocols)
  30. : base(subProtocols)
  31. {
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="WebSocketServer"/> class.
  35. /// </summary>
  36. /// <param name="subProtocol">The sub protocol.</param>
  37. public OCPPWSServer(ISubProtocol<ClientData> subProtocol)
  38. : base(subProtocol)
  39. {
  40. }
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="WebSocketServer"/> class.
  43. /// </summary>
  44. public OCPPWSServer()
  45. : base(new List<ISubProtocol<ClientData>>())
  46. {
  47. }
  48. protected override bool ValidateClientCertificate(ClientData session, object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  49. {
  50. Console.WriteLine(string.Format("{0} :{1}", session.ChargeBoxId + " ValidateClientCertificate", sslPolicyErrors));
  51. return true;
  52. // return base.ValidateClientCertificate(session, sender, certificate, chain, sslPolicyErrors);
  53. }
  54. protected override bool ValidateHandshake(ClientData session, string origin)
  55. {
  56. string[] words = session.Path.Split('/');
  57. session.ChargeBoxId = words.Last();
  58. Console.WriteLine(string.Format("{0} :ValidateHandshake: {1}", DateTime.Now.ToString("yy/MM/dd HH:mm:ss.fff"), session.Path));
  59. bool isExistedSN = false;
  60. using (var db = new MainDBContext())
  61. {
  62. var machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId).FirstOrDefault();
  63. session.CustomerId = machine == null ? Guid.Empty : machine.CustomerId;
  64. isExistedSN = machine == null ? false : true;
  65. }
  66. if (!isExistedSN)
  67. {
  68. StringBuilder responseBuilder = new StringBuilder();
  69. responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}", "1.1",
  70. (int)HttpStatusCode.NotFound, @"Not Found");
  71. responseBuilder.AppendWithCrCf();
  72. string sb = responseBuilder.ToString();
  73. byte[] data = Encoding.UTF8.GetBytes(sb);
  74. ((IWebSocketSession)session).SendRawData(data, 0, data.Length);
  75. return false;
  76. }
  77. return true;
  78. }
  79. }
  80. }