ServerSetFeeJob.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. foreach (var item in _copyClientDic)
  45. {
  46. try
  47. {
  48. ClientData session = item.Value;
  49. if (!session.IsCheckIn)
  50. {
  51. continue;
  52. }
  53. string displayPriceText = await SetDefaultFee(session);
  54. if (string.IsNullOrEmpty(displayPriceText) || displayPriceText == session.DisplayPrice)
  55. {
  56. continue;
  57. }
  58. protalServer.UpdateClientDisplayPrice(item.Key, displayPriceText);
  59. var _CheckFeeDt = DateTime.UtcNow;
  60. using (var db = await maindbContextFactory.CreateDbContextAsync())
  61. {
  62. db.ServerMessage.Add(new ServerMessage()
  63. {
  64. ChargeBoxId = session.ChargeBoxId,
  65. CreatedBy = "Server",
  66. CreatedOn = _CheckFeeDt,
  67. OutAction = Actions.ChangeConfiguration.ToString(),
  68. OutRequest = JsonConvert.SerializeObject(
  69. new ChangeConfigurationRequest()
  70. {
  71. key = "DefaultPrice",
  72. value = displayPriceText
  73. },
  74. new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  75. SerialNo = Guid.NewGuid().ToString(),
  76. InMessage = string.Empty
  77. });
  78. if (session.CustomerId == new Guid("10C7F5BD-C89A-4E2A-8611-B617E0B41A73"))
  79. {
  80. db.ServerMessage.Add(new ServerMessage()
  81. {
  82. ChargeBoxId = session.ChargeBoxId,
  83. CreatedBy = "Server",
  84. CreatedOn = _CheckFeeDt,
  85. OutAction = Actions.ChangeConfiguration.ToString(),
  86. OutRequest = JsonConvert.SerializeObject(
  87. new ChangeConfigurationRequest()
  88. {
  89. key = "ConnectionTimeOut",
  90. value = "120"
  91. },
  92. new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  93. SerialNo = Guid.NewGuid().ToString(),
  94. InMessage = string.Empty
  95. });
  96. db.ServerMessage.Add(new ServerMessage()
  97. {
  98. ChargeBoxId = session.ChargeBoxId,
  99. CreatedBy = "Server",
  100. CreatedOn = _CheckFeeDt,
  101. OutAction = Actions.ChangeConfiguration.ToString(),
  102. OutRequest = JsonConvert.SerializeObject(
  103. new ChangeConfigurationRequest()
  104. {
  105. key = "MeterValueSampleInterval",
  106. value = "3"
  107. },
  108. new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  109. SerialNo = Guid.NewGuid().ToString(),
  110. InMessage = string.Empty
  111. });
  112. }
  113. await db.SaveChangesAsync();
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. logger.LogError("ServerSetFeeTrigger ChargeBoxId:{0} Ex:{1}", item.Key, ex.ToString());
  119. }
  120. }
  121. }
  122. async private Task<string> SetDefaultFee(ClientData client)
  123. {
  124. string displayPriceText = string.Empty;
  125. string charingPriceText = string.Empty;
  126. if (string.IsNullOrEmpty(client.ChargeBoxId)) return displayPriceText;
  127. using (SqlConnection conn = new SqlConnection(webConnectionString))
  128. {
  129. var parameters = new DynamicParameters();
  130. parameters.Add("@MachineId", client.MachineId, DbType.String, ParameterDirection.Input, 36);
  131. string displayPricestrSql = "";
  132. string strSql = "";
  133. if (client.IsAC)
  134. {
  135. displayPricestrSql = """
  136. SELECT [AC_BillingMethod] as BillingMethod,[AC_FeeName] as FeeName,[AC_Fee] as ChargingFeebyHour ,[AC_ParkingFee] as ParkingFee, [Currency]
  137. FROM[StationMachine] left join[dbo].[Station]
  138. on[StationMachine].StationId = Station.[Id]
  139. where StationMachine.MachineId=@MachineId and Station.IsBilling=1;
  140. """;
  141. strSql = """
  142. SELECT CAST( [StartTime] as varchar(5)) StartTime,CAST( [EndTime] as varchar(5)) EndTime,[Fee]
  143. FROM[StationMachine] left join [dbo].[StationFee]
  144. on[StationMachine].StationId = StationFee.StationId
  145. where StationMachine.MachineId =@MachineId and StationFee.IsAC=1;
  146. """;
  147. }
  148. else
  149. {
  150. displayPricestrSql = """
  151. SELECT [DC_BillingMethod] as BillingMethod,[DC_FeeName] as FeeName,[DC_Fee] as ChargingFeebyHour ,[DC_ParkingFee] as ParkingFee, [Currency]
  152. FROM[StationMachine] left join[dbo].[Station]
  153. on[StationMachine].StationId = Station.[Id]
  154. where StationMachine.MachineId=@MachineId and Station.IsBilling=1;
  155. """;
  156. strSql = """
  157. SELECT CAST( [StartTime] as varchar(5)) StartTime,CAST( [EndTime] as varchar(5)) EndTime,[Fee]
  158. FROM[StationMachine] left join [dbo].[StationFee]
  159. on[StationMachine].StationId = StationFee.StationId
  160. where StationMachine.MachineId =@MachineId and StationFee.IsAC=0;
  161. """;
  162. }
  163. var result = await conn.QueryAsync<StationFee>(displayPricestrSql, parameters);
  164. if (result.Count() == 0)
  165. {
  166. return string.Empty;
  167. }
  168. var stationPrice = result.First();
  169. if (stationPrice.BillingMethod == 1)
  170. {
  171. var chargingPriceResult = await conn.QueryAsync<ChargingPrice>(strSql, parameters);
  172. client.ChargingPrices = chargingPriceResult.ToList();
  173. if (string.IsNullOrEmpty(client.ChargingPrices[0].StartTime))
  174. {
  175. client.ChargingPrices = new List<ChargingPrice>();
  176. }
  177. }
  178. displayPriceText = stationPrice.FeeName;
  179. client.BillingMethod = stationPrice.BillingMethod;
  180. client.Currency = stationPrice.Currency;
  181. client.ChargingFeebyHour = stationPrice.ChargingFeebyHour;
  182. client.ParkingFee = stationPrice.ParkingFee;
  183. client.IsBilling = true;
  184. }
  185. return displayPriceText;
  186. }
  187. }