StationConfigService.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using EVCB_OCPP.Packet.Messages.Core;
  2. using EVCB_OCPP.WSServer.Message;
  3. using EVCB_OCPP.WSServer.Service.DbService;
  4. using EVCB_OCPP.WSServer.Service.WsService;
  5. using log4net.Core;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Hosting;
  8. using Microsoft.Extensions.Logging;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace EVCB_OCPP.WSServer.Service;
  15. public static class StationConfigServiceExt
  16. {
  17. public static Task InitStationConfigService(this IHost host)
  18. {
  19. var server = host.Services.GetRequiredService<ProtalServer>();
  20. var stationConfigService = host.Services.GetRequiredService<StationConfigService>();
  21. server.InitActions.Add(stationConfigService.CheckAndUpdateEvseConfig);
  22. return host.Services.GetRequiredService<StationConfigService>().Init();
  23. }
  24. }
  25. public class StationConfigService
  26. {
  27. public StationConfigService(
  28. ProtalServer portalServer
  29. , WebDbService webDbService
  30. , ServerMessageService messageService
  31. , ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice
  32. , ILogger<StationConfigService> logger)
  33. {
  34. this.portalServer = portalServer;
  35. this.webDbService = webDbService;
  36. this.messageService = messageService;
  37. this.confirmWaitingMessageSerevice = confirmWaitingMessageSerevice;
  38. this.logger = logger;
  39. }
  40. private static string Session_Station_Key = "StationConfigService_Station";
  41. private readonly ProtalServer portalServer;
  42. private readonly WebDbService webDbService;
  43. private readonly ServerMessageService messageService;
  44. private readonly ConfirmWaitingMessageSerevice confirmWaitingMessageSerevice;
  45. private readonly ILogger<StationConfigService> logger;
  46. internal static Dictionary<int, Dictionary<string, string>> stationConfigRecord = null;
  47. public async Task Init()
  48. {
  49. stationConfigRecord = await webDbService.GetStationEvseConfigs();
  50. }
  51. public async Task CheckAndUpdateEvseConfig(WsClientData session, CancellationToken token = default)
  52. {
  53. var chargeBoxId = session.ChargeBoxId;
  54. var stationId = await webDbService.GetEvseStation(chargeBoxId, token);
  55. if (stationId is null)
  56. {
  57. return;
  58. }
  59. int? sessionStationId = GetSessionStation(session);
  60. if (sessionStationId != stationId)
  61. {
  62. SetSessionStation(session, stationId);
  63. await UpdateEvseConfig(chargeBoxId, stationId.Value, token);
  64. }
  65. return;
  66. }
  67. public async Task CheckAndUpdateStationConfig()
  68. {
  69. await UpdateStationConfigChangedEvses();
  70. await UpdateStationChangedEvses();
  71. return;
  72. }
  73. private async Task UpdateStationConfigChangedEvses()
  74. {
  75. List<int> modifiedStations = new();
  76. var dbStationEvseConfig = await webDbService.GetStationEvseConfigs();
  77. foreach (var stationConfig in dbStationEvseConfig)
  78. {
  79. if (!stationConfigRecord.ContainsKey(stationConfig.Key) ||
  80. !CheckIsEqual(stationConfig.Value, stationConfigRecord[stationConfig.Key]))
  81. {
  82. modifiedStations.Add(stationConfig.Key);
  83. }
  84. }
  85. if (modifiedStations.Count == 0)
  86. {
  87. return;
  88. }
  89. stationConfigRecord = dbStationEvseConfig;
  90. Dictionary<string, WsClientData>.ValueCollection connectedEvses = portalServer.GetClientDic().Values;
  91. foreach (WsClientData evse in connectedEvses)
  92. {
  93. int? sessionStationId = GetSessionStation(evse);
  94. if (sessionStationId is not null &&
  95. modifiedStations.Contains(sessionStationId.Value))
  96. {
  97. await UpdateEvseConfig(evse.ChargeBoxId, sessionStationId.Value);
  98. }
  99. }
  100. }
  101. private async Task UpdateStationChangedEvses()
  102. {
  103. List<string> modifiedEvses = new();
  104. var connectedEvses = portalServer.GetClientDic().Values.ToList();
  105. var evseStationPair = await webDbService.GetEvseStationPair(connectedEvses.Select(x => x.ChargeBoxId).ToList());
  106. foreach (var evse in connectedEvses)
  107. {
  108. //var currentStation = await webDbService.GetEvseStation(evse.ChargeBoxId);
  109. int? currentStation = evseStationPair.ContainsKey(evse.ChargeBoxId) ? evseStationPair[evse.ChargeBoxId] : null;
  110. if (currentStation is null)
  111. {
  112. SetSessionStation(evse, null);
  113. continue;
  114. }
  115. int? sessionStationId = GetSessionStation(evse);
  116. if (sessionStationId != currentStation)
  117. {
  118. sessionStationId = currentStation;
  119. await UpdateEvseConfig(evse.ChargeBoxId, currentStation.Value);
  120. }
  121. }
  122. }
  123. private async Task UpdateEvseConfig(string chargeBoxId, int stationId, CancellationToken token = default)
  124. {
  125. MessageResult getEvseCurrentConfigResponse = await GetEvseCurrentConfig(chargeBoxId, token);
  126. if (!getEvseCurrentConfigResponse.Success || getEvseCurrentConfigResponse.Message is not GetConfigurationConfirmation confirmation)
  127. {
  128. logger.LogWarning("{chargeBoxId} get config from evse failed", chargeBoxId);
  129. return;
  130. }
  131. if (!stationConfigRecord.ContainsKey(stationId))
  132. {
  133. logger.LogInformation("{chargeBoxId} doesnt has station config", chargeBoxId);
  134. return;
  135. }
  136. var dbConfigs = stationConfigRecord[stationId];
  137. Dictionary<string, string> evseCurrentConfigs = new Dictionary<string, string>();
  138. evseCurrentConfigs = confirmation.configurationKey.DistinctBy(x=>x.key).ToDictionary(x => x.key, x => x.value);
  139. await ComparenUpdateConfig(chargeBoxId,
  140. evseCurrentConfigs: evseCurrentConfigs,
  141. evseDbConfigs: dbConfigs,
  142. token);
  143. }
  144. private async Task<MessageResult> GetEvseCurrentConfig(string chargeBoxId, CancellationToken token = default)
  145. {
  146. var sendTask = async () => await messageService.SendGetEVSEConfigureRequest(chargeBoxId);
  147. var response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
  148. return response;
  149. }
  150. private async Task<Dictionary<string, string>> GetEvseDBCurrentConfig(string chargeBoxId, CancellationToken token = default)
  151. {
  152. var receivedStaionID = await webDbService.GetEvseStation(chargeBoxId, token);
  153. if (receivedStaionID is null)
  154. {
  155. logger.LogInformation("{chargeBoxId} station not found", chargeBoxId);
  156. return null;
  157. }
  158. var staionID = receivedStaionID.Value;
  159. if (!stationConfigRecord.Keys.Contains(staionID))
  160. {
  161. stationConfigRecord[staionID] = await webDbService.GetEvseStationConfig(staionID);
  162. }
  163. return stationConfigRecord[staionID];
  164. //return webDbService.GetCustomerStationEvseConfig(chargeBoxId, token);
  165. }
  166. internal async Task ComparenUpdateConfig(string chargeBoxId,
  167. Dictionary<string, string> evseCurrentConfigs,
  168. Dictionary<string, string> evseDbConfigs,
  169. CancellationToken token = default)
  170. {
  171. foreach (var config in evseDbConfigs)
  172. {
  173. if (evseCurrentConfigs.Keys.Contains(config.Key) &&
  174. evseCurrentConfigs[config.Key] == config.Value)
  175. {
  176. continue;
  177. }
  178. MessageResult response = null;
  179. var sendTask = async () => await messageService.SendChangeConfigurationRequest(chargeBoxId, config.Key, config.Value);
  180. response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
  181. }
  182. }
  183. private bool CheckIsEqual(Dictionary<string, string> d1, Dictionary<string, string> d2)
  184. {
  185. return d1.Count == d2.Count && d1.All(
  186. (d1KV) => d2.TryGetValue(d1KV.Key, out var d2Value) && (
  187. d1KV.Value == d2Value ||
  188. d1KV.Value?.Equals(d2Value) == true)
  189. );
  190. }
  191. private int? GetSessionStation(WsClientData session)
  192. {
  193. if (session is null ||
  194. !session.Data.ContainsKey(Session_Station_Key))
  195. {
  196. return null;
  197. }
  198. return (int?)session.Data[Session_Station_Key];
  199. }
  200. private void SetSessionStation(WsClientData session, int? stationId)
  201. {
  202. if (session is null)
  203. {
  204. return;
  205. }
  206. session.Data[Session_Station_Key] = stationId;
  207. }
  208. }