ServerSetFeeJob.cs 8.7 KB

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