HealthCheckTriggerJob.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. [DisallowConcurrentExecution]
  12. public class HealthCheckTriggerJob : IJob
  13. {
  14. public HealthCheckTriggerJob(
  15. ProtalServer protalServer,
  16. ILogger<HealthCheckTriggerJob> logger)
  17. {
  18. this.protalServer = protalServer;
  19. this.logger = logger;
  20. }
  21. private readonly ProtalServer protalServer;
  22. private readonly ILogger<HealthCheckTriggerJob> logger;
  23. public async Task Execute(IJobExecutionContext context)
  24. {
  25. logger.LogDebug("{0} Started", nameof(HealthCheckTriggerJob));
  26. Dictionary<string, ClientData> _copyClientDic = protalServer.ClientDic;
  27. var removeClients = _copyClientDic.Where(x => x.Value.LastActiveTime < DateTime.UtcNow.AddSeconds(-300)).Select(x => x.Value).ToList();
  28. foreach (var session in removeClients)
  29. {
  30. Console.WriteLine(string.Format("Server forced to shut down ChargeBox ({0}: LastActiveTime{1})", session.ChargeBoxId, session.LastActiveTime));
  31. protalServer.RemoveClient(session);
  32. }
  33. }
  34. }