WsClientData.cs 3.9 KB

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