重构CpuType
This commit is contained in:
@@ -447,4 +447,96 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将变量表关联到对应的设备上。
|
||||
/// </summary>
|
||||
public void AssociateVariableTablesToDevices()
|
||||
{
|
||||
// 1. 创建一个字典,按 DeviceId 分组所有变量表,以便高效查找
|
||||
var variableTablesGroupedByDevice = _variableTables
|
||||
.GroupBy(vt => vt.DeviceId)
|
||||
.ToDictionary(g => g.Key, g => g.ToList());
|
||||
|
||||
foreach (var device in _devices)
|
||||
{
|
||||
// 获取当前设备应该关联的所有变量表
|
||||
List<VariableTableItemViewModel> associatedVariableTables = new List<VariableTableItemViewModel>();
|
||||
if (variableTablesGroupedByDevice.TryGetValue(device.Id, out var foundTables))
|
||||
{
|
||||
associatedVariableTables = foundTables;
|
||||
}
|
||||
|
||||
// 创建一个HashSet,用于快速查找当前设备应有的变量表ID
|
||||
var shouldHaveVariableTableIds = new HashSet<int>(associatedVariableTables.Select(vt => vt.Id));
|
||||
|
||||
// 2. 移除不再关联的变量表
|
||||
// 从后往前遍历,避免在循环中修改集合导致索引问题
|
||||
for (int i = device.VariableTables.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var existingVariableTable = device.VariableTables[i];
|
||||
if (!shouldHaveVariableTableIds.Contains(existingVariableTable.Id))
|
||||
{
|
||||
device.VariableTables.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 添加新关联的变量表
|
||||
var currentlyHasVariableTableIds = new HashSet<int>(device.VariableTables.Select(vt => vt.Id));
|
||||
foreach (var newVariableTable in associatedVariableTables)
|
||||
{
|
||||
if (!currentlyHasVariableTableIds.Contains(newVariableTable.Id))
|
||||
{
|
||||
device.VariableTables.Add(newVariableTable);
|
||||
}
|
||||
// 如果已经存在,则不需要额外操作,因为 LoadVariableTables 已经更新了 _variableTables 中的实例
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将变量关联到对应的变量表上。
|
||||
/// </summary>
|
||||
public void AssociateVariablesToVariableTables()
|
||||
{
|
||||
// 1. 创建一个字典,按 VariableTableId 分组所有变量,以便高效查找
|
||||
var variablesGroupedByVariableTable = _variables
|
||||
.GroupBy(v => v.VariableTableId)
|
||||
.ToDictionary(g => g.Key, g => g.ToList());
|
||||
|
||||
foreach (var variableTable in _variableTables)
|
||||
{
|
||||
// 获取当前变量表应该关联的所有变量
|
||||
List<VariableItemViewModel> associatedVariables = new List<VariableItemViewModel>();
|
||||
if (variablesGroupedByVariableTable.TryGetValue(variableTable.Id, out var foundVariables))
|
||||
{
|
||||
associatedVariables = foundVariables;
|
||||
}
|
||||
|
||||
// 创建一个HashSet,用于快速查找当前变量表应有的变量ID
|
||||
var shouldHaveVariableIds = new HashSet<int>(associatedVariables.Select(v => v.Id));
|
||||
|
||||
// 2. 移除不再关联的变量
|
||||
// 从后往前遍历,避免在循环中修改集合导致索引问题
|
||||
for (int i = variableTable.Variables.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var existingVariable = variableTable.Variables[i];
|
||||
if (!shouldHaveVariableIds.Contains(existingVariable.Id))
|
||||
{
|
||||
variableTable.Variables.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 添加新关联的变量
|
||||
var currentlyHasVariableIds = new HashSet<int>(variableTable.Variables.Select(v => v.Id));
|
||||
foreach (var newVariable in associatedVariables)
|
||||
{
|
||||
if (!currentlyHasVariableIds.Contains(newVariable.Id))
|
||||
{
|
||||
variableTable.Variables.Add(newVariable);
|
||||
}
|
||||
// 如果已经存在,则不需要额外操作,因为 LoadVariables 已经更新了 _variables 中的实例
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,25 +113,25 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
|
||||
PrimaryButContent = "添加设备"
|
||||
};
|
||||
// 1. 显示添加设备对话框
|
||||
// DeviceItemViewModel device = await _dialogService.ShowDialogAsync(deviceDialogViewModel);
|
||||
// // 如果用户取消或对话框未返回设备,则直接返回
|
||||
// if (device == null)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
DeviceItemViewModel device = await _dialogService.ShowDialogAsync(deviceDialogViewModel);
|
||||
// 如果用户取消或对话框未返回设备,则直接返回
|
||||
if (device == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DeviceItemViewModel device = new DeviceItemViewModel()
|
||||
{
|
||||
Name = "Test",
|
||||
Description = "Test Device",
|
||||
IpAddress = "127.0.0.1",
|
||||
Port = 8080,
|
||||
Protocol = ProtocolType.S7,
|
||||
CpuType = "S7-1200",
|
||||
DeviceType = DeviceType.SiemensPLC,
|
||||
IsActive = true,
|
||||
|
||||
};
|
||||
// DeviceItemViewModel device = new DeviceItemViewModel()
|
||||
// {
|
||||
// Name = "Test",
|
||||
// Description = "Test Device",
|
||||
// IpAddress = "127.0.0.1",
|
||||
// Port = 8080,
|
||||
// Protocol = ProtocolType.S7,
|
||||
// CpuType = "S7-1200",
|
||||
// DeviceType = DeviceType.SiemensPLC,
|
||||
// IsActive = true,
|
||||
//
|
||||
// };
|
||||
|
||||
|
||||
CreateDeviceWithDetailsDto dto = new CreateDeviceWithDetailsDto();
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
// 文件: DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs
|
||||
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Core.Enums;
|
||||
@@ -35,7 +37,7 @@ public partial class DeviceItemViewModel : ObservableObject
|
||||
private int _slot;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _cpuType;
|
||||
private CpuType _cpuType;
|
||||
|
||||
[ObservableProperty]
|
||||
private DeviceType _deviceType;
|
||||
@@ -51,8 +53,8 @@ public partial class DeviceItemViewModel : ObservableObject
|
||||
|
||||
[ObservableProperty]
|
||||
private string _status;
|
||||
|
||||
public List<VariableTableItemViewModel> VariableTables { get; set; }
|
||||
|
||||
public ObservableCollection<VariableTableItemViewModel> VariableTables { get; set; } = new();
|
||||
|
||||
public DeviceItemViewModel(DeviceDto dto)
|
||||
{
|
||||
|
||||
@@ -29,7 +29,7 @@ public partial class VariableItemViewModel : ObservableObject
|
||||
private List<VariableMqttAliasDto>? _mqttAliases;
|
||||
|
||||
[ObservableProperty]
|
||||
private SignalType _signalType;
|
||||
private SignalType _signalType ;
|
||||
|
||||
[ObservableProperty]
|
||||
private PollLevelType _pollLevel;
|
||||
|
||||
@@ -46,6 +46,10 @@ public partial class SplashViewModel : ObservableObject
|
||||
await _dataServices.LoadVariableTables();
|
||||
await _dataServices.LoadVariables();
|
||||
await _dataServices.LoadMenus();
|
||||
|
||||
_dataServices.AssociateVariableTablesToDevices();
|
||||
_dataServices.AssociateVariablesToVariableTables();
|
||||
|
||||
// 可以在这里添加加载配置的逻辑
|
||||
await Task.Delay(500); // 模拟耗时
|
||||
|
||||
|
||||
@@ -31,6 +31,8 @@
|
||||
EnumType="{x:Type enums:DeviceType}" />
|
||||
<ex:EnumBindingSource x:Key="protocolType"
|
||||
EnumType="{x:Type enums:ProtocolType}" />
|
||||
<ex:EnumBindingSource x:Key="cpuType"
|
||||
EnumType="{x:Type enums:CpuType}" />
|
||||
<vc:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
|
||||
|
||||
|
||||
@@ -125,7 +127,7 @@
|
||||
HorizontalAlignment="Left"
|
||||
Style="{StaticResource TextBlockSubTitle}" />
|
||||
<ComboBox SelectedItem="{Binding Device.CpuType}"
|
||||
ItemsSource="{Binding CpuTypes}" />
|
||||
ItemsSource="{Binding Source={StaticResource cpuType}}" />
|
||||
|
||||
<!-- Rack -->
|
||||
<TextBlock Text="机架号"
|
||||
|
||||
Reference in New Issue
Block a user