|
@@ -1,4 +1,5 @@
|
|
|
using EVCB_OCPP.Domain;
|
|
|
+using EVCB_OCPP.WSServer.Service.DbService;
|
|
|
using EVCB_OCPP.WSServer.Service.WsService;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
@@ -16,28 +17,64 @@ 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 Task Execute(IJobExecutionContext context)
|
|
|
+ public async Task Execute(IJobExecutionContext context)
|
|
|
{
|
|
|
//logger.LogDebug("{0} Started", nameof(HealthCheckTriggerJob));
|
|
|
|
|
|
Dictionary<string, WsClientData> _copyClientDic = protalServer.GetClientDic();
|
|
|
|
|
|
- var removeClients = _copyClientDic.Where(x => x.Value.LastActiveTime < DateTime.UtcNow.AddSeconds(-300)).Select(x => x.Value).ToList();
|
|
|
+ Dictionary<string, int> chargeBoxHeartBeatIngerval = await GetChargeBoxIdelSeconds(_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 Task.CompletedTask;
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<Dictionary<string, int>> GetChargeBoxIdelSeconds(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;
|
|
|
}
|
|
|
}
|