Browse Source

update comm dll

Robert 1 year ago
parent
commit
6ff4354d30

BIN
EVCB_OCPP.TaskScheduler/DLL/EVCB_OCPP.Domain.dll


+ 1 - 1
EVCB_OCPP.TaskScheduler/EVCB_OCPP.TaskScheduler.csproj

@@ -14,7 +14,7 @@
     <PackageReference Include="Dapper" Version="2.0.123" />
     <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
     <PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" />
-    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
+    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
     <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
     <PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
     <PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />

+ 6 - 104
EVCB_OCPP.TaskScheduler/Helper/AddPortalDbContext.cs

@@ -1,4 +1,6 @@
 using EVCB_OCPP.Domain;
+using EVCB_OCPP.Domain.Extensions;
+using EVCB_OCPP.TaskScheduler.Services;
 using Microsoft.Data.SqlClient;
 using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.Configuration;
@@ -15,54 +17,6 @@ namespace EVCB_OCPP.TaskScheduler.Helper;
 public static class AddPortalDbContext
 {
     public const string CommandTimeoutKey = "CommandTimeout";
-    public static IServiceCollection AddMainDbContext(this IServiceCollection services, IConfiguration configuration)
-    {
-        const string DbUserIdKey = "MainDbUserIdKey";
-        const string DbPassKey = "MainDbPass";
-        const string DbConnectionStringKey = "MainDBContext";
-
-        var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
-
-        services.AddSingleton(
-            new SqlConnectionFactory<MainDBContext>()
-            {
-                ConnectionString = conneciotnString
-            });
-        AddPortalDbContextInternal<MainDBContext>(services, configuration, conneciotnString, logToConsole: false);
-        return services;
-    }
-
-    public static IServiceCollection AddMeterValueDbContext(this IServiceCollection services, IConfiguration configuration)
-    {
-        const string DbUserIdKey = "MeterValueDbUserId";
-        const string DbPassKey = "MeterValueDbPass";
-        const string DbConnectionStringKey = "MeterValueDBContext";
-
-        var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
-        services.AddSingleton(
-            new SqlConnectionFactory<MeterValueDBContext>()
-            {
-                ConnectionString = conneciotnString
-            });
-        AddPortalDbContextInternal<MeterValueDBContext>(services, configuration, conneciotnString, logToConsole: false);
-        return services;
-    }
-
-    public static IServiceCollection AddConnectionLogDbContext(this IServiceCollection services, IConfiguration configuration)
-    {
-        const string DbUserIdKey = "ConnectionLogDbUserId";
-        const string DbPassKey = "ConnectionLogDbPass";
-        const string DbConnectionStringKey = "ConnectionLogDBContext";
-
-        var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
-        services.AddSingleton(
-            new SqlConnectionFactory<ConnectionLogDBContext>()
-            {
-                ConnectionString = conneciotnString
-            });
-        AddPortalDbContextInternal<ConnectionLogDBContext>(services, configuration, conneciotnString);
-        return services;
-    }
 
     public static IServiceCollection AddWebDBConetext(this IServiceCollection services, IConfiguration configuration)
     {
@@ -70,12 +24,8 @@ public static class AddPortalDbContext
         const string DbPassKey = "WebDbPass";
         const string DbConnectionStringKey = "WebDBContext";
 
-        var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
-        services.AddSingleton(
-            new SqlConnectionFactory<WebDBConetext>()
-            {
-                ConnectionString = conneciotnString
-            });
+        var conneciotnString = configuration.GetConnectionString(DbUserIdKey, DbPassKey, DbConnectionStringKey);
+        services.AddSqlConnectionFactory<WebDBConetext>(conneciotnString);
         return services;
     }
 
@@ -85,58 +35,10 @@ public static class AddPortalDbContext
         const string DbPassKey = "OnlineLogDbPass";
         const string DbConnectionStringKey = "OnlineLogDBContext";
 
-        var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
-        services.AddSingleton(
-            new SqlConnectionFactory<OnlineLogDBContext>()
-            {
-                ConnectionString = conneciotnString
-            });
+        var conneciotnString = configuration.GetConnectionString(DbUserIdKey, DbPassKey, DbConnectionStringKey);
+        services.AddSqlConnectionFactory<OnlineLogDBContext>(conneciotnString);
         return services;
     }
