2025-07-04 22:39:44 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-07-18 19:56:00 +08:00
|
|
|
|
using DMS.Data.Repositories;
|
2025-07-18 22:21:16 +08:00
|
|
|
|
using DMS.Core.Enums;
|
2025-07-18 19:56:00 +08:00
|
|
|
|
using DMS.Helper;
|
|
|
|
|
|
using DMS.Models;
|
|
|
|
|
|
using DMS.Services;
|
2025-07-04 22:39:44 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-07-18 19:56:00 +08:00
|
|
|
|
using DMS.Data;
|
|
|
|
|
|
using DMS.Views;
|
2025-07-04 22:39:44 +08:00
|
|
|
|
|
2025-07-18 19:56:00 +08:00
|
|
|
|
namespace DMS.ViewModels;
|
2025-07-04 22:39:44 +08:00
|
|
|
|
|
|
|
|
|
|
public partial class MqttsViewModel : ViewModelBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly DataServices _dataServices;
|
|
|
|
|
|
private readonly IDialogService _dialogService;
|
|
|
|
|
|
private readonly MqttRepository _mqttRepository;
|
|
|
|
|
|
private readonly ILogger<MqttsViewModel> _logger;
|
2025-07-05 22:57:54 +08:00
|
|
|
|
private readonly NavgatorServices _navgatorServices;
|
2025-07-15 17:13:27 +08:00
|
|
|
|
[ObservableProperty]
|
2025-07-04 22:39:44 +08:00
|
|
|
|
private ObservableCollection<Mqtt> _mqtts;
|
|
|
|
|
|
|
2025-07-15 17:13:27 +08:00
|
|
|
|
// 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;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
2025-07-08 12:52:33 +08:00
|
|
|
|
|
2025-07-04 22:39:44 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private Mqtt _selectedMqtt;
|
|
|
|
|
|
|
|
|
|
|
|
public MqttsViewModel(
|
2025-07-15 22:18:37 +08:00
|
|
|
|
ILogger<MqttsViewModel> logger, IDialogService dialogService, DataServices dataServices, NavgatorServices navgatorServices,MqttRepository mqttRepository
|
2025-07-04 22:39:44 +08:00
|
|
|
|
)
|
|
|
|
|
|
{
|
2025-07-15 22:18:37 +08:00
|
|
|
|
_mqttRepository = mqttRepository;
|
2025-07-04 22:39:44 +08:00
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_dialogService = dialogService;
|
|
|
|
|
|
_dataServices = dataServices;
|
2025-07-05 22:57:54 +08:00
|
|
|
|
_navgatorServices = navgatorServices;
|
2025-07-04 22:39:44 +08:00
|
|
|
|
|
2025-07-04 23:34:54 +08:00
|
|
|
|
if (dataServices.Mqtts == null || dataServices.Mqtts.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Mqtts = new ObservableCollection<Mqtt>(dataServices.Mqtts);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-13 16:22:07 +08:00
|
|
|
|
_dataServices.OnMqttListChanged += ( mqtts) =>
|
2025-07-04 23:34:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
Mqtts = new ObservableCollection<Mqtt>(mqtts);
|
|
|
|
|
|
};
|
2025-07-04 22:39:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-08 12:52:33 +08:00
|
|
|
|
private async void Mqtt_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (e.PropertyName == nameof(Mqtt.IsActive))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sender is Mqtt mqtt)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-16 19:37:13 +08:00
|
|
|
|
await _mqttRepository.UpdateAsync(mqtt);
|
2025-07-08 12:52:33 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"MQTT: {mqtt.Name} 的启用状态已更新。");
|
|
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError($"更新MQTT启用状态失败: {mqtt.Name} - {ex.Message}", ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-04 22:39:44 +08:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async void AddMqtt()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var mqtt = await _dialogService.ShowAddMqttDialog();
|
|
|
|
|
|
if (mqtt == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("用户取消了添加MQTT操作。");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-16 19:37:13 +08:00
|
|
|
|
await _mqttRepository.AddAsync(mqtt);
|
2025-07-05 18:15:21 +08:00
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
|
2025-07-05 21:49:41 +08:00
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
2025-07-04 22:39:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"添加MQTT的过程中发生错误:{e.Message}", e);
|
2025-07-04 22:39:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async void DeleteMqtt()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedMqtt == null)
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError("你没有选择任何MQTT,请选择MQTT后再点击删除");
|
2025-07-04 22:39:44 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string msg = $"确认要删除MQTT名为:{SelectedMqtt.Name}";
|
|
|
|
|
|
var isDel = await _dialogService.ShowConfrimeDialog("删除MQTT", msg, "删除MQTT");
|
|
|
|
|
|
if (isDel)
|
|
|
|
|
|
{
|
2025-07-16 19:37:13 +08:00
|
|
|
|
await _mqttRepository.DeleteAsync(SelectedMqtt);
|
2025-07-04 22:39:44 +08:00
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
|
2025-07-05 21:49:41 +08:00
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"删除MQTT成功,MQTT名:{SelectedMqtt.Name}");
|
2025-07-04 22:39:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"删除MQTT的过程中发生错误:{e.Message}", e);
|
2025-07-04 22:39:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async void EditMqtt()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedMqtt == null)
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError("你没有选择任何MQTT,请选择MQTT后再点击编辑");
|
2025-07-04 22:39:44 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var editMqtt = await _dialogService.ShowEditMqttDialog(SelectedMqtt);
|
|
|
|
|
|
if (editMqtt != null)
|
|
|
|
|
|
{
|
2025-07-16 19:37:13 +08:00
|
|
|
|
var res = await _mqttRepository.UpdateAsync(editMqtt);
|
2025-07-04 22:39:44 +08:00
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"编辑MQTT的过程中发生错误:{e.Message}", e);
|
2025-07-04 22:39:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-05 22:57:54 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 导航到MQTT服务器详情页面。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private void NavigateToMqttDetail()
|
|
|
|
|
|
{
|
|
|
|
|
|
// if (SelectedMqtt == null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// NotificationHelper.ShowMessage("请选择一个MQTT服务器以查看详情。", NotificationType.Warning);
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// _navgatorServices.NavigateTo<MqttServerDetailView>(SelectedMqtt);
|
|
|
|
|
|
}
|
2025-07-04 22:39:44 +08:00
|
|
|
|
}
|