using EVCB_OCPP.Packet.Messages.Basic;
using EVCB_OCPP.WSServer.Dto;
using log4net.Core;
using Microsoft.Extensions.Logging;
namespace EVCB_OCPP.WSServer.Service.WsService;
public enum BootStatus
{
Startup = 0,
Initializing,
Pending,
Accepted
}
public class WsClientData : WsSession
{ ///
/// 根據unique id來儲存.取出OCPP Request
///
public Queue queue = new Queue();
public EVCB_OCPP20.Packet.Messages.Basic.Queue queue20 = new EVCB_OCPP20.Packet.Messages.Basic.Queue();
public BootStatus BootStatus { get; set; } = BootStatus.Startup;
//public bool? IsPending { set; get; }
public bool IsCheckIn { set; get; } = false;
public string ChargeBoxId { set; get; }
public Guid CustomerId { get; set; }
public string MachineId { set; get; }
public bool ISOCPP20 { set; get; }
public bool ResetSecurityProfile { set; get; }
public bool IsAC { set; get; } = true;
#region Billing
public Dictionary UserPrices { set; get; } = new Dictionary();
public Dictionary UserDisplayPrices { set; get; } = new Dictionary();
public List ChargingPrices { set; get; }
///
/// 電樁顯示費率
///
public string DisplayPrice { set; get; }
///
/// 充電費率 以小時計費
///
public decimal ChargingFeebyHour { set; get; }
///
/// 停車費率 以小時計費
///
public decimal ParkingFee { set; get; }
///
/// 電樁是否計費
///
public bool IsBilling { set; get; }
///
/// 收費方式 1: 以度計費 2:以小時計費
///
public int BillingMethod { set; get; }
///
/// 電樁適用幣別
///
public string Currency { get; internal set; }
#endregion
public string CustomerName { get; set; }
public int? StationId { set; get; } = null;
public event EventHandler m_ReceiveData;
private string stringBuffer = string.Empty;
private readonly ILogger logger;
private List AttachedTasks = new List();
public WsClientData(ILogger logger) : base(logger)
{
ChargeBoxId = SessionID;
MachineId = SessionID;
this.logger = logger;
}
public void AddTask(Task toAddTask)
{
AttachedTasks.Add(toAddTask);
toAddTask.ContinueWith(t => {
AttachedTasks.Remove(toAddTask);
});
}
internal override void HandleReceivedData(string data)
{
stringBuffer += data;
//logger.LogInformation("{StringBuffer}", stringBuffer);
while (TryGetOCPPMsg(ref stringBuffer, out var msg))
{
m_ReceiveData?.Invoke(this, msg);
}
}
private bool TryGetOCPPMsg(ref string buffer, out string msg)
{
msg = string.Empty;
int? startIndex = null;
int? stopIndex = null;
uint cnt = 0;
for (int index = 0; index < buffer.Length; index++)
{
if (buffer[index] == '[')
{
cnt++;
if (startIndex == null)
{
startIndex = index;
}
}
if (startIndex != null && buffer[index] == ']')
{
cnt--;
}
if (startIndex != null && cnt == 0)
{
stopIndex = index;
break;
}
}
if (startIndex is not null && stopIndex is not null)
{
msg = buffer.Substring(startIndex.Value, stopIndex.Value - startIndex.Value);
buffer = buffer.Substring(stopIndex.Value + 1);
return true;
}
return false;
}
}