diff --git a/DMS.Application/Services/DeviceAppService.cs b/DMS.Application/Services/DeviceAppService.cs index 45d4dd0..cd90249 100644 --- a/DMS.Application/Services/DeviceAppService.cs +++ b/DMS.Application/Services/DeviceAppService.cs @@ -62,8 +62,6 @@ public class DeviceAppService : IDeviceAppService await _repoManager.BeginTranAsync(); var device = _mapper.Map(dto.Device); - if (device.Protocol == ProtocolType.OpcUA) - device.OpcUaServerUrl = $"opc.tcp://{device.IpAddress}:{device.Port}"; var addDevice = await _repoManager.Devices.AddAsync(device); if (addDevice == null || addDevice.Id == 0) { diff --git a/DMS.Core/Enums/ProtocolType.cs b/DMS.Core/Enums/ProtocolType.cs index 775fa36..beb5431 100644 --- a/DMS.Core/Enums/ProtocolType.cs +++ b/DMS.Core/Enums/ProtocolType.cs @@ -21,5 +21,4 @@ public enum ProtocolType /// Modbus TCP 协议。 /// ModbusTcp, - OpcUA } \ No newline at end of file diff --git a/DMS.Infrastructure/Services/OpcUaBackgroundService.cs b/DMS.Infrastructure/Services/OpcUaBackgroundService.cs index f6c6977..e8e8ca8 100644 --- a/DMS.Infrastructure/Services/OpcUaBackgroundService.cs +++ b/DMS.Infrastructure/Services/OpcUaBackgroundService.cs @@ -144,7 +144,7 @@ public class OpcUaBackgroundService : BackgroundService private async void HandleDeviceIsActiveChanged(Device device, bool isActive) { - if (device.Protocol != ProtocolType.OpcUA) + if (device.Protocol != ProtocolType.OpcUa) return; _logger.LogInformation($"设备 {device.Name} (ID: {device.Id}) 的IsActive状态改变为 {isActive}。"); @@ -194,7 +194,7 @@ public class OpcUaBackgroundService : BackgroundService _logger.LogInformation("开始加载OPC UA变量...."); var opcUaDevices = _deviceDataService - .Devices.Where(d => d.Protocol == ProtocolType.OpcUA && d.IsActive == true) + .Devices.Where(d => d.Protocol == ProtocolType.OpcUa && d.IsActive == true) .ToList(); if (opcUaDevices.Count == 0) @@ -213,7 +213,7 @@ public class OpcUaBackgroundService : BackgroundService //查找设备中所有要轮询的变量 var dPollList = opcUaDevice.VariableTables?.SelectMany(vt => vt.Variables) .Where(vd => vd.IsActive == true && - vd.Protocol == ProtocolType.OpcUA && + vd.Protocol == ProtocolType.OpcUa && vd.OpcUaUpdateType == OpcUaUpdateType.OpcUaPoll) .ToList(); // 将变量保存到字典中,方便Read后还原 @@ -229,7 +229,7 @@ public class OpcUaBackgroundService : BackgroundService //查找设备中所有要订阅的变量 var dSubList = opcUaDevice.VariableTables?.SelectMany(vt => vt.Variables) .Where(vd => vd.IsActive == true && - vd.Protocol == ProtocolType.OpcUA && + vd.Protocol == ProtocolType.OpcUa && vd.OpcUaUpdateType == OpcUaUpdateType.OpcUaSubscription) .ToList(); totalSubVariableCount += dSubList.Count; diff --git a/DMS.WPF/Profiles/MappingProfile.cs b/DMS.WPF/Profiles/MappingProfile.cs index f219148..9f9ad32 100644 --- a/DMS.WPF/Profiles/MappingProfile.cs +++ b/DMS.WPF/Profiles/MappingProfile.cs @@ -8,17 +8,19 @@ namespace DMS.WPF.Profiles { public MappingProfile() { - CreateMap().ConstructUsing(src => new DeviceItemViewModel(src)); - CreateMap(); + CreateMap() + .ReverseMap(); + CreateMap() .ForMember(dest => dest.Children, opt => opt.Ignore()) - .ConstructUsing(src => new MenuBeanItemViewModel(src, null)); // 假设 NavigationService 可以通过依赖注入获取或在ViewModel中处理 - CreateMap().ConstructUsing(src => new MqttServerItemViewModel(src)); - CreateMap().ConstructUsing(src => new UserItemViewModel(src)); - CreateMap().ConstructUsing(src => new VariableHistoryItemViewModel(src)); - CreateMap().ConstructUsing(src => new VariableItemViewModel(src)); - CreateMap().ConstructUsing(src => new VariableMqttAliasItemViewModel(src)); - CreateMap().ConstructUsing(src => new VariableTableItemViewModel(src)); + .ConstructUsing(src => new MenuBeanItemViewModel( + src, null)); // 假设 NavigationService 可以通过依赖注入获取或在ViewModel中处理 + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); } } } \ No newline at end of file diff --git a/DMS.WPF/Services/DataServices.cs b/DMS.WPF/Services/DataServices.cs index 13a0b46..a987583 100644 --- a/DMS.WPF/Services/DataServices.cs +++ b/DMS.WPF/Services/DataServices.cs @@ -97,39 +97,6 @@ public partial class DataServices : ObservableRecipient, IRecipient // AllVariables = new ConcurrentDictionary(); } - // /// - // /// 接收加载消息,根据消息类型从数据库加载对应的数据。 - // /// - // /// 加载消息,包含要加载的数据类型。 - // /// 如果加载类型未知,可能会抛出此异常(尽管当前实现中未显式抛出)。 - // public async void Receive(LoadMessage message) - // { - // try - // { - // switch (message.LoadType) - // { - // case LoadTypes.All: // 加载所有数据 - // await LoadDevices(); - // await LoadMenus(); - // await LoadMqtts(); - // break; - // case LoadTypes.Devices: // 仅加载设备数据 - // await LoadDevices(); - // break; - // case LoadTypes.Menu: // 仅加载菜单数据 - // await LoadMenus(); - // break; - // case LoadTypes.Mqtts: // 仅加载MQTT配置数据 - // await LoadMqtts(); - // break; - // } - // } - // catch (Exception e) - // { - // // 捕获加载数据时发生的异常,并通过通知和日志记录错误信息。 - // NotificationHelper.ShowError($"加载数据出现了错误:{e.Message}", e); - // } - // } /// /// 异步加载设备数据,并以高效的方式更新UI集合。 @@ -186,17 +153,6 @@ public partial class DataServices : ObservableRecipient, IRecipient } } - // private void Device_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) - // { - // if (e.PropertyName == nameof(Device.IsActive)) - // { - // if (sender is Device device) - // { - // NlogHelper.Info($"设备 {device.Name} 的IsActive状态改变为 {device.IsActive},触发设备IsActive状态变更事件。"); - // OnDeviceIsActiveChanged?.Invoke(device, device.IsActive); - // } - // } - // } /// /// 异步加载菜单数据,并以高效的方式更新UI集合。 @@ -292,36 +248,6 @@ public partial class DataServices : ObservableRecipient, IRecipient } - - // /// - // /// 异步根据ID获取设备数据。 - // /// - // /// 设备ID。 - // /// 设备对象,如果不存在则为null。 - // public async Task GetDeviceByIdAsync(int id) - // { - // return await _deviceRepository.GetByIdAsync(id); - // } - // - // /// - // /// 异步加载变量数据。 - // /// - // /// 表示异步操作的任务。 - // private async Task LoadVariables() - // { - // Variables = await _varDataRepository.GetAllAsync(); - // } - - // /// - // /// 异步更新变量数据。 - // /// - // /// 要更新的变量数据。 - // /// 表示异步操作的任务。 - // public async Task UpdateVariableAsync(Variable variable) - // { - // await _variableAppService.UpdateVariableAsync(_mapper.Map(variable)); - // } - public void Receive(LoadMessage message) { } diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs index 8772690..328a0e2 100644 --- a/DMS.WPF/ViewModels/DevicesViewModel.cs +++ b/DMS.WPF/ViewModels/DevicesViewModel.cs @@ -24,7 +24,7 @@ namespace DMS.WPF.ViewModels; /// public partial class DevicesViewModel : ViewModelBase, INavigatable { - public DataServices DataServices { get; } + public DataServices DataServices { get; } private readonly IDeviceAppService _deviceAppService; private readonly IMapper _mapper; private readonly IDialogService _dialogService; @@ -120,29 +120,10 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable 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(device); - dto.VariableTable = new VariableTableDto() - { - Name = "默认变量表", - Description = "默认变量表", - IsActive = true - }; dto.DeviceMenu = new MenuBeanDto() { @@ -150,18 +131,34 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable Icon = SegoeFluentIcons.Devices2.Glyph, TargetViewKey = "DevicesView" }; - - dto.VariableTableMenu = new MenuBeanDto() + if (device.IsAddDefVarTable) + { + dto.VariableTable = new VariableTableDto() { - Header = dto.VariableTable.Name, - Icon = SegoeFluentIcons.DataSense.Glyph, - TargetViewKey = "VariableTableView" + Name = "默认变量表", + Description = "默认变量表", + IsActive = true }; + 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(addDto.Device)); - // - // await _deviceRepository.AddAsync(device); + + DataServices.Devices.Add(_mapper.Map(addDto.Device)); + if (addDto.DeviceMenu != null) + { + var deviceMenu = DataServices.Menus.FirstOrDefault(m => m.Id == addDto.DeviceMenu.ParentId); + if (deviceMenu!=null) + { + deviceMenu.Children.Add(_mapper.Map(addDto.DeviceMenu)); + } + } } catch (Exception e) { diff --git a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs index 8a29df2..40dadb6 100644 --- a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs @@ -2,7 +2,6 @@ using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; -using DMS.Application.DTOs; using DMS.Core.Enums; namespace DMS.WPF.ViewModels.Items; @@ -53,55 +52,26 @@ public partial class DeviceItemViewModel : ObservableObject [ObservableProperty] private string _status; + + [ObservableProperty] + private bool _isAddDefVarTable; - partial void OnProtocolChanged(ProtocolType oldValue, ProtocolType newValue) + partial void OnIpAddressChanged(string newIpAddress) { - + if (Protocol == ProtocolType.OpcUa) + { + OpcUaServerUrl="opc.tcp://" + IpAddress+":"+Port; + } + } + + partial void OnPortChanged(int newPort) + { + if (Protocol == ProtocolType.OpcUa) + { + OpcUaServerUrl="opc.tcp://" + IpAddress+":"+Port; + } } public ObservableCollection VariableTables { get; set; } = new(); - 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 - }; - } } diff --git a/DMS.WPF/ViewModels/Items/MqttServerItemViewModel.cs b/DMS.WPF/ViewModels/Items/MqttServerItemViewModel.cs index 0c0591f..11b0755 100644 --- a/DMS.WPF/ViewModels/Items/MqttServerItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/MqttServerItemViewModel.cs @@ -52,22 +52,5 @@ public partial class MqttServerItemViewModel : ObservableObject [ObservableProperty] private ObservableCollection _variableAliases = new(); - public MqttServerItemViewModel(MqttServerDto dto) - { - Id = dto.Id; - _serverName = dto.ServerName; - _brokerAddress = dto.BrokerAddress; - _port = dto.Port; - _username = dto.Username; - _password = dto.Password; - _isActive = dto.IsActive; - _subscribeTopic = dto.SubscribeTopic; - _publishTopic = dto.PublishTopic; - _clientId = dto.ClientId; - _createdAt = dto.CreatedAt; - _connectedAt = dto.ConnectedAt; - _connectionDuration = dto.ConnectionDuration; - _messageFormat = dto.MessageFormat; - _variableAliases = new ObservableCollection(dto.VariableAliases.Select(va => new VariableMqttAliasItemViewModel(va))); - } + } diff --git a/DMS.WPF/ViewModels/Items/UserItemViewModel.cs b/DMS.WPF/ViewModels/Items/UserItemViewModel.cs index 9282f7d..85fb3b0 100644 --- a/DMS.WPF/ViewModels/Items/UserItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/UserItemViewModel.cs @@ -16,11 +16,5 @@ public partial class UserItemViewModel : ObservableObject [ObservableProperty] private bool _isActive; - public UserItemViewModel(UserDto dto) - { - Id = dto.Id; - _username = dto.Username; - _role = dto.Role; - _isActive = dto.IsActive; - } + } diff --git a/DMS.WPF/ViewModels/Items/VariableHistoryItemViewModel.cs b/DMS.WPF/ViewModels/Items/VariableHistoryItemViewModel.cs index e49eaa6..1a633c2 100644 --- a/DMS.WPF/ViewModels/Items/VariableHistoryItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/VariableHistoryItemViewModel.cs @@ -17,11 +17,5 @@ public partial class VariableHistoryItemViewModel : ObservableObject [ObservableProperty] private DateTime _timestamp; - public VariableHistoryItemViewModel(VariableHistoryDto dto) - { - Id = dto.Id; - _variableId = dto.VariableId; - _value = dto.Value; - _timestamp = dto.Timestamp; - } + } diff --git a/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs b/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs index 704dfcc..fea2fe0 100644 --- a/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs @@ -89,34 +89,5 @@ public partial class VariableItemViewModel : ObservableObject [ObservableProperty] private OpcUaUpdateType _opcUaUpdateType; - public VariableItemViewModel(VariableDto dto) - { - Id = dto.Id; - _name = dto.Name; - _s7Address = dto.S7Address; - _dataValue = dto.DataValue; - _displayValue = dto.DisplayValue; - _variableTable = dto.VariableTable; - _mqttAliases = dto.MqttAliases; - _signalType = dto.SignalType; - _pollLevel = dto.PollLevel; - _isActive = dto.IsActive; - _variableTableId = dto.VariableTableId; - _opcUaNodeId = dto.OpcUaNodeId; - _isHistoryEnabled = dto.IsHistoryEnabled; - _historyDeadband = dto.HistoryDeadband; - _isAlarmEnabled = dto.IsAlarmEnabled; - _alarmMinValue = dto.AlarmMinValue; - _alarmMaxValue = dto.AlarmMaxValue; - _alarmDeadband = dto.AlarmDeadband; - _protocol = dto.Protocol; - _cSharpDataType = dto.CSharpDataType; - _conversionFormula = dto.ConversionFormula; - _createdAt = dto.CreatedAt; - _updatedAt = dto.UpdatedAt; - _updatedBy = dto.UpdatedBy; - _isModified = dto.IsModified; - _description = dto.Description; - _opcUaUpdateType = dto.OpcUaUpdateType; - } + } diff --git a/DMS.WPF/ViewModels/Items/VariableMqttAliasItemViewModel.cs b/DMS.WPF/ViewModels/Items/VariableMqttAliasItemViewModel.cs index 7bde250..6110043 100644 --- a/DMS.WPF/ViewModels/Items/VariableMqttAliasItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/VariableMqttAliasItemViewModel.cs @@ -19,12 +19,5 @@ public partial class VariableMqttAliasItemViewModel : ObservableObject [ObservableProperty] private string _alias; - public VariableMqttAliasItemViewModel(VariableMqttAliasDto dto) - { - Id = dto.Id; - _variableId = dto.VariableId; - _mqttServerId = dto.MqttServerId; - _mqttServerName = dto.MqttServerName; - _alias = dto.Alias; - } + } diff --git a/DMS.WPF/ViewModels/Items/VariableTableItemViewModel.cs b/DMS.WPF/ViewModels/Items/VariableTableItemViewModel.cs index 5ead7da..c07103f 100644 --- a/DMS.WPF/ViewModels/Items/VariableTableItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/VariableTableItemViewModel.cs @@ -28,14 +28,5 @@ public partial class VariableTableItemViewModel : ObservableObject [ObservableProperty] private ObservableCollection _variables = new(); - public VariableTableItemViewModel(VariableTableDto dto) - { - Id = dto.Id; - _name = dto.Name; - _description = dto.Description; - _isActive = dto.IsActive; - _deviceId = dto.DeviceId; - _protocol = dto.Protocol; - _variables = new ObservableCollection(dto.Variables.Select(v => new VariableItemViewModel(v))); - } + } diff --git a/DMS.WPF/Views/Dialogs/DeviceDialog.xaml b/DMS.WPF/Views/Dialogs/DeviceDialog.xaml index 58f0c26..e7d13aa 100644 --- a/DMS.WPF/Views/Dialogs/DeviceDialog.xaml +++ b/DMS.WPF/Views/Dialogs/DeviceDialog.xaml @@ -65,7 +65,7 @@ - @@ -111,6 +111,25 @@ + + + + + + + + +