1. 在 AppSettings 类中添加了 DefaultPollingInterval 属性,用于存储全局默认轮询间隔值
2. 更新了 SettingViewModel 以包含对轮询间隔设置的支持,允许用户在设置页面中修改该值 3. 修改了 VariableItemViewModel,使其在初始化时从 AppSettings 加载默认轮询间隔值 4. 更新了 SettingView.xaml,在设置页面的"通用设置"部分添加了轮询间隔配置项
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@@ -15,6 +16,10 @@ namespace DMS.Infrastructure.Configurations
|
||||
|
||||
public class AppSettings
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
|
||||
|
||||
public DatabaseSettings Database { get; set; } = new DatabaseSettings();
|
||||
public string Theme { get; set; } = "跟随系统";
|
||||
public bool EnableS7Service { get; set; } = true;
|
||||
@@ -22,15 +27,18 @@ namespace DMS.Infrastructure.Configurations
|
||||
public bool EnableOpcUaService { get; set; } = true;
|
||||
public bool MinimizeToTrayOnClose { get; set; } = true;
|
||||
public List<MenuBean> Menus { get; set; } = new List<MenuBean>();
|
||||
public int DefaultPollingInterval { get; set; } = 30000; // 默认轮询间隔30秒
|
||||
|
||||
private static readonly string SettingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appSettings.json");
|
||||
|
||||
public static AppSettings Load()
|
||||
public AppSettings Load()
|
||||
{
|
||||
if (File.Exists(SettingsFilePath))
|
||||
{
|
||||
string json = File.ReadAllText(SettingsFilePath);
|
||||
return JsonConvert.DeserializeObject<AppSettings>(json);
|
||||
AppSettings appSettings = JsonConvert.DeserializeObject<AppSettings>(json);
|
||||
|
||||
return appSettings;
|
||||
}
|
||||
return new AppSettings();
|
||||
}
|
||||
|
||||
@@ -28,7 +28,8 @@ namespace DMS.WPF.Profiles
|
||||
CreateMap<MqttServer, MqttServerItemViewModel>().ReverseMap();
|
||||
CreateMap<UserDto, UserItemViewModel>().ReverseMap();
|
||||
CreateMap<VariableHistoryDto, VariableHistoryItemViewModel>().ReverseMap();
|
||||
CreateMap<VariableDto, VariableItemViewModel>().ReverseMap();
|
||||
CreateMap<VariableDto, VariableItemViewModel>()
|
||||
.ReverseMap();
|
||||
CreateMap<VariableMqttAliasDto, VariableMqttAliasItemViewModel>().ReverseMap();
|
||||
CreateMap<VariableTableDto, VariableTableItemViewModel>().ReverseMap();
|
||||
CreateMap<NlogDto, NlogItemViewModel>().ReverseMap();
|
||||
|
||||
@@ -3,6 +3,8 @@ using DMS.Application.DTOs;
|
||||
using DMS.Core.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DMS.Infrastructure.Configurations;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace DMS.WPF.ViewModels.Items;
|
||||
|
||||
@@ -116,7 +118,15 @@ public partial class VariableItemViewModel : ObservableObject
|
||||
/// 用于决定数据采集的频率。
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private int _pollingInterval = 30000; // ThirtySeconds
|
||||
private int _pollingInterval;
|
||||
|
||||
public VariableItemViewModel()
|
||||
{
|
||||
// 使用默认值,实际的默认值应该从外部传入或通过其他方式设置
|
||||
// 保持构造函数轻量级,不加载配置文件
|
||||
_pollingInterval = App.Current.Services.GetRequiredService<AppSettings>().DefaultPollingInterval; // 默认值,可通过外部设置覆盖
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置一个值,该值指示此变量是否被激活。
|
||||
|
||||
@@ -1,151 +1,173 @@
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using DMS.Infrastructure.Configurations;
|
||||
using DMS.WPF.Helper;
|
||||
using DMS.WPF.Interfaces;
|
||||
|
||||
namespace DMS.WPF.ViewModels;
|
||||
|
||||
public partial class SettingViewModel : ViewModelBase
|
||||
{
|
||||
// private AppSettings _settings;
|
||||
private AppSettings _settings;
|
||||
private readonly INotificationService _notificationService;
|
||||
|
||||
public SettingViewModel()
|
||||
public SettingViewModel(AppSettings appSettings,INotificationService notificationService)
|
||||
{
|
||||
// _settings = AppSettings.Load();
|
||||
// AvailableDbTypes = Enum.GetNames(typeof(SqlSugar.DbType)).ToList();
|
||||
// Themes = new List<string> { "浅色", "深色", "跟随系统" };
|
||||
// this.transaction = transaction;
|
||||
_settings = appSettings;
|
||||
_notificationService = notificationService;
|
||||
AvailableDbTypes = Enum.GetNames(typeof(SqlSugar.DbType)).ToList();
|
||||
Themes = new List<string> { "浅色", "深色", "跟随系统" };
|
||||
}
|
||||
|
||||
public List<string> Themes { get; }
|
||||
|
||||
// public string SelectedTheme
|
||||
// {
|
||||
// get => _settings.Theme;
|
||||
// set
|
||||
// {
|
||||
// if (_settings.Theme != value)
|
||||
// {
|
||||
// _settings.Theme = value;
|
||||
// OnPropertyChanged();
|
||||
// _settings.Save();
|
||||
// ThemeHelper.ApplyTheme(value);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
public string SelectedTheme
|
||||
{
|
||||
get => _settings.Theme;
|
||||
set
|
||||
{
|
||||
if (_settings.Theme != value)
|
||||
{
|
||||
_settings.Theme = value;
|
||||
OnPropertyChanged();
|
||||
_settings.Save();
|
||||
ThemeHelper.ApplyTheme(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> AvailableDbTypes { get; set; }
|
||||
|
||||
// public string SelectedDbType
|
||||
// {
|
||||
// get => _settings.Database.DbType;
|
||||
// set
|
||||
// {
|
||||
// if (_settings.Database.DbType != value)
|
||||
// {
|
||||
// _settings.Database.DbType = value;
|
||||
// OnPropertyChanged();
|
||||
// _settings.Save();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
public string SelectedDbType
|
||||
{
|
||||
get => _settings.Database.DbType;
|
||||
set
|
||||
{
|
||||
if (_settings.Database.DbType != value)
|
||||
{
|
||||
_settings.Database.DbType = value;
|
||||
OnPropertyChanged();
|
||||
_settings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// public string Server
|
||||
// {
|
||||
// get => _settings.Database.Server;
|
||||
// set
|
||||
// {
|
||||
// if (_settings.Database.Server != value)
|
||||
// {
|
||||
// _settings.Database.Server = value;
|
||||
// OnPropertyChanged();
|
||||
// _settings.Save();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
public string Server
|
||||
{
|
||||
get => _settings.Database.Server;
|
||||
set
|
||||
{
|
||||
if (_settings.Database.Server != value)
|
||||
{
|
||||
_settings.Database.Server = value;
|
||||
OnPropertyChanged();
|
||||
_settings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// public int Port
|
||||
// {
|
||||
// get => _settings.Database.Port;
|
||||
// set
|
||||
// {
|
||||
// if (_settings.Database.Port != value)
|
||||
// {
|
||||
// _settings.Database.Port = value;
|
||||
// OnPropertyChanged();
|
||||
// _settings.Save();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
public int Port
|
||||
{
|
||||
get => _settings.Database.Port;
|
||||
set
|
||||
{
|
||||
if (_settings.Database.Port != value)
|
||||
{
|
||||
_settings.Database.Port = value;
|
||||
OnPropertyChanged();
|
||||
_settings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// public string UserId
|
||||
// {
|
||||
// get => _settings.Database.UserId;
|
||||
// set
|
||||
// {
|
||||
// if (_settings.Database.UserId != value)
|
||||
// {
|
||||
// _settings.Database.UserId = value;
|
||||
// OnPropertyChanged();
|
||||
// _settings.Save();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public string Password
|
||||
// {
|
||||
// get => _settings.Database.Password;
|
||||
// set
|
||||
// {
|
||||
// if (_settings.Database.Password != value)
|
||||
// {
|
||||
// _settings.Database.Password = value;
|
||||
// OnPropertyChanged();
|
||||
// _settings.Save();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public string Database
|
||||
// {
|
||||
// get => _settings.Database.Database;
|
||||
// set
|
||||
// {
|
||||
// if (_settings.Database.Database != value)
|
||||
// {
|
||||
// _settings.Database.Database = value;
|
||||
// OnPropertyChanged();
|
||||
// _settings.Save();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
public string UserId
|
||||
{
|
||||
get => _settings.Database.UserId;
|
||||
set
|
||||
{
|
||||
if (_settings.Database.UserId != value)
|
||||
{
|
||||
_settings.Database.UserId = value;
|
||||
OnPropertyChanged();
|
||||
_settings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// public bool MinimizeToTrayOnClose
|
||||
// {
|
||||
// get => _settings.MinimizeToTrayOnClose;
|
||||
// set
|
||||
// {
|
||||
// if (_settings.MinimizeToTrayOnClose != value)
|
||||
// {
|
||||
// _settings.MinimizeToTrayOnClose = value;
|
||||
// OnPropertyChanged(nameof(MinimizeToTrayOnClose));
|
||||
// _settings.Save();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
public string Password
|
||||
{
|
||||
get => _settings.Database.Password;
|
||||
set
|
||||
{
|
||||
if (_settings.Database.Password != value)
|
||||
{
|
||||
_settings.Database.Password = value;
|
||||
OnPropertyChanged();
|
||||
_settings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Database
|
||||
{
|
||||
get => _settings.Database.Database;
|
||||
set
|
||||
{
|
||||
if (_settings.Database.Database != value)
|
||||
{
|
||||
_settings.Database.Database = value;
|
||||
OnPropertyChanged();
|
||||
_settings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool MinimizeToTrayOnClose
|
||||
{
|
||||
get => _settings.MinimizeToTrayOnClose;
|
||||
set
|
||||
{
|
||||
if (_settings.MinimizeToTrayOnClose != value)
|
||||
{
|
||||
_settings.MinimizeToTrayOnClose = value;
|
||||
OnPropertyChanged(nameof(MinimizeToTrayOnClose));
|
||||
_settings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int DefaultPollingInterval
|
||||
{
|
||||
get => _settings.DefaultPollingInterval;
|
||||
set
|
||||
{
|
||||
if (_settings.DefaultPollingInterval != value)
|
||||
{
|
||||
_settings.DefaultPollingInterval = value;
|
||||
OnPropertyChanged();
|
||||
_settings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task TestConnection()
|
||||
{
|
||||
// try
|
||||
// {
|
||||
// using (var db = transaction.GetInstance())
|
||||
// {
|
||||
// await db.Ado.OpenAsync();
|
||||
// NotificationHelper.ShowSuccess("连接成功!");
|
||||
// }
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// NotificationHelper.ShowError($"连接失败:{ex.Message}", ex);
|
||||
// }
|
||||
try
|
||||
{
|
||||
// 使用当前配置测试数据库连接
|
||||
using var db = new SqlSugar.SqlSugarScope(new SqlSugar.ConnectionConfig()
|
||||
{
|
||||
DbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), _settings.Database.DbType),
|
||||
ConnectionString = _settings.ToConnectionString(),
|
||||
IsAutoCloseConnection = true
|
||||
});
|
||||
|
||||
await db.Ado.ExecuteCommandAsync("SELECT 1");
|
||||
_notificationService.ShowSuccess("连接成功!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_notificationService.ShowError($"连接失败:{ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ using System;
|
||||
using System.Data;
|
||||
using System.Threading.Tasks;
|
||||
using DMS.Application.Services;
|
||||
using DMS.Infrastructure.Configurations;
|
||||
using DMS.WPF.Helper;
|
||||
using DMS.WPF.Interfaces;
|
||||
using DMS.WPF.Views;
|
||||
@@ -26,18 +27,20 @@ public partial class SplashViewModel : ObservableObject
|
||||
private readonly IInitializeService _initializeService;
|
||||
private readonly IDataEventService _dataEventService;
|
||||
private readonly IAppDataCenterService _appDataCenterService;
|
||||
private readonly AppSettings _appSettings;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _loadingMessage = "正在加载...";
|
||||
|
||||
public SplashViewModel(ILogger<SplashViewModel> logger,IServiceProvider serviceProvider, IInitializeService initializeService,IDataEventService dataEventService,
|
||||
IAppDataCenterService appDataCenterService)
|
||||
IAppDataCenterService appDataCenterService,AppSettings appSettings)
|
||||
{
|
||||
_logger = logger;
|
||||
_serviceProvider = serviceProvider;
|
||||
_initializeService = initializeService;
|
||||
_dataEventService = dataEventService;
|
||||
this._appDataCenterService = appDataCenterService;
|
||||
_appSettings = appSettings;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -52,6 +55,8 @@ public partial class SplashViewModel : ObservableObject
|
||||
_initializeService.InitializeTables();
|
||||
_initializeService.InitializeMenus();
|
||||
LoadingMessage = "正在加载系统配置...";
|
||||
|
||||
|
||||
await _appDataCenterService.DataLoaderService.LoadAllDataToMemoryAsync();
|
||||
|
||||
// 可以在这里添加加载配置的逻辑
|
||||
|
||||
@@ -26,6 +26,11 @@
|
||||
IsClickEnabled="True">
|
||||
<ui:ToggleSwitch IsOn="{Binding MinimizeToTrayOnClose, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</ui:SettingsCard>
|
||||
<ui:SettingsCard Header="默认轮询间隔"
|
||||
Description="设置变量的默认轮询间隔时间(毫秒)">
|
||||
<TextBox Width="120"
|
||||
Text="{Binding DefaultPollingInterval, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</ui:SettingsCard>
|
||||
</ui:SettingsExpander.Items>
|
||||
</ui:SettingsExpander>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user