ServerSetFeeJob.cs 8.5 KB

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