将所有的仓库异步方法都在名称后面添加Async
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PMSWPF.Data;
|
||||
using PMSWPF.Data.Repositories;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
@@ -12,6 +15,8 @@ public partial class DeviceDetailViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IDialogService _dialogService;
|
||||
private readonly VarTableRepository _varTableRepository;
|
||||
private readonly MenuRepository _menuRepository;
|
||||
private readonly DataServices _dataServices;
|
||||
|
||||
[ObservableProperty]
|
||||
private Device _currentDevice;
|
||||
@@ -19,10 +24,12 @@ public partial class DeviceDetailViewModel : ViewModelBase
|
||||
[ObservableProperty]
|
||||
private VariableTable _selectedVariableTable;
|
||||
|
||||
public DeviceDetailViewModel(IDialogService dialogService, VarTableRepository varTableRepository)
|
||||
public DeviceDetailViewModel(IDialogService dialogService, VarTableRepository varTableRepository, MenuRepository menuRepository, DataServices dataServices)
|
||||
{
|
||||
_dialogService = dialogService;
|
||||
_varTableRepository = varTableRepository;
|
||||
_menuRepository = menuRepository;
|
||||
_dataServices = dataServices;
|
||||
}
|
||||
|
||||
public override void OnLoaded()
|
||||
@@ -42,25 +49,55 @@ public partial class DeviceDetailViewModel : ViewModelBase
|
||||
[RelayCommand]
|
||||
private async Task AddVariableTable()
|
||||
{
|
||||
|
||||
using var db = DbContext.GetInstance();
|
||||
try
|
||||
{
|
||||
// 1. Show dialog to get new variable table details
|
||||
var newVarTable = await _dialogService.ShowAddVarTableDialog();
|
||||
if (newVarTable == null) return;
|
||||
if (newVarTable == null) return; // User cancelled
|
||||
|
||||
// 2. Set properties for the new variable table
|
||||
newVarTable.DeviceId = CurrentDevice.Id;
|
||||
newVarTable = await _varTableRepository.AddAsync(newVarTable);
|
||||
if (newVarTable != null)
|
||||
newVarTable.ProtocolType = CurrentDevice.ProtocolType;
|
||||
newVarTable.IsActive = true;
|
||||
|
||||
// 3. Find the parent menu for the current device
|
||||
var parentMenu = DataServicesHelper.FindMenusForDevice(CurrentDevice, _dataServices.MenuTrees);
|
||||
if (parentMenu == null)
|
||||
{
|
||||
CurrentDevice.VariableTables.Add(newVarTable);
|
||||
NotificationHelper.ShowSuccess($"变量表 {newVarTable.Name} 添加成功。");
|
||||
NotificationHelper.ShowError("无法找到当前设备的父级菜单,无法添加变量表菜单。");
|
||||
return;
|
||||
}
|
||||
else
|
||||
|
||||
// 4. Start database transaction
|
||||
await db.BeginTranAsync();
|
||||
|
||||
// 5. AddAsync variable table to the database
|
||||
var addedVarTable = await _varTableRepository.AddAsync(newVarTable, db);
|
||||
|
||||
// 6. Create and add the corresponding menu item
|
||||
var newMenu = new MenuBean
|
||||
{
|
||||
NotificationHelper.ShowError($"变量表 {newVarTable.Name} 添加失败。");
|
||||
}
|
||||
Name = addedVarTable.Name,
|
||||
DataId = addedVarTable.Id,
|
||||
Type = MenuType.VariableTableMenu,
|
||||
ParentId = parentMenu.Id,
|
||||
Icon = iNKORE.UI.WPF.Modern.Common.IconKeys.SegoeFluentIcons.Tablet.Glyph
|
||||
};
|
||||
await _menuRepository.AddAsync(newMenu, db);
|
||||
|
||||
// 7. Commit transaction
|
||||
await db.CommitTranAsync();
|
||||
|
||||
// 8. Update UI
|
||||
CurrentDevice.VariableTables.Add(addedVarTable);
|
||||
MessageHelper.SendLoadMessage(Enums.LoadTypes.Menu); // Refresh the main navigation menu
|
||||
NotificationHelper.ShowSuccess($"变量表 {addedVarTable.Name} 添加成功。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await db.RollbackTranAsync();
|
||||
NotificationHelper.ShowError($"添加变量表时发生错误: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
@@ -148,4 +185,14 @@ public partial class DeviceDetailViewModel : ViewModelBase
|
||||
NotificationHelper.ShowInfo("删除设备功能待实现。");
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void NavigateToVariableTable()
|
||||
{
|
||||
if (SelectedVariableTable == null) return;
|
||||
|
||||
var variableTableVm = App.Current.Services.GetRequiredService<VariableTableViewModel>();
|
||||
variableTableVm.VariableTable = SelectedVariableTable;
|
||||
MessageHelper.SendNavgatorMessage(variableTableVm);
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<Device> _devices;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 当前选中的设备。
|
||||
/// </summary>
|
||||
@@ -64,6 +65,37 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
if (_dataServices.Devices!=null && _dataServices.Devices.Count>0)
|
||||
{
|
||||
Devices=new ObservableCollection<Device>(_dataServices.Devices);
|
||||
foreach (var device in Devices)
|
||||
{
|
||||
device.OnDeviceIsActiveChanged += HandleDeviceIsActiveChanged;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override Task<bool> OnExitAsync()
|
||||
{
|
||||
if (_dataServices.Devices!=null && _dataServices.Devices.Count>0)
|
||||
{
|
||||
foreach (var device in Devices)
|
||||
{
|
||||
device.OnDeviceIsActiveChanged -= HandleDeviceIsActiveChanged;
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
private async void HandleDeviceIsActiveChanged(Device device, bool isActive)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _deviceRepository.UpdateAsync(device);
|
||||
NotificationHelper.ShowSuccess($"设备 {device.Name} 的激活状态已更新。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NotificationHelper.ShowError($"更新设备 {device.Name} 激活状态失败: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +119,7 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
if (device.ProtocolType == ProtocolType.OpcUA)
|
||||
device.OpcUaEndpointUrl = $"opc.tcp://{device.Ip}:{device.Prot}";
|
||||
|
||||
await _deviceRepository.Add(device);
|
||||
await _deviceRepository.AddAsync(device);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -116,7 +148,7 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
{
|
||||
|
||||
// 删除设备
|
||||
await _deviceRepository.Delete(SelectedDevice ,_dataServices.MenuTrees);
|
||||
await _deviceRepository.DeleteAsync(SelectedDevice ,_dataServices.MenuTrees);
|
||||
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Devices);
|
||||
@@ -147,10 +179,10 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
if (editDievce != null)
|
||||
{
|
||||
// 更新菜单
|
||||
var res = await _deviceRepository.Edit(editDievce);
|
||||
var res = await _deviceRepository.UpdateAsync(editDievce);
|
||||
var menu = DataServicesHelper.FindMenusForDevice(editDievce, _dataServices.MenuTrees);
|
||||
if (menu != null)
|
||||
await _menuRepository.Edit(menu);
|
||||
await _menuRepository.UpdateAsync(menu);
|
||||
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Devices);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using PMSWPF.Models;
|
||||
using S7.Net; // Add this using directive
|
||||
using S7.Net; // AddAsync this using directive
|
||||
|
||||
namespace PMSWPF.ViewModels.Dialogs;
|
||||
|
||||
@@ -25,7 +25,7 @@ public partial class DeviceDialogViewModel : ObservableObject
|
||||
_device = device;
|
||||
}
|
||||
|
||||
// Add a property to expose CpuType enum values for ComboBox
|
||||
// AddAsync a property to expose CpuType enum values for ComboBox
|
||||
public Array CpuTypes => Enum.GetValues(typeof(CpuType));
|
||||
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ public partial class MqttSelectionDialogViewModel : ObservableObject
|
||||
{
|
||||
try
|
||||
{
|
||||
var allMqtts = await _mqttRepository.GetAll();
|
||||
var allMqtts = await _mqttRepository.GetAllAsync();
|
||||
Mqtts = new ObservableCollection<Mqtt>(allMqtts);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -13,8 +13,8 @@ using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
using PMSWPF.Views;
|
||||
|
||||
// Add this using directive
|
||||
// Add this using directive
|
||||
// AddAsync this using directive
|
||||
// AddAsync this using directive
|
||||
|
||||
namespace PMSWPF.ViewModels;
|
||||
|
||||
@@ -126,7 +126,7 @@ public partial class MainViewModel : ViewModelBase
|
||||
// 假设 _varTableRepository.AddAsync 返回一个布尔值表示成功,或者一个表示 ID 的整数
|
||||
// 这里为了演示,我们假设它返回新添加的ID,如果失败则返回0
|
||||
await db.BeginTranAsync();
|
||||
var addVarTable = await _varTableRepository.Add(varTable, db);
|
||||
var addVarTable = await _varTableRepository.AddAsync(varTable, db);
|
||||
|
||||
// 5. 添加变量表菜单
|
||||
MenuBean newMenu = new MenuBean
|
||||
@@ -138,7 +138,7 @@ public partial class MainViewModel : ViewModelBase
|
||||
ParentId = menu.Parent.Id
|
||||
};
|
||||
|
||||
var addMenuRes = await _menuRepository.Add(newMenu, db);
|
||||
var addMenuRes = await _menuRepository.AddAsync(newMenu, db);
|
||||
if (addMenuRes > 0)
|
||||
{
|
||||
await db.CommitTranAsync();
|
||||
|
||||
@@ -132,7 +132,7 @@ namespace PMSWPF.ViewModels
|
||||
// 例如:var selectedVariables = await _dialogService.ShowVariableSelectionDialogAsync();
|
||||
// 这里只是一个占位符,实际需要一个UI来选择变量
|
||||
NotificationHelper.ShowInfo("添加变量功能待实现,需要一个变量选择对话框。");
|
||||
_logger.LogInformation("Add variables to MQTT server initiated.");
|
||||
_logger.LogInformation("AddAsync variables to MQTT server initiated.");
|
||||
|
||||
// 假设我们已经通过对话框获取到了一些要添加的变量
|
||||
// List<VariableData> newVariables = ...;
|
||||
@@ -141,9 +141,9 @@ namespace PMSWPF.ViewModels
|
||||
// if (variable.Mqtts == null) variable.Mqtts = new List<Mqtt>();
|
||||
// if (!variable.Mqtts.Any(m => m.Id == CurrentMqtt.Id))
|
||||
// {
|
||||
// variable.Mqtts.Add(CurrentMqtt);
|
||||
// variable.Mqtts.AddAsync(CurrentMqtt);
|
||||
// variable.IsModified = true; // 标记为已修改
|
||||
// AssociatedVariables.Add(variable);
|
||||
// AssociatedVariables.AddAsync(variable);
|
||||
// }
|
||||
// }
|
||||
// NotificationHelper.ShowMessage("变量添加成功,请记得保存更改。", NotificationType.Success);
|
||||
|
||||
@@ -84,7 +84,7 @@ public partial class MqttsViewModel : ViewModelBase
|
||||
{
|
||||
try
|
||||
{
|
||||
await _mqttRepository.Edit(mqtt);
|
||||
await _mqttRepository.UpdateAsync(mqtt);
|
||||
NotificationHelper.ShowSuccess($"MQTT: {mqtt.Name} 的启用状态已更新。");
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
|
||||
}
|
||||
@@ -108,7 +108,7 @@ public partial class MqttsViewModel : ViewModelBase
|
||||
return;
|
||||
}
|
||||
|
||||
await _mqttRepository.Add(mqtt);
|
||||
await _mqttRepository.AddAsync(mqtt);
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||
}
|
||||
@@ -133,7 +133,7 @@ public partial class MqttsViewModel : ViewModelBase
|
||||
var isDel = await _dialogService.ShowConfrimeDialog("删除MQTT", msg, "删除MQTT");
|
||||
if (isDel)
|
||||
{
|
||||
await _mqttRepository.Delete(SelectedMqtt);
|
||||
await _mqttRepository.DeleteAsync(SelectedMqtt);
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||
NotificationHelper.ShowSuccess($"删除MQTT成功,MQTT名:{SelectedMqtt.Name}");
|
||||
@@ -159,7 +159,7 @@ public partial class MqttsViewModel : ViewModelBase
|
||||
var editMqtt = await _dialogService.ShowEditMqttDialog(SelectedMqtt);
|
||||
if (editMqtt != null)
|
||||
{
|
||||
var res = await _mqttRepository.Edit(editMqtt);
|
||||
var res = await _mqttRepository.UpdateAsync(editMqtt);
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Mqtts);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,17 +14,11 @@ namespace PMSWPF.ViewModels;
|
||||
public partial class SettingViewModel : ViewModelBase
|
||||
{
|
||||
private ConnectionSettings _connectionSettings;
|
||||
private readonly S7BackgroundService _s7BackgroundService;
|
||||
private readonly MqttBackgroundService _mqttBackgroundService;
|
||||
private readonly OpcUaBackgroundService _opcUaBackgroundService;
|
||||
|
||||
public SettingViewModel(S7BackgroundService s7BackgroundService, MqttBackgroundService mqttBackgroundService, OpcUaBackgroundService opcUaBackgroundService)
|
||||
public SettingViewModel()
|
||||
{
|
||||
_connectionSettings = ConnectionSettings.Load();
|
||||
AvailableDbTypes = Enum.GetNames(typeof(SqlSugar.DbType)).ToList();
|
||||
_s7BackgroundService = s7BackgroundService;
|
||||
_mqttBackgroundService = mqttBackgroundService;
|
||||
_opcUaBackgroundService = opcUaBackgroundService;
|
||||
Themes = new List<string> { "浅色", "深色", "跟随系统" };
|
||||
}
|
||||
|
||||
@@ -131,72 +125,6 @@ public partial class SettingViewModel : ViewModelBase
|
||||
}
|
||||
}
|
||||
|
||||
public bool EnableS7Service
|
||||
{
|
||||
get => _connectionSettings.EnableS7Service;
|
||||
set
|
||||
{
|
||||
if (_connectionSettings.EnableS7Service != value)
|
||||
{
|
||||
_connectionSettings.EnableS7Service = value;
|
||||
OnPropertyChanged();
|
||||
_connectionSettings.Save();
|
||||
if (value)
|
||||
{
|
||||
// _s7BackgroundService.StartService();
|
||||
}
|
||||
else
|
||||
{
|
||||
// _s7BackgroundService.StopService();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool EnableMqttService
|
||||
{
|
||||
get => _connectionSettings.EnableMqttService;
|
||||
set
|
||||
{
|
||||
if (_connectionSettings.EnableMqttService != value)
|
||||
{
|
||||
_connectionSettings.EnableMqttService = value;
|
||||
OnPropertyChanged();
|
||||
_connectionSettings.Save();
|
||||
if (value)
|
||||
{
|
||||
_mqttBackgroundService.StartService();
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttBackgroundService.StopService();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool EnableOpcUaService
|
||||
{
|
||||
get => _connectionSettings.EnableOpcUaService;
|
||||
set
|
||||
{
|
||||
if (_connectionSettings.EnableOpcUaService != value)
|
||||
{
|
||||
_connectionSettings.EnableOpcUaService = value;
|
||||
OnPropertyChanged();
|
||||
_connectionSettings.Save();
|
||||
// if (value)
|
||||
// {
|
||||
// _opcUaBackgroundService.StartService();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// _opcUaBackgroundService.StopService();
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool MinimizeToTrayOnClose
|
||||
{
|
||||
get => _connectionSettings.MinimizeToTrayOnClose;
|
||||
|
||||
@@ -443,11 +443,11 @@ partial class VariableTableViewModel : ViewModelBase
|
||||
private async Task RefreshDataView()
|
||||
{
|
||||
// // 更新界面显示的数据:重新从数据库加载所有变量数据
|
||||
// VariableTable.DataVariables = await _varDataRepository.GetByVariableTableId(VariableTable.Id);
|
||||
// VariableTable.DataVariables = await _varDataRepository.GetByVariableTableIdAsync(VariableTable.Id);
|
||||
// DataVariables.Clear();
|
||||
// foreach (var item in VariableTable.DataVariables)
|
||||
// {
|
||||
// DataVariables.Add(item);
|
||||
// DataVariables.AddAsync(item);
|
||||
// }
|
||||
//
|
||||
// VariableDataView.Refresh();
|
||||
|
||||
Reference in New Issue
Block a user