修复了添加设备不更新的问题和一些已知的问题

This commit is contained in:
2025-06-30 22:03:49 +08:00
parent a1de03a94e
commit 5e0c530187
9 changed files with 171 additions and 136 deletions

View File

@@ -15,9 +15,9 @@ namespace PMSWPF.Services;
public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
{
private readonly ILogger<DataServices> _logger;
[ObservableProperty] private List<Device> _devices = new List<Device>();
[ObservableProperty] private List<VariableTable> _variableTables = new ();
[ObservableProperty] private List<MenuBean> menuBeans = new List<MenuBean>();
[ObservableProperty] private List<Device> _devices;
[ObservableProperty] private List<VariableTable> _variableTables;
[ObservableProperty] private List<MenuBean> menuBeans;
private readonly DeviceRepository _deviceRepository;
private readonly MenuRepository _menuRepository;
@@ -28,12 +28,21 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
partial void OnDevicesChanged(List<Device> devices)
{
OnDeviceListChanged?.Invoke(devices);
FillMenuData(MenuBeans);
if (menuBeans!=null && Devices!=null)
{
FillMenuData(MenuBeans,Devices);
}
}
partial void OnMenuBeansChanged(List<MenuBean> menuBeans)
{
OnMenuListChanged?.Invoke(menuBeans);
if (MenuBeans!=null && Devices!=null)
{
FillMenuData(MenuBeans,Devices);
}
}
@@ -43,43 +52,42 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
IsActive = true;
_deviceRepository = new DeviceRepository();
_menuRepository = new MenuRepository();
}
/// <summary>
/// 给Menu菜单的Data填充数据
/// </summary>
/// <param name="menuBeans"></param>
private void FillMenuData(List<MenuBean> menuBeans)
private void FillMenuData(List<MenuBean> menuBeans,List<Device> devices)
{
if (menuBeans == null || menuBeans.Count == 0)
return;
foreach (MenuBean menuBean in menuBeans)
{
switch (menuBean.Type)
{
{
case MenuType.MainMenu:
menuBean.ViewModel= GetMainViewModel(menuBean.Name);
menuBean.ViewModel = GetMainViewModel(menuBean.Name);
break;
case MenuType.DeviceMenu:
menuBean.ViewModel = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
menuBean.Data= Devices.FirstOrDefault(d => d.Id == menuBean.DataId);
menuBean.Data = devices.FirstOrDefault(d => d.Id == menuBean.DataId);
break;
case MenuType.VariableTableMenu:
var varTableVM = App.Current.Services.GetRequiredService<VariableTableViewModel>();
varTableVM.VariableTable = FindVarTableForDevice(menuBean.DataId);
menuBean.ViewModel = varTableVM;
menuBean.Data = varTableVM.VariableTable;
break;
case MenuType.AddVariableTableMenu:
break;
}
if (menuBean.Items!=null && menuBean.Items.Count>0)
if (menuBean.Items != null && menuBean.Items.Count > 0)
{
FillMenuData(menuBean.Items);
FillMenuData(menuBean.Items,devices);
}
}
}
@@ -102,8 +110,8 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
navgateVM = App.Current.Services.GetRequiredService<SettingViewModel>();
break;
}
return navgateVM;
return navgateVM;
}
private VariableTable FindVarTableForDevice(int vtableId)
@@ -111,9 +119,11 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
VariableTable varTable = null;
foreach (var device in _devices)
{
varTable= device.VariableTables.FirstOrDefault(v => v.Id == vtableId);
return varTable;
varTable = device.VariableTables.FirstOrDefault(v => v.Id == vtableId);
if (varTable!=null)
return varTable;
}
return varTable;
}
@@ -128,22 +138,29 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
{
case LoadTypes.All:
Devices = await _deviceRepository.GetAll();
MenuBeans = await _menuRepository.GetMenu();
break;
await LoadMenus();
break;
case LoadTypes.Devices:
Devices = await _deviceRepository.GetAll();
break;
case LoadTypes.Menu:
MenuBeans = await _menuRepository.GetMenu();
await LoadMenus();
break;
}
}
catch (Exception e)
{
NotificationHelper.ShowMessage($"加载数据出现了错误:{e.Message}");
_logger.LogError($"加载数据出现了错误:{e.Message}");
_logger.LogError($"加载数据出现了错误:{e}");
}
}
private async Task LoadMenus()
{
MenuBeans = await _menuRepository.GetMenu();
foreach (MenuBean menu in MenuBeans)
{
MenuHelper.MenuAddParent(menu);
}
}
}