From d6939a7e66b0b9c93f77d146c64bce1cebe66893 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sat, 5 Jul 2025 16:13:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=AE=E6=94=B9=E8=BD=AE?= =?UTF-8?q?=E8=AF=A2=E6=97=B6=E9=97=B4=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 1 + Models/VariableData.cs | 7 ++++- Services/DialogService.cs | 13 ++++++++ Services/IDialogService.cs | 3 ++ Services/S7BackgroundService.cs | 2 +- .../Dialogs/PollLevelDialogViewModel.cs | 22 ++++++++++++++ ViewModels/VariableTableViewModel.cs | 18 +++++++++++ Views/Dialogs/PollLevelDialog.xaml | 30 +++++++++++++++++++ Views/VariableTableView.xaml | 29 +++++++++++++++++- 9 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 ViewModels/Dialogs/PollLevelDialogViewModel.cs create mode 100644 Views/Dialogs/PollLevelDialog.xaml diff --git a/App.xaml.cs b/App.xaml.cs index 8b5455c..a6cc4a5 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -13,6 +13,7 @@ using PMSWPF.Services; using PMSWPF.ViewModels; using PMSWPF.Views; using Microsoft.Extensions.Hosting; +using PMSWPF.ViewModels.Dialogs; using SqlSugar; using LogLevel = Microsoft.Extensions.Logging.LogLevel; diff --git a/Models/VariableData.cs b/Models/VariableData.cs index 2d6cc2f..fd15096 100644 --- a/Models/VariableData.cs +++ b/Models/VariableData.cs @@ -81,7 +81,8 @@ public partial class VariableData : ObservableObject /// /// 轮询级别,例如1秒、5秒等。 /// - public PollLevelType PollLevelType { get; set; } = PollLevelType.ThirtySeconds; + [ObservableProperty] + private PollLevelType pollLevelType = PollLevelType.ThirtySeconds; /// /// 最后一次轮询时间。 @@ -148,4 +149,8 @@ public partial class VariableData : ObservableObject /// public List Mqtts { get; set; } + partial void OnPollLevelTypeChanged(PollLevelType value) + { + IsModified = true; + } } \ No newline at end of file diff --git a/Services/DialogService.cs b/Services/DialogService.cs index 45b2abc..f3373ae 100644 --- a/Services/DialogService.cs +++ b/Services/DialogService.cs @@ -1,6 +1,7 @@ using HandyControl.Tools.Extension; using iNKORE.UI.WPF.Modern.Controls; using NPOI.SS.Formula.Functions; +using PMSWPF.Enums; using PMSWPF.Models; using PMSWPF.ViewModels.Dialogs; using PMSWPF.Views.Dialogs; @@ -158,4 +159,16 @@ public class DialogService :IDialogService _ = dialog.ShowAsync(); // 不await,让它在后台显示 return dialog; } + + public async Task ShowPollLevelDialog(PollLevelType pollLevelType) + { + var vm = new PollLevelDialogViewModel(pollLevelType); + var dialog = new PollLevelDialog(vm); + var result = await dialog.ShowAsync(); + if (result == ContentDialogResult.Primary) + { + return vm.SelectedPollLevelType; + } + return null; + } } \ No newline at end of file diff --git a/Services/IDialogService.cs b/Services/IDialogService.cs index 71ec6fb..96b8436 100644 --- a/Services/IDialogService.cs +++ b/Services/IDialogService.cs @@ -1,4 +1,5 @@ using iNKORE.UI.WPF.Modern.Controls; +using PMSWPF.Enums; using PMSWPF.Models; namespace PMSWPF.Services; @@ -19,4 +20,6 @@ public interface IDialogService Task ShowEditVarDataDialog(VariableData variableData); Task ShowImportExcelDialog(); ContentDialog ShowProcessingDialog(string title, string message); + Task ShowPollLevelDialog(PollLevelType pollLevelType); + } \ No newline at end of file diff --git a/Services/S7BackgroundService.cs b/Services/S7BackgroundService.cs index bda4ac5..29c067c 100644 --- a/Services/S7BackgroundService.cs +++ b/Services/S7BackgroundService.cs @@ -222,7 +222,7 @@ namespace PMSWPF.Services variable.DataValue = value.ToString(); variable.DisplayValue = SiemensHelper.ConvertS7Value(value, variable.DataType, variable.Converstion); variable.LastPollTime = DateTime.Now; // 更新最后轮询时间 - _logger.LogDebug($"线程ID:{Environment.CurrentManagedThreadId},已读取变量 {variable.Name}: {variable.DataValue}"); + // _logger.LogDebug($"线程ID:{Environment.CurrentManagedThreadId},已读取变量 {variable.Name}: {variable.DataValue}"); } } catch (Exception ex) diff --git a/ViewModels/Dialogs/PollLevelDialogViewModel.cs b/ViewModels/Dialogs/PollLevelDialogViewModel.cs new file mode 100644 index 0000000..9d29644 --- /dev/null +++ b/ViewModels/Dialogs/PollLevelDialogViewModel.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using CommunityToolkit.Mvvm.ComponentModel; +using PMSWPF.Enums; + +namespace PMSWPF.ViewModels.Dialogs +{ + public partial class PollLevelDialogViewModel : ObservableObject + { + [ObservableProperty] + private PollLevelType _selectedPollLevelType; + + public List PollLevelTypes { get; } + + public PollLevelDialogViewModel(PollLevelType currentPollLevelType) + { + PollLevelTypes = Enum.GetValues(typeof(PollLevelType)).Cast().ToList(); + SelectedPollLevelType = currentPollLevelType; + } + } +} diff --git a/ViewModels/VariableTableViewModel.cs b/ViewModels/VariableTableViewModel.cs index 906be53..07ffc2b 100644 --- a/ViewModels/VariableTableViewModel.cs +++ b/ViewModels/VariableTableViewModel.cs @@ -253,6 +253,24 @@ partial class VariableTableViewModel : ViewModelBase } } + [RelayCommand] + private async Task ChangePollLevel() + { + if (SelectedVariableData == null) + { + NotificationHelper.ShowMessage("请选择一个变量", NotificationType.Warning); + return; + } + + var newPollLevelType = await _dialogService.ShowPollLevelDialog(SelectedVariableData.PollLevelType); + if (newPollLevelType.HasValue) + { + SelectedVariableData.PollLevelType = newPollLevelType.Value; + await _varDataRepository.UpdateAsync(SelectedVariableData); + NotificationHelper.ShowMessage($"变量 {SelectedVariableData.Name} 的轮询频率已更新", NotificationType.Success); + } + } + // [RelayCommand] // private async void ImportFromExcel() // { diff --git a/Views/Dialogs/PollLevelDialog.xaml b/Views/Dialogs/PollLevelDialog.xaml new file mode 100644 index 0000000..7d7ebc2 --- /dev/null +++ b/Views/Dialogs/PollLevelDialog.xaml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + diff --git a/Views/VariableTableView.xaml b/Views/VariableTableView.xaml index 14be491..8019985 100644 --- a/Views/VariableTableView.xaml +++ b/Views/VariableTableView.xaml @@ -21,6 +21,8 @@ +