WsClientData.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 string ChargePointVendor { get; set; }
  60. //public int? StationId { set; get; } = null;
  61. public Dictionary<string, object> Data = new Dictionary<string, object>();
  62. public event EventHandler<string> m_ReceiveData;
  63. private string stringBuffer = string.Empty;
  64. private readonly ILogger<WsClientData> logger;
  65. private List<Task> AttachedTasks = new List<Task>();
  66. public WsClientData(ILogger<WsClientData> logger) : base(logger)
  67. {
  68. ChargeBoxId = SessionID;
  69. MachineId = SessionID;
  70. this.logger = logger;
  71. }
  72. public void AddTask(Task toAddTask)
  73. {
  74. AttachedTasks.Add(toAddTask);
  75. toAddTask.ContinueWith(t => {
  76. AttachedTasks.Remove(toAddTask);
  77. });
  78. }
  79. internal override void HandleReceivedData(string data)
  80. {
  81. stringBuffer += data;
  82. //logger.LogInformation("{StringBuffer}", stringBuffer);
  83. while (TryGetOCPPMsg(ref stringBuffer, out var msg))
  84. {
  85. m_ReceiveData?.Invoke(this, msg);
  86. }
  87. }
  88. private bool TryGetOCPPMsg(ref string buffer, out string msg)
  89. {
  90. msg = string.Empty;
  91. int? startIndex = null;
  92. int? stopIndex = null;
  93. uint cnt = 0;
  94. for (int index = 0; index < buffer.Length; index++)
  95. {
  96. if (buffer[index] == '[')
  97. {
  98. cnt++;
  99. if (startIndex == null)
  100. {
  101. startIndex = index;
  102. }
  103. }
  104. if (startIndex != null && buffer[index] == ']')
  105. {
  106. cnt--;
  107. }
  108. if (startIndex != null && cnt == 0)
  109. {
  110. stopIndex = index;
  111. break;
  112. }
  113. }
  114. if (startIndex is not null && stopIndex is not null)
  115. {
  116. msg = buffer.Substring(startIndex.Value, (stopIndex.Value - startIndex.Value)+1);
  117. buffer = buffer.Substring(stopIndex.Value + 1);
  118. return true;
  119. }
  120. return false;
  121. }
  122. }