ソースを参照

Update Server - Client Regist Device

Zixuan 11 時間 前
コミット
d507bffa71

+ 1 - 1
Data/Device/DeviceInfo.cs

@@ -10,7 +10,7 @@ public class DeviceInfo
     public string? Position { get; set; }
     public string? SoftwareVersion { get; set; }
     public Guid? Guid { get; set; }
-    public string? IP { get; set; }
+    public string IP { get; set; } = string.Empty;
     public ushort Port { get; set; }
     public string? DBConnectionString { get; set; }
     public DateTime UpdateTime { get; set; }

+ 4 - 0
DataBase/ORM/IORM.cs

@@ -28,6 +28,10 @@ public interface IORM : IDisposable
     Task<bool> Query<T>(string tableName, Action<List<T>> results) where T : class, new();
     Task<bool> Query<T>(Expression<Func<T, bool>> expression, Action<List<T>> results) where T : class, new();
     Task<bool> Query<T>(string tableName, Expression<Func<T, bool>> expression, Action<List<T>> results) where T : class, new();
+    bool Query<T>(out List<T>? results);
+    bool Query<T>(string tableName, out List<T>? results);
+    bool Query<T>(string tableName, Expression<Func<T, bool>> expression, out List<T>? results);
+    bool AddOrUpdate<T>(string tableName, T data, Expression<Func<T, bool>> expression) where T : class, new();
 
     bool Delete<T>(string tableName, Expression<Func<T, bool>> expression) where T : class, new();
 }

+ 130 - 44
DataBase/SqlSugarORM/SqlSugarCustom.cs

@@ -4,7 +4,7 @@ using System.Linq.Expressions;
 using Universal;
 
 namespace SqlSugarORM;
