StationConfigService.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. List<Task> updateTasks = new List<Task>();
  92. foreach (WsClientData evse in connectedEvses)
  93. {
  94. int? sessionStationId = GetSessionStation(evse);
  95. if (sessionStationId is not null &&
  96. modifiedStations.Contains(sessionStationId.Value))
  97. {
  98. var tmp = UpdateEvseConfig(evse.ChargeBoxId, sessionStationId.Value);
  99. }
  100. }
  101. await Task.WhenAll(updateTasks);
  102. }
  103. private async Task UpdateStationChangedEvses()
  104. {
  105. List<string> modifiedEvses = new();
  106. var connectedEvses = portalServer.GetClientDic().Values.Where(x => x.IsCheckIn).ToList();
  107. var evseStationPair = await webDbService.GetEvseStationPair(connectedEvses.Select(x => x.ChargeBoxId).ToList());
  108. foreach (var evse in connectedEvses)
  109. {
  110. //var currentStation = await webDbService.GetEvseStation(evse.ChargeBoxId);
  111. int? currentStation = evseStationPair.ContainsKey(evse.ChargeBoxId) ? evseStationPair[evse.ChargeBoxId] : null;
  112. if (currentStation is null)
  113. {
  114. SetSessionStation(evse, null);
  115. continue;
  116. }
  117. int? sessionStationId = GetSessionStation(evse);
  118. if (sessionStationId != currentStation)
  119. {
  120. sessionStationId = currentStation;
  121. await UpdateEvseConfig(evse.ChargeBoxId, currentStation.Value);
  122. }
  123. }
  124. }
  125. private async Task UpdateEvseConfig(string chargeBoxId, int stationId, CancellationToken token = default)
  126. {
  127. GetConfigurationConfirmation confirmation = await GetEvseCurrentConfig(chargeBoxId, token);
  128. if (confirmation is null)
  129. {
  130. logger.LogWarning("{chargeBoxId} get config from evse failed", chargeBoxId);
  131. return;
  132. }
  133. if (!stationConfigRecord.ContainsKey(stationId))
  134. {
  135. logger.LogInformation("{chargeBoxId} doesnt has station config", chargeBoxId);
  136. return;
  137. }
  138. var dbConfigs = stationConfigRecord[stationId];
  139. Dictionary<string, string> evseCurrentConfigs = new Dictionary<string, string>();
  140. evseCurrentConfigs = confirmation.configurationKey.DistinctBy(x=>x.key).ToDictionary(x => x.key, x => x.value);
  141. await ComparenUpdateConfig(chargeBoxId,
  142. evseCurrentConfigs: evseCurrentConfigs,
  143. evseDbConfigs: dbConfigs,
  144. token);
  145. }
  146. private async Task<GetConfigurationConfirmation> GetEvseCurrentConfig(string chargeBoxId, CancellationToken token = default)
  147. {
  148. var sendTask = async (string serialNo) => await messageService.SendGetEVSEConfigureRequest(chargeBoxId, serialNo: serialNo);
  149. var response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
  150. if (response is GetConfigurationConfirmation confirmation)
  151. {
  152. return confirmation;
  153. }
  154. return null;
  155. }
  156. private async Task<Dictionary<string, string>> GetEvseDBCurrentConfig(string chargeBoxId, CancellationToken token = default)
  157. {
  158. var receivedStaionID = await webDbService.GetEvseStation(chargeBoxId, token);
  159. if (receivedStaionID is null)
  160. {
  161. logger.LogInformation("{chargeBoxId} station not found", chargeBoxId);
  162. return null;
  163. }
  164. var staionID = receivedStaionID.Value;
  165. if (!stationConfigRecord.Keys.Contains(staionID))
  166. {
  167. stationConfigRecord[staionID] = await webDbService.GetEvseStationConfig(staionID);
  168. }
  169. return stationConfigRecord[staionID];
  170. //return webDbService.GetCustomerStationEvseConfig(chargeBoxId, token);
  171. }
  172. internal async Task ComparenUpdateConfig(string chargeBoxId,
  173. Dictionary<string, string> evseCurrentConfigs,
  174. Dictionary<string, string> evseDbConfigs,
  175. CancellationToken token = default)
  176. {
  177. foreach (var config in evseDbConfigs)
  178. {
  179. if (evseCurrentConfigs.Keys.Contains(config.Key) &&
  180. evseCurrentConfigs[config.Key] == config.Value)
  181. {
  182. continue;
  183. }
  184. object response = null;
  185. var sendTask = async (string serialNo) => await messageService.SendChangeConfigurationRequest(chargeBoxId, config.Key, config.Value, serialNo: serialNo);
  186. response = await confirmWaitingMessageSerevice.SendAndWaitUntilResultAsync(sendTask, token);
  187. }
  188. }
  189. private bool CheckIsEqual(Dictionary<string, string> d1, Dictionary<string, string> d2)
  190. {
  191. return d1.Count == d2.Count && d1.All(
  192. (d1KV) => d2.TryGetValue(d1KV.Key, out var d2Value) && (
  193. d1KV.Value == d2Value ||
  194. d1KV.Value?.Equals(d2Value) == true)
  195. );
  196. }
  197. private int? GetSessionStation(WsClientData session)
  198. {
  199. if (session is null ||
  200. !session.Data.ContainsKey(Session_Station_Key))
  201. {
  202. return null;
  203. }
  204. return (int?)session.Data[Session_Station_Key];
  205. }
  206. private void SetSessionStation(WsClientData session, int? stationId)
  207. {
  208. if (session is null)
  209. {
  210. return;
  211. }
  212. session.Data[Session_Station_Key] = stationId;
  213. }
  214. }