DenyModelCheckJob.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Dapper;
  2. using DnsClient.Internal;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.Logging;
  5. using OCPPServer.Protocol;
  6. using Quartz;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Configuration;
  10. using System.Data.SqlClient;
  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. IConfiguration configuration,
  21. ILogger<DenyModelCheckJob> logger)
  22. {
  23. this.webConnectionString = configuration.GetConnectionString("WebDBContext");
  24. this.protalServer = protalServer;
  25. this.logger = logger;
  26. }
  27. private readonly string webConnectionString;
  28. private readonly ProtalServer protalServer;
  29. private readonly ILogger<DenyModelCheckJob> logger;
  30. public async Task Execute(IJobExecutionContext context)
  31. {
  32. logger.LogDebug("{0} Started", nameof(DenyModelCheckJob));
  33. try
  34. {
  35. using (SqlConnection conn = new SqlConnection(webConnectionString))
  36. {
  37. string strSql = "SELECT [Value] FROM[StandardOCPP_Web].[dbo].[KernelConfig]" +
  38. "where SystemKey = 'DenyModelNames'; ";
  39. var result = await conn.QueryFirstOrDefaultAsync<string>(strSql);
  40. GlobalConfig.DenyModelNames = result.Split(',').ToList();
  41. logger.LogDebug("Current DenyList:[{0}]", string.Join(",", GlobalConfig.DenyModelNames));
  42. }
  43. if (string.IsNullOrEmpty(GlobalConfig.DenyModelNames[0]))
  44. {
  45. return;
  46. }
  47. Dictionary<string, ClientData> _copyClientDic = protalServer.ClientDic;
  48. foreach (var denyName in GlobalConfig.DenyModelNames)
  49. {
  50. var removeClients = _copyClientDic.Where(x => x.Key.StartsWith(denyName)).Select(x => x.Value).ToList();
  51. foreach (var session in removeClients)
  52. {
  53. Console.WriteLine(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. }