Browse Source

add clinet diconnected handler while Validating

Robert 1 year ago
parent
commit
a302a285c0

+ 26 - 21
EVCB_OCPP.WSServer/Service/MainDbService.cs

@@ -17,11 +17,11 @@ namespace EVCB_OCPP.WSServer.Service;
 
 public interface IMainDbService
 {
-    Task<string> GetMachineAuthorizationKey(string ChargeBoxId);
-    Task<string> GetMachineConfiguration(string ChargeBoxId, string configName);
+    Task<string> GetMachineAuthorizationKey(string ChargeBoxId, CancellationToken token = default);
+    Task<string> GetMachineConfiguration(string ChargeBoxId, string configName, CancellationToken token = default);
     Task<string> GetMachineHeartbeatInterval(string ChargeBoxId);
-    Task<MachineAndCustomerInfo> GetMachineIdAndCustomerInfo(string ChargeBoxId);
-    Task<string> GetMachineSecurityProfile(string ChargeBoxId);
+    Task<MachineAndCustomerInfo> GetMachineIdAndCustomerInfo(string ChargeBoxId, CancellationToken token = default);
+    Task<string> GetMachineSecurityProfile(string ChargeBoxId, CancellationToken token = default);
     Task UpdateMachineBasicInfo(string ChargeBoxId, Machine machine);
     Task AddOCMF(Ocmf oCMF);
     ValueTask<ConnectorStatus> GetConnectorStatus(string ChargeBoxId, int ConnectorId);
@@ -31,8 +31,8 @@ public interface IMainDbService
     Task AddServerMessage(ServerMessage message);
     Task AddServerMessage(string ChargeBoxId, string OutAction, object OutRequest, string CreatedBy = "", DateTime? CreatedOn = null, string SerialNo = "", string InMessage = "");
     ValueTask AddMachineError(byte ConnectorId, DateTime CreatedOn, int Status, string ChargeBoxId, int ErrorCodeId, string ErrorInfo, int PreStatus, string VendorErrorCode, string VendorId);
-    ValueTask<Customer> GetCustomer(string id);
-    ValueTask<Customer> GetCustomer(Guid id);
+    ValueTask<Customer> GetCustomer(string id, CancellationToken token = default);
+    ValueTask<Customer> GetCustomer(Guid id, CancellationToken token = default);
     Task<Guid> GetCustomerIdByChargeBoxId(string chargeboxId);
     Task<int?> TryGetDuplicatedTransactionId(string chargeBoxId, Guid customerId, int connectorId, DateTime timestamp);
     Task<int> AddNewTransactionRecord(TransactionRecord newTransaction);
@@ -88,39 +88,44 @@ public class MainDbService : IMainDbService
     private GroupHandler<UpdateMachineBasicInfoParam> updateMachineBasicInfoHandler;
     private GroupHandler<ServerMessage> addServerMessageHandler;
 
-    public async Task<MachineAndCustomerInfo> GetMachineIdAndCustomerInfo(string ChargeBoxId)
+    public async Task<MachineAndCustomerInfo> GetMachineIdAndCustomerInfo(string ChargeBoxId, CancellationToken token = default)
     {
         using var semaphoreWrapper = await startupSemaphore.GetToken();
-        using var db = await contextFactory.CreateDbContextAsync();
+        using var db = await contextFactory.CreateDbContextAsync(token);
 
-        var machine = await db.Machines.Where(x => x.ChargeBoxId == ChargeBoxId && x.IsDelete == false).Select(x => new { x.CustomerId, x.Id }).AsNoTracking().FirstOrDefaultAsync();
+        var machine = await db.Machines
+            .Where(x => x.ChargeBoxId == ChargeBoxId && x.IsDelete == false)
+            .Select(x => new { x.CustomerId, x.Id })
+            .AsNoTracking()
+            .FirstOrDefaultAsync(token);
         if (machine == null)
         {
             return new MachineAndCustomerInfo(string.Empty, Guid.Empty, "Unknown");
         }
         //var customerName = await db.Customer.Where(x => x.Id == machine.CustomerId).Select(x => x.Name).FirstOrDefaultAsync();
-        var customer = await GetCustomer(machine.CustomerId);
+        var customer = await GetCustomer(machine.CustomerId, token);
         var customerName = customer?.Name;
         return new MachineAndCustomerInfo(machine.Id, machine.CustomerId, customerName);
     }
 
-    public async Task<string> GetMachineConfiguration(string ChargeBoxId, string configName)
+    public async Task<string> GetMachineConfiguration(string ChargeBoxId, string configName, CancellationToken token = default)
     {
         using var semaphoreWrapper = await startupSemaphore.GetToken();
-        using var db = await contextFactory.CreateDbContextAsync();
+        using var db = await contextFactory.CreateDbContextAsync(token);
         return await db.MachineConfigurations
             .Where(x => x.ChargeBoxId == ChargeBoxId && x.ConfigureName == configName)
-            .Select(x => x.ConfigureSetting).FirstOrDefaultAsync();
+            .Select(x => x.ConfigureSetting)
+            .FirstOrDefaultAsync(token);
     }
 
-    public Task<string> GetMachineSecurityProfile(string ChargeBoxId)
+    public Task<string> GetMachineSecurityProfile(string ChargeBoxId, CancellationToken token = default)
     {
         return GetMachineConfiguration(ChargeBoxId, StandardConfiguration.SecurityProfile);
     }
 
-    public Task<string> GetMachineAuthorizationKey(string ChargeBoxId)
+    public Task<string> GetMachineAuthorizationKey(string ChargeBoxId, CancellationToken token = default)
     {
-        return GetMachineConfiguration(ChargeBoxId, StandardConfiguration.AuthorizationKey);
+        return GetMachineConfiguration(ChargeBoxId, StandardConfiguration.AuthorizationKey, token);
     }
 
     public Task<string> GetMachineHeartbeatInterval(string ChargeBoxId)
@@ -261,10 +266,10 @@ public class MainDbService : IMainDbService
         return AddServerMessageDapper(message);
     }
 
