StationConfigService.cs 8.7 KB

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