2025-06-28 19:32:51 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
2025-06-30 14:19:22 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using PMSWPF.Data.Repositories;
|
|
|
|
|
|
using PMSWPF.Enums;
|
|
|
|
|
|
using PMSWPF.Helper;
|
|
|
|
|
|
using PMSWPF.Message;
|
|
|
|
|
|
using PMSWPF.Models;
|
2025-06-30 14:19:22 +08:00
|
|
|
|
using PMSWPF.ViewModels;
|
2025-07-01 21:34:20 +08:00
|
|
|
|
using SqlSugar;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
|
|
|
|
|
|
namespace PMSWPF.Services;
|
|
|
|
|
|
|
2025-06-30 13:06:51 +08:00
|
|
|
|
public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
2025-06-28 19:32:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<DataServices> _logger;
|
2025-06-30 22:03:49 +08:00
|
|
|
|
[ObservableProperty] private List<Device> _devices;
|
|
|
|
|
|
[ObservableProperty] private List<VariableTable> _variableTables;
|
2025-07-01 21:34:20 +08:00
|
|
|
|
[ObservableProperty] private List<MenuBean> menuTrees;
|
2025-07-04 22:39:44 +08:00
|
|
|
|
[ObservableProperty] private List<Mqtt> _mqtts;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
private readonly DeviceRepository _deviceRepository;
|
|
|
|
|
|
private readonly MenuRepository _menuRepository;
|
2025-07-04 22:39:44 +08:00
|
|
|
|
private readonly MqttRepository _mqttRepository;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
|
2025-07-02 12:01:20 +08:00
|
|
|
|
|
2025-06-28 19:32:51 +08:00
|
|
|
|
public event Action<List<Device>> OnDeviceListChanged;
|
2025-07-01 21:34:20 +08:00
|
|
|
|
public event Action<List<MenuBean>> OnMenuTreeListChanged;
|
2025-07-04 22:39:44 +08:00
|
|
|
|
public event Action<List<Mqtt>> OnMqttListChanged;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-30 13:06:51 +08:00
|
|
|
|
partial void OnDevicesChanged(List<Device> devices)
|
2025-06-28 19:32:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
OnDeviceListChanged?.Invoke(devices);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
|
partial void OnMenuTreesChanged(List<MenuBean> MenuTrees)
|
2025-06-28 19:32:51 +08:00
|
|
|
|
{
|
2025-07-01 21:34:20 +08:00
|
|
|
|
OnMenuTreeListChanged?.Invoke(MenuTrees);
|
2025-06-28 19:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-04 22:39:44 +08:00
|
|
|
|
partial void OnMqttsChanged(List<Mqtt> mqtts)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnMqttListChanged?.Invoke(mqtts);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-30 13:06:51 +08:00
|
|
|
|
|
|
|
|
|
|
public DataServices(ILogger<DataServices> logger)
|
2025-06-28 19:32:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
IsActive = true;
|
2025-06-30 13:06:51 +08:00
|
|
|
|
_deviceRepository = new DeviceRepository();
|
|
|
|
|
|
_menuRepository = new MenuRepository();
|
2025-07-04 22:39:44 +08:00
|
|
|
|
_mqttRepository = new MqttRepository();
|
2025-06-28 19:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-01 11:11:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 接受加载消息,收到消息后从数据库加载对应的数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="message">消息的类型,如加载菜单LoadMessage.Menu</param>
|
|
|
|
|
|
/// <exception cref="ArgumentException"></exception>
|
2025-06-30 13:06:51 +08:00
|
|
|
|
public async void Receive(LoadMessage message)
|
|
|
|
|
|
{
|
2025-06-28 19:32:51 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-02 12:01:20 +08:00
|
|
|
|
switch (message.LoadType)
|
2025-06-28 19:32:51 +08:00
|
|
|
|
{
|
2025-06-30 13:06:51 +08:00
|
|
|
|
case LoadTypes.All:
|
2025-07-02 12:01:20 +08:00
|
|
|
|
await LoadDevices();
|
2025-06-30 22:03:49 +08:00
|
|
|
|
await LoadMenus();
|
2025-07-04 22:39:44 +08:00
|
|
|
|
await LoadMqtts();
|
2025-06-30 22:03:49 +08:00
|
|
|
|
break;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
case LoadTypes.Devices:
|
2025-07-02 12:01:20 +08:00
|
|
|
|
await LoadDevices();
|
2025-06-28 19:32:51 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case LoadTypes.Menu:
|
2025-06-30 22:03:49 +08:00
|
|
|
|
await LoadMenus();
|
2025-06-28 19:32:51 +08:00
|
|
|
|
break;
|
2025-07-04 22:39:44 +08:00
|
|
|
|
case LoadTypes.Mqtts:
|
|
|
|
|
|
await LoadMqtts();
|
|
|
|
|
|
break;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}");
|
2025-06-30 22:03:49 +08:00
|
|
|
|
_logger.LogError($"加载数据出现了错误:{e}");
|
2025-06-28 19:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-30 13:06:51 +08:00
|
|
|
|
|
2025-07-02 12:01:20 +08:00
|
|
|
|
private async Task LoadDevices()
|
|
|
|
|
|
{
|
|
|
|
|
|
Devices = await _deviceRepository.GetAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-30 22:03:49 +08:00
|
|
|
|
private async Task LoadMenus()
|
|
|
|
|
|
{
|
2025-07-01 21:34:20 +08:00
|
|
|
|
MenuTrees = await _menuRepository.GetMenuTrees();
|
|
|
|
|
|
foreach (MenuBean menu in MenuTrees)
|
2025-06-30 22:03:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
MenuHelper.MenuAddParent(menu);
|
2025-07-02 12:01:20 +08:00
|
|
|
|
DataServicesHelper.SortMenus(menu);
|
2025-06-30 22:03:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-04 22:39:44 +08:00
|
|
|
|
|
|
|
|
|
|
private async Task LoadMqtts()
|
|
|
|
|
|
{
|
|
|
|
|
|
Mqtts = await _mqttRepository.GetAll();
|
|
|
|
|
|
}
|
2025-06-28 19:32:51 +08:00
|
|
|
|
}
|