DenyModelCheckJob.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Dapper;
  2. using DnsClient.Internal;
  3. using EVCB_OCPP.WSServer.Service;
  4. using Microsoft.Data.SqlClient;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.Logging;
  7. using OCPPServer.Protocol;
  8. using Quartz;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Configuration;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace EVCB_OCPP.WSServer.Jobs;
  16. [DisallowConcurrentExecution]
  17. public class DenyModelCheckJob : IJob
  18. {
  19. public DenyModelCheckJob(
  20. ProtalServer protalServer,
  21. WebDbService webDbService,
  22. //IConfiguration configuration,
  23. ILogger<DenyModelCheckJob> logger)
  24. {
  25. //this.webConnectionString = configuration.GetConnectionString("WebDBContext");
  26. this.protalServer = protalServer;
  27. this.webDbService = webDbService;
  28. this.logger = logger;
  29. }
  30. //private readonly string webConnectionString;
  31. private readonly ProtalServer protalServer;
  32. private readonly WebDbService webDbService;
  33. private readonly ILogger<DenyModelCheckJob> logger;
  34. public async Task Execute(IJobExecutionContext context)
  35. {
  36. //logger.LogDebug("{0} Started", nameof(DenyModelCheckJob));
  37. try
  38. {
  39. GlobalConfig.DenyModelNames = await webDbService.GetDenyModelNames();
  40. //logger.LogDebug("Current DenyList:[{0}]", string.Join(",", GlobalConfig.DenyModelNames));
  41. if (string.IsNullOrEmpty(GlobalConfig.DenyModelNames[0]))
  42. {
  43. return;
  44. }
  45. Dictionary<string, ClientData> _copyClientDic = protalServer.ClientDic;
  46. foreach (var denyName in GlobalConfig.DenyModelNames)
  47. {
  48. var removeClients = _copyClientDic.Where(x => x.Key.StartsWith(denyName)).Select(x => x.Value).ToList();
  49. foreach (var session in removeClients)
  50. {
  51. //Console.WriteLine(string.Format("Server forced to shut down ChargeBox ({0}: Reason: DenyModelName-{1}", session.ChargeBoxId, denyName));
  52. logger.LogInformation(string.Format("Server forced to shut down ChargeBox ({0}: Reason: DenyModelName-{1}", session.ChargeBoxId, denyName));
  53. protalServer.RemoveClient(session);
  54. }
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. logger.LogError("DenyModelCheckTrigger Ex:{0}", ex.ToString());
  60. }
  61. }
  62. }