123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 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<ProtalServer>();
- var vendorIdReplaceService = host.Services.GetRequiredService<VendorIdUpdateService>();
- server.InitActions.Add(vendorIdReplaceService.GetAndRecordVendorId);
- return host.Services.GetRequiredService<StationConfigService>().Init();
- }
- }
- public class VendorIdUpdateService
- {
- public VendorIdUpdateService(
- ProtalServer protalServer
- , ServerMessageService messageService
- , ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice
- , ILogger<VendorIdUpdateService> 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<VendorIdUpdateService> 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<GetConfigurationConfirmation> 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;
- }
- }
- }
|