SmartChargingJob.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using EVCB_OCPP.WSServer.Message;
  2. using EVCB_OCPP.WSServer.Service;
  3. using Microsoft.Data.SqlClient;
  4. using Quartz;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using EVCB_OCPP.WSServer.Dto;
  11. using Microsoft.Extensions.Configuration;
  12. using Dapper;
  13. using Microsoft.Extensions.Logging;
  14. namespace EVCB_OCPP.WSServer.Jobs;
  15. [DisallowConcurrentExecution]
  16. public class SmartChargingJob : IJob
  17. {
  18. public SmartChargingJob(
  19. ProtalServer protalServer,
  20. IConfiguration configuration,
  21. ILogger<SmartChargingJob> 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<SmartChargingJob> logger;
  30. private static List<StationInfoDto> _StationInfo = new List<StationInfoDto>();
  31. public async Task Execute(IJobExecutionContext context)
  32. {
  33. //logger.LogDebug("{0} Started", nameof(SmartChargingJob));
  34. List<StationInfoDto> stations = null;
  35. using (SqlConnection conn = new SqlConnection(webConnectionString))
  36. {
  37. string strSql = "SELECT[Id],[LBMode],[LBCurrent] as Availability FROM[StandardOCPP_Web].[dbo].[Station]" +
  38. "where LBMode = 1; ";
  39. var result = await conn.QueryAsync<StationInfoDto>(strSql);
  40. stations = result.ToList();
  41. }
  42. foreach (var station in stations)
  43. {
  44. var compareStation = _StationInfo.Where(x => x.Id == station.Id).FirstOrDefault();
  45. if (compareStation != null && (station.Id != compareStation.Id || station.Availability == compareStation.Availability))
  46. {
  47. continue;
  48. }
  49. var _powerDic = await protalServer.LoadingBalanceService.GetSettingPower(station.Id);
  50. if (_powerDic == null)
  51. {
  52. continue;
  53. }
  54. foreach (var kv in _powerDic)
  55. {
  56. try
  57. {
  58. if (kv.Value.HasValue)
  59. {
  60. protalServer.ProfileHandler.SetChargingProfile(kv.Key, kv.Value.Value, Packet.Messages.SubTypes.ChargingRateUnitType.W);
  61. }
  62. }
  63. catch (Exception ex)
  64. {
  65. logger.LogError("Set Profile Exception: {0}", ex.ToString());
  66. }
  67. }
  68. }
  69. _StationInfo = stations;
  70. }
  71. }