-    public ValueTask<Customer> GetCustomer(string id)
-        => GetCustomer(new Guid(id));
+    public ValueTask<Customer> GetCustomer(string id, CancellationToken token = default)
+        => GetCustomer(new Guid(id), token);
 
-    public async ValueTask<Customer> GetCustomer(Guid id)
+    public async ValueTask<Customer> GetCustomer(Guid id, CancellationToken token = default)
     {
         var key = string.Format(CustomerMemCacheKeyFromat, id);
         if (memoryCache.TryGetValue<Customer>(key, out var customer))
@@ -273,9 +278,9 @@ public class MainDbService : IMainDbService
         }
 
         Customer toReturn = null;
-        using (var db = await contextFactory.CreateDbContextAsync())
+        using (var db = await contextFactory.CreateDbContextAsync(token))
         {
-            toReturn = await db.Customers.FirstOrDefaultAsync(x => x.Id == id);
+            toReturn = await db.Customers.FirstOrDefaultAsync(x => x.Id == id, token);
         }
 
         if (toReturn is not null)

+ 3 - 3
EVCB_OCPP.WSServer/Service/WsService/OcppWebsocketService.cs

@@ -119,7 +119,7 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
         bool isExistedSN = false;
         bool authorizated = false;
 
-        var info = await mainDbService.GetMachineIdAndCustomerInfo(session.ChargeBoxId);
+        var info = await mainDbService.GetMachineIdAndCustomerInfo(session.ChargeBoxId, context.RequestAborted);
         //var machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.IsDelete == false).Select(x => new { x.CustomerId, x.Id }).AsNoTracking().FirstOrDefault();
         //session.CustomerName = machine == null ? "Unknown" : db.Customer.Where(x => x.Id == machine.CustomerId).Select(x => x.Name).FirstOrDefault();
         //session.CustomerId = machine == null ? Guid.Empty : machine.CustomerId;
@@ -139,7 +139,7 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
 
         //var configVaule = db.MachineConfigurations.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.SecurityProfile)
         //                  .Select(x => x.ConfigureSetting).FirstOrDefault();
-        var configVaule = mainDbService.GetMachineSecurityProfile(session.ChargeBoxId).Result;
+        var configVaule = await mainDbService.GetMachineSecurityProfile(session.ChargeBoxId, context.RequestAborted);
         int.TryParse(configVaule, out securityProfile);
 
         if (session.ISOCPP20)
@@ -167,7 +167,7 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
             {
                 //authorizationKey = db.MachineConfigurations.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.AuthorizationKey)
                 //                    .Select(x => x.ConfigureSetting).FirstOrDefault();
-                authorizationKey = await mainDbService.GetMachineAuthorizationKey(session.ChargeBoxId);
+                authorizationKey = await mainDbService.GetMachineAuthorizationKey(session.ChargeBoxId, context.RequestAborted);
 
                 if (session.ISOCPP20)
                 {

+ 0 - 1
EVCB_OCPP.WSServer/Service/WsService/WebsocketService.cs

@@ -23,7 +23,6 @@ public class WebsocketService<T> where T : WsSession
         {
             return;
         }
-
         var portocol = await ValidateSupportedPortocol(context);
         if (string.IsNullOrEmpty(portocol))
         {