diff --git a/Data/Repositories/MqttRepository.cs b/Data/Repositories/MqttRepository.cs
index 04e2bc5..0cee916 100644
--- a/Data/Repositories/MqttRepository.cs
+++ b/Data/Repositories/MqttRepository.cs
@@ -3,6 +3,8 @@ using System.Diagnostics;
using System.Threading.Tasks;
using NLog;
using PMSWPF.Data.Entities;
+using PMSWPF.Models;
+using PMSWPF.Extensions;
namespace PMSWPF.Data.Repositories;
@@ -18,7 +20,7 @@ public class MqttRepository
///
/// 主键ID
///
- public async Task GetByIdAsync(int id)
+ public async Task GetById(int id)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
@@ -27,7 +29,7 @@ public class MqttRepository
var result = await _db.Queryable().In(id).SingleAsync();
stopwatch.Stop();
Logger.Info($"根据ID '{id}' 获取Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms");
- return result;
+ return result.CopyTo();
}
}
@@ -35,7 +37,7 @@ public class MqttRepository
/// 获取所有Mqtt配置
///
///
- public async Task> GetAllAsync()
+ public async Task> GetAll()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
@@ -44,7 +46,7 @@ public class MqttRepository
var result = await _db.Queryable().ToListAsync();
stopwatch.Stop();
Logger.Info($"获取所有Mqtt配置耗时:{stopwatch.ElapsedMilliseconds}ms");
- return result;
+ return result.CopyTo>();
}
}
@@ -53,13 +55,13 @@ public class MqttRepository
///
/// Mqtt实体
///
- public async Task AddAsync(DbMqtt mqtt)
+ public async Task Add(Mqtt mqtt)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
- var result = await _db.Insertable(mqtt).ExecuteReturnIdentityAsync();
+ var result = await _db.Insertable(mqtt.CopyTo()).ExecuteReturnIdentityAsync();
stopwatch.Stop();
Logger.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
@@ -71,13 +73,13 @@ public class MqttRepository
///
/// Mqtt实体
///
- public async Task UpdateAsync(DbMqtt mqtt)
+ public async Task Edit(Mqtt mqtt)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
- var result = await _db.Updateable(mqtt).ExecuteCommandAsync();
+ var result = await _db.Updateable(mqtt.CopyTo()).ExecuteCommandAsync();
stopwatch.Stop();
Logger.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
@@ -87,17 +89,17 @@ public class MqttRepository
///
/// 根据ID删除Mqtt配置
///
- /// 主键ID
+ /// Mqtt实体
///
- public async Task DeleteAsync(int id)
+ public async Task Delete(Mqtt mqtt)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
- var result = await _db.Deleteable().In(id).ExecuteCommandAsync();
+ var result = await _db.Deleteable().In(mqtt.Id).ExecuteCommandAsync();
stopwatch.Stop();
- Logger.Info($"删除Mqtt配置ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
+ Logger.Info($"删除Mqtt配置ID '{mqtt.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
diff --git a/Enums/LoadTypes.cs b/Enums/LoadTypes.cs
index f5078b0..479b264 100644
--- a/Enums/LoadTypes.cs
+++ b/Enums/LoadTypes.cs
@@ -4,6 +4,6 @@ public enum LoadTypes
{
Devices,
Menu,
- Mqtt,
+ Mqtts,
All
}
\ No newline at end of file
diff --git a/PMSWPF.csproj b/PMSWPF.csproj
index 700607c..8e7d34a 100644
--- a/PMSWPF.csproj
+++ b/PMSWPF.csproj
@@ -34,6 +34,20 @@
+
+
+ MSBuild:Compile
+
+
+ MqttsView.xaml
+
+
+ MSBuild:Compile
+
+
+ MqttDialog.xaml
+
+
diff --git a/Services/DataServices.cs b/Services/DataServices.cs
index 8f59e7b..859448c 100644
--- a/Services/DataServices.cs
+++ b/Services/DataServices.cs
@@ -18,12 +18,15 @@ public partial class DataServices : ObservableRecipient, IRecipient
[ObservableProperty] private List _devices;
[ObservableProperty] private List _variableTables;
[ObservableProperty] private List menuTrees;
+ [ObservableProperty] private List _mqtts;
private readonly DeviceRepository _deviceRepository;
private readonly MenuRepository _menuRepository;
+ private readonly MqttRepository _mqttRepository;
public event Action> OnDeviceListChanged;
public event Action> OnMenuTreeListChanged;
+ public event Action> OnMqttListChanged;
partial void OnDevicesChanged(List devices)
@@ -36,6 +39,11 @@ public partial class DataServices : ObservableRecipient, IRecipient
OnMenuTreeListChanged?.Invoke(MenuTrees);
}
+ partial void OnMqttsChanged(List mqtts)
+ {
+ OnMqttListChanged?.Invoke(mqtts);
+ }
+
public DataServices(ILogger logger)
{
@@ -43,6 +51,7 @@ public partial class DataServices : ObservableRecipient, IRecipient
IsActive = true;
_deviceRepository = new DeviceRepository();
_menuRepository = new MenuRepository();
+ _mqttRepository = new MqttRepository();
}
@@ -60,6 +69,7 @@ public partial class DataServices : ObservableRecipient, IRecipient
case LoadTypes.All:
await LoadDevices();
await LoadMenus();
+ await LoadMqtts();
break;
case LoadTypes.Devices:
await LoadDevices();
@@ -67,6 +77,9 @@ public partial class DataServices : ObservableRecipient, IRecipient
case LoadTypes.Menu:
await LoadMenus();
break;
+ case LoadTypes.Mqtts:
+ await LoadMqtts();
+ break;
}
}
catch (Exception e)
@@ -90,4 +103,9 @@ public partial class DataServices : ObservableRecipient, IRecipient
DataServicesHelper.SortMenus(menu);
}
}
+
+ private async Task LoadMqtts()
+ {
+ Mqtts = await _mqttRepository.GetAll();
+ }
}
\ No newline at end of file
diff --git a/Services/DialogService.cs b/Services/DialogService.cs
index 2bd03e3..45b2abc 100644
--- a/Services/DialogService.cs
+++ b/Services/DialogService.cs
@@ -43,6 +43,34 @@ public class DialogService :IDialogService
}
+ public async Task ShowAddMqttDialog()
+ {
+ var mqtt = new Mqtt();
+ MqttDialogViewModel vm = new MqttDialogViewModel(mqtt);
+ vm.Title = "添加MQTT";
+ vm.PrimaryButContent = "添加MQTT";
+ return await ShowConentDialog(vm, mqtt);
+ }
+
+ public async Task ShowEditMqttDialog(Mqtt mqtt)
+ {
+ MqttDialogViewModel vm = new MqttDialogViewModel(mqtt);
+ vm.Title = "编辑MQTT";
+ vm.PrimaryButContent = "编辑MQTT";
+ return await ShowConentDialog(vm, mqtt);
+ }
+
+ private static async Task ShowConentDialog(MqttDialogViewModel viewModel, Mqtt mqtt)
+ {
+ var dialog = new MqttDialog(viewModel);
+ var res = await dialog.ShowAsync();
+ if (res == ContentDialogResult.Primary)
+ {
+ return mqtt;
+ }
+ return null;
+ }
+
public async Task ShowConfrimeDialog(string title, string message,string buttonText="确认")
{
ConfrimDialogViewModel vm = new ConfrimDialogViewModel();
diff --git a/Services/IDialogService.cs b/Services/IDialogService.cs
index 836651f..71ec6fb 100644
--- a/Services/IDialogService.cs
+++ b/Services/IDialogService.cs
@@ -7,7 +7,8 @@ public interface IDialogService
{
Task ShowAddDeviceDialog();
Task ShowEditDeviceDialog(Device device);
-
+ Task ShowAddMqttDialog();
+ Task ShowEditMqttDialog(Mqtt mqtt);
Task ShowConfrimeDialog(string title, string message,string buttonText="确认");
Task ShowAddVarTableDialog();
diff --git a/ViewModels/Dialogs/MqttDialogViewModel.cs b/ViewModels/Dialogs/MqttDialogViewModel.cs
new file mode 100644
index 0000000..4ab5369
--- /dev/null
+++ b/ViewModels/Dialogs/MqttDialogViewModel.cs
@@ -0,0 +1,26 @@
+using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Input;
+using PMSWPF.Models;
+
+namespace PMSWPF.ViewModels.Dialogs;
+
+public partial class MqttDialogViewModel : ObservableObject
+{
+ [ObservableProperty]
+ private Mqtt _mqtt;
+
+ [ObservableProperty] private string title ;
+ [ObservableProperty] private string primaryButContent ;
+
+ public MqttDialogViewModel(Mqtt mqtt)
+ {
+ _mqtt = mqtt;
+ }
+
+
+ [RelayCommand]
+ public void AddMqtt()
+ {
+
+ }
+}
\ No newline at end of file
diff --git a/ViewModels/MqttsViewModel.cs b/ViewModels/MqttsViewModel.cs
new file mode 100644
index 0000000..0500468
--- /dev/null
+++ b/ViewModels/MqttsViewModel.cs
@@ -0,0 +1,113 @@
+using System.Collections.ObjectModel;
+using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Input;
+using Microsoft.Extensions.Logging;
+using PMSWPF.Data;
+using PMSWPF.Data.Repositories;
+using PMSWPF.Enums;
+using PMSWPF.Helper;
+using PMSWPF.Models;
+using PMSWPF.Services;
+
+namespace PMSWPF.ViewModels;
+
+public partial class MqttsViewModel : ViewModelBase
+{
+ private readonly DataServices _dataServices;
+ private readonly IDialogService _dialogService;
+ private readonly MqttRepository _mqttRepository;
+ private readonly ILogger _logger;
+
+ [ObservableProperty]
+ private ObservableCollection _mqtts;
+
+ [ObservableProperty]
+ private Mqtt _selectedMqtt;
+
+ public MqttsViewModel(
+ ILogger logger, IDialogService dialogService, DataServices dataServices
+ )
+ {
+ _mqttRepository = new MqttRepository();
+ _logger = logger;
+ _dialogService = dialogService;
+ _dataServices = dataServices;
+
+ MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
+ _dataServices.OnMqttListChanged += (mqtts) => { Mqtts = new ObservableCollection(mqtts); };
+ }
+
+ [RelayCommand]
+ public async void AddMqtt()
+ {
+ try
+ {
+ var mqtt = await _dialogService.ShowAddMqttDialog();
+ if (mqtt == null)
+ {
+ _logger.LogInformation("用户取消了添加MQTT操作。");
+ return;
+ }
+
+ await _mqttRepository.Add(mqtt);
+ }
+ catch (Exception e)
+ {
+ NotificationHelper.ShowMessage($"添加MQTT的过程中发生错误:{e.Message}", NotificationType.Error);
+ _logger.LogError($"添加MQTT的过程中发生错误:{e}");
+ }
+ }
+
+ [RelayCommand]
+ public async void DeleteMqtt()
+ {
+ try
+ {
+ if (SelectedMqtt == null)
+ {
+ NotificationHelper.ShowMessage("你没有选择任何MQTT,请选择MQTT后再点击删除", NotificationType.Error);
+ return;
+ }
+
+ string msg = $"确认要删除MQTT名为:{SelectedMqtt.Name}";
+ var isDel = await _dialogService.ShowConfrimeDialog("删除MQTT", msg, "删除MQTT");
+ if (isDel)
+ {
+ await _mqttRepository.Delete(SelectedMqtt);
+
+ MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
+ NotificationHelper.ShowMessage($"删除MQTT成功,MQTT名:{SelectedMqtt.Name}", NotificationType.Success);
+ }
+ }
+ catch (Exception e)
+ {
+ NotificationHelper.ShowMessage($"删除MQTT的过程中发生错误:{e.Message}", NotificationType.Error);
+ _logger.LogError($"删除MQTT的过程中发生错误:{e}");
+ }
+ }
+
+ [RelayCommand]
+ public async void EditMqtt()
+ {
+ try
+ {
+ if (SelectedMqtt == null)
+ {
+ NotificationHelper.ShowMessage("你没有选择任何MQTT,请选择MQTT后再点击编辑", NotificationType.Error);
+ return;
+ }
+
+ var editMqtt = await _dialogService.ShowEditMqttDialog(SelectedMqtt);
+ if (editMqtt != null)
+ {
+ var res = await _mqttRepository.Edit(editMqtt);
+ MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
+ }
+ }
+ catch (Exception e)
+ {
+ NotificationHelper.ShowMessage($"编辑MQTT的过程中发生错误:{e.Message}", NotificationType.Error);
+ _logger.LogError($"编辑MQTT的过程中发生错误:{e}");
+ }
+ }
+}
\ No newline at end of file
diff --git a/Views/Dialogs/MqttDialog.xaml b/Views/Dialogs/MqttDialog.xaml
new file mode 100644
index 0000000..4def479
--- /dev/null
+++ b/Views/Dialogs/MqttDialog.xaml
@@ -0,0 +1,74 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Views/Dialogs/MqttDialog.xaml.cs b/Views/Dialogs/MqttDialog.xaml.cs
new file mode 100644
index 0000000..9f0a740
--- /dev/null
+++ b/Views/Dialogs/MqttDialog.xaml.cs
@@ -0,0 +1,15 @@
+using System.Windows.Controls;
+using iNKORE.UI.WPF.Modern.Controls;
+
+namespace PMSWPF.Views.Dialogs;
+
+using PMSWPF.ViewModels.Dialogs;
+
+public partial class MqttDialog : ContentDialog
+{
+ public MqttDialog(MqttDialogViewModel viewModel)
+ {
+ InitializeComponent();
+ DataContext = viewModel;
+ }
+}
\ No newline at end of file
diff --git a/Views/MqttsView.xaml b/Views/MqttsView.xaml
new file mode 100644
index 0000000..89e1f1b
--- /dev/null
+++ b/Views/MqttsView.xaml
@@ -0,0 +1,136 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Views/MqttsView.xaml.cs b/Views/MqttsView.xaml.cs
new file mode 100644
index 0000000..531ef37
--- /dev/null
+++ b/Views/MqttsView.xaml.cs
@@ -0,0 +1,11 @@
+using System.Windows.Controls;
+
+namespace PMSWPF.Views;
+
+public partial class MqttsView : UserControl
+{
+ public MqttsView()
+ {
+ InitializeComponent();
+ }
+}
\ No newline at end of file