using EVCB_OCPP.Packet.Features; using EVCB_OCPP.Packet.Messages; using EVCB_OCPP.Packet.Messages.Core; using EVCB_OCPP.WSServer.Message; using EVCB_OCPP.WSServer.Service.WsService; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EVCB_OCPP.WSServer.Service { public static class VendorIdUpdateServiceExt { public static Task InitVendorIdReplaceService(this IHost host) { var server = host.Services.GetRequiredService(); var vendorIdReplaceService = host.Services.GetRequiredService(); server.InitActions.Add(vendorIdReplaceService.GetAndRecordVendorId); return host.Services.GetRequiredService().Init(); } } public class VendorIdUpdateService { public VendorIdUpdateService( ProtalServer protalServer , ServerMessageService messageService , ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice , ILogger logger ) { this.protalServer = protalServer; this.messageService = messageService; this.confirmWaitingMessageSerevice = confirmWaitingMessageSerevice; this.logger = logger; } private static string Session_VID_Key = "StationConfigService_Station"; private readonly ProtalServer protalServer; private readonly ServerMessageService messageService; private readonly ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice; private readonly ILogger logger; internal async Task GetAndRecordVendorId(WsClientData wsClient, CancellationToken token) { var confirmation = await GetEvseCurrentConfig(wsClient.ChargeBoxId, token); if (confirmation is null) { logger.LogWarning("{chargeBoxId} get config from evse failed", wsClient.ChargeBoxId); return; } var vendorId = confirmation.configurationKey?.FirstOrDefault(x => x.key == "ChargePointVendorId"); if (vendorId is null) { logger.LogWarning("{chargeBoxId} get vendorId from evse failed", wsClient.ChargeBoxId); return; } SetSessionVendorId(wsClient, vendorId.value); } internal void ReplaceVID(WsClientData session, Actions action, IRequest request) { if (action != Actions.DataTransfer || request is not DataTransferRequest dataTransferRequest) { return; } string vid = GetSessionVendorId(session); if (string.IsNullOrEmpty(vid)) { return; } dataTransferRequest.vendorId = vid; } private async Task GetEvseCurrentConfig(string chargeBoxId, CancellationToken token = default) { var sendTask = async () => await messageService.SendGetEVSEConfigureRequest(chargeBoxId); var response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token); if (response is GetConfigurationConfirmation confirm) { return confirm; } return null; } private string GetChargeBoxIdVendorId(string chargeBoxId) { var clients = protalServer.GetClientDic(); if (!clients.ContainsKey(chargeBoxId)) { return null; } var session = clients[chargeBoxId]; return GetSessionVendorId(session); } private string GetSessionVendorId(WsClientData session) { if (session is null || !session.Data.ContainsKey(Session_VID_Key)) { return null; } return (string)session.Data[Session_VID_Key]; } private void SetSessionVendorId(WsClientData session, string vid) { if (session is null) { return; } session.Data[Session_VID_Key] = vid; } } }