初步完成变量选择Mqtt服务器
This commit is contained in:
@@ -1,34 +1,100 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.WPF.ViewModels.Items;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DMS.WPF.ViewModels.Dialogs;
|
||||
|
||||
public partial class MqttAliasBatchEditDialogViewModel : ObservableObject
|
||||
namespace DMS.WPF.ViewModels.Dialogs
|
||||
{
|
||||
[ObservableProperty]
|
||||
private string _title = "批量设置MQTT别名";
|
||||
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<VariableMqttAlias> _variablesToEdit;
|
||||
|
||||
public MqttServerItemViewModel SelectedMqtt { get; private set; }
|
||||
|
||||
public MqttAliasBatchEditDialogViewModel(List<VariableItemViewModel> selectedVariables, MqttServerItemViewModel selectedMqtt)
|
||||
/// <summary>
|
||||
/// MQTT别名批量编辑对话框的视图模型
|
||||
/// </summary>
|
||||
public partial class MqttAliasBatchEditDialogViewModel : DialogViewModelBase<List<VariableMqttAliasItemViewModel>>
|
||||
{
|
||||
SelectedMqtt = selectedMqtt;
|
||||
Title=$"设置:{SelectedMqtt.ServerName}-MQTT服务器关联变量的别名";
|
||||
// VariablesToEdit = new ObservableCollection<VariableMqttAlias>(
|
||||
// selectedVariables.Select(v => new VariableMqttAlias(v, selectedMqtt))
|
||||
// );
|
||||
}
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<VariableMqttAliasItemViewModel> _variableMqttAliases = new();
|
||||
|
||||
public MqttAliasBatchEditDialogViewModel()
|
||||
{
|
||||
// For design time
|
||||
// VariablesToEdit = new ObservableCollection<VariableMqtt>();
|
||||
[ObservableProperty]
|
||||
private MqttServerItemViewModel _selectedMqttServer;
|
||||
|
||||
public MqttAliasBatchEditDialogViewModel(
|
||||
List<VariableItemViewModel> variables,
|
||||
MqttServerItemViewModel mqttServer)
|
||||
{
|
||||
_selectedMqttServer = mqttServer;
|
||||
InitializeVariableMqttAliases(variables);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化变量MQTT别名列表
|
||||
/// </summary>
|
||||
private void InitializeVariableMqttAliases(List<VariableItemViewModel> variables)
|
||||
{
|
||||
VariableMqttAliases.Clear();
|
||||
|
||||
foreach (var variable in variables)
|
||||
{
|
||||
// 检查该变量是否已经有针对此MQTT服务器的别名
|
||||
var existingAlias = variable.MqttAliases?.FirstOrDefault(ma => ma.MqttServerId == SelectedMqttServer.Id);
|
||||
|
||||
var variableMqttAlias = new VariableMqttAliasItemViewModel
|
||||
{
|
||||
VariableId = variable.Id,
|
||||
MqttServerId = SelectedMqttServer.Id,
|
||||
MqttServerName = SelectedMqttServer.ServerName,
|
||||
MqttServer = SelectedMqttServer,
|
||||
Alias = existingAlias?.Alias ?? GenerateDefaultAlias(variable)
|
||||
};
|
||||
|
||||
VariableMqttAliases.Add(variableMqttAlias);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成默认别名
|
||||
/// </summary>
|
||||
private string GenerateDefaultAlias(VariableItemViewModel variable)
|
||||
{
|
||||
// 可以根据需要自定义默认别名生成逻辑
|
||||
return $"{variable.Name}_{Guid.NewGuid().ToString("N")[..8]}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 确认编辑
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void Confirm()
|
||||
{
|
||||
var result = VariableMqttAliases.ToList();
|
||||
Close(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消编辑
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void Cancel()
|
||||
{
|
||||
Close(null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 全部应用相同的别名前缀
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void ApplySamePrefix(string prefix)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(prefix))
|
||||
return;
|
||||
|
||||
foreach (var alias in VariableMqttAliases)
|
||||
{
|
||||
alias.Alias = $"{prefix}_{alias.VariableId}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user