将所有的仓库异步方法都在名称后面添加Async

This commit is contained in:
2025-07-16 19:37:13 +08:00
parent f24769e94c
commit 9fb9e53331
17 changed files with 140 additions and 138 deletions

View File

@@ -150,7 +150,7 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
}
}
Devices = await _deviceRepository.GetAll();
Devices = await _deviceRepository.GetAllAsync();
// 订阅新设备的属性变更事件
if (Devices != null)
@@ -189,7 +189,7 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
/// <returns>表示异步操作的任务。</returns>
private async Task LoadMenus()
{
MenuTrees = await _menuRepository.GetMenuTrees();
MenuTrees = await _menuRepository.GetMenuTreesAsync();
foreach (MenuBean menu in MenuTrees)
{
MenuHelper.MenuAddParent(menu); // 为菜单添加父级引用
@@ -205,7 +205,7 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
/// <returns>包含所有MQTT配置的列表。</returns>
public async Task<List<Mqtt>> GetMqttsAsync()
{
var mqtts = await _mqttRepository.GetAll();
var mqtts = await _mqttRepository.GetAllAsync();
OnMqttListChanged?.Invoke(mqtts);
return mqtts;
}
@@ -216,7 +216,7 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
/// <returns>表示异步操作的任务。</returns>
private async Task LoadMqtts()
{
Mqtts = await _mqttRepository.GetAll();
Mqtts = await _mqttRepository.GetAllAsync();
}
@@ -227,7 +227,7 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
/// <returns>设备对象如果不存在则为null。</returns>
public async Task<Device> GetDeviceByIdAsync(int id)
{
return await _deviceRepository.GetById(id);
return await _deviceRepository.GetByIdAsync(id);
}
/// <summary>

View File

@@ -4,6 +4,7 @@ using MQTTnet.Client;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Options;
using PMSWPF.Data.Repositories;
using PMSWPF.Helper;
using PMSWPF.Models;
@@ -16,6 +17,7 @@ namespace PMSWPF.Services
{
// 数据服务实例用于访问和操作应用程序数据如MQTT配置和变量数据。
private readonly DataServices _dataServices;
private readonly MqttRepository _mqttRepository;
// 存储MQTT客户端实例的字典键为MQTT配置ID值为IMqttClient对象。
private readonly Dictionary<int, IMqttClient> _mqttClients;
@@ -38,9 +40,10 @@ namespace PMSWPF.Services
/// 构造函数注入DataServices。
/// </summary>
/// <param name="dataServices">数据服务实例。</param>
public MqttBackgroundService(DataServices dataServices)
public MqttBackgroundService(DataServices dataServices, MqttRepository mqttRepository)
{
_dataServices = dataServices;
_mqttRepository = mqttRepository;
_mqttClients = new Dictionary<int, IMqttClient>();
_mqttConfigDic = new Dictionary<int, Mqtt>();
_reconnectAttempts = new Dictionary<int, int>();
@@ -132,7 +135,7 @@ namespace PMSWPF.Services
}
catch (Exception e)
{
NlogHelper.Error($"MqttID:{mqttId},断开连接的过程中发生了错误:{e.Message}",e);
NlogHelper.Error($"MqttID:{mqttId},断开连接的过程中发生了错误:{e.Message}", e);
}
}
}
@@ -195,6 +198,9 @@ namespace PMSWPF.Services
_mqttClients.Remove(mqtt.Id);
NlogHelper.Info($"{mqtt.Name}的客户端,与服务器断开连接.");
}
await _mqttRepository.UpdateAsync(mqtt);
NotificationHelper.ShowSuccess($"Mqtt客户端{mqtt.Name},激活状态修改成功。");
}
catch (Exception e)
{