Bläddra i källkod

isolate period energy request

shayne_lo 7 månader sedan
förälder
incheckning
573b59160f

+ 21 - 6
EVCB_OCPP.WSServer/Message/CoreProfileHandler.cs

@@ -77,7 +77,8 @@ internal partial class ProfileHandler
 	//private readonly IDbContextFactory<MeterValueDBContext> metervaluedbContextFactory;
 	private readonly IBusinessServiceFactory businessServiceFactory;
 	private readonly IMainDbService mainDbService;
-	private OuterHttpClient httpClient;
+    private readonly WebDbService webDbService;
+    private OuterHttpClient httpClient;
 
 	public ProfileHandler(
 		IConfiguration configuration,
@@ -87,7 +88,8 @@ internal partial class ProfileHandler
 		ConnectorStatusDbService connectorStatusDbService,
 		IBusinessServiceFactory businessServiceFactory,
 		IMainDbService mainDbService,
-		ILogger<ProfileHandler> logger,
+        WebDbService webDbService,
+        ILogger<ProfileHandler> logger,
 		ServerMessageService messageService,
 		OuterHttpClient httpClient)
 	{
@@ -100,8 +102,8 @@ internal partial class ProfileHandler
 		this.meterValueDbService = meterValueDbService;
 		this.connectorStatusDbService = connectorStatusDbService;
 		this.mainDbService = mainDbService;
-	
-		this.businessServiceFactory = businessServiceFactory;
+        this.webDbService = webDbService;
+        this.businessServiceFactory = businessServiceFactory;
 		this.httpClient = httpClient;
 	}
 
@@ -804,7 +806,7 @@ internal partial class ProfileHandler
 									});
 								}
 
-								if (session.IsBilling)
+								if (session.IsBilling || await webDbService.GetStationIsPeriodEnergyRequired(session.ChargeBoxId))
 								{
 									await messageService.SendDataTransferRequest(
 										session.ChargeBoxId,
@@ -1011,7 +1013,20 @@ internal partial class ProfileHandler
 									var txEnergy = JsonConvert.DeserializeObject<TransactionEnergy>(_confirm.data);
 									var feedto = await db.TransactionRecord.Where(x => x.Id == txEnergy.TxId).Select(x => new { Id = x.Id, ConnectorId = x.ConnectorId, Fee = x.Fee, StopTime = x.StopTime, StartTime = x.StartTime, NotifyPnC = x.NotifyPnC }).FirstOrDefaultAsync();
 									decimal chargedEnergy = 0m;
-									if (feedto == null || string.IsNullOrEmpty(feedto.Fee)) return result;
+
+									if (feedto == null)
+										return result;
+
+									if (string.IsNullOrEmpty(feedto.Fee))
+									{
+										if (feedto.StopTime != GlobalConfig.DefaultNullTime)
+										{
+                                            await mainDbService.InsertOrUpdateTransactionPeriodEnergy(txEnergy.TxId, txEnergy.PeriodEnergy);
+                                        }
+										return result;
+									}
+
+                                    if (feedto == null || string.IsNullOrEmpty(feedto.Fee)) return result;
 
 									if (!feedto.NotifyPnC && !string.IsNullOrEmpty(txEnergy.EVCCID))
 									{

+ 32 - 0
EVCB_OCPP.WSServer/Service/DbService/WebDbService.cs

@@ -6,6 +6,7 @@ using EVCB_OCPP.WSServer.Service.WsService;
 using Microsoft.Data.SqlClient;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.Logging;
+using Newtonsoft.Json.Linq;
 using System;
 using System.Collections.Generic;
 using System.Data;
@@ -234,4 +235,35 @@ public class WebDbService
             return new Dictionary<int, Dictionary<string, string>>();
         }
     }
+
+    internal async Task<bool> GetStationIsPeriodEnergyRequired(string chargeBoxId, CancellationToken token = default)
+    {
+        var stationId = await GetEvseStation(chargeBoxId, token);
+        if (stationId is null)
+        {
+            return false;
+        }
+
+        string getSql = """
+                SELECT [IsPeriodEnergyRequired]
+                FROM [dbo].[Station]
+                WHERE [Id] = @StationId
+                """; 
+
+        var parameters = new DynamicParameters();
+        parameters.Add("@StationId", stationId, direction: ParameterDirection.Input);
+
+        using SqlConnection conn = await webDbConnectionFactory.CreateAsync();
+        try
+        {
+            bool? isPeriodEnergyRequired = await conn.QueryFirstOrDefaultAsync<bool?>(new CommandDefinition(getSql, parameters, cancellationToken: token));
+            return isPeriodEnergyRequired is null ? false : isPeriodEnergyRequired.Value;
+        }
+        catch (Exception e)
+        {
+            logger.LogWarning(e.Message);
+            logger.LogWarning(e.StackTrace);
+            return true;
+        }
+    }
 }