SmartChargingJob.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. {
  16. [DisallowConcurrentExecution]
  17. public class SmartChargingJob : IJob
  18. {
  19. public SmartChargingJob(
  20. ProtalServer protalServer,
  21. IConfiguration configuration,
  22. ILogger<SmartChargingJob> 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<SmartChargingJob> logger;
  31. private static List<StationInfoDto> _StationInfo = new List<StationInfoDto>();
  32. public async Task Execute(IJobExecutionContext context)
  33. {
  34. logger.LogDebug("{0} Started", nameof(SmartChargingJob));
  35. List<StationInfoDto> stations = null;
  36. using (SqlConnection conn = new SqlConnection(webConnectionString))
  37. {
  38. string strSql = "SELECT[Id],[LBMode],[LBCurrent] as Availability FROM[StandardOCPP_Web].[dbo].[Station]" +
  39. "where LBMode = 1; ";
  40. var result = await conn.QueryAsync<StationInfoDto>(strSql);
  41. stations = result.ToList();
  42. }
  43. foreach (var station in stations)
  44. {
  45. var compareStation = _StationInfo.Where(x => x.Id == station.Id).FirstOrDefault();
  46. if (compareStation != null && (station.Id != compareStation.Id || station.Availability == compareStation.Availability))
  47. {
  48. continue;
  49. }
  50. var _powerDic = await protalServer.LoadingBalanceService.GetSettingPower(station.Id);
  51. if (_powerDic == null)
  52. {
  53. continue;
  54. }
  55. foreach (var kv in _powerDic)
  56. {
  57. try
  58. {
  59. if (kv.Value.HasValue)
  60. {
  61. protalServer.ProfileHandler.SetChargingProfile(kv.Key, kv.Value.Value, Packet.Messages.SubTypes.ChargingRateUnitType.W);
  62. }
  63. }
  64. catch (Exception ex)
  65. {
  66. logger.LogError("Set Profile Exception: {0}", ex.ToString());
  67. }
  68. }
  69. }
  70. _StationInfo = stations;
  71. }
  72. }
  73. }