OCPPWSServer.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. isExistedSN = machine == null ? false : true;
  64. }
  65. if (!isExistedSN)
  66. {
  67. StringBuilder responseBuilder = new StringBuilder();
  68. responseBuilder.AppendFormatWithCrCf(@"HTTP/{0} {1} {2}","1.1",
  69. (int)HttpStatusCode.NotFound, @"Not Found");
  70. responseBuilder.AppendWithCrCf();
  71. string sb = responseBuilder.ToString();
  72. byte[] data = Encoding.UTF8.GetBytes(sb);
  73. ((IWebSocketSession)session).SendRawData(data,0,data.Length);
  74. return false;
  75. }
  76. return true;
  77. }
  78. }
  79. }