StationConfigService.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using EVCB_OCPP.Packet.Messages.Core;
  2. using EVCB_OCPP.WSServer.Message;
  3. using EVCB_OCPP.WSServer.Service.DbService;
  4. using log4net.Core;
  5. using Microsoft.Extensions.Logging;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace EVCB_OCPP.WSServer.Service;
  12. internal class StationConfigService
  13. {
  14. public StationConfigService(
  15. WebDbService webDbService
  16. , ServerMessageService messageService
  17. , ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice
  18. , ILogger<StationConfigService> logger)
  19. {
  20. this.webDbService = webDbService;
  21. this.messageService = messageService;
  22. this.confirmWaitingMessageSerevice = confirmWaitingMessageSerevice;
  23. this.logger = logger;
  24. }
  25. private readonly WebDbService webDbService;
  26. private readonly ServerMessageService messageService;
  27. private readonly ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice;
  28. private readonly ILogger<StationConfigService> logger;
  29. private readonly Dictionary<int, Dictionary<string, string>> stationConfigRecord = new();
  30. public async Task CheckAndUpdateEvseConfig(string chargeBoxId, CancellationToken token = default)
  31. {
  32. MessageResult response = await GetEvseCurrentConfig(chargeBoxId, token);
  33. if (!response.Success || response.Message is not GetConfigurationConfirmation confirmation)
  34. {
  35. return;
  36. }
  37. Dictionary<string, string> configs = await GetEvseDBCurrentConfig(chargeBoxId, token);
  38. if (configs == null)
  39. {
  40. logger.LogInformation("{0} get station config failed", chargeBoxId);
  41. return;
  42. }
  43. await ComparenUpdateConfig(chargeBoxId,
  44. evseCurrentConfigs: confirmation.configurationKey.ToDictionary(x => x.key, x => x.value),
  45. evseDbConfigs: configs,
  46. token);
  47. }
  48. private async Task<MessageResult> GetEvseCurrentConfig(string chargeBoxId, CancellationToken token = default)
  49. {
  50. var sendTask = async () => await messageService.SendGetEVSEConfigureRequest(chargeBoxId);
  51. var response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
  52. return response;
  53. }
  54. private async Task<Dictionary<string, string>> GetEvseDBCurrentConfig(string chargeBoxId, CancellationToken token = default)
  55. {
  56. var receivedStaionID = await webDbService.GetEvseStation(chargeBoxId, token);
  57. if (receivedStaionID is null)
  58. {
  59. logger.LogInformation("{chargeBoxId} station not found", chargeBoxId);
  60. return null;
  61. }
  62. var staionID = receivedStaionID.Value;
  63. if (!stationConfigRecord.Keys.Contains(staionID))
  64. {
  65. await webDbService.GetEvseStationConfig(staionID);
  66. }
  67. return stationConfigRecord[staionID];
  68. return webDbService.GetCustomerStationEvseConfig(chargeBoxId, token);
  69. }
  70. internal async Task ComparenUpdateConfig(string chargeBoxId,
  71. Dictionary<string, string> evseCurrentConfigs,
  72. Dictionary<string, string> evseDbConfigs,
  73. CancellationToken token = default)
  74. {
  75. foreach (var config in evseDbConfigs)
  76. {
  77. if (evseCurrentConfigs.Keys.Contains(config.Key) &&
  78. evseCurrentConfigs[config.Key] == config.Value)
  79. {
  80. continue;
  81. }
  82. MessageResult response = null;
  83. var sendTask = async () => await messageService.SendChangeConfigurationRequest(chargeBoxId, config.Key, config.Value);
  84. response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
  85. }
  86. }
  87. }