WsClientData.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 
  2. using EVCB_OCPP.Packet.Messages.Basic;
  3. using EVCB_OCPP.WSServer.Dto;
  4. namespace EVCB_OCPP.WSServer.Service.WsService
  5. {
  6. public class WsClientData : WsSession
  7. { /// <summary>
  8. /// 根據unique id來儲存.取出OCPP Request
  9. /// </summary>
  10. public Queue queue = new Queue();
  11. public EVCB_OCPP20.Packet.Messages.Basic.Queue queue20 = new EVCB_OCPP20.Packet.Messages.Basic.Queue();
  12. public bool? IsPending { set; get; }
  13. public bool IsCheckIn { set; get; } = false;
  14. public string ChargeBoxId { set; get; }
  15. public Guid CustomerId { get; set; }
  16. public string MachineId { set; get; }
  17. public bool ISOCPP20 { set; get; }
  18. public bool ResetSecurityProfile { set; get; }
  19. public bool IsAC { set; get; } = true;
  20. #region Billing
  21. public Dictionary<string, string> UserPrices { set; get; } = new Dictionary<string, string>();
  22. public Dictionary<string, string> UserDisplayPrices { set; get; } = new Dictionary<string, string>();
  23. public List<ChargingPrice> ChargingPrices { set; get; }
  24. /// <summary>
  25. /// 電樁顯示費率
  26. /// </summary>
  27. public string DisplayPrice { set; get; }
  28. /// <summary>
  29. /// 充電費率 以小時計費
  30. /// </summary>
  31. public decimal ChargingFeebyHour { set; get; }
  32. /// <summary>
  33. /// 停車費率 以小時計費
  34. /// </summary>
  35. public decimal ParkingFee { set; get; }
  36. /// <summary>
  37. /// 電樁是否計費
  38. /// </summary>
  39. public bool IsBilling { set; get; }
  40. /// <summary>
  41. /// 收費方式 1: 以度計費 2:以小時計費
  42. /// </summary>
  43. public int BillingMethod { set; get; }
  44. /// <summary>
  45. /// 電樁適用幣別
  46. /// </summary>
  47. public string Currency { get; internal set; }
  48. #endregion
  49. public string CustomerName { get; set; }
  50. public string StationId { set; get; }
  51. public event EventHandler<string> m_ReceiveData;
  52. private string stringBuffer = string.Empty;
  53. public WsClientData()
  54. {
  55. ChargeBoxId = SessionID;
  56. MachineId = SessionID;
  57. }
  58. /// <summary>
  59. /// Sends the raw binary data to client.
  60. /// </summary>
  61. /// <param name="data">The data.</param>
  62. /// <param name="offset">The offset.</param>
  63. /// <param name="length">The length.</param>
  64. public void SendRawData(byte[] data, int offset, int length)
  65. {
  66. Send(data, offset, length);
  67. }
  68. internal override void HandleReceivedData(string data)
  69. {
  70. stringBuffer += data;
  71. while (TryGetOCPPMsg(ref stringBuffer, out var msg))
  72. {
  73. m_ReceiveData?.Invoke(this, msg);
  74. }
  75. }
  76. private bool TryGetOCPPMsg(ref string buffer, out string msg)
  77. {
  78. msg = string.Empty;
  79. int? startIndex = null;
  80. int? stopIndex = null;
  81. uint cnt = 0;
  82. for (int index = 0; index < buffer.Length; index++)
  83. {
  84. if (buffer[index] == '[')
  85. {
  86. cnt++;
  87. if (startIndex == null)
  88. {
  89. startIndex = index;
  90. }
  91. }
  92. if (startIndex != null && buffer[index] == ']')
  93. {
  94. cnt--;
  95. }
  96. if (startIndex != null && cnt == 0)
  97. {
  98. stopIndex = index;
  99. break;
  100. }
  101. }
  102. if (startIndex is not null && stopIndex is not null)
  103. {
  104. msg = buffer.Substring(startIndex.Value, stopIndex.Value - startIndex.Value);
  105. buffer = buffer.Substring(stopIndex.Value);
  106. return true;
  107. }
  108. return false;
  109. }
  110. }
  111. }