StationConfigService.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.Where(x => x.IsCheckIn).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. GetConfigurationConfirmation confirmation = await GetEvseCurrentConfig(chargeBoxId, token);
  126. if (confirmation is null)
  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<GetConfigurationConfirmation> GetEvseCurrentConfig(string chargeBoxId, CancellationToken token = default)
  145. {
  146. var sendTask = async () => await messageService.SendGetEVSEConfigureRequest(chargeBoxId);
  147. var response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
  148. if (response is GetConfigurationConfirmation confirmation)
  149. {
  150. return confirmation;
  151. }
  152. return null;
  153. }
  154. private async Task<Dictionary<string, string>> GetEvseDBCurrentConfig(string chargeBoxId, CancellationToken token = default)
  155. {
  156. var receivedStaionID = await webDbService.GetEvseStation(chargeBoxId, token);
  157. if (receivedStaionID is null)
  158. {
  159. logger.LogInformation("{chargeBoxId} station not found", chargeBoxId);
  160. return null;
  161. }
  162. var staionID = receivedStaionID.Value;
  163. if (!stationConfigRecord.Keys.Contains(staionID))
  164. {
  165. stationConfigRecord[staionID] = await webDbService.GetEvseStationConfig(staionID);
  166. }
  167. return stationConfigRecord[staionID];
  168. //return webDbService.GetCustomerStationEvseConfig(chargeBoxId, token);
  169. }
  170. internal async Task ComparenUpdateConfig(string chargeBoxId,
  171. Dictionary<string, string> evseCurrentConfigs,
  172. Dictionary<string, string> evseDbConfigs,
  173. CancellationToken token = default)
  174. {
  175. foreach (var config in evseDbConfigs)
  176. {
  177. if (evseCurrentConfigs.Keys.Contains(config.Key) &&
  178. evseCurrentConfigs[config.Key] == config.Value)
  179. {
  180. continue;
  181. }
  182. object response = null;
  183. var sendTask = async () => await messageService.SendChangeConfigurationRequest(chargeBoxId, config.Key, config.Value);
  184. response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
  185. }
  186. }
  187. private bool CheckIsEqual(Dictionary<string, string> d1, Dictionary<string, string> d2)
  188. {
  189. return d1.Count == d2.Count && d1.All(
  190. (d1KV) => d2.TryGetValue(d1KV.Key, out var d2Value) && (
  191. d1KV.Value == d2Value ||
  192. d1KV.Value?.Equals(d2Value) == true)
  193. );
  194. }
  195. private int? GetSessionStation(WsClientData session)
  196. {
  197. if (session is null ||
  198. !session.Data.ContainsKey(Session_Station_Key))
  199. {
  200. return null;
  201. }
  202. return (int?)session.Data[Session_Station_Key];
  203. }
  204. private void SetSessionStation(WsClientData session, int? stationId)
  205. {
  206. if (session is null)
  207. {
  208. return;
  209. }
  210. session.Data[Session_Station_Key] = stationId;
  211. }
  212. }