ServerSetFeeJob.cs 7.4 KB

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