1 feat: 优化MQTT服务器详情页面功能
2
3 - 修改MQTT发布处理器,使用DisplayValue代替DataValue进行数据发布
4 - 在MqttServiceManager中使用DisplayValue进行消息内容替换
5 - 增加MQTT服务器编辑功能,允许用户修改服务器配置
6 - 添加变量MQTT发布别名修改功能,支持右键菜单修改发布名称
7 - 实现变量值变化事件监听,实时更新UI显示
8 - 优化MQTT服务器详情页面UI,改进变量关联数据显示
9 - 修复变量表关联MQTT服务器时的逻辑问题
10 - 完善导航生命周期事件处理
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using DMS.Application.Events;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Application.Interfaces.Management;
|
||||
using DMS.Core.Models;
|
||||
using DMS.WPF.Interfaces;
|
||||
using DMS.WPF.ViewModels.Dialogs;
|
||||
using DMS.WPF.ViewModels.Items;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.ObjectModel;
|
||||
@@ -18,7 +21,9 @@ namespace DMS.WPF.ViewModels
|
||||
private readonly ILogger<MqttServerDetailViewModel> _logger;
|
||||
private readonly IDialogService _dialogService;
|
||||
private readonly INotificationService _notificationService;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IMqttManagementService _mqttManagementService;
|
||||
private readonly IWPFDataService _wpfDataService;
|
||||
private readonly IDataStorageService _dataStorageService;
|
||||
private readonly INavigationService _navigationService;
|
||||
|
||||
@@ -47,20 +52,85 @@ namespace DMS.WPF.ViewModels
|
||||
public MqttServerDetailViewModel(ILogger<MqttServerDetailViewModel> logger,
|
||||
IDialogService dialogService,
|
||||
INotificationService notificationService,
|
||||
IEventService eventService,
|
||||
IMqttManagementService mqttManagementService,
|
||||
IWPFDataService wpfDataService,
|
||||
IDataStorageService dataStorageService,
|
||||
INavigationService navigationService)
|
||||
{
|
||||
_logger = logger;
|
||||
_dialogService = dialogService;
|
||||
_notificationService = notificationService;
|
||||
this._eventService = eventService;
|
||||
_mqttManagementService = mqttManagementService;
|
||||
this._wpfDataService = wpfDataService;
|
||||
this._dataStorageService = dataStorageService;
|
||||
_navigationService = navigationService;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 编辑当前MQTT服务器
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private async Task EditMqtt()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (CurrentMqtt == null)
|
||||
{
|
||||
_notificationService.ShowError("没有选中的MQTT服务器,无法编辑。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建编辑对话框的视图模型
|
||||
var mqttDialogViewModel = new MqttDialogViewModel(CurrentMqtt)
|
||||
{
|
||||
Title = "编辑MQTT服务器",
|
||||
PrimaryButText = "保存修改"
|
||||
};
|
||||
|
||||
// 显示对话框
|
||||
var updatedMqtt = await _dialogService.ShowDialogAsync(mqttDialogViewModel);
|
||||
|
||||
if (updatedMqtt == null)
|
||||
{
|
||||
return; // 用户取消了编辑
|
||||
}
|
||||
|
||||
// 更新MQTT服务器
|
||||
var result = await _wpfDataService.MqttDataService.UpdateMqttServer(updatedMqtt);
|
||||
|
||||
if (result)
|
||||
{
|
||||
// 更新当前视图模型的数据
|
||||
CurrentMqtt.ServerName = updatedMqtt.ServerName;
|
||||
CurrentMqtt.ServerUrl = updatedMqtt.ServerUrl;
|
||||
CurrentMqtt.Port = updatedMqtt.Port;
|
||||
CurrentMqtt.ClientId = updatedMqtt.ClientId;
|
||||
CurrentMqtt.Username = updatedMqtt.Username;
|
||||
CurrentMqtt.Password = updatedMqtt.Password;
|
||||
CurrentMqtt.PublishTopic = updatedMqtt.PublishTopic;
|
||||
CurrentMqtt.SubscribeTopic = updatedMqtt.SubscribeTopic;
|
||||
CurrentMqtt.MessageHeader = updatedMqtt.MessageHeader;
|
||||
CurrentMqtt.MessageContent = updatedMqtt.MessageContent;
|
||||
CurrentMqtt.MessageFooter = updatedMqtt.MessageFooter;
|
||||
|
||||
_notificationService.ShowSuccess($"MQTT服务器编辑成功:{updatedMqtt.ServerName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_notificationService.ShowError("更新MQTT服务器失败。");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "编辑MQTT服务器过程中发生错误");
|
||||
_notificationService.ShowError($"编辑MQTT服务器过程中发生错误:{e.Message}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重新加载当前MQTT服务器数据
|
||||
/// </summary>
|
||||
@@ -107,7 +177,81 @@ namespace DMS.WPF.ViewModels
|
||||
CurrentMqtt = mqttServerItem;
|
||||
|
||||
}
|
||||
_eventService.OnVariableValueChanged += OnVariableValueChanged;
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e)
|
||||
{
|
||||
|
||||
var variableAlias=CurrentMqtt.VariableAliases.FirstOrDefault(v => v.Variable.Id == e.Variable.Id);
|
||||
if (variableAlias is not null)
|
||||
{
|
||||
variableAlias.Variable.DisplayValue=e.Variable.DisplayValue;
|
||||
variableAlias.Variable.UpdatedAt=e.Variable.UpdatedAt;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改变量的MQTT发送名称
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private async Task ModifyAlias(VariableMqttAlias variableAlias)
|
||||
{
|
||||
if (variableAlias == null)
|
||||
{
|
||||
_notificationService.ShowError("请选择要修改的变量项。");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 创建一个用于输入新名称的简单对话框
|
||||
var oldAlias = variableAlias.Alias;
|
||||
InputDialogViewModel viewModel = new InputDialogViewModel("修改发送名称", "请输入新的MQTT发送名称:", oldAlias);
|
||||
var dialogResult = await _dialogService.ShowDialogAsync(viewModel);
|
||||
|
||||
if (dialogResult != null) // 用户没有取消操作
|
||||
{
|
||||
var newAlias = dialogResult.Trim();
|
||||
|
||||
if (string.IsNullOrEmpty(newAlias))
|
||||
{
|
||||
_notificationService.ShowWarn("发送名称不能为空。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新变量的发送名称
|
||||
variableAlias.Alias = newAlias;
|
||||
|
||||
// 保存更改到数据服务
|
||||
var result = await _wpfDataService.UpdateMqttServer(CurrentMqtt);
|
||||
|
||||
if (result)
|
||||
{
|
||||
_notificationService.ShowSuccess($"变量 '{variableAlias.Variable.Name}' 的发送名称已更新为 '{newAlias}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
_notificationService.ShowError("更新发送名称失败。");
|
||||
// 如果更新失败,恢复原来的值
|
||||
variableAlias.Alias = oldAlias;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "修改变量发送名称时发生错误");
|
||||
_notificationService.ShowError($"修改发送名称时发生错误:{e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public override Task OnNavigatedFromAsync(NavigationParameter parameter)
|
||||
{
|
||||
|
||||
_eventService.OnVariableValueChanged -= OnVariableValueChanged;
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -184,7 +184,20 @@ public partial class MqttsViewModel : ViewModelBase
|
||||
|
||||
// 更新UI
|
||||
_mapper.Map(mqtt, SelectedMqtt);
|
||||
|
||||
|
||||
// 更新当前视图模型的数据
|
||||
mqtt.ServerName = SelectedMqtt.ServerName;
|
||||
mqtt.ServerUrl = SelectedMqtt.ServerUrl;
|
||||
mqtt.Port = SelectedMqtt.Port;
|
||||
mqtt.ClientId = SelectedMqtt.ClientId;
|
||||
mqtt.Username = SelectedMqtt.Username;
|
||||
mqtt.Password = SelectedMqtt.Password;
|
||||
mqtt.PublishTopic = SelectedMqtt.PublishTopic;
|
||||
mqtt.SubscribeTopic = SelectedMqtt.SubscribeTopic;
|
||||
mqtt.MessageHeader = SelectedMqtt.MessageHeader;
|
||||
mqtt.MessageContent = SelectedMqtt.MessageContent;
|
||||
mqtt.MessageFooter = SelectedMqtt.MessageFooter;
|
||||
|
||||
_notificationService.ShowSuccess($"编辑MQTT服务器成功:{mqtt.ServerName}");
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -661,7 +661,8 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
|
||||
MqttServer = selectedMqtt,
|
||||
Variable = originalVariable
|
||||
};
|
||||
// originalVariable.MqttAliases.Add(variableMqtt);
|
||||
originalVariable.MqttAliases.Add(variableMqtt);
|
||||
selectedMqtt.VariableAliases.Add(variableMqtt);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -38,14 +38,8 @@
|
||||
DefaultLabelPosition="Right"
|
||||
IsOpen="False"
|
||||
HorizontalAlignment="Right">
|
||||
<!-- 重新加载 -->
|
||||
<ui:AppBarButton Command="{Binding ReloadCommand}" Label="重新加载">
|
||||
<ui:AppBarButton.Icon>
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Refresh}" />
|
||||
</ui:AppBarButton.Icon>
|
||||
</ui:AppBarButton>
|
||||
|
||||
<ui:AppBarButton x:Name="EditButton" Label="编辑">
|
||||
<ui:AppBarButton x:Name="EditButton" Label="编辑" Command="{Binding EditMqttCommand}">
|
||||
<ui:AppBarButton.Icon>
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
|
||||
</ui:AppBarButton.Icon>
|
||||
@@ -272,18 +266,24 @@
|
||||
</Grid.RowDefinitions>
|
||||
<TextBlock Grid.Row="0" Text="关联变量" Style="{StaticResource SubtitleTextBlockStyle}" Margin="0,0,0,10"/>
|
||||
<DataGrid Grid.Row="1" x:Name="AssociatedVariablesDataGrid"
|
||||
ItemsSource="{Binding AssociatedVariables}"
|
||||
ItemsSource="{Binding CurrentMqtt.VariableAliases}"
|
||||
AutoGenerateColumns="False"
|
||||
CanUserAddRows="False"
|
||||
CanUserDeleteRows="False"
|
||||
IsReadOnly="True"
|
||||
SelectionMode="Extended">
|
||||
<DataGrid.ContextMenu>
|
||||
<ContextMenu>
|
||||
<MenuItem Header="修改发送名称" Command="{Binding ModifyAliasCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=SelectedItems[0]}"/>
|
||||
</ContextMenu>
|
||||
</DataGrid.ContextMenu>
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="设备名称" Binding="{Binding Variable.VariableTable.Device.Name}"/>
|
||||
<DataGridTextColumn Header="变量表名称" Binding="{Binding Variable.VariableTable.Name}"/>
|
||||
<DataGridTextColumn Header="变量名称" Binding="{Binding Variable.Name}"/>
|
||||
<DataGridTextColumn Header="MQTT发送名称" Binding="{Binding MqttAlias}"/>
|
||||
<DataGridTextColumn Header="MQTT发送名称" Binding="{Binding Alias}"/>
|
||||
<DataGridTextColumn Header="地址" Binding="{Binding Variable.S7Address}"/>
|
||||
<DataGridTextColumn Header="数据类型" Binding="{Binding Variable.SignalType}"/>
|
||||
<DataGridTextColumn Header="当前值" Binding="{Binding Variable.DataValue}"/>
|
||||
<DataGridTextColumn Header="数据类型" Binding="{Binding Variable.DataType}"/>
|
||||
<DataGridTextColumn Header="显示值" Binding="{Binding Variable.DisplayValue}"/>
|
||||
<DataGridTextColumn Header="更新时间" Binding="{Binding Variable.UpdatedAt, StringFormat='yyyy-MM-dd HH:mm:ss'}"/>
|
||||
</DataGrid.Columns>
|
||||
|
||||
Reference in New Issue
Block a user