1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using EVCB_OCPP.Packet.Messages.Core;
- using EVCB_OCPP.WSServer.Message;
- using EVCB_OCPP.WSServer.Service.DbService;
- using log4net.Core;
- 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;
- internal class StationConfigService
- {
- public StationConfigService(
- WebDbService webDbService
- , ServerMessageService messageService
- , ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice
- , ILogger<StationConfigService> logger)
- {
- this.webDbService = webDbService;
- this.messageService = messageService;
- this.confirmWaitingMessageSerevice = confirmWaitingMessageSerevice;
- this.logger = logger;
- }
- private readonly WebDbService webDbService;
- private readonly ServerMessageService messageService;
- private readonly ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice;
- private readonly ILogger<StationConfigService> logger;
- private readonly Dictionary<int, Dictionary<string, string>> stationConfigRecord = new();
- public async Task CheckAndUpdateEvseConfig(string chargeBoxId, CancellationToken token = default)
- {
- MessageResult response = await GetEvseCurrentConfig(chargeBoxId, token);
- if (!response.Success || response.Message is not GetConfigurationConfirmation confirmation)
- {
- return;
- }
- Dictionary<string, string> configs = await GetEvseDBCurrentConfig(chargeBoxId, token);
- if (configs == null)
- {
- logger.LogInformation("{0} get station config failed", chargeBoxId);
- return;
- }
- await ComparenUpdateConfig(chargeBoxId,
- evseCurrentConfigs: confirmation.configurationKey.ToDictionary(x => x.key, x => x.value),
- evseDbConfigs: configs,
- token);
- }
- private async Task<MessageResult> GetEvseCurrentConfig(string chargeBoxId, CancellationToken token = default)
- {
- var sendTask = async () => await messageService.SendGetEVSEConfigureRequest(chargeBoxId);
- var response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
- return response;
- }
- private async Task<Dictionary<string, string>> GetEvseDBCurrentConfig(string chargeBoxId, CancellationToken token = default)
- {
- var receivedStaionID = await webDbService.GetEvseStation(chargeBoxId, token);
- if (receivedStaionID is null)
- {
- logger.LogInformation("{chargeBoxId} station not found", chargeBoxId);
- return null;
- }
- var staionID = receivedStaionID.Value;
- if (!stationConfigRecord.Keys.Contains(staionID))
- {
- await webDbService.GetEvseStationConfig(staionID);
- }
- return stationConfigRecord[staionID];
- return webDbService.GetCustomerStationEvseConfig(chargeBoxId, token);
- }
- internal async Task ComparenUpdateConfig(string chargeBoxId,
- Dictionary<string, string> evseCurrentConfigs,
- Dictionary<string, string> evseDbConfigs,
- CancellationToken token = default)
- {
- foreach (var config in evseDbConfigs)
- {
- if (evseCurrentConfigs.Keys.Contains(config.Key) &&
- evseCurrentConfigs[config.Key] == config.Value)
- {
- continue;
- }
- MessageResult response = null;
- var sendTask = async () => await messageService.SendChangeConfigurationRequest(chargeBoxId, config.Key, config.Value);
- response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
- }
- }
- }
|