ServerSetFeeJob.cs 7.6 KB

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