123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using EVCB_OCPP.Domain;
- using EVCB_OCPP.Domain.Models.Database;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Logging;
- using OCPPServer.Protocol;
- using Quartz;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace EVCB_OCPP.WSServer.Jobs
- {
- [DisallowConcurrentExecution]
- public class HeartBeatCheckJob : IJob
- {
- public HeartBeatCheckJob(
- ProtalServer protalServer,
- IDbContextFactory<MainDBContext> maindbContextFactory,
- ILogger<HeartBeatCheckJob> logger)
- {
- this.protalServer = protalServer;
- this.maindbContextFactory = maindbContextFactory;
- this.logger = logger;
- }
- private readonly ProtalServer protalServer;
- private readonly IDbContextFactory<MainDBContext> maindbContextFactory;
- private readonly ILogger<HeartBeatCheckJob> logger;
- public async Task Execute(IJobExecutionContext context)
- {
- logger.LogDebug("{0} Started", nameof(HeartBeatCheckJob));
- try
- {
- Stopwatch watch = new Stopwatch();
- Dictionary<string, ClientData> _copyClientDic = protalServer.ClientDic;
- var cdt = DateTime.UtcNow;
- var clients = _copyClientDic.Where(x => x.Value.LastActiveTime > cdt.AddSeconds(-120)).Select(x => x.Value).ToList();
- watch.Start();
- var wssClients = clients.Where(x => x.UriScheme.Equals("wss")).ToList();
- var wsClients = clients.Except(wssClients).ToList();
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- foreach (var session in wssClients)
- {
- var machine = new Machine() { Id = session.MachineId };
- if (machine != null)
- {
- //db.Configuration.AutoDetectChangesEnabled = false;
- //db.Configuration.ValidateOnSaveEnabled = false;
- db.Machine.Attach(machine);
- machine.HeartbeatUpdatedOn = DateTime.UtcNow;
- machine.ConnectionType = 2;
- db.Entry(machine).Property(x => x.HeartbeatUpdatedOn).IsModified = true;
- db.Entry(machine).Property(x => x.ConnectionType).IsModified = true;
- }
- }
- await db.SaveChangesAsync();
- }
- using (var db = await maindbContextFactory.CreateDbContextAsync())
- {
- foreach (var session in wsClients)
- {
- var machine = new Machine() { Id = session.MachineId };
- if (machine != null)
- {
- //db.Configuration.AutoDetectChangesEnabled = false;
- //db.Configuration.ValidateOnSaveEnabled = false;
- db.Machine.Attach(machine);
- machine.HeartbeatUpdatedOn = DateTime.UtcNow;
- machine.ConnectionType = 1;
- db.Entry(machine).Property(x => x.HeartbeatUpdatedOn).IsModified = true;
- db.Entry(machine).Property(x => x.ConnectionType).IsModified = true;
- }
- }
- await db.SaveChangesAsync();
- }
- //foreach (var session in clients)
- //{
- // using (var db = await maindbContextFactory.CreateDbContextAsync())
- // {
- // var machine = new Machine() { Id = session.MachineId };
- // if (machine != null)
- // {
- // //db.Configuration.AutoDetectChangesEnabled = false;
- // //db.Configuration.ValidateOnSaveEnabled = false;
- // db.Machine.Attach(machine);
- // machine.HeartbeatUpdatedOn = DateTime.UtcNow;
- // machine.ConnectionType = session.UriScheme.Equals("wss") ? 2 : 1;
- // db.Entry(machine).Property(x => x.HeartbeatUpdatedOn).IsModified = true;
- // db.Entry(machine).Property(x => x.ConnectionType).IsModified = true;
- // await db.SaveChangesAsync();
- // }
- // }
- //}
- watch.Stop();
- if (watch.ElapsedMilliseconds / 1000 > 5)
- {
- logger.LogCritical("Update HeartBeatCheckTrigger cost " + watch.ElapsedMilliseconds / 1000 + " seconds.");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("***********************************************************");
- logger.LogError(string.Format("HeartBeatCheckTrigger Ex:{0}", ex.ToString()));
- }
- }
- }
- }
|