HealthCheckTriggerJob.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using EVCB_OCPP.Domain;
  2. using EVCB_OCPP.WSServer.Service.DbService;
  3. using EVCB_OCPP.WSServer.Service.WsService;
  4. using Microsoft.Extensions.Logging;
  5. using Quartz;
  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.Jobs;
  12. [DisallowConcurrentExecution]
  13. public class HealthCheckTriggerJob : IJob
  14. {
  15. public HealthCheckTriggerJob(
  16. ProtalServer protalServer,
  17. IMainDbService mainDbService,
  18. ILogger<HealthCheckTriggerJob> logger)
  19. {
  20. this.protalServer = protalServer;
  21. this.mainDbService = mainDbService;
  22. this.logger = logger;
  23. }
  24. private readonly ProtalServer protalServer;
  25. private readonly IMainDbService mainDbService;
  26. private readonly ILogger<HealthCheckTriggerJob> logger;
  27. public async Task Execute(IJobExecutionContext context)
  28. {
  29. //logger.LogDebug("{0} Started", nameof(HealthCheckTriggerJob));
  30. Dictionary<string, WsClientData> _copyClientDic = protalServer.GetClientDic();
  31. Dictionary<string, int> chargeBoxHeartBeatIngerval = await GetChargeBoxIdleSeconds(_copyClientDic?.Keys?.ToList());
  32. var removeClients = _copyClientDic
  33. .Where(x => x.Value.LastActiveTime < DateTime.UtcNow.AddSeconds(-1 * chargeBoxHeartBeatIngerval[x.Key]))
  34. .Select(x => x.Value)
  35. .ToList();
  36. foreach (var session in removeClients)
  37. {
  38. logger.LogDebug("Server forced to shut down ChargeBox ({0}: LastActiveTime{1})", session.ChargeBoxId, session.LastActiveTime);
  39. protalServer.RemoveClient(session, "Inactive");
  40. }
  41. return ;
  42. }
  43. private async Task<Dictionary<string, int>> GetChargeBoxIdleSeconds(List<string> chageboxList)
  44. {
  45. if (chageboxList is null)
  46. {
  47. return new Dictionary<string, int>();
  48. }
  49. var getHeartbeatIntervalTasks = chageboxList.Select(async x => (x, await mainDbService.GetMachineHeartbeatInterval(x))).ToList();
  50. await Task.WhenAll(getHeartbeatIntervalTasks);
  51. var chargeBoxHeartbeatIntervalIntervalPair = getHeartbeatIntervalTasks.Select(x => x.Result).ToDictionary(x => x.x, x=> x.Item2 );
  52. Dictionary<string, int> toReturn = new Dictionary<string, int>();
  53. foreach (var chageboxId in chageboxList)
  54. {
  55. //var heartBeatIntervalString = await mainDbService.GetMachineHeartbeatInterval(chageboxId);
  56. var heartBeatIntervalString = chargeBoxHeartbeatIntervalIntervalPair.Keys.Contains(chageboxId) ? chargeBoxHeartbeatIntervalIntervalPair[chageboxId] : "0";
  57. int idelSeconds = 300;
  58. if (int.TryParse(heartBeatIntervalString, out var parsedInterval)
  59. && parsedInterval * 2 > idelSeconds)
  60. {
  61. idelSeconds = parsedInterval * 2;
  62. }
  63. toReturn.Add(chageboxId, idelSeconds);
  64. }
  65. return toReturn;
  66. }
  67. }