梳理了所有的Db,DTO,ItemViewModel的属性

This commit is contained in:
2025-07-27 21:08:58 +08:00
parent e509b7de0b
commit 4a56405629
38 changed files with 417 additions and 479 deletions

View File

@@ -1,6 +1,9 @@
using System.Collections.ObjectModel;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.Core.Helper;
using DMS.Core.Models;
@@ -10,16 +13,20 @@ using DMS.WPF.Helper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using DMS.WPF.Services;
using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.ViewModels.Items;
using iNKORE.UI.WPF.Modern.Common.IconKeys;
namespace DMS.WPF.ViewModels;
/// <summary>
/// 设备管理视图模型,负责设备的增删改查操作。
/// </summary>
public partial class DevicesViewModel : ViewModelBase,INavigatable
public partial class DevicesViewModel : ViewModelBase, INavigatable
{
private readonly DataServices _dataServices;
public DataServices DataServices { get; }
private readonly IDeviceAppService _deviceAppService;
private readonly IMapper _mapper;
private readonly IDialogService _dialogService;
@@ -29,7 +36,7 @@ public partial class DevicesViewModel : ViewModelBase,INavigatable
[ObservableProperty]
private ObservableCollection<DeviceItemViewModel> _devices;
/// <summary>
/// 当前选中的设备。
/// </summary>
@@ -42,17 +49,15 @@ public partial class DevicesViewModel : ViewModelBase,INavigatable
/// <param name="logger">日志记录器。</param>
/// <param name="dialogService">对话框服务。</param>
/// <param name="dataServices">数据服务。</param>
public DevicesViewModel(
IDialogService dialogService, DataServices dataServices)
public DevicesViewModel(IMapper mapper,
IDialogService dialogService, DataServices dataServices, IDeviceAppService deviceAppService)
{
_mapper = mapper;
_dialogService = dialogService;
_dataServices = dataServices;
DataServices = dataServices;
_deviceAppService = deviceAppService;
Devices = new ObservableCollection<DeviceItemViewModel>();
_dataServices.OnDeviceListChanged += (devices) =>
{
};
DataServices.OnDeviceListChanged += (devices) => { };
}
// public override void OnLoaded()
@@ -98,28 +103,71 @@ public partial class DevicesViewModel : ViewModelBase,INavigatable
/// 添加设备命令。
/// </summary>
[RelayCommand]
public async void AddDevice()
public async Task AddDevice()
{
// try
// {
// // 1. 显示添加设备对话框
// var device = await _dialogService.ShowAddDeviceDialog();
// // 如果用户取消或对话框未返回设备,则直接返回
// if (device == null)
// {
// NlogHelper.Info("用户取消了添加设备操作。");
// return;
// }
//
// if (device.ProtocolType == ProtocolType.OpcUA)
// device.OpcUaEndpointUrl = $"opc.tcp://{device.Ip}:{device.Prot}";
//
// await _deviceRepository.AddAsync(device);
// }
// catch (Exception e)
// {
// NotificationHelper.ShowError($"添加设备的过程中发生错误:{e.Message}", e);
// }
try
{
DeviceItemViewModel deviceItemViewModel = new DeviceItemViewModel();
DeviceDialogViewModel deviceDialogViewModel = new DeviceDialogViewModel(deviceItemViewModel)
{
PrimaryButContent = "添加设备"
};
// 1. 显示添加设备对话框
// 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,
};
CreateDeviceWithDetailsDto dto = new CreateDeviceWithDetailsDto();
dto.Device = _mapper.Map<DeviceDto>(device);
dto.VariableTable = new VariableTableDto()
{
Name = "默认变量表",
Description = "默认变量表",
IsActive = true
};
dto.DeviceMenu = new MenuBeanDto()
{
Header = device.Name,
Icon = SegoeFluentIcons.Devices2.Glyph,
TargetViewKey = "DevicesView"
};
dto.VariableTableMenu = new MenuBeanDto()
{
Header = dto.VariableTable.Name,
Icon = SegoeFluentIcons.DataSense.Glyph,
TargetViewKey = "VariableTableView"
};
var addDto = await _deviceAppService.CreateDeviceWithDetailsAsync(dto);
DataServices.Devices.Add(_mapper.Map<DeviceItemViewModel>(addDto.Device));
//
// await _deviceRepository.AddAsync(device);
}
catch (Exception e)
{
Console.WriteLine(e);
NotificationHelper.ShowError($"添加设备的过程中发生错误:{e.Message}", e);
}
}
/// <summary>
@@ -128,7 +176,6 @@ public partial class DevicesViewModel : ViewModelBase,INavigatable
[RelayCommand]
public async void DeleteDevice()
{
// try
// {
// if (SelectedDevice == null)
@@ -188,7 +235,7 @@ public partial class DevicesViewModel : ViewModelBase,INavigatable
// NotificationHelper.ShowError($"编辑设备的过程中发生错误:{e.Message}", e);
// }
}
[RelayCommand]
public void NavigateToDetail()
{
@@ -200,6 +247,5 @@ public partial class DevicesViewModel : ViewModelBase,INavigatable
public async Task OnNavigatedToAsync(object parameter)
{
}
}

View File