-    private static void AddPortalDbContextInternal<T>(
-        IServiceCollection services, IConfiguration configuration,
-        string connectionString, bool logToConsole = false) where T : DbContext
-    {
-
-        var commandTimeout = int.TryParse(configuration[CommandTimeoutKey], out var temp) ? temp : 180;
-
-        services.AddPooledDbContextFactory<T>((serviceProvider, options) => {
-            options.UseSqlServer(connectionString, dbOptions =>
-            {
-                dbOptions.CommandTimeout(commandTimeout);
-            });
-            if (logToConsole)
-            {
-                options.LogTo(Console.WriteLine);
-            }
-        });
-    }
-
-    private static string GetConnectionString(IConfiguration configuration, string UserIdKey, string DbPassKey, string ConnectionStringKey)
-    {
-        string mainDbUserId = string.IsNullOrEmpty(configuration[UserIdKey]) ? string.Empty : $"user id={configuration[UserIdKey]};";
-        string mainDbUserPass = string.IsNullOrEmpty(configuration[DbPassKey]) ? string.Empty : $"password={configuration[DbPassKey]};";
-        return $"{configuration.GetConnectionString(ConnectionStringKey)}{mainDbUserId}{mainDbUserPass}";
-    }
-}
-
-public class SqlConnectionFactory<T> where T : DbContext
-{
-    public string ConnectionString { get; init; }
-    public SqlConnectionFactory() { }
-    public SqlConnection Create()
-    {
-        var sqlConnection = new SqlConnection(ConnectionString);
-        sqlConnection.Open();
-        return sqlConnection;
-    }
-
-    public async Task<SqlConnection> CreateAsync()
-    {
-        var sqlConnection = new SqlConnection(ConnectionString);
-        await sqlConnection.OpenAsync();
-        return sqlConnection;
-    }
 }
 
 /// <summary>

+ 7 - 4
EVCB_OCPP.TaskScheduler/Jobs/CheckEVSEOnlineJob.cs

@@ -12,6 +12,8 @@ using EVCB_OCPP.TaskScheduler.Helper;
 using EVCB_OCPP.Domain;
 using EVCB_OCPP.TaskScheduler.Services;
 using Azure.Core;
