ServerSetFeeJob.cs 7.0 KB

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