1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using EVCB_OCPP.Domain;
- using EVCB_OCPP.WSServer.Service.DbService;
- using EVCB_OCPP.WSServer.Service.WsService;
- using Microsoft.Extensions.Logging;
- using Quartz;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace EVCB_OCPP.WSServer.Jobs;
- [DisallowConcurrentExecution]
- public class HealthCheckTriggerJob : IJob
- {
- public HealthCheckTriggerJob(
- ProtalServer protalServer,
- IMainDbService mainDbService,
- ILogger<HealthCheckTriggerJob> logger)
- {
- this.protalServer = protalServer;
- this.mainDbService = mainDbService;
- this.logger = logger;
- }
- private readonly ProtalServer protalServer;
- private readonly IMainDbService mainDbService;
- private readonly ILogger<HealthCheckTriggerJob> logger;
- public async Task Execute(IJobExecutionContext context)
- {
- //logger.LogDebug("{0} Started", nameof(HealthCheckTriggerJob));
- Dictionary<string, WsClientData> _copyClientDic = protalServer.GetClientDic();
- Dictionary<string, int> chargeBoxHeartBeatIngerval = await GetChargeBoxIdleSeconds(_copyClientDic?.Keys?.ToList());
- var removeClients = _copyClientDic
- .Where(x => x.Value.LastActiveTime < DateTime.UtcNow.AddSeconds(-1 * chargeBoxHeartBeatIngerval[x.Key]))
- .Select(x => x.Value)
- .ToList();
- foreach (var session in removeClients)
- {
- logger.LogDebug("Server forced to shut down ChargeBox ({0}: LastActiveTime{1})", session.ChargeBoxId, session.LastActiveTime);
- protalServer.RemoveClient(session, "Inactive");
- }
- return ;
- }
- private async Task<Dictionary<string, int>> GetChargeBoxIdleSeconds(List<string> chageboxList)
- {
- if (chageboxList is null)
- {
- return new Dictionary<string, int>();
- }
- var getHeartbeatIntervalTasks = chageboxList.Select(async x => (x, await mainDbService.GetMachineHeartbeatInterval(x))).ToList();
- await Task.WhenAll(getHeartbeatIntervalTasks);
- var chargeBoxHeartbeatIntervalIntervalPair = getHeartbeatIntervalTasks.Select(x => x.Result).ToDictionary(x => x.x, x=> x.Item2 );
- Dictionary<string, int> toReturn = new Dictionary<string, int>();
- foreach (var chageboxId in chageboxList)
- {
- //var heartBeatIntervalString = await mainDbService.GetMachineHeartbeatInterval(chageboxId);
- var heartBeatIntervalString = chargeBoxHeartbeatIntervalIntervalPair.Keys.Contains(chageboxId) ? chargeBoxHeartbeatIntervalIntervalPair[chageboxId] : "0";
- int idelSeconds = 300;
- if (int.TryParse(heartBeatIntervalString, out var parsedInterval)
- && parsedInterval * 2 > idelSeconds)
- {
- idelSeconds = parsedInterval * 2;
- }
- toReturn.Add(chageboxId, idelSeconds);
- }
- return toReturn;
- }
- }
|