Files
DMS/DMS.WPF/ViewModels/MqttsViewModel.cs

182 lines
5.7 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Core.Enums;
using DMS.Helper;
using DMS.Services;
2025-07-26 10:05:43 +08:00
using DMS.WPF.Helper;
using DMS.WPF.Services;
using DMS.WPF.ViewModels.Items;
using Microsoft.Extensions.Logging;
2025-07-19 11:11:01 +08:00
using DMS.WPF.Views;
2025-07-19 11:11:01 +08:00
namespace DMS.WPF.ViewModels;
public partial class MqttsViewModel : ViewModelBase
{
private readonly DataServices _dataServices;
private readonly IDialogService _dialogService;
private readonly ILogger<MqttsViewModel> _logger;
private readonly NavgatorServices _navgatorServices;
[ObservableProperty]
2025-07-26 10:05:43 +08:00
private ObservableCollection<MqttServerItemViewModel> _mqtts;
// public ObservableCollection<Mqtt> Mqtts
// {
// get => _mqtts;
// set
// {
// if (_mqtts != null)
// {
// foreach (var mqtt in _mqtts)
// {
// mqtt.PropertyChanged -= Mqtt_PropertyChanged;
// }
// }
//
// SetProperty(ref _mqtts, value);
//
// if (_mqtts != null)
// {
// foreach (var mqtt in _mqtts)
// {
// mqtt.PropertyChanged += Mqtt_PropertyChanged;
// }
// }
// }
// }
[ObservableProperty]
2025-07-26 10:05:43 +08:00
private MqttServerItemViewModel _selectedMqtt;
public MqttsViewModel(
2025-07-26 18:58:52 +08:00
ILogger<MqttsViewModel> logger, IDialogService dialogService, DataServices dataServices
)
{
_logger = logger;
_dialogService = dialogService;
_dataServices = dataServices;
2025-08-24 14:42:31 +08:00
// if (_dataServices.Mqtts == null || _dataServices.Mqtts.Count == 0)
2025-07-26 10:05:43 +08:00
// {
// MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
// }
// else
// {
2025-08-24 14:42:31 +08:00
// Mqtts = new ObservableCollection<Mqtt>(_dataServices.Mqtts);
2025-07-26 10:05:43 +08:00
// }
//
//
// _dataServices.OnMqttListChanged += ( mqtts) =>
// {
// Mqtts = new ObservableCollection<Mqtt>(mqtts);
// };
}
private async void Mqtt_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
2025-07-26 10:05:43 +08:00
// if (e.PropertyName == nameof(Mqtt.IsActive))
// {
// if (sender is Mqtt mqtt)
// {
// try
// {
// await _mqttRepository.UpdateAsync(mqtt);
// NotificationHelper.ShowSuccess($"MQTT: {mqtt.Name} 的启用状态已更新。");
// MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
// }
// catch (Exception ex)
// {
// NotificationHelper.ShowError($"更新MQTT启用状态失败: {mqtt.Name} - {ex.Message}", ex);
// }
// }
// }
}
[RelayCommand]
public async void AddMqtt()
{
2025-07-26 10:05:43 +08:00
// try
// {
// var mqtt = await _dialogService.ShowAddMqttDialog();
// if (mqtt == null)
// {
// _logger.LogInformation("用户取消了添加MQTT操作。");
// return;
// }
//
// await _mqttRepository.AddAsync(mqtt);
// MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
// MessageHelper.SendLoadMessage(LoadTypes.Menu);
// }
// catch (Exception e)
// {
// NotificationHelper.ShowError($"添加MQTT的过程中发生错误{e.Message}", e);
// }
}
[RelayCommand]
public async void DeleteMqtt()
{
2025-07-26 10:05:43 +08:00
// try
// {
// if (SelectedMqtt == null)
// {
// NotificationHelper.ShowError("你没有选择任何MQTT请选择MQTT后再点击删除");
// return;
// }
//
// string msg = $"确认要删除MQTT名为:{SelectedMqtt.Name}";
// var isDel = await _dialogService.ShowConfrimeDialog("删除MQTT", msg, "删除MQTT");
// if (isDel)
// {
// await _mqttRepository.DeleteAsync(SelectedMqtt);
// MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
// MessageHelper.SendLoadMessage(LoadTypes.Menu);
// NotificationHelper.ShowSuccess($"删除MQTT成功,MQTT名{SelectedMqtt.Name}");
// }
// }
// catch (Exception e)
// {
// NotificationHelper.ShowError($"删除MQTT的过程中发生错误{e.Message}", e);
// }
}
[RelayCommand]
public async void EditMqtt()
{
2025-07-26 10:05:43 +08:00
// try
// {
// if (SelectedMqtt == null)
// {
// NotificationHelper.ShowError("你没有选择任何MQTT请选择MQTT后再点击编辑");
// return;
// }
//
// var editMqtt = await _dialogService.ShowEditMqttDialog(SelectedMqtt);
// if (editMqtt != null)
// {
// var res = await _mqttRepository.UpdateAsync(editMqtt);
// MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
// }
// }
// catch (Exception e)
// {
// NotificationHelper.ShowError($"编辑MQTT的过程中发生错误{e.Message}", e);
// }
}
/// <summary>
/// 导航到MQTT服务器详情页面。
/// </summary>
[RelayCommand]
private void NavigateToMqttDetail()
{
// if (SelectedMqtt == null)
// {
// NotificationHelper.ShowMessage("请选择一个MQTT服务器以查看详情。", NotificationType.Warning);
// return;
// }
// _navgatorServices.NavigateTo<MqttServerDetailView>(SelectedMqtt);
}
}