初步完成变量选择Mqtt服务器

This commit is contained in:
2025-09-06 19:10:25 +08:00
parent 31c4e77232
commit 5db88f5709
11 changed files with 455 additions and 220 deletions

View File

@@ -1,22 +1,90 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.WPF.Services;
using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.WPF.ViewModels.Items;
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
namespace DMS.WPF.ViewModels.Dialogs;
public partial class MqttSelectionDialogViewModel : ObservableObject
namespace DMS.WPF.ViewModels.Dialogs
{
/// <summary>
/// MQTT服务器选择对话框的视图模型
/// </summary>
public partial class MqttSelectionDialogViewModel : DialogViewModelBase<MqttServerItemViewModel>
{
private readonly IMqttAppService _mqttAppService;
// [ObservableProperty]
// private ObservableCollection<Mqtt> mqtts;
//
// [ObservableProperty]
// private Mqtt? selectedMqtt;
//
// public MqttSelectionDialogViewModel(DataServices _dataServices)
// {
// Mqtts = new ObservableCollection<Mqtt>(_dataServices.Mqtts);
// }
[ObservableProperty]
private ObservableCollection<MqttServerItemViewModel> _mqttServers = new();
[ObservableProperty]
private MqttServerItemViewModel _selectedMqttServer;
public MqttSelectionDialogViewModel(IMqttAppService mqttAppService)
{
_mqttAppService = mqttAppService;
LoadMqttServersAsync();
}
/// <summary>
/// 异步加载所有MQTT服务器
/// </summary>
private async void LoadMqttServersAsync()
{
try
{
var mqttServerDtos = await _mqttAppService.GetAllMqttServersAsync();
MqttServers.Clear();
foreach (var dto in mqttServerDtos)
{
MqttServers.Add(new MqttServerItemViewModel
{
Id = dto.Id,
ServerName = dto.ServerName,
ServerUrl = dto.ServerUrl,
Port = dto.Port,
Username = dto.Username,
Password = dto.Password,
IsActive = dto.IsActive,
SubscribeTopic = dto.SubscribeTopic,
PublishTopic = dto.PublishTopic,
ClientId = dto.ClientId,
CreatedAt = dto.CreatedAt,
ConnectedAt = dto.ConnectedAt,
ConnectionDuration = dto.ConnectionDuration,
MessageFormat = dto.MessageFormat
});
}
}
catch (Exception ex)
{
// 记录错误日志
System.Console.WriteLine($"加载MQTT服务器失败: {ex.Message}");
}
}
/// <summary>
/// 确认选择
/// </summary>
[RelayCommand]
private void Confirm()
{
if (SelectedMqttServer != null)
{
Close(SelectedMqttServer);
}
}
/// <summary>
/// 取消选择
/// </summary>
[RelayCommand]
private void Cancel()
{
Close(null);
}
}
}