WsClientData.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. private readonly ILogger<WsClientData> logger;
  55. public WsClientData(ILogger<WsClientData> logger) : base(logger)
  56. {
  57. ChargeBoxId = SessionID;
  58. MachineId = SessionID;
  59. this.logger = logger;
  60. }
  61. internal override void HandleReceivedData(string data)
  62. {
  63. stringBuffer += data;
  64. //logger.LogInformation("{StringBuffer}", stringBuffer);
  65. while (TryGetOCPPMsg(ref stringBuffer, out var msg))
  66. {
  67. m_ReceiveData?.Invoke(this, msg);
  68. }
  69. }
  70. private bool TryGetOCPPMsg(ref string buffer, out string msg)
  71. {
  72. msg = string.Empty;
  73. int? startIndex = null;
  74. int? stopIndex = null;
  75. uint cnt = 0;
  76. for (int index = 0; index < buffer.Length; index++)
  77. {
  78. if (buffer[index] == '[')
  79. {
  80. cnt++;
  81. if (startIndex == null)
  82. {
  83. startIndex = index;
  84. }
  85. }
  86. if (startIndex != null && buffer[index] == ']')
  87. {
  88. cnt--;
  89. }
  90. if (startIndex != null && cnt == 0)
  91. {
  92. stopIndex = index;
  93. break;
  94. }
  95. }
  96. if (startIndex is not null && stopIndex is not null)
  97. {
  98. msg = buffer.Substring(startIndex.Value, (stopIndex.Value - startIndex.Value)+1);
  99. buffer = buffer.Substring(stopIndex.Value + 1);
  100. return true;
  101. }
  102. return false;
  103. }
  104. }