Эх сурвалжийг харах

add InstantStopTxReport record

shayne_lo 6 сар өмнө
parent
commit
93ef5bffc6

+ 5 - 1
EVCB_OCPP.WSServer/Message/CoreProfileHandler.cs

@@ -1014,7 +1014,11 @@ internal partial class ProfileHandler
                                         await mainDbService.SetTransactionBillingDone(txEnergy.TxId, 0, string.Empty);
 
                                         IBusinessService customer = await businessServiceFactory.CreateBusinessService(session.CustomerId);
-										await customer.NotifyTransactionCompleted(feedto, roundedPeriodEnergy);
+                                        NotifyTransactionCompletedResult response = await customer.NotifyTransactionCompleted(feedto, roundedPeriodEnergy);
+										if (response != null && response.Success)
+										{
+											await mainDbService.ReportStopTx(feedto.Id, response);
+                                        }
                                     }
                                     return result;
                                 }

+ 1 - 1
EVCB_OCPP.WSServer/Service/BusinessService/BusinessServiceFactory.cs

@@ -20,7 +20,7 @@ public interface IBusinessService
 
     Task NotifyConnectorUnplugged(string chargeBoxId, string data);
 
-    ValueTask<NotifyTransactionCompletedResponse> NotifyTransactionCompleted(TransactionRecord tx, Dictionary<string, decimal> roundedPeriodEnergy);
+    ValueTask<NotifyTransactionCompletedResult> NotifyTransactionCompleted(TransactionRecord tx, Dictionary<string, decimal> roundedPeriodEnergy);
 
 }
 

+ 2 - 2
EVCB_OCPP.WSServer/Service/BusinessService/LocalBusinessService.cs

@@ -91,9 +91,9 @@ namespace EVCB_OCPP.WSServer.Service.BusinessService
             await Task.Delay(10);
         }
 
-        public ValueTask<NotifyTransactionCompletedResponse> NotifyTransactionCompleted(TransactionRecord tx, Dictionary<string, decimal> roundedPeriodEnergy)
+        public ValueTask<NotifyTransactionCompletedResult> NotifyTransactionCompleted(TransactionRecord tx, Dictionary<string, decimal> roundedPeriodEnergy)
         {
-            return ValueTask.FromResult<NotifyTransactionCompletedResponse>(null);
+            return ValueTask.FromResult<NotifyTransactionCompletedResult>(null);
         }
     }
 }

+ 4 - 2
EVCB_OCPP.WSServer/Service/BusinessService/Model.cs

@@ -6,9 +6,11 @@ using System.Threading.Tasks;
 
 namespace EVCB_OCPP.WSServer.Service.BusinessService
 {
-    public class NotifyTransactionCompletedResponse
+    public class NotifyTransactionCompletedResult
     {
         public decimal? CouponPoint { set; get; } = null;
-        public string? FarewellMessage { set; get; } = null;
+        public string FarewellMessage { set; get; } = null;
+        public string ErrorMsg { get; set; } = null;
+        public bool Success => string.IsNullOrEmpty(ErrorMsg);
     }
 }

+ 9 - 4
EVCB_OCPP.WSServer/Service/BusinessService/OuterBusinessService.cs

@@ -315,14 +315,14 @@ namespace EVCB_OCPP.WSServer.Service.BusinessService
             throw new NotImplementedException();
         }
 
-        public async ValueTask<NotifyTransactionCompletedResponse> NotifyTransactionCompleted(TransactionRecord tx, Dictionary<string, decimal> roundedPeriodEnergy)
+        public async ValueTask<NotifyTransactionCompletedResult> NotifyTransactionCompleted(TransactionRecord tx, Dictionary<string, decimal> roundedPeriodEnergy)
         {
             if (signMaterial == null || !signMaterial.InstantStopTxReport)
             {
                 return null;
             }
 
-            var toReturn = new NotifyTransactionCompletedResponse();
+            var toReturn = new NotifyTransactionCompletedResult();
 
             //var PeriodEnergy = await mainDbService.GetTransactionPeriodEnergy(tx.Id);
 
@@ -358,7 +358,13 @@ namespace EVCB_OCPP.WSServer.Service.BusinessService
 
             logger.LogDebug("completed_session Response" + response.Response);
 
-            if (response.Success && !string.IsNullOrEmpty(response.Response))
+            if (!response.Success)
+            {
+                toReturn.ErrorMsg = !string.IsNullOrEmpty(response.Response) ? response.Response : response.StatusCode.ToString();
+                return toReturn;
+            }
+
+            if (!string.IsNullOrEmpty(response.Response))
             {
                 var _httpResult = JsonConvert.DeserializeObject<CPOOuterResponse>(response.Response);
                 logger.LogDebug("completed_session Response" + response.Response);
@@ -366,7 +372,6 @@ namespace EVCB_OCPP.WSServer.Service.BusinessService
                 if (jo.ContainsKey("CouponPoint"))
                 {
                     toReturn.CouponPoint = jo["CouponPoint"].Value<Decimal>();
-
                 }
 
                 if (jo.ContainsKey("FarewellMessage"))

+ 19 - 0
EVCB_OCPP.WSServer/Service/DbService/MainDbService.cs

@@ -13,6 +13,7 @@ using EVCB_OCPP.Packet.Messages.Security;
 using EVCB_OCPP.Packet.Messages.SmartCharging;
 using EVCB_OCPP.Packet.Messages.SubTypes;
 using EVCB_OCPP.WSServer.Helper;
+using EVCB_OCPP.WSServer.Service.BusinessService;
 using Microsoft.Data.SqlClient;
 using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.Caching.Memory;
@@ -21,6 +22,7 @@ using Microsoft.Extensions.Logging;
 using Newtonsoft.Json;
 using OCPPPackage.Profiles;
 using System.Data;
+using System.Diagnostics;
 
 namespace EVCB_OCPP.WSServer.Service.DbService;
 
@@ -72,6 +74,7 @@ public interface IMainDbService
     Task RecordBoardVersions(string machineId, string data);
     Task<TransactionRecord> GetTransaction(int txId);
     Task SetPncNotifiyResult(int txid, bool isNotifySuccess, string eVCCID);
+    Task ReportStopTx(int txid, NotifyTransactionCompletedResult reportResults);
 }
 
 public class MainDbService : IMainDbService
@@ -644,6 +647,22 @@ public class MainDbService : IMainDbService
         await db.SaveChangesAsync();
     }
 
+    public async Task ReportStopTx(int txid, NotifyTransactionCompletedResult reportResults)
+    {
+        using var dbConn = await sqlConnectionFactory.CreateAsync();
+
+        var cmd = """
+            UPDATE [dbo].[TransactionRecord] 
+            SET StopTransactionReportedOn=@StopTransactionReportedOn, ErrorMsg=@ErrorMsg  
+            WHERE Id=@Id
+            """;
+
+        var parameters = new DynamicParameters();
+        parameters.Add("@Id", txid, DbType.Int32, ParameterDirection.Input);
+        parameters.Add("@StopTransactionReportedOn", DateTime.UtcNow, DbType.DateTime, ParameterDirection.Input);
+        parameters.Add("@ErrorMsg", reportResults.ErrorMsg, DbType.String, ParameterDirection.Input, -1);
+        await dbConn.ExecuteAsync(cmd, parameters);
+    }
 
     private void InitUpdateConnectorStatusHandler()
     {