-public class SqlSugarCustom
+public class SqlSugarCustom : IORM
 {
     public SqlSugarCustom()
     {
@@ -13,7 +13,7 @@ public class SqlSugarCustom
 
     #region Internal
     private IOrmProvider? _provider;
-    public SqlSugarClient? Client;
+    private SqlSugarClient? _Client;
     private bool disposedValue;
     private readonly EventQueue<(string, DateTime, LogLevel)> _logQueue;
     private void LogQueueHandler((string log, DateTime time, LogLevel level) logItem)
@@ -31,7 +31,7 @@ public class SqlSugarCustom
     }
     #endregion
 
-    public bool Initialize(IOrmProvider? notify)
+    bool IORM.Initialize(IOrmProvider? notify)
     {
         if (_provider is not null)
             return false;
@@ -41,22 +41,22 @@ public class SqlSugarCustom
         return true;
     }
 
-    public bool Open(string connectionString, SqlSugar.DbType dbType, bool isAutoConnection)
+    bool IORM.Open(string connectionString, ORM.DbType dbType, bool isAutoConnection)
     {
-        if (this.Client is not null)
+        if (this._Client is not null)
             return false;
 
         ConnectionConfig config = new()
         {
             ConnectionString = connectionString,
-            DbType = dbType,
+            DbType = (SqlSugar.DbType)dbType,
             IsAutoCloseConnection = isAutoConnection
         };
         try
         {
             SqlSugarClient Db = new(config, Config);
             Db.DbMaintenance.CreateDatabase();
-            this.Client = Db;
+            this._Client = Db;
         }
         catch
         {
@@ -65,9 +65,9 @@ public class SqlSugarCustom
         return true;
     }
 
-    public bool CreateDataBase(string dbName)
+    bool IORM.CreateDataBase(string dbName)
     {
-        if (this.Client is null)
+        if (this._Client is null)
             return false;
 
         if (string.IsNullOrEmpty(dbName))
@@ -75,7 +75,7 @@ public class SqlSugarCustom
 
         try
         {
-            this.Client.DbMaintenance.CreateDatabase(dbName);
+            this._Client.DbMaintenance.CreateDatabase(dbName);
         }
         catch
         {
@@ -86,17 +86,17 @@ public class SqlSugarCustom
         return true;
     }
 
-    public bool CreateTable<T>(string? tableName)
+    bool IORM.CreateTable<T>(string? tableName)
     {
-        if (this.Client is null)
+        if (this._Client is null)
             return false;
 
         try
         {
             if (string.IsNullOrEmpty(tableName))
-                this.Client.CodeFirst.InitTables<T>();
+                this._Client.CodeFirst.InitTables<T>();
             else
-                this.Client.CodeFirst.As(typeof(T), tableName).InitTables<T>();
+                this._Client.CodeFirst.As(typeof(T), tableName).InitTables<T>();
         }
         catch
         {
@@ -108,15 +108,14 @@ public class SqlSugarCustom
         return true;
     }
 
-    public bool Insert<T>(T data) where T : class, new()
+
+    bool IORM.Insert<T>(T data)
     {
-        if (this.Client is null)
-            return false;
-        if (data is null)
+        if (this._Client is null)
             return false;
         try
         {
-            this.Client.Insertable(data).ExecuteCommand();
+            this._Client.Insertable(data).ExecuteCommand();
         }
         catch
         {
@@ -128,18 +127,16 @@ public class SqlSugarCustom
         return true;
     }
 
-    public bool Insert<T>(string tablename, T? data) where T : class, new()
+    bool IORM.Insert<T>(string tablename, T data)
     {
-        if (this.Client is null)
-            return false;
-        if (data is null)
+        if (this._Client is null)
             return false;
         try
         {
             if (string.IsNullOrEmpty(tablename))
-                this.Client.Insertable(data).ExecuteCommand();
+                this._Client.Insertable(data).ExecuteCommand();
             else
-                this.Client.Insertable(data).AS(tablename).ExecuteCommand();
+                this._Client.Insertable(data).AS(tablename).ExecuteCommand();
         }
         catch
         {
@@ -154,16 +151,16 @@ public class SqlSugarCustom
     }
 
 
-    public async Task<bool> Query<T>(Action<List<T>> results)
+    async Task<bool> IORM.Query<T>(Action<List<T>> results)
     {
-        if (this.Client is null)
+        if (this._Client is null)
             return false;
 
         return await Task<bool>.Factory.StartNew(() =>
         {
             try
             {
-                results?.Invoke(this.Client.Queryable<T>().ToList());
+                results?.Invoke(this._Client.Queryable<T>().ToList());
             }
             catch
             {
@@ -174,9 +171,9 @@ public class SqlSugarCustom
         });
     }
 
-    public async Task<bool> Query<T>(string tableName, Action<List<T>> results)
+    async Task<bool> IORM.Query<T>(string tableName, Action<List<T>> results)
     {
-        if (this.Client is null)
+        if (this._Client is null)
             return false;
 
         return await Task<bool>.Factory.StartNew(() =>
@@ -184,9 +181,9 @@ public class SqlSugarCustom
             try
             {
                 if (string.IsNullOrEmpty(tableName))
-                    results?.Invoke(this.Client.Queryable<T>().ToList());
+                    results?.Invoke(this._Client.Queryable<T>().ToList());
                 else
-                    results?.Invoke(this.Client.Queryable<T>().AS(tableName).ToList());
+                    results?.Invoke(this._Client.Queryable<T>().AS(tableName).ToList());
             }
             catch
             {
@@ -197,16 +194,76 @@ public class SqlSugarCustom
         });
     }
 
-    public async Task<bool> Query<T>(Expression<Func<T, bool>> expression, Action<List<T>> results)
+    bool IORM.Query<T>(string tableName, out List<T>? results)
+    {
+        results = null;
+        if (this._Client is null)
+            return false;
+
+        try
+        {
+            results = string.IsNullOrEmpty(tableName) ?
+                      this._Client.Queryable<T>().ToList() :
+                      this._Client.Queryable<T>().AS(tableName).ToList();
+        }
+        catch
+        {
+            return false;
+        }
+
+        return true;
+    }
+
+    bool IORM.Query<T>(out List<T>? results)
+    {
+        results = null;
+        if (this._Client is null)
+            return false;
+
+        try
+        {
+            results = this._Client.Queryable<T>().ToList();
+        }
+        catch
+        {
+            return false;
+        }
+
+        return true;
+    }
+
+    bool IORM.Query<T>(string tableName, Expression<Func<T, bool>> expression, out List<T>? results)
+    {
+        results = default;
+        if (this._Client is null)
+            return false;
+
+        try
+        {
+            results = string.IsNullOrEmpty(tableName) ?
+                this._Client.Queryable<T>().Where(expression).ToList() :
+                this._Client.Queryable<T>().AS(tableName).Where(expression).ToList();
+        }
+        catch
+        {
+            return false;
+        }
+
+
+        return true;
+    }
+
+
+    async Task<bool> IORM.Query<T>(Expression<Func<T, bool>> expression, Action<List<T>> results)
     {
-        if (this.Client is null)
+        if (this._Client is null)
             return false;
 
         return await Task<bool>.Factory.StartNew(() =>
             {
                 try
                 {
-                    results?.Invoke(this.Client.Queryable<T>().Where(expression).ToList());
+                    results?.Invoke(this._Client.Queryable<T>().Where(expression).ToList());
                 }
                 catch
                 {
@@ -217,9 +274,9 @@ public class SqlSugarCustom
             });
     }
 
-    public async Task<bool> Query<T>(string tableName, Expression<Func<T, bool>> expression, Action<List<T>> results)
+    async Task<bool> IORM.Query<T>(string tableName, Expression<Func<T, bool>> expression, Action<List<T>> results)
     {
-        if (this.Client is null)
+        if (this._Client is null)
             return false;
 
         return await Task<bool>.Factory.StartNew(() =>
@@ -227,9 +284,9 @@ public class SqlSugarCustom
               try
               {
                   if (string.IsNullOrEmpty(tableName))
-                      results?.Invoke(this.Client.Queryable<T>().Where(expression).ToList());
+                      results?.Invoke(this._Client.Queryable<T>().Where(expression).ToList());
                   else
-                      results?.Invoke(this.Client.Queryable<T>().AS(tableName).Where(expression).ToList());
+                      results?.Invoke(this._Client.Queryable<T>().AS(tableName).Where(expression).ToList());
               }
               catch
               {
@@ -244,16 +301,45 @@ public class SqlSugarCustom
           });
     }
 
-    public bool Delete<T>(string tableName, Expression<Func<T, bool>> expression) where T : class, new()
+    bool IORM.Delete<T>(string tableName, Expression<Func<T, bool>> expression)
+    {
+        if (this._Client is null)
+            return false;
+        try
+        {
+            this._Client.Deleteable<T>().AS(tableName).Where(expression).ExecuteCommand();
+        }
+        catch
+        {
+            return false;
+        }
+        return true;
+    }
+
+    bool IORM.AddOrUpdate<T>(string tableName, T data, Expression<Func<T, bool>> expression)
     {
-        if (this.Client is null)
+        if (this._Client is null)
             return false;
+
         try
         {
-            this.Client.Deleteable<T>().AS(tableName).Where(expression).ExecuteCommand();
+            if (string.IsNullOrEmpty(tableName))
+            {
+                if (this._Client.Updateable(data).Where(expression).ExecuteCommand() == 0)
+                    return (this as IORM).Insert(data);
+            }
+            else
+            {
+                if (this._Client.Updateable(data).Where(expression).AS(tableName).ExecuteCommand() == 0)
+                    return (this as IORM).Insert(tableName, data);
+            }
         }
         catch
         {
+            if (string.IsNullOrEmpty(tableName))
+                _logQueue?.Enqueue(new($"Update {data.ToString} Failed", DateTime.Now, LogLevel.Error));
+            else
+                _logQueue?.Enqueue(new($"Update {data.ToString} to table {tableName} Failed", DateTime.Now, LogLevel.Error));
             return false;
         }
         return true;
@@ -269,8 +355,8 @@ public class SqlSugarCustom
                 while (_logQueue is not null && _logQueue.Count != 0)
                     Thread.Sleep(200);
 
-                this.Client?.Dispose();
-                this.Client = null;
+                this._Client?.Dispose();
+                this._Client = null;
                 this._logQueue?.Dispose();
                 this._provider = null;
             }

+ 1 - 0
EEMSCenterUI/EEMSCenterUI.csproj

@@ -18,6 +18,7 @@
 		<FrameworkReference Include="Microsoft.AspNetCore.App" />
 	</ItemGroup>
 	<ItemGroup>
+	  <ProjectReference Include="..\DataBase\SqlSugarORM\SqlSugarORM.csproj" />
 	  <ProjectReference Include="..\Server\EEMSService\EEMSServerCore.csproj" />
 	  <ProjectReference Include="..\UICommon\UICommon.csproj" />
 	</ItemGroup>

+ 4 - 0
EEMSMain.sln

@@ -6,6 +6,9 @@ MinimumVisualStudioVersion = 10.0.40219.1
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EEMSMain", "EEMSMain\EEMSMain.csproj", "{90A034C5-B072-4048-8184-D78CC7ABFC59}"
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Module", "Module", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
+	ProjectSection(SolutionItems) = preProject
+		Server\ServiceBase\UIInterfaces.cs = Server\ServiceBase\UIInterfaces.cs
+	EndProjectSection
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UICommon", "UICommon\UICommon.csproj", "{B87CA3CD-A947-49C4-8F1C-3755374F7E9B}"
 EndProject
@@ -214,6 +217,7 @@ Global
 		{3F13236F-F673-42A2-B928-61A5F4B4C3AD} = {9EF4E4C0-6304-4339-8367-3BFD5E816464}
 		{8E249AE0-505B-4481-8C83-42C71561370E} = {9EF4E4C0-6304-4339-8367-3BFD5E816464}
 		{E1F62B2E-BC79-4A21-9458-B5CA3641BD08} = {A6000E18-5F55-4CD7-B3F5-82BB4A4A0E80}
+		{0B0BF6F8-6BFC-4294-A9C9-014075A09392} = {00000000-0000-0000-0000-000000000000}
 		{0C79BB69-475D-40CC-AC3A-583AFD8F7527} = {A6000E18-5F55-4CD7-B3F5-82BB4A4A0E80}
 		{4D7874BD-0AFD-1BDF-FC6D-74C2F1EE9448} = {A6000E18-5F55-4CD7-B3F5-82BB4A4A0E80}
 	EndGlobalSection

+ 10 - 1
Server/EEMSService/EEMSBaseServer.cs

@@ -1,4 +1,7 @@
-namespace EEMSServerCore;
+using ORM;
+using SqlSugarORM;
+
+namespace EEMSServerCore;
 
 public class EEMSBaseServer : IDisposable
 {
@@ -11,6 +14,11 @@ public class EEMSBaseServer : IDisposable
         if (WebApplication is not null)
             return false;
 
+        IORM orm = new SqlSugarCustom();
+        orm.Initialize();
+        orm.Open("Database=EEMSServer;Password=123456;Host=localhost;Username=postgres;Persist Security Info=True", DbType.PostgreSQL);
+        orm.CreateTable<DeviceInfo>("Devices");
+
         WebApplicationBuilder builder = WebApplication.CreateBuilder();
         builder.Services.AddSignalR(
             options =>
@@ -26,6 +34,7 @@ public class EEMSBaseServer : IDisposable
         builder.Services.AddSingleton<IUIProvider, UICaller>();
         builder.Services.AddSingleton<IClientProvider>(clientProvider);
         builder.Services.AddSingleton<IEEMSBaseServerProvider>(provider);
+        builder.Services.AddSingleton<IORM>(orm);
 
         WebApplication = builder.Build();
         WebApplication.MapHub<UIHub>("/UIHub");

+ 1 - 0
Server/EEMSService/EEMSServerCore.csproj

@@ -17,6 +17,7 @@
 	  <Folder Include="Helpers\" />
 	</ItemGroup>
 	<ItemGroup>
+	  <ProjectReference Include="..\..\DataBase\SqlSugarORM\SqlSugarORM.csproj" />
 	  <ProjectReference Include="..\..\Data\Device\Device.csproj" />
 	  <ProjectReference Include="..\..\Universal\Universal.csproj" />
 	  <ProjectReference Include="..\ServiceBase\ServiceBase.csproj" />

+ 2 - 2
Server/EEMSService/HostLifeTime.cs

@@ -7,11 +7,11 @@ using System.Threading.Tasks;
 
 namespace EEMSServerCore;
 
-internal class HostLifeTime : IHostedService
+internal class HostLifeTime(IEEMSBaseServerProvider provider) : IHostedService
 {
     public Task StartAsync(CancellationToken cancellationToken)
     {
-
+        provider.Started();
         return Task.CompletedTask;
     }
 

+ 1 - 1
Server/EEMSService/HubSender/UICaller.cs

@@ -12,7 +12,7 @@ internal class UICaller : IUIProvider
     public Task<bool> UpdateDeviceList(IEnumerable<DeviceInfo> device)
     {
         throw new NotImplementedException();
-    }
+    }  
 
     private Task<bool> SendAsync(string name)
     {

+ 9 - 1
Server/EEMSService/Hubs/ClientsMainHub.cs

@@ -1,8 +1,9 @@
 using Microsoft.AspNetCore.Http.Features;
+using ORM;
 
 namespace EEMSServerCore.Hubs;
 
-internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServerProvider provider) : Hub, IClientCaller
+internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServerProvider provider, IORM orm) : Hub, IClientCaller
 {
     public override Task OnConnectedAsync()
     {
@@ -28,6 +29,13 @@ internal partial class ClientsMainHub(DeviceManager deviceManager, IEEMSBaseServ
     public Task<Guid> RegisterDevice(DeviceInfo deviceInfo)
     {
         deviceInfo.Guid ??= Guid.NewGuid();
+        deviceInfo.IP ??= string.Empty;
+        //if (!orm.Query<DeviceInfo>("Device", t => t.Guid == deviceInfo.Guid, out List<DeviceInfo>? devices) || devices is null)
+        //    return Task.FromResult(Guid.Empty);
+
+        if (!orm.AddOrUpdate("Devices", deviceInfo, t => t.Guid == deviceInfo.Guid))
+            return Task.FromResult(Guid.Empty);
+
         deviceManager.LoginDevice(Context.ConnectionId, deviceInfo);
         ClientManager.DeviceClients[deviceInfo.Guid.Value] = Clients.Caller;
         return Task.FromResult(deviceInfo.Guid.Value);