+using EVCB_OCPP.Domain.ConnectionFactory;
+using System.Diagnostics;
 
 namespace EVCB_OCPP.TaskScheduler.Jobs
 {
@@ -28,8 +30,8 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
             ILogger<CheckEVSEOnlineJob> logger,
             OnlineLogDbService onlineLogDbService,
             MainDbService mainDbService,
-            SqlConnectionFactory<MainDBContext> mainDbConnectionFactory,
-            SqlConnectionFactory<OnlineLogDBContext> onlineLogDbConnectionFactory)
+            ISqlConnectionFactory<MainDBContext> mainDbConnectionFactory,
+            ISqlConnectionFactory<OnlineLogDBContext> onlineLogDbConnectionFactory)
         {
             this.logger = logger;
             this.onlineLogDbService = onlineLogDbService;
@@ -43,8 +45,8 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
         private readonly ILogger logger;
         private readonly OnlineLogDbService onlineLogDbService;
         private readonly MainDbService mainDbService;
-        private readonly SqlConnectionFactory<MainDBContext> mainDbConnectionFactory;
-        private readonly SqlConnectionFactory<OnlineLogDBContext> onlineLogDbConnectionFactory;
+        private readonly ISqlConnectionFactory<MainDBContext> mainDbConnectionFactory;
+        private readonly ISqlConnectionFactory<OnlineLogDBContext> onlineLogDbConnectionFactory;
 
         private DateTime latestHeartbeatTime = DateTime.Now;
         private List<EVSEOnlineRecord> updateData = new List<EVSEOnlineRecord>();
@@ -83,6 +85,7 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
 
                         for (int evseIndex = 0; evseIndex < _EVSEs.Count; evseIndex++)
                         {
+
                             var evse = _EVSEs[evseIndex];
                             //saveTasks.Add(UpdateEVSECurrentStatus(evse.CustomerId.ToString(), evse.ChargeBoxId, false, evse.HeartbeatUpdatedOn));
                             saveTasks.Add(mainDbService.UpdateEvseOnlineStatus(evse.ChargeBoxId, online: false, offlineTime: evse.HeartbeatUpdatedOn));

+ 5 - 2
EVCB_OCPP.TaskScheduler/Jobs/DeleteServerMessageJob.cs

@@ -1,5 +1,6 @@
 using Dapper;
 using EVCB_OCPP.Domain;
+using EVCB_OCPP.Domain.ConnectionFactory;
 using EVCB_OCPP.TaskScheduler.Helper;
 using Microsoft.Data.SqlClient;
 using Microsoft.Extensions.Logging;
@@ -15,14 +16,16 @@ namespace EVCB_OCPP.TaskScheduler.Jobs
     [DisallowConcurrentExecution]
     public class DeleteServerMessageJob : IJob
     {
-        public DeleteServerMessageJob(ILogger<DeleteServerMessageJob> logger, SqlConnectionFactory<MainDBContext> mainDBConnectionFactory)
+        public DeleteServerMessageJob(
+            ILogger<DeleteServerMessageJob> logger, 
+            ISqlConnectionFactory<MainDBContext> mainDBConnectionFactory)
         {
             this.logger = logger;
             this.mainDBConnectionFactory = mainDBConnectionFactory;
         }
 
         private readonly ILogger<DeleteServerMessageJob> logger;
-        private readonly SqlConnectionFactory<MainDBContext> mainDBConnectionFactory;
+        private readonly ISqlConnectionFactory<MainDBContext> mainDBConnectionFactory;
 
         public async Task Execute(IJobExecutionContext context)
         {

+ 1 - 0
EVCB_OCPP.TaskScheduler/Program.cs

@@ -1,4 +1,5 @@
 
+using EVCB_OCPP.Domain.Extensions;
 using EVCB_OCPP.TaskScheduler.Helper;
 using EVCB_OCPP.TaskScheduler.Jobs;
 using EVCB_OCPP.TaskScheduler.Services;

+ 6 - 7
EVCB_OCPP.TaskScheduler/Services/DatabaseService.cs

@@ -1,5 +1,5 @@
 using Dapper;
-using EVCB_OCPP.Domain.Models.Database;
+using EVCB_OCPP.Domain.ConnectionFactory;
 using EVCB_OCPP.TaskScheduler.Helper;
 using EVCB_OCPP.TaskScheduler.Models;
 using Microsoft.Data.SqlClient;
@@ -8,7 +8,6 @@ using Polly;
 using System;
 using System.Collections.Generic;
 using System.Data;
-using System.Data.SqlTypes;
 using System.Linq;
 using System.Threading.Tasks;
 using MachineOperateRecord = EVCB_OCPP.TaskScheduler.Models.MachineOperateRecord;
@@ -19,18 +18,18 @@ namespace EVCB_OCPP.TaskScheduler.Services;
 public class DatabaseService
 {
     private readonly ILogger logger;
-    private readonly SqlConnectionFactory<Domain.MainDBContext> mainDbConnectionFactory;
+    private readonly ISqlConnectionFactory<Domain.MainDBContext> mainDbConnectionFactory;
     //private readonly SqlConnectionFactory<OnlineLogDBContext> onlineLogDbConnectionFactory;
-    private readonly SqlConnectionFactory<WebDBConetext> webDbConnectionFactory;
+    private readonly ISqlConnectionFactory<WebDBConetext> webDbConnectionFactory;
 
     //private readonly string mainDBConnectString;
     //private readonly string onlineDBConnectString;
 
     public DatabaseService(
         ILogger<DatabaseService> logger,
-        SqlConnectionFactory<Domain.MainDBContext> mainDbConnectionFactory,
-        SqlConnectionFactory<OnlineLogDBContext> onlineLogDbConnectionFactory,
-        SqlConnectionFactory<WebDBConetext> webDbConnectionFactory)
+        ISqlConnectionFactory<Domain.MainDBContext> mainDbConnectionFactory,
+        ISqlConnectionFactory<OnlineLogDBContext> onlineLogDbConnectionFactory,
+        ISqlConnectionFactory<WebDBConetext> webDbConnectionFactory)
     {
         this.logger = logger;
         this.mainDbConnectionFactory = mainDbConnectionFactory;

+ 3 - 2
EVCB_OCPP.TaskScheduler/Services/MainDbService.cs

@@ -1,5 +1,6 @@
 using Dapper;
 using EVCB_OCPP.Domain;
+using EVCB_OCPP.Domain.ConnectionFactory;
 using EVCB_OCPP.TaskScheduler.Helper;
 using EVCB_OCPP.TaskScheduler.Models;
 using Microsoft.Extensions.Logging;
@@ -14,7 +15,7 @@ namespace EVCB_OCPP.TaskScheduler.Services;
 public class MainDbService
 {
     public MainDbService(
-        SqlConnectionFactory<MainDBContext> connectionFactory
+        ISqlConnectionFactory<MainDBContext> connectionFactory
         , ILogger<MainDbService> logger)
     {
         this.sqlConnectionFactory = connectionFactory;
@@ -23,7 +24,7 @@ public class MainDbService
         InitUpdateEvseOnlineStatusHandler();
     }
 
-    private readonly SqlConnectionFactory<MainDBContext> sqlConnectionFactory;
+    private readonly ISqlConnectionFactory<MainDBContext> sqlConnectionFactory;
     private readonly ILogger<MainDbService> logger;
     private GroupHandler<UpdateEvseOnlineModel> updateEvseOnlineStatusHandler;
 

+ 3 - 2
EVCB_OCPP.TaskScheduler/Services/OnlineLogDbService.cs

@@ -1,4 +1,5 @@
 using Dapper;
+using EVCB_OCPP.Domain.ConnectionFactory;
 using EVCB_OCPP.TaskScheduler.Helper;
 using EVCB_OCPP.TaskScheduler.Models;
 using Microsoft.Extensions.Logging;
@@ -13,7 +14,7 @@ namespace EVCB_OCPP.TaskScheduler.Services;
 public class OnlineLogDbService
 {
     public OnlineLogDbService(
-        SqlConnectionFactory<OnlineLogDBContext> connectionFactory,
+        ISqlConnectionFactory<OnlineLogDBContext> connectionFactory,
         ILogger<OnlineLogDbService> logger
         )
     {
@@ -56,7 +57,7 @@ public class OnlineLogDbService
             );
     }
 
-    private readonly SqlConnectionFactory<OnlineLogDBContext> connectionFactory;
+    private readonly ISqlConnectionFactory<OnlineLogDBContext> connectionFactory;
     private readonly ILogger<OnlineLogDbService> logger;
 
     private GroupHandler<EVSEOnlineRecord> insertOnlineLogHandler;

+ 2 - 1
EVCB_OCPP.TaskScheduler/appsettings.json

@@ -36,6 +36,7 @@
   },
   "ConnectionStrings": {
     "OnlineLogDBContext": "data source=zerova-ev-dev.database.windows.net;initial catalog=StandardOCPP_OnlineRecord;;persist security info=True;user id=azdevsoftware;password=1h52dev#az;MultipleActiveResultSets=False;App=EntityFramework;TrustServerCertificate=True;Connection Lifetime=0;Pooling=true;Max Pool Size=500;",
-    "MainDBContext": "data source=zerova-ev-dev.database.windows.net;initial catalog=StandardOCPP_Main;;persist security info=True;user id=azdevsoftware;password=1h52dev#az;MultipleActiveResultSets=False;App=EntityFramework;TrustServerCertificate=true;Pooling=true;Max Pool Size=500;"
+    "MainDBContext": "data source=zerova-ev-dev.database.windows.net;initial catalog=StandardOCPP_Main;;persist security info=True;user id=azdevsoftware;password=1h52dev#az;MultipleActiveResultSets=False;App=EntityFramework;TrustServerCertificate=true;Pooling=true;Max Pool Size=500;",
+    "WebDBContext": "data source=zerova-ev-dev.database.windows.net;initial catalog=StandardOCPP_Web;;persist security info=True;user id=azdevsoftware;password=1h52dev#az;MultipleActiveResultSets=False;App=EntityFramework;TrustServerCertificate=true;Pooling=true;Max Pool Size=500;"
   }
 }