给变量添加Mqtt服务器的功能
This commit is contained in:
@@ -52,7 +52,7 @@ public partial class App : Application
|
||||
InitializeMenu()
|
||||
.Await((e) => { NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}"); },
|
||||
() => { MessageHelper.SendLoadMessage(LoadTypes.Menu); });
|
||||
|
||||
Host.Services.GetRequiredService<GrowlNotificationService>();
|
||||
MainWindow = Host.Services.GetRequiredService<MainView>();
|
||||
MainWindow.Show();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Helper;
|
||||
|
||||
namespace PMSWPF.Extensions;
|
||||
|
||||
@@ -60,6 +63,16 @@ public static class ObjectExtensions
|
||||
PropertyInfo targetProperty =
|
||||
targetType.GetProperty(sourceProperty.Name, BindingFlags.Public | BindingFlags.Instance);
|
||||
|
||||
// 特殊处理 Mqtt.ConnTime 和 DbMqtt.CreateTime 的映射
|
||||
if (sourceProperty.Name == "ConnTime" && targetType == typeof(DbMqtt))
|
||||
{
|
||||
targetProperty = targetType.GetProperty("CreateTime", BindingFlags.Public | BindingFlags.Instance);
|
||||
}
|
||||
else if (sourceProperty.Name == "CreateTime" && targetType == typeof(Mqtt))
|
||||
{
|
||||
targetProperty = targetType.GetProperty("ConnTime", BindingFlags.Public | BindingFlags.Instance);
|
||||
}
|
||||
|
||||
// 确保目标属性存在且可写
|
||||
if (targetProperty != null && targetProperty.CanWrite)
|
||||
{
|
||||
@@ -78,7 +91,48 @@ public static class ObjectExtensions
|
||||
// 场景 2: 属性类型不同,但可能是泛型 List<T> 类型
|
||||
else if (isTargetList && isSourceList)
|
||||
{
|
||||
CopyGenericList(ttarget, sourceProperty, targetProperty, sourceValue);
|
||||
// 特殊处理 Mqtts 列表的复制
|
||||
if (sourceProperty.Name == "Mqtts")
|
||||
{
|
||||
if (sourceType == typeof(VariableData) && targetType == typeof(DbVariableData))
|
||||
{
|
||||
// 从 VariableData (List<Mqtt>) 转换为 DbVariableData (List<DbMqtt>)
|
||||
var sourceMqtts = sourceValue as List<Mqtt>;
|
||||
if (sourceMqtts != null)
|
||||
{
|
||||
var targetMqtts = sourceMqtts.Select(m => m.CopyTo<DbMqtt>()).ToList();
|
||||
targetProperty.SetValue(ttarget, targetMqtts);
|
||||
}
|
||||
else
|
||||
{
|
||||
targetProperty.SetValue(ttarget, null);
|
||||
}
|
||||
}
|
||||
else if (sourceType == typeof(DbVariableData) && targetType == typeof(VariableData))
|
||||
{
|
||||
// 从 DbVariableData (List<DbMqtt>) 转换为 VariableData (List<Mqtt>)
|
||||
var sourceDbMqtts = sourceValue as List<DbMqtt>;
|
||||
if (sourceDbMqtts != null)
|
||||
{
|
||||
var targetMqtts = sourceDbMqtts.Select(m => m.CopyTo<Mqtt>()).ToList();
|
||||
targetProperty.SetValue(ttarget, targetMqtts);
|
||||
}
|
||||
else
|
||||
{
|
||||
targetProperty.SetValue(ttarget, null);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 其他 List<T> 类型,使用通用复制逻辑
|
||||
CopyGenericList(ttarget, sourceProperty, targetProperty, sourceValue);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 其他 List<T> 类型,使用通用复制逻辑
|
||||
CopyGenericList(ttarget, sourceProperty, targetProperty, sourceValue);
|
||||
}
|
||||
}
|
||||
// 场景 3: 属性类型不同,但是属性名称一样
|
||||
else
|
||||
|
||||
@@ -171,4 +171,16 @@ public class DialogService :IDialogService
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<Mqtt?> ShowMqttSelectionDialog()
|
||||
{
|
||||
var vm = new MqttSelectionDialogViewModel();
|
||||
var dialog = new MqttSelectionDialog(vm);
|
||||
var result = await dialog.ShowAsync();
|
||||
if (result == ContentDialogResult.Primary)
|
||||
{
|
||||
return vm.SelectedMqtt;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -21,5 +21,5 @@ public interface IDialogService
|
||||
Task<string> ShowImportExcelDialog();
|
||||
ContentDialog ShowProcessingDialog(string title, string message);
|
||||
Task<PollLevelType?> ShowPollLevelDialog(PollLevelType pollLevelType);
|
||||
|
||||
Task<Mqtt?> ShowMqttSelectionDialog();
|
||||
}
|
||||
41
ViewModels/Dialogs/MqttSelectionDialogViewModel.cs
Normal file
41
ViewModels/Dialogs/MqttSelectionDialogViewModel.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Data.Repositories;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PMSWPF.ViewModels.Dialogs;
|
||||
|
||||
public partial class MqttSelectionDialogViewModel : ObservableObject
|
||||
{
|
||||
private readonly MqttRepository _mqttRepository;
|
||||
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<Mqtt> mqtts;
|
||||
|
||||
[ObservableProperty]
|
||||
private Mqtt? selectedMqtt;
|
||||
|
||||
public MqttSelectionDialogViewModel()
|
||||
{
|
||||
_mqttRepository = new MqttRepository();
|
||||
LoadMqtts();
|
||||
}
|
||||
|
||||
private async void LoadMqtts()
|
||||
{
|
||||
try
|
||||
{
|
||||
var allMqtts = await _mqttRepository.GetAll();
|
||||
Mqtts = new ObservableCollection<Mqtt>(allMqtts);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 这里需要一个日志记录器,但由于ViewModel中没有直接注入ILogger,
|
||||
// 暂时使用Console.WriteLine或NotificationHelper
|
||||
// 更好的做法是注入ILogger或使用静态日志类
|
||||
Console.WriteLine($"加载MQTT服务器列表失败: {ex.Message}");
|
||||
// 或者使用NotificationHelper.ShowMessage("加载MQTT服务器列表失败", NotificationType.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,7 @@ public partial class MqttsViewModel : ViewModelBase
|
||||
}
|
||||
|
||||
await _mqttRepository.Add(mqtt);
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@@ -282,6 +282,48 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public async Task AddMqttServerToVariables(IList<VariableData> variablesToAddMqtt)
|
||||
{
|
||||
if (variablesToAddMqtt == null || !variablesToAddMqtt.Any())
|
||||
{
|
||||
NotificationHelper.ShowMessage("请选择要添加MQTT服务器的变量", NotificationType.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var selectedMqtt = await _dialogService.ShowMqttSelectionDialog();
|
||||
if (selectedMqtt == null)
|
||||
{
|
||||
return; // 用户取消选择
|
||||
}
|
||||
|
||||
foreach (VariableData variable in variablesToAddMqtt)
|
||||
{
|
||||
if (variable.Mqtts == null)
|
||||
{
|
||||
variable.Mqtts = new List<Mqtt>();
|
||||
}
|
||||
// 避免重复添加
|
||||
if (!variable.Mqtts.Any(m => m.Id == selectedMqtt.Id))
|
||||
{
|
||||
variable.Mqtts.Add(selectedMqtt);
|
||||
variable.IsModified = true; // 标记为已修改
|
||||
}
|
||||
}
|
||||
|
||||
// 批量更新数据库
|
||||
await _varDataRepository.UpdateAsync(variablesToAddMqtt.ToList());
|
||||
NotificationHelper.ShowMessage($"已成功为 {variablesToAddMqtt.Count} 个变量添加MQTT服务器: {selectedMqtt.Name}", NotificationType.Success);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, "添加MQTT服务器到变量时发生错误。");
|
||||
NotificationHelper.ShowMessage($"添加MQTT服务器失败: {ex.Message}", NotificationType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
// [RelayCommand]
|
||||
// private async void ImportFromExcel()
|
||||
// {
|
||||
|
||||
21
Views/Dialogs/MqttSelectionDialog.xaml
Normal file
21
Views/Dialogs/MqttSelectionDialog.xaml
Normal file
@@ -0,0 +1,21 @@
|
||||
<controls:ContentDialog x:Class="PMSWPF.Views.Dialogs.MqttSelectionDialog"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vm="clr-namespace:PMSWPF.ViewModels.Dialogs"
|
||||
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
|
||||
xmlns:controls="http://schemas.inkore.net/lib/ui/wpf/modern"
|
||||
mc:Ignorable="d"
|
||||
d:DataContext="{d:DesignInstance vm:MqttSelectionDialogViewModel}"
|
||||
Title="选择MQTT服务器"
|
||||
PrimaryButtonText="确定"
|
||||
SecondaryButtonText="取消"
|
||||
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
|
||||
SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
|
||||
<Grid>
|
||||
<ListBox ItemsSource="{Binding Mqtts}"
|
||||
SelectedItem="{Binding SelectedMqtt}"
|
||||
DisplayMemberPath="Name" />
|
||||
</Grid>
|
||||
</controls:ContentDialog>
|
||||
@@ -191,6 +191,12 @@
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
<MenuItem Header="添加MQTT服务器"
|
||||
Click="AddMqttServerToVariables_Click">
|
||||
<MenuItem.Icon>
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" />
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
</ContextMenu>
|
||||
</DataGrid.ContextMenu>
|
||||
<DataGrid.RowStyle>
|
||||
|
||||
@@ -145,4 +145,27 @@ public partial class VariableTableView : UserControl
|
||||
NotificationHelper.ShowMessage(msg + e.Message, NotificationType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async void AddMqttServerToVariables_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
_viewModel = (VariableTableViewModel)this.DataContext;
|
||||
var selectedVariables = BasicGridView.SelectedItems.Cast<VariableData>().ToList();
|
||||
if (selectedVariables.Any())
|
||||
{
|
||||
await _viewModel.AddMqttServerToVariables(selectedVariables);
|
||||
}
|
||||
else
|
||||
{
|
||||
NotificationHelper.ShowMessage("请选择要添加MQTT服务器的变量", NotificationType.Warning);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string msg = "添加MQTT服务器时发生了错误:";
|
||||
Logger.Error(msg + ex);
|
||||
NotificationHelper.ShowMessage(msg + ex.Message, NotificationType.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user