HealthCheckTriggerJob.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using EVCB_OCPP.Domain;
  2. using Microsoft.Extensions.Logging;
  3. using OCPPServer.Protocol;
  4. using Quartz;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace EVCB_OCPP.WSServer.Jobs
  11. {
  12. [DisallowConcurrentExecution]
  13. public class HealthCheckTriggerJob : IJob
  14. {
  15. public HealthCheckTriggerJob(
  16. ProtalServer protalServer,
  17. ILogger<HealthCheckTriggerJob> logger)
  18. {
  19. this.protalServer = protalServer;
  20. this.logger = logger;
  21. }
  22. private readonly ProtalServer protalServer;
  23. private readonly ILogger<HealthCheckTriggerJob> logger;
  24. public async Task Execute(IJobExecutionContext context)
  25. {
  26. logger.LogDebug("{0} Started", nameof(HealthCheckTriggerJob));
  27. Dictionary<string, ClientData> _copyClientDic = protalServer.ClientDic;
  28. var removeClients = _copyClientDic.Where(x => x.Value.LastActiveTime < DateTime.UtcNow.AddSeconds(-300)).Select(x => x.Value).ToList();
  29. foreach (var session in removeClients)
  30. {
  31. Console.WriteLine(string.Format("Server forced to shut down ChargeBox ({0}: LastActiveTime{1})", session.ChargeBoxId, session.LastActiveTime));
  32. protalServer.RemoveClient(session);
  33. }
  34. }
  35. }
  36. }