ServerSetFeeJob.cs 7.6 KB

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