ServerSetFeeJob.cs 6.9 KB

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