DenyModelCheckJob.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. {
  16. [DisallowConcurrentExecution]
  17. public class DenyModelCheckJob : IJob
  18. {
  19. public DenyModelCheckJob(
  20. ProtalServer protalServer,
  21. IConfiguration configuration,
  22. ILogger<DenyModelCheckJob> logger)
  23. {
  24. this.webConnectionString = configuration.GetConnectionString("WebDBContext");
  25. this.protalServer = protalServer;
  26. this.logger = logger;
  27. }
  28. private readonly string webConnectionString;
  29. private readonly ProtalServer protalServer;
  30. private readonly ILogger<DenyModelCheckJob> logger;
  31. public async Task Execute(IJobExecutionContext context)
  32. {
  33. logger.LogDebug("{0} Started", nameof(DenyModelCheckJob));
  34. try
  35. {
  36. using (SqlConnection conn = new SqlConnection(webConnectionString))
  37. {
  38. string strSql = "SELECT [Value] FROM[StandardOCPP_Web].[dbo].[KernelConfig]" +
  39. "where SystemKey = 'DenyModelNames'; ";
  40. var result = await conn.QueryAsync<string>(strSql);
  41. GlobalConfig.DenyModelNames = result.FirstOrDefault().Split(',').ToList();
  42. logger.LogDebug("Current DenyList:[{0}]", string.Join(",", GlobalConfig.DenyModelNames));
  43. }
  44. if (string.IsNullOrEmpty(GlobalConfig.DenyModelNames[0]))
  45. {
  46. return;
  47. }
  48. Dictionary<string, ClientData> _copyClientDic = protalServer.ClientDic;
  49. foreach (var denyName in GlobalConfig.DenyModelNames)
  50. {
  51. var removeClients = _copyClientDic.Where(x => x.Key.StartsWith(denyName)).Select(x => x.Value).ToList();
  52. foreach (var session in removeClients)
  53. {
  54. Console.WriteLine(string.Format("Server forced to shut down ChargeBox ({0}: Reason: DenyModelName-{1}", session.ChargeBoxId, denyName));
  55. protalServer.RemoveClient(session);
  56. }
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61. logger.LogError("DenyModelCheckTrigger Ex:{0}", ex.ToString());
  62. }
  63. }
  64. }
  65. }