DenyModelCheckJob.cs 2.4 KB

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