From ec1f94a898d19d84ac10e7a3a5833c0aba67e11d Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sat, 4 Oct 2025 18:42:12 +0800 Subject: [PATCH] =?UTF-8?q?=20=20=20=201=20refactor(config):=20=E5=B0=86Ap?= =?UTF-8?q?pSettings=E7=A7=BB=E8=87=B3Application=E5=B1=82=E5=B9=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=85=8D=E7=BD=AE=E7=BB=93=E6=9E=84=20=20=20?= =?UTF-8?q?=20=202=20=20=20=20=203=20-=20=E5=B0=86AppSettings=E4=BB=8EDMS.?= =?UTF-8?q?Infrastructure=E7=A7=BB=E8=87=B3DMS.Application=20=20=20=20=204?= =?UTF-8?q?=20-=20=E5=B0=86DatabaseSettings=E9=87=8D=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E4=B8=BAAppSettings.Database=E5=B9=B6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=89=80=E6=9C=89=E5=BC=95=E7=94=A8=20=20=20=20=205=20-=20?= =?UTF-8?q?=E5=B0=86=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E4=BB=8EappSettin?= =?UTF-8?q?gs.json=E6=9B=B4=E6=94=B9=E4=B8=BAdms=5Fconfig.json=20=20=20=20?= =?UTF-8?q?=206=20-=20=E6=9B=B4=E6=96=B0=E6=89=80=E6=9C=89=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E5=BC=95=E7=94=A8=E4=BB=A5=E4=BD=BF=E7=94=A8=E6=96=B0?= =?UTF-8?q?=E7=9A=84AppSettings=E5=91=BD=E5=90=8D=E7=A9=BA=E9=97=B4=20=20?= =?UTF-8?q?=20=20=207=20-=20=E7=A7=BB=E9=99=A4DI=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E4=B8=AD=E7=9A=84SqlSugarDbContext=E7=9B=B4=E6=8E=A5=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E5=8C=96=20=20=20=20=208=20-=20=E6=B7=BB=E5=8A=A0Mate?= =?UTF-8?q?rial=20Design=20Icons=E5=AD=97=E4=BD=93=E5=B9=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E8=AE=BE=E7=BD=AE=E8=A7=86=E5=9B=BEUI=20=20=20=20=209?= =?UTF-8?q?=20-=20=E9=80=9A=E8=BF=87=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E5=8F=98=E9=87=8F=E6=98=A0=E5=B0=84=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=9D=A5=E4=BC=98=E5=8C=96S7=E6=9C=8D=E5=8A=A1=20=20?= =?UTF-8?q?=20=2010=20-=20=E5=B0=86=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=B1=9E=E6=80=A7=E5=90=8D?= =?UTF-8?q?=E4=BB=8EDatabase=E6=9B=B4=E6=96=B0=E4=B8=BADbName?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.Application/Configurations/AppSettings.cs | 42 ++-- .../Services/BaseServiceTest.cs | 8 +- DMS.Infrastructure/DMS.Infrastructure.csproj | 4 + DMS.Infrastructure/Data/SqlSugarDbContext.cs | 4 +- .../Repositories/InitializeRepository.cs | 1 - .../Services/S7/S7ServiceManager.cs | 12 - .../ViewModelTest/BaseServiceTest.cs | 8 +- DMS.WPF/App.xaml | 3 + DMS.WPF/App.xaml.cs | 9 +- DMS.WPF/DMS.WPF.csproj | 9 +- .../ViewModels/Items/VariableItemViewModel.cs | 2 +- DMS.WPF/ViewModels/SettingViewModel.cs | 42 ++-- DMS.WPF/ViewModels/SplashViewModel.cs | 20 +- DMS.WPF/Views/SettingView.xaml | 210 ++++++------------ 14 files changed, 152 insertions(+), 222 deletions(-) diff --git a/DMS.Application/Configurations/AppSettings.cs b/DMS.Application/Configurations/AppSettings.cs index fc5960b..ba83bff 100644 --- a/DMS.Application/Configurations/AppSettings.cs +++ b/DMS.Application/Configurations/AppSettings.cs @@ -1,46 +1,46 @@ -using AutoMapper; using DMS.Core.Models; using Newtonsoft.Json; namespace DMS.Application.Configurations { - public class DatabaseSettings - { - public string DbType { get; set; } = "MySql"; - public string Server { get; set; } = "127.0.0.1"; - public int Port { get; set; } = 3306; - public string UserId { get; set; } = "root"; - public string Password { get; set; } = "Pgw15221236646"; - public string Database { get; set; } = "pmswpf"; - } + public class AppSettings { - private readonly IMapper _mapper; - - + public class Database + { + public string DbType { get; set; } = "MySql"; + public string Server { get; set; } = "127.0.0.1"; + public int Port { get; set; } = 3306; + public string UserId { get; set; } = "root"; + public string Password { get; set; } = "Pgw15221236646"; + public string DbName { get; set; } = "dms_test"; + } - public DatabaseSettings Database { get; set; } = new DatabaseSettings(); + + public Database Db { get; set; } = new Database(); public string Theme { get; set; } = "跟随系统"; public bool EnableS7Service { get; set; } = true; public bool EnableMqttService { get; set; } = true; public bool EnableOpcUaService { get; set; } = true; public bool MinimizeToTrayOnClose { get; set; } = true; public List Menus { get; set; } = new List(); + public int DefaultPollingInterval { get; set; } = 30000; // 默认轮询间隔30秒 - private static readonly string SettingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appSettings.json"); + private static readonly string SettingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigJsonFileName); + + private const string ConfigJsonFileName = "dms_config.json"; - public AppSettings Load() + public AppSettings? Load() { if (File.Exists(SettingsFilePath)) { string json = File.ReadAllText(SettingsFilePath); - AppSettings appSettings = JsonConvert.DeserializeObject(json); - - return appSettings; + AppSettings? appSettings = JsonConvert.DeserializeObject(json); + return appSettings ?? throw new InvalidOperationException("加载配置文件出现了错误。"); } - return new AppSettings(); + return null; } public void Save() @@ -51,7 +51,7 @@ namespace DMS.Application.Configurations public string ToConnectionString() { - return $"server={Database.Server};port={Database.Port};user={Database.UserId};password={Database.Password};database={Database.Database};"; + return $"server={Db.Server};port={Db.Port};user={Db.UserId};password={Db.Password};database={Db.DbName};"; } } } diff --git a/DMS.Infrastructure.UnitTests/Services/BaseServiceTest.cs b/DMS.Infrastructure.UnitTests/Services/BaseServiceTest.cs index 41c1048..df40f39 100644 --- a/DMS.Infrastructure.UnitTests/Services/BaseServiceTest.cs +++ b/DMS.Infrastructure.UnitTests/Services/BaseServiceTest.cs @@ -2,6 +2,7 @@ using AutoMapper; using AutoMapper.Internal; +using DMS.Application.Configurations; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; using DMS.Application.Services; @@ -11,7 +12,6 @@ using DMS.Core.Interfaces.Repositories; using DMS.Infrastructure.Data; using DMS.Infrastructure.Repositories; using Microsoft.Extensions.DependencyInjection; -using DMS.Infrastructure.Configurations; namespace DMS.Infrastructure.UnitTests.Services; @@ -37,11 +37,7 @@ public class BaseServiceTest }); // 2. 配置数据库上下文 (在测试中通常使用单例) - services.AddSingleton(_ => - { - var appSettings = new AppSettings { Database = { Database = "dms_test" } }; - return new SqlSugarDbContext(appSettings); - }); + services.AddSingleton(); // --- 注册服务和仓储 --- // 使用 Transient 或 Scoped 取决于服务的生命周期需求,对于测试,Transient 通常更安全。 diff --git a/DMS.Infrastructure/DMS.Infrastructure.csproj b/DMS.Infrastructure/DMS.Infrastructure.csproj index aab12e8..fda924a 100644 --- a/DMS.Infrastructure/DMS.Infrastructure.csproj +++ b/DMS.Infrastructure/DMS.Infrastructure.csproj @@ -26,4 +26,8 @@ + + + + diff --git a/DMS.Infrastructure/Data/SqlSugarDbContext.cs b/DMS.Infrastructure/Data/SqlSugarDbContext.cs index b87a271..9049479 100644 --- a/DMS.Infrastructure/Data/SqlSugarDbContext.cs +++ b/DMS.Infrastructure/Data/SqlSugarDbContext.cs @@ -1,4 +1,4 @@ -using DMS.Infrastructure.Configurations; +using DMS.Application.Configurations; using SqlSugar; namespace DMS.Infrastructure.Data; @@ -16,7 +16,7 @@ public class SqlSugarDbContext public SqlSugarClient GetInstance() { var connectionString = _settings.ToConnectionString(); - var dbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), _settings.Database.DbType); + var dbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), _settings.Db.DbType); return new SqlSugarClient(new ConnectionConfig { diff --git a/DMS.Infrastructure/Repositories/InitializeRepository.cs b/DMS.Infrastructure/Repositories/InitializeRepository.cs index 6cdbb09..21d5898 100644 --- a/DMS.Infrastructure/Repositories/InitializeRepository.cs +++ b/DMS.Infrastructure/Repositories/InitializeRepository.cs @@ -1,7 +1,6 @@ using DMS.Core.Enums; using DMS.Core.Interfaces.Repositories; using DMS.Core.Models; -using DMS.Infrastructure.Configurations; using DMS.Infrastructure.Data; using DMS.Infrastructure.Entities; using Microsoft.Extensions.Logging; diff --git a/DMS.Infrastructure/Services/S7/S7ServiceManager.cs b/DMS.Infrastructure/Services/S7/S7ServiceManager.cs index 086d501..65e2b05 100644 --- a/DMS.Infrastructure/Services/S7/S7ServiceManager.cs +++ b/DMS.Infrastructure/Services/S7/S7ServiceManager.cs @@ -426,18 +426,6 @@ namespace DMS.Infrastructure.Services.S7 // 如果变量的S7相关属性发生变化 switch (e.PropertyType) { - case VariablePropertyType.S7Address: - // S7地址变化,需要更新设备上下文中的变量映射 - if (_deviceContexts.TryGetValue(e.Variable.VariableTable.DeviceId, out var context)) - { - // 先移除旧地址的变量 - context.Variables.Remove(e.Variable.S7Address, out _); - // 添加新地址的变量 - context.Variables.AddOrUpdate(e.Variable.S7Address, e.Variable, (key, oldValue) => e.Variable); - _logger.LogInformation("已更新设备 {DeviceId} 中变量 {VariableId} 的S7地址映射", - e.Variable.VariableTable.DeviceId, e.Variable.Id); - } - break; case VariablePropertyType.IsActive: // 变量激活状态变化,更新变量列表 diff --git a/DMS.WPF.UnitTests/ViewModelTest/BaseServiceTest.cs b/DMS.WPF.UnitTests/ViewModelTest/BaseServiceTest.cs index de225e4..3b5a4bd 100644 --- a/DMS.WPF.UnitTests/ViewModelTest/BaseServiceTest.cs +++ b/DMS.WPF.UnitTests/ViewModelTest/BaseServiceTest.cs @@ -2,13 +2,13 @@ using AutoMapper; using AutoMapper.Internal; +using DMS.Application.Configurations; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; using DMS.Application.Services; using DMS.Application.Services.Database; using DMS.Core.Interfaces; using DMS.Core.Interfaces.Repositories; -using DMS.Infrastructure.Configurations; using DMS.Infrastructure.Data; using DMS.Infrastructure.Repositories; using DMS.WPF.Interfaces; @@ -40,11 +40,7 @@ public class BaseServiceTest }); // 2. 配置数据库上下文 (在测试中通常使用单例) - services.AddSingleton(_ => - { - var appSettings = new AppSettings { Database = { Database = "dms_test" } }; - return new SqlSugarDbContext(appSettings); - }); + services.AddSingleton(); // --- 注册服务和仓储 --- // 使用 Transient 或 Scoped 取决于服务的生命周期需求,对于测试,Transient 通常更安全。 diff --git a/DMS.WPF/App.xaml b/DMS.WPF/App.xaml index b87ada4..53958a4 100644 --- a/DMS.WPF/App.xaml +++ b/DMS.WPF/App.xaml @@ -34,6 +34,9 @@ + + /Assets/Fonts/materialdesignicons-webfont.ttf#Material Design Icons + diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index 8b1e67b..903f7e6 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -1,6 +1,7 @@ using System.Windows; using AutoMapper; using AutoMapper.Internal; +using DMS.Application.Configurations; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; using DMS.Application.Interfaces.Management; @@ -15,7 +16,6 @@ using DMS.Core.Interfaces.Repositories; using DMS.Core.Interfaces.Repositories.Triggers; using DMS.Core.Interfaces.Services; using DMS.Infrastructure.Configuration; -using DMS.Infrastructure.Configurations; using DMS.Infrastructure.Data; using DMS.Infrastructure.Interfaces; using DMS.Infrastructure.Interfaces.Services; @@ -174,6 +174,7 @@ public partial class App : System.Windows.Application services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + // 注册核心服务 services.AddAutoMapper(cfg => @@ -218,11 +219,7 @@ public partial class App : System.Windows.Application // 注册Core中的仓库 services.AddSingleton(); - services.AddSingleton(_ => - { - var appSettings = new AppSettings { Database = { Database = "dms_test" } }; - return new SqlSugarDbContext(appSettings); - }); + services.AddSingleton(); diff --git a/DMS.WPF/DMS.WPF.csproj b/DMS.WPF/DMS.WPF.csproj index b296b56..d66434a 100644 --- a/DMS.WPF/DMS.WPF.csproj +++ b/DMS.WPF/DMS.WPF.csproj @@ -6,6 +6,7 @@ enable enable true + true Assets\AppIcon2.ico @@ -18,6 +19,9 @@ + + Always + @@ -169,21 +173,24 @@ + + + Always - + \ No newline at end of file diff --git a/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs b/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs index 36ad484..d3d94cd 100644 --- a/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs @@ -3,7 +3,7 @@ using DMS.Application.DTOs; using DMS.Core.Enums; using System; using System.Collections.Generic; -using DMS.Infrastructure.Configurations; +using DMS.Application.Configurations; using Microsoft.Extensions.DependencyInjection; namespace DMS.WPF.ViewModels.Items; diff --git a/DMS.WPF/ViewModels/SettingViewModel.cs b/DMS.WPF/ViewModels/SettingViewModel.cs index dd00205..5d46b0e 100644 --- a/DMS.WPF/ViewModels/SettingViewModel.cs +++ b/DMS.WPF/ViewModels/SettingViewModel.cs @@ -1,5 +1,5 @@ using CommunityToolkit.Mvvm.Input; -using DMS.Infrastructure.Configurations; +using DMS.Application.Configurations; using DMS.WPF.Helper; using DMS.WPF.Interfaces; @@ -39,12 +39,12 @@ public partial class SettingViewModel : ViewModelBase public string SelectedDbType { - get => _settings.Database.DbType; + get => _settings.Db.DbType; set { - if (_settings.Database.DbType != value) + if (_settings.Db.DbType != value) { - _settings.Database.DbType = value; + _settings.Db.DbType = value; OnPropertyChanged(); _settings.Save(); } @@ -53,12 +53,12 @@ public partial class SettingViewModel : ViewModelBase public string Server { - get => _settings.Database.Server; + get => _settings.Db.Server; set { - if (_settings.Database.Server != value) + if (_settings.Db.Server != value) { - _settings.Database.Server = value; + _settings.Db.Server = value; OnPropertyChanged(); _settings.Save(); } @@ -67,12 +67,12 @@ public partial class SettingViewModel : ViewModelBase public int Port { - get => _settings.Database.Port; + get => _settings.Db.Port; set { - if (_settings.Database.Port != value) + if (_settings.Db.Port != value) { - _settings.Database.Port = value; + _settings.Db.Port = value; OnPropertyChanged(); _settings.Save(); } @@ -81,12 +81,12 @@ public partial class SettingViewModel : ViewModelBase public string UserId { - get => _settings.Database.UserId; + get => _settings.Db.UserId; set { - if (_settings.Database.UserId != value) + if (_settings.Db.UserId != value) { - _settings.Database.UserId = value; + _settings.Db.UserId = value; OnPropertyChanged(); _settings.Save(); } @@ -95,26 +95,26 @@ public partial class SettingViewModel : ViewModelBase public string Password { - get => _settings.Database.Password; + get => _settings.Db.Password; set { - if (_settings.Database.Password != value) + if (_settings.Db.Password != value) { - _settings.Database.Password = value; + _settings.Db.Password = value; OnPropertyChanged(); _settings.Save(); } } } - public string Database + public string DbName { - get => _settings.Database.Database; + get => _settings.Db.DbName; set { - if (_settings.Database.Database != value) + if (_settings.Db.DbName != value) { - _settings.Database.Database = value; + _settings.Db.DbName = value; OnPropertyChanged(); _settings.Save(); } @@ -157,7 +157,7 @@ public partial class SettingViewModel : ViewModelBase // 使用当前配置测试数据库连接 using var db = new SqlSugar.SqlSugarScope(new SqlSugar.ConnectionConfig() { - DbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), _settings.Database.DbType), + DbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), _settings.Db.DbType), ConnectionString = _settings.ToConnectionString(), IsAutoCloseConnection = true }); diff --git a/DMS.WPF/ViewModels/SplashViewModel.cs b/DMS.WPF/ViewModels/SplashViewModel.cs index 3a2574a..39aefe6 100644 --- a/DMS.WPF/ViewModels/SplashViewModel.cs +++ b/DMS.WPF/ViewModels/SplashViewModel.cs @@ -7,8 +7,8 @@ using DMS.WPF.Services; using System; using System.Data; using System.Threading.Tasks; +using DMS.Application.Configurations; using DMS.Application.Services; -using DMS.Infrastructure.Configurations; using DMS.WPF.Helper; using DMS.WPF.Interfaces; using DMS.WPF.Views; @@ -32,8 +32,9 @@ public partial class SplashViewModel : ObservableObject [ObservableProperty] private string _loadingMessage = "正在加载..."; - public SplashViewModel(ILogger logger,IServiceProvider serviceProvider, IInitializeService initializeService,IDataEventService dataEventService, - IAppDataCenterService appDataCenterService,AppSettings appSettings) + public SplashViewModel(ILogger logger, IServiceProvider serviceProvider, + IInitializeService initializeService, IDataEventService dataEventService, + IAppDataCenterService appDataCenterService, AppSettings appSettings) { _logger = logger; _serviceProvider = serviceProvider; @@ -50,13 +51,18 @@ public partial class SplashViewModel : ObservableObject { try { + LoadingMessage = "正在加载系统配置..."; + if (_appSettings.Load() == null) + { + //程序第一次启动 + } + _logger.LogInformation("正在初始化数据库..."); LoadingMessage = "正在初始化数据库..."; _initializeService.InitializeTables(); _initializeService.InitializeMenus(); - LoadingMessage = "正在加载系统配置..."; - - + + await _appDataCenterService.DataLoaderService.LoadAllDataToMemoryAsync(); // 可以在这里添加加载配置的逻辑 @@ -80,7 +86,7 @@ public partial class SplashViewModel : ObservableObject { // 处理初始化过程中的异常 LoadingMessage = $"初始化失败: {ex.Message}"; - _logger.LogError(ex,$"初始化失败: {ex}"); + _logger.LogError(ex, $"初始化失败: {ex}"); // 在此可以记录日志或显示错误对话框 return false; } diff --git a/DMS.WPF/Views/SettingView.xaml b/DMS.WPF/Views/SettingView.xaml index e0926a9..1e674fa 100644 --- a/DMS.WPF/Views/SettingView.xaml +++ b/DMS.WPF/Views/SettingView.xaml @@ -1,161 +1,95 @@ - + - + + - - + + - + - - + + - + + + - + - -