@@ -1,36 +1,33 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.WPF.ViewModels.Items;
using System.Threading.Tasks;
namespace DMS.WPF.ViewModels.Dialogs;
public partial class DeviceDialogViewModel : ObservableObject
public partial class DeviceDialogViewModel : DialogViewModelBase<DeviceItemViewModel>
{
[ObservableProperty]
private DeviceItemViewModel _device;
partial void OnDeviceChanged(DeviceItemViewModel value)
{
// if (value != null)
// {
// System.Diagnostics.Debug.WriteLine($"Device ProtocolType changed to: {value.ProtocolType}");
// }
}
[ObservableProperty] private string title ;
[ObservableProperty] private string primaryButContent ;
public DeviceDialogViewModel(DeviceItemViewModel device)
{
_device = device;
}
// AddAsync a property to expose CpuType enum values for ComboBox
// public Array CpuTypes => Enum.GetValues(typeof(CpuType));
[RelayCommand]
private async Task Save()
{
// Here you can add validation logic before closing.
await Close(Device);
}
[RelayCommand]
public void AddDevice()
private async Task Cancel()
{
await Close(null);
}
}

View File

@@ -16,6 +16,9 @@ public partial class DeviceItemViewModel : ObservableObject
[ObservableProperty]
private string _name;
[ObservableProperty]
private string _description;
[ObservableProperty]
private ProtocolType _protocol;
@@ -31,42 +34,66 @@ public partial class DeviceItemViewModel : ObservableObject
[ObservableProperty]
private int _slot;
[ObservableProperty]
private string _cpuType;
[ObservableProperty]
private DeviceType _deviceType;
[ObservableProperty]
private string _opcUaServerUrl;
[ObservableProperty]
private bool _isActive;
[ObservableProperty]
private bool _isRunning;
[ObservableProperty]
private string _status;
public List<VariableTableItemViewModel> VariableTables { get; set; }
public DeviceItemViewModel(DeviceDto dto)
{
Id = dto.Id;
_name = dto.Name;
_description = dto.Description;
_protocol = dto.Protocol;
_ipAddress = dto.IpAddress;
_port = dto.Port;
_rack = dto.Rack;
_slot = dto.Slot;
_cpuType = dto.CpuType;
_deviceType = dto.DeviceType;
_opcUaServerUrl = dto.OpcUaServerUrl;
_isActive = dto.IsActive;
_isRunning = dto.IsRunning;
_status = dto.Status;
}
public DeviceItemViewModel()
{
}
public DeviceDto ToDto()
{
return new DeviceDto
{
Id = this.Id,
Name = this.Name,
Description = this.Description,
Protocol = this.Protocol,
IpAddress = this.IpAddress,
Port = this.Port,
Rack = this.Rack,
Slot = this.Slot,
CpuType = this.CpuType,
DeviceType = this.DeviceType,
OpcUaServerUrl = this.OpcUaServerUrl,
IsActive = this.IsActive,
IsRunning = this.IsRunning,
Status = this.Status
};
}

View File

@@ -1,6 +1,8 @@
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Application.DTOs;
using System;
using System.Collections.ObjectModel;
using System.Linq;
namespace DMS.WPF.ViewModels.Items;
@@ -47,6 +49,9 @@ public partial class MqttServerItemViewModel : ObservableObject
[ObservableProperty]
private string _messageFormat;
[ObservableProperty]
private ObservableCollection<VariableMqttAliasItemViewModel> _variableAliases = new();
public MqttServerItemViewModel(MqttServerDto dto)
{
Id = dto.Id;
@@ -63,5 +68,6 @@ public partial class MqttServerItemViewModel : ObservableObject
_connectedAt = dto.ConnectedAt;
_connectionDuration = dto.ConnectionDuration;
_messageFormat = dto.MessageFormat;
_variableAliases = new ObservableCollection<VariableMqttAliasItemViewModel>(dto.VariableAliases.Select(va => new VariableMqttAliasItemViewModel(va)));
}
}

View File

@@ -29,7 +29,7 @@ public partial class VariableItemViewModel : ObservableObject
private List<VariableMqttAliasDto>? _mqttAliases;
[ObservableProperty]
private SignalType _dataType;
private SignalType _signalType;
[ObservableProperty]
private PollLevelType _pollLevel;
@@ -85,6 +85,10 @@ public partial class VariableItemViewModel : ObservableObject
[ObservableProperty]
private string _description;
[ObservableProperty]
private OpcUaUpdateType _opcUaUpdateType;
public VariableItemViewModel(VariableDto dto)
{
Id = dto.Id;
@@ -94,7 +98,7 @@ public partial class VariableItemViewModel : ObservableObject
_displayValue = dto.DisplayValue;
_variableTable = dto.VariableTable;
_mqttAliases = dto.MqttAliases;
_dataType = dto.DataType;
_signalType = dto.SignalType;
_pollLevel = dto.PollLevel;
_isActive = dto.IsActive;
_variableTableId = dto.VariableTableId;
@@ -113,5 +117,6 @@ public partial class VariableItemViewModel : ObservableObject
_updatedBy = dto.UpdatedBy;
_isModified = dto.IsModified;
_description = dto.Description;
_opcUaUpdateType = dto.OpcUaUpdateType;
}
}

View File

@@ -1,6 +1,8 @@
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Application.DTOs;
using DMS.Core.Enums;
using System.Collections.ObjectModel;
using System.Linq;
namespace DMS.WPF.ViewModels.Items;
@@ -23,6 +25,9 @@ public partial class VariableTableItemViewModel : ObservableObject
[ObservableProperty]
private ProtocolType _protocol;
[ObservableProperty]
private ObservableCollection<VariableItemViewModel> _variables = new();
public VariableTableItemViewModel(VariableTableDto dto)
{
Id = dto.Id;
@@ -31,5 +36,6 @@ public partial class VariableTableItemViewModel : ObservableObject
_isActive = dto.IsActive;
_deviceId = dto.DeviceId;
_protocol = dto.Protocol;
_variables = new ObservableCollection<VariableItemViewModel>(dto.Variables.Select(v => new VariableItemViewModel(v)));
}
}