WsClientData.cs 3.5 KB

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