添加修改轮询时间的功能

This commit is contained in:
2025-07-05 16:13:46 +08:00
parent b019269bd7
commit d6939a7e66
9 changed files with 122 additions and 3 deletions

View File

@@ -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;

View File

@@ -81,7 +81,8 @@ public partial class VariableData : ObservableObject
/// <summary>
/// 轮询级别例如1秒、5秒等。
/// </summary>
public PollLevelType PollLevelType { get; set; } = PollLevelType.ThirtySeconds;
[ObservableProperty]
private PollLevelType pollLevelType = PollLevelType.ThirtySeconds;
/// <summary>
/// 最后一次轮询时间。
@@ -148,4 +149,8 @@ public partial class VariableData : ObservableObject
/// </summary>
public List<Mqtt> Mqtts { get; set; }
partial void OnPollLevelTypeChanged(PollLevelType value)
{
IsModified = true;
}
}

View File

@@ -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<PollLevelType?> 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;
}
}

View File

@@ -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<VariableData> ShowEditVarDataDialog(VariableData variableData);
Task<string> ShowImportExcelDialog();
ContentDialog ShowProcessingDialog(string title, string message);
Task<PollLevelType?> ShowPollLevelDialog(PollLevelType pollLevelType);
}

View File

@@ -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)

View File

@@ -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<PollLevelType> PollLevelTypes { get; }
public PollLevelDialogViewModel(PollLevelType currentPollLevelType)
{
PollLevelTypes = Enum.GetValues(typeof(PollLevelType)).Cast<PollLevelType>().ToList();
SelectedPollLevelType = currentPollLevelType;
}
}
}

View File

@@ -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()
// {

View File

@@ -0,0 +1,30 @@
<ui:ContentDialog x:Class="PMSWPF.Views.Dialogs.PollLevelDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:enums="clr-namespace:PMSWPF.Enums"
xmlns:extensions="clr-namespace:PMSWPF.Extensions"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:vc="clr-namespace:PMSWPF.ValueConverts"
mc:Ignorable="d"
Title="修改轮询频率"
PrimaryButtonText="确定"
CloseButtonText="取消"
d:DesignHeight="150" d:DesignWidth="300">
<ui:ContentDialog.Resources>
<vc:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
</ui:ContentDialog.Resources>
<Grid>
<ComboBox
ItemsSource="{Binding PollLevelTypes}"
SelectedItem="{Binding SelectedPollLevelType}"
Margin="20">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</ui:ContentDialog>

View File

@@ -21,6 +21,8 @@
<ex:EnumBindingSource x:Key="protocolType"
EnumType="{x:Type en:ProtocolType}" />
<vc:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
<ex:EnumBindingSource x:Key="pollLevelType"
EnumType="{x:Type en:PollLevelType}" />
<!-- 标签字体的样式 -->
<Style TargetType="TextBlock"
@@ -183,6 +185,12 @@
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Import}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="修改轮询频率"
Command="{Binding ChangePollLevelCommand}">
<MenuItem.Icon>
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.RowStyle>
@@ -250,6 +258,25 @@
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="轮询频率">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding PollLevelType, Converter={StaticResource EnumDescriptionConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource pollLevelType}}"
SelectedItem="{Binding PollLevelType}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn IsReadOnly="True"
Header="S7地址"
Binding="{Binding S7Address}" />
@@ -284,4 +311,4 @@
</DataGrid>
</DockPanel>
</UserControl>
</UserControl>