refactor:完成重构设备的添加,删除,更新。
This commit is contained in:
218
DMS.WPF/Services/ViewService/DeviceViewService.cs
Normal file
218
DMS.WPF/Services/ViewService/DeviceViewService.cs
Normal file
@@ -0,0 +1,218 @@
|
||||
using AutoMapper;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Events;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Events;
|
||||
using DMS.WPF.Interfaces;
|
||||
using DMS.WPF.ItemViewModel;
|
||||
using DMS.WPF.ViewModels;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace DMS.WPF.Services.ViewService;
|
||||
|
||||
/// <summary>
|
||||
/// 设备数据服务类,负责管理设备相关的数据和操作。
|
||||
/// </summary>
|
||||
public class DeviceViewService : IDeviceDataService
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IAppCenterService _appCenterService;
|
||||
private readonly IAppDataService _appDataService;
|
||||
private readonly IViewDataService _viewDataService;
|
||||
private readonly IVariableTableViewService _variableTableDataService;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly INotificationService _notificationService;
|
||||
private readonly IMenuViewService _menuDataService;
|
||||
private readonly IVariableViewService _variableDataService;
|
||||
private readonly Dispatcher _uiDispatcher;
|
||||
|
||||
/// <summary>
|
||||
/// DeviceDataService类的构造函数。
|
||||
/// </summary>
|
||||
/// <param name="mapper">AutoMapper 实例。</param>
|
||||
/// <param name="appCenterService">数据服务中心实例。</param>
|
||||
public DeviceViewService(IMapper mapper, IAppCenterService appCenterService,
|
||||
IAppDataService appStorageService, IViewDataService dataStorageService,
|
||||
IVariableTableViewService variableTableDataService,
|
||||
IEventService eventService, INotificationService notificationService,
|
||||
IMenuViewService menuDataService, IVariableViewService variableDataService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_appCenterService = appCenterService;
|
||||
_appDataService = appStorageService;
|
||||
_viewDataService = dataStorageService;
|
||||
_variableTableDataService = variableTableDataService;
|
||||
_eventService = eventService;
|
||||
_notificationService = notificationService;
|
||||
_menuDataService = menuDataService;
|
||||
_variableDataService = variableDataService;
|
||||
_uiDispatcher = Dispatcher.CurrentDispatcher;
|
||||
|
||||
_eventService.OnDeviceStateChanged += OnDeviceStateChanged;
|
||||
}
|
||||
|
||||
private void OnDeviceStateChanged(object? sender, DeviceStateChangedEventArgs e)
|
||||
{
|
||||
// 只处理连接状态变化
|
||||
if (e.StateType == DeviceStateType.Connection)
|
||||
{
|
||||
_uiDispatcher.Invoke(() =>
|
||||
{
|
||||
if (_viewDataService.Devices.TryGetValue(e.DeviceId, out DeviceItem device))
|
||||
{
|
||||
device.IsRunning = e.StateValue;
|
||||
if (device.IsRunning)
|
||||
{
|
||||
_notificationService.ShowSuccess($"设备:{device.Name},连接成功。");
|
||||
}
|
||||
else
|
||||
{
|
||||
_notificationService.ShowSuccess($"设备:{device.Name},已断开连接。");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载所有设备数据。
|
||||
/// </summary>
|
||||
public void LoadAllDevices()
|
||||
{
|
||||
foreach (var device in _appDataService.Devices.Values)
|
||||
{
|
||||
_viewDataService.Devices.Add(device.Id, _mapper.Map<DeviceItem>(device));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加设备。
|
||||
/// </summary>
|
||||
public async Task<CreateDeviceWithDetailsDto?> AddDeviceAsync(CreateDeviceWithDetailsDto dto)
|
||||
{
|
||||
// 添加null检查
|
||||
if (dto is null) return null;
|
||||
|
||||
if (dto.VariableTable is not null)
|
||||
{
|
||||
dto.VariableTable.Protocol = dto.Device.Protocol;
|
||||
dto.VariableTableMenu.MenuType = MenuType.VariableTableMenu;
|
||||
}
|
||||
|
||||
|
||||
//查找到设备的根菜单
|
||||
var parentMenu
|
||||
= _appDataService.Menus.Values.FirstOrDefault(m => m.TargetViewKey == nameof(DevicesViewModel) &&
|
||||
m.TargetId == 0);
|
||||
if (parentMenu is not null)
|
||||
{
|
||||
dto.DeviceMenu.MenuType = MenuType.DeviceMenu;
|
||||
dto.DeviceMenu.ParentId = parentMenu.Id;
|
||||
}
|
||||
|
||||
var addDto = await _appCenterService.DeviceManagementService.CreateDeviceWithDetailsAsync(dto);
|
||||
|
||||
// 添加null检查
|
||||
if (addDto is null) return null;
|
||||
|
||||
//给界面添加设备
|
||||
DeviceItem deviceItem = _mapper.Map<DeviceItem>(addDto.Device);
|
||||
_viewDataService.Devices.Add(addDto.Device.Id, deviceItem);
|
||||
foreach (var variableTable in deviceItem.VariableTables)
|
||||
{
|
||||
_viewDataService.VariableTables.Add(variableTable.Id,variableTable);
|
||||
}
|
||||
|
||||
// 给界面添加设备菜单
|
||||
if (addDto.DeviceMenu != null)
|
||||
{
|
||||
MenuItem menuItem = _mapper.Map<MenuItem>(addDto.DeviceMenu);
|
||||
_menuDataService.AddMenuToView(menuItem);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return addDto;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除设备。
|
||||
/// </summary>
|
||||
public async Task<bool> DeleteAsync(DeviceItem deviceItem)
|
||||
{
|
||||
|
||||
if (!_appDataService.Devices.TryGetValue(deviceItem.Id, out var device))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//从数据库和内存中删除设备相关数据
|
||||
if (!await _appCenterService.DeviceManagementService.DeleteAsync(device))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// 从界面删除设备相关数据集
|
||||
foreach (var variableTable in deviceItem.VariableTables)
|
||||
{
|
||||
foreach (var variableItem in variableTable.Variables)
|
||||
{
|
||||
_viewDataService.Variables.Remove(variableItem.Id);
|
||||
}
|
||||
_viewDataService.VariableTables.Remove(deviceItem.Id);
|
||||
}
|
||||
|
||||
//删除菜单
|
||||
var deviceMenu
|
||||
= _viewDataService.Menus.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu &&
|
||||
m.TargetId == deviceItem.Id);
|
||||
if (deviceMenu != null)
|
||||
{
|
||||
_menuDataService.DeleteMenuToView(deviceMenu);
|
||||
}
|
||||
|
||||
_viewDataService.Devices.Remove(deviceItem.Id);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新设备。
|
||||
/// </summary>
|
||||
public async Task<bool> UpdateDevice(DeviceItem device)
|
||||
{
|
||||
if (!_appDataService.Devices.TryGetValue(device.Id, out var deviceInMen))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
//更新内存中的设备数据
|
||||
_mapper.Map(device, deviceInMen);
|
||||
|
||||
if (await _appCenterService.DeviceManagementService.UpdateDeviceAsync(deviceInMen) > 0)
|
||||
{
|
||||
//更新界面中的设备数据
|
||||
if (_viewDataService.Devices.TryGetValue(device.Id, out var deviceInView))
|
||||
{
|
||||
_mapper.Map(device, deviceInView);
|
||||
}
|
||||
//更新设备菜单名称
|
||||
var deviceMenuItem = _viewDataService.Menus
|
||||
.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu && m.TargetId == device.Id);
|
||||
if (deviceMenuItem is not null && deviceMenuItem.Header != device.Name)
|
||||
{
|
||||
deviceMenuItem.Header = device.Name;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
123
DMS.WPF/Services/ViewService/VariableTableViewService.cs
Normal file
123
DMS.WPF/Services/ViewService/VariableTableViewService.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using AutoMapper;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Models;
|
||||
using DMS.WPF.Interfaces;
|
||||
using DMS.WPF.ItemViewModel;
|
||||
|
||||
namespace DMS.WPF.Services.ViewService;
|
||||
|
||||
public class VariableTableViewService : IVariableTableViewService
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IViewDataService _viewDataService;
|
||||
private readonly IAppCenterService _appCenterService;
|
||||
private readonly IMenuViewService _menuDataService;
|
||||
|
||||
|
||||
|
||||
public VariableTableViewService(IMapper mapper, IViewDataService dataStorageService, IAppCenterService appCenterService,
|
||||
IMenuViewService menuDataService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_viewDataService = dataStorageService;
|
||||
_appCenterService = appCenterService;
|
||||
_menuDataService = menuDataService;
|
||||
}
|
||||
|
||||
|
||||
public void LoadAllVariableTables()
|
||||
{
|
||||
foreach (var device in _viewDataService.Devices)
|
||||
{
|
||||
foreach (var variableTable in device.Value.VariableTables)
|
||||
{
|
||||
_viewDataService.VariableTables.Add(variableTable.Id,variableTable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> AddVariableTable(VariableTable variableTable,
|
||||
MenuBean menuDto = null, bool isAddDb = false)
|
||||
{
|
||||
if (variableTable == null)
|
||||
return 0;
|
||||
|
||||
if (isAddDb && menuDto != null)
|
||||
{
|
||||
CreateVariableTableWithMenuDto createDto = new CreateVariableTableWithMenuDto();
|
||||
createDto.VariableTable = variableTable;
|
||||
createDto.DeviceId = variableTable.DeviceId;
|
||||
createDto.Menu = menuDto;
|
||||
var resDto = await _appCenterService.VariableTableManagementService.CreateVariableTableAsync(createDto);
|
||||
|
||||
_menuDataService.AddMenuToView(_mapper.Map<MenuItem>(resDto.Menu));
|
||||
return resDto.VariableTable.Id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> UpdateVariableTable(VariableTableItem variableTable)
|
||||
{
|
||||
if (variableTable == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var variableTable_mapped = _mapper.Map<VariableTable>(variableTable);
|
||||
if (await _appCenterService.VariableTableManagementService.UpdateVariableTableAsync(variableTable_mapped) > 0)
|
||||
{
|
||||
// 更新数据库后会自动更新内存,无需额外操作
|
||||
|
||||
var menu = _viewDataService.Menus.FirstOrDefault(m =>
|
||||
m.MenuType == MenuType.VariableTableMenu &&
|
||||
m.TargetId == variableTable.Id);
|
||||
if (menu != null)
|
||||
{
|
||||
menu.Header = variableTable.Name;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteVariableTable(VariableTableItem variableTable, bool isDeleteDb = false)
|
||||
{
|
||||
if (variableTable == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isDeleteDb)
|
||||
{
|
||||
if (!await _appCenterService.VariableTableManagementService.DeleteVariableTableAsync(variableTable.Id))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 删除与当前变量表关联的所有变量
|
||||
foreach (var variable in variableTable.Variables)
|
||||
{
|
||||
_viewDataService.Variables.Remove(variable.Id);
|
||||
}
|
||||
|
||||
// 删除变量表界面相关的菜单
|
||||
|
||||
var variableTableMenu
|
||||
=_viewDataService.Menus.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu && m.TargetId == variableTable.Id);
|
||||
_menuDataService.DeleteMenuToView(variableTableMenu);
|
||||
// 删除变量表
|
||||
_viewDataService.VariableTables.Remove(variableTable.Id);
|
||||
variableTable.Device.VariableTables.Remove(variableTable);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
221
DMS.WPF/Services/ViewService/VariableViewService.cs
Normal file
221
DMS.WPF/Services/ViewService/VariableViewService.cs
Normal file
@@ -0,0 +1,221 @@
|
||||
using AutoMapper;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using DMS.WPF.Interfaces;
|
||||
using DMS.WPF.ItemViewModel;
|
||||
using System.Collections.ObjectModel;
|
||||
using DMS.Application.Interfaces.Management;
|
||||
using DMS.Application.Services.Management;
|
||||
|
||||
namespace DMS.WPF.Services.ViewService;
|
||||
|
||||
/// <summary>
|
||||
/// 变量数据服务类,负责管理变量表和变量相关的数据和操作。
|
||||
/// </summary>
|
||||
public class VariableViewService : IVariableViewService
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IAppDataService _appDataService;
|
||||
private readonly IVariableManagementService _variableManagementService;
|
||||
private readonly IViewDataService _viewDataService;
|
||||
private readonly IAppCenterService _appCenterService;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// VariableDataService类的构造函数。
|
||||
/// </summary>
|
||||
/// <param name="mapper">AutoMapper 实例。</param>
|
||||
/// <param name="appCenterService">数据服务中心实例。</param>
|
||||
public VariableViewService(IMapper mapper, IAppDataService appStorageService,
|
||||
IVariableManagementService variableManagementService, IViewDataService dataStorageService,
|
||||
IAppCenterService appCenterService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_appDataService = appStorageService;
|
||||
_variableManagementService = variableManagementService;
|
||||
_viewDataService = dataStorageService;
|
||||
_appCenterService = appCenterService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载所有变量
|
||||
/// </summary>
|
||||
public void LoadAllVariables()
|
||||
{
|
||||
foreach (var variableTable in _viewDataService.VariableTables)
|
||||
{
|
||||
foreach (var variable in variableTable.Value.Variables)
|
||||
{
|
||||
_viewDataService.Variables.Add(variable.Id, variable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加变量表。
|
||||
/// </summary>
|
||||
public async Task<bool> AddVariableTableToView(VariableTable tableDto)
|
||||
{
|
||||
// 添加null检查
|
||||
if (tableDto == null || tableDto.DeviceId == 0)
|
||||
return false;
|
||||
|
||||
if (_viewDataService.Devices.TryGetValue(tableDto.DeviceId, out var device))
|
||||
{
|
||||
var variableTableItem = _mapper.Map<VariableTableItem>(tableDto);
|
||||
device.VariableTables.Add(variableTableItem);
|
||||
_viewDataService.VariableTables.TryAdd(variableTableItem.Id, variableTableItem);
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新变量表。
|
||||
/// </summary>
|
||||
public async Task<bool> UpdateVariableTable(VariableTableItem variableTableItem)
|
||||
{
|
||||
if (variableTableItem is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var variableTable = _mapper.Map<VariableTable>(variableTableItem);
|
||||
if (await _appCenterService.VariableTableManagementService.UpdateVariableTableAsync(variableTable) > 0)
|
||||
{
|
||||
// 更新数据库后会自动更新内存,无需额外操作
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除变量表。
|
||||
/// </summary>
|
||||
public async Task<bool> DeleteVariableTable(VariableTableItem variableTable, bool isDeleteDb = false)
|
||||
{
|
||||
if (variableTable == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isDeleteDb)
|
||||
{
|
||||
if (!await _appCenterService.VariableTableManagementService.DeleteVariableTableAsync(variableTable.Id))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 删除与当前变量表关联的所有变量
|
||||
foreach (var variable in variableTable.Variables)
|
||||
{
|
||||
_viewDataService.Variables.Remove(variable.Id);
|
||||
}
|
||||
|
||||
// 删除变量表
|
||||
_viewDataService.VariableTables.Remove(variableTable.Id);
|
||||
variableTable.Device.VariableTables.Remove(variableTable);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加变量。
|
||||
/// </summary>
|
||||
public void AddVariable(VariableItem variableItem)
|
||||
{
|
||||
if (variableItem == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_viewDataService.Variables.Add(variableItem.Id, variableItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加变量。
|
||||
/// </summary>
|
||||
public async Task<bool> AddAsync(List<VariableItem> variableItems, VariableTableItem variableTable)
|
||||
{
|
||||
|
||||
foreach (var variableDto in variableItems)
|
||||
{
|
||||
variableDto.IsActive = true;
|
||||
variableDto.CreatedAt = DateTime.Now;
|
||||
variableDto.UpdatedAt = DateTime.Now;
|
||||
variableDto.VariableTableId = variableTable.Id;
|
||||
}
|
||||
|
||||
var resVariables = await _variableManagementService.AddAsync(_mapper.Map<List<Variable>>(variableItems));
|
||||
|
||||
|
||||
|
||||
List<VariableItem> resVariableItems = _mapper.Map<List<VariableItem>>(resVariables);
|
||||
foreach (var variableItem in resVariableItems)
|
||||
{
|
||||
_viewDataService.Variables.TryAdd(variableItem.Id, variableItem);
|
||||
variableTable.Variables.Add(variableItem);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除变量。
|
||||
/// </summary>
|
||||
public void DeleteVariable(int id)
|
||||
{
|
||||
if (!_viewDataService.Variables.TryGetValue(id, out var variableItem))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_viewDataService.VariableTables.TryGetValue(variableItem.VariableTableId, out var variableTable))
|
||||
{
|
||||
variableTable.Variables.Remove(variableItem);
|
||||
}
|
||||
|
||||
_viewDataService.Variables.Remove(variableItem.Id);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(List<VariableItem> variableItems)
|
||||
{
|
||||
List<Variable> deleteList = new List<Variable>();
|
||||
foreach (var variable in variableItems)
|
||||
{
|
||||
if (_appDataService.Variables.TryGetValue(variable.Id, out var mVariable))
|
||||
{
|
||||
deleteList.Add(mVariable);
|
||||
}
|
||||
}
|
||||
|
||||
return await _variableManagementService.DeleteAsync(deleteList);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public List<VariableItem> CheckExistingVariables(List<VariableItem> importedVariables, DeviceItem device)
|
||||
{
|
||||
List<VariableItem>? existList = new List<VariableItem>();
|
||||
var allVariable = device.VariableTables.SelectMany(vt => vt.Variables);
|
||||
foreach (var variable in allVariable)
|
||||
{
|
||||
foreach (var item in importedVariables)
|
||||
{
|
||||
bool slelect = item.Name == variable.Name || item.S7Address == variable.S7Address || item.OpcUaNodeId == variable.OpcUaNodeId;
|
||||
if (slelect)
|
||||
{
|
||||
existList.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return existList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user