WsClientData.cs 3.3 KB

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