DenyModelCheckJob.cs 2.5 KB

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