ServerSetFeeJob.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Dapper;
  2. using EVCB_OCPP.Domain;
  3. using EVCB_OCPP.Domain.ConnectionFactory;
  4. using EVCB_OCPP.Packet.Features;
  5. using EVCB_OCPP.Packet.Messages.Core;
  6. using EVCB_OCPP.WSServer.Dto;
  7. using EVCB_OCPP.WSServer.Helper;
  8. using EVCB_OCPP.WSServer.Message;
  9. using EVCB_OCPP.WSServer.Service;
  10. using EVCB_OCPP.WSServer.Service.WsService;
  11. using Microsoft.Data.SqlClient;
  12. using Microsoft.EntityFrameworkCore;
  13. using Microsoft.Extensions.Configuration;
  14. using Microsoft.Extensions.Logging;
  15. using Newtonsoft.Json;
  16. using Quartz;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Data;
  20. using System.Linq;
  21. using System.Threading.Tasks;
  22. namespace EVCB_OCPP.WSServer.Jobs;
  23. [DisallowConcurrentExecution]
  24. public class ServerSetFeeJob : IJob
  25. {
  26. private readonly ProtalServer protalServer;
  27. private readonly ISqlConnectionFactory<WebDBConetext> webDbConnectionFactory;
  28. private readonly ServerMessageService messageService;
  29. private readonly IMainDbService mainDbService;
  30. private readonly ILogger<ServerSetFeeJob> logger;
  31. //private readonly string webConnectionString;
  32. public ServerSetFeeJob(
  33. ProtalServer protalServer,
  34. //IConfiguration configuration,
  35. ISqlConnectionFactory<WebDBConetext> sqlConnectionFactory,
  36. ServerMessageService messageService,
  37. IMainDbService mainDbService,
  38. ILogger<ServerSetFeeJob> logger)
  39. {
  40. this.protalServer = protalServer;
  41. this.webDbConnectionFactory = sqlConnectionFactory;
  42. this.messageService = messageService;
  43. this.mainDbService = mainDbService;
  44. this.logger = logger;
  45. //this.webConnectionString = configuration.GetConnectionString("WebDBContext");
  46. }
  47. public async Task Execute(IJobExecutionContext context)
  48. {
  49. //logger.LogDebug("{0} Started", nameof(ServerSetFeeJob));
  50. //BasicMessageHandler msgAnalyser = new BasicMessageHandler();
  51. Dictionary<string, WsClientData> _copyClientDic = protalServer.GetClientDic();
  52. //using var db = maindbContextFactory.CreateDbContextAsync();
  53. foreach (var item in _copyClientDic)
  54. {
  55. try
  56. {
  57. WsClientData session = item.Value;
  58. if (!session.IsCheckIn)
  59. {
  60. continue;
  61. }
  62. string displayPriceText = await SetDefaultFee(session);
  63. if (string.IsNullOrEmpty(displayPriceText) || displayPriceText == session.DisplayPrice)
  64. {
  65. continue;
  66. }
  67. protalServer.UpdateClientDisplayPrice(item.Key, displayPriceText);
  68. await messageService.SendChangeConfigurationRequest(
  69. session.ChargeBoxId, key: "DefaultPrice", value: displayPriceText);
  70. if (session.CustomerId == new Guid("10C7F5BD-C89A-4E2A-8611-B617E0B41A73"))
  71. {
  72. await messageService.SendChangeConfigurationRequest(
  73. session.ChargeBoxId, key: "ConnectionTimeOut", value: "120");
  74. await messageService.SendChangeConfigurationRequest(
  75. session.ChargeBoxId, key: "MeterValueSampleInterval", value: "3");
  76. }
  77. }
  78. catch (Exception ex)
  79. {
  80. logger.LogError("ServerSetFeeTrigger ChargeBoxId:{0} Ex:{1}", item.Key, ex.ToString());
  81. }
  82. }
  83. }
  84. async private Task<string> SetDefaultFee(WsClientData client)
  85. {
  86. string displayPriceText = string.Empty;
  87. string charingPriceText = string.Empty;
  88. if (string.IsNullOrEmpty(client.ChargeBoxId)) return displayPriceText;
  89. try
  90. {
  91. using (SqlConnection conn = await webDbConnectionFactory.CreateAsync())
  92. {
  93. var parameters = new DynamicParameters();
  94. parameters.Add("@MachineId", client.MachineId, DbType.String, ParameterDirection.Input, 36);
  95. string displayPricestrSql = "";
  96. string strSql = "";
  97. if (client.IsAC)
  98. {
  99. displayPricestrSql = """
  100. SELECT [AC_BillingMethod] as BillingMethod,[AC_FeeName] as FeeName,[AC_Fee] as ChargingFeebyHour ,[AC_ParkingFee] as ParkingFee, [Currency]
  101. FROM[StationMachine] left join[dbo].[Station]
  102. on[StationMachine].StationId = Station.[Id]
  103. where StationMachine.MachineId=@MachineId and Station.IsBilling=1;
  104. """;
  105. strSql = """
  106. SELECT CAST( [StartTime] as varchar(5)) StartTime,CAST( [EndTime] as varchar(5)) EndTime,[Fee]
  107. FROM[StationMachine] left join [dbo].[StationFee]
  108. on[StationMachine].StationId = StationFee.StationId
  109. where StationMachine.MachineId =@MachineId and StationFee.IsAC=1;
  110. """;
  111. }
  112. else
  113. {
  114. displayPricestrSql = """
  115. SELECT [DC_BillingMethod] as BillingMethod,[DC_FeeName] as FeeName,[DC_Fee] as ChargingFeebyHour ,[DC_ParkingFee] as ParkingFee, [Currency]
  116. FROM[StationMachine] left join[dbo].[Station]
  117. on[StationMachine].StationId = Station.[Id]
  118. where StationMachine.MachineId=@MachineId and Station.IsBilling=1;
  119. """;
  120. strSql = """
  121. SELECT CAST( [StartTime] as varchar(5)) StartTime,CAST( [EndTime] as varchar(5)) EndTime,[Fee]
  122. FROM[StationMachine] left join [dbo].[StationFee]
  123. on[StationMachine].StationId = StationFee.StationId
  124. where StationMachine.MachineId =@MachineId and StationFee.IsAC=0;
  125. """;
  126. }
  127. var result = await conn.QueryAsync<StationFee>(displayPricestrSql, parameters);
  128. if (result.Count() == 0)
  129. {
  130. return string.Empty;
  131. }
  132. var stationPrice = result.First();
  133. if (stationPrice.BillingMethod == 1)
  134. {
  135. var chargingPriceResult = await conn.QueryAsync<ChargingPrice>(strSql, parameters);
  136. client.ChargingPrices = chargingPriceResult.ToList();
  137. if (string.IsNullOrEmpty(client.ChargingPrices[0].StartTime))
  138. {
  139. client.ChargingPrices = new List<ChargingPrice>();
  140. }
  141. }
  142. displayPriceText = stationPrice.FeeName;
  143. client.BillingMethod = stationPrice.BillingMethod;
  144. client.Currency = stationPrice.Currency;
  145. client.ChargingFeebyHour = stationPrice.ChargingFeebyHour;
  146. client.ParkingFee = stationPrice.ParkingFee;
  147. client.IsBilling = true;
  148. }
  149. }
  150. catch (Exception ex)
  151. {
  152. logger.LogError("SetDefaultFee", ex.ToString());
  153. }
  154. return displayPriceText;
  155. }
  156. }