2025-06-13 18:54:17 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-07-18 19:56:00 +08:00
|
|
|
|
using DMS.Data.Repositories;
|
2025-07-18 22:21:16 +08:00
|
|
|
|
using DMS.Core.Enums;
|
2025-07-18 19:56:00 +08:00
|
|
|
|
using DMS.Helper;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
using DMS.WPF.Models;
|
2025-07-18 19:56:00 +08:00
|
|
|
|
using DMS.Services;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-06-23 13:42:02 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-07-18 19:56:00 +08:00
|
|
|
|
using DMS.Data;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
using DMS.WPF.Services;
|
|
|
|
|
|
using DMS.Infrastructure.Repositories;
|
|
|
|
|
|
using DMS.WPF.Models;
|
2025-06-10 20:55:39 +08:00
|
|
|
|
|
2025-07-19 11:11:01 +08:00
|
|
|
|
namespace DMS.WPF.ViewModels;
|
2025-06-12 13:15:55 +08:00
|
|
|
|
|
2025-07-03 13:17:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设备管理视图模型,负责设备的增删改查操作。
|
|
|
|
|
|
/// </summary>
|
2025-06-12 13:15:55 +08:00
|
|
|
|
public partial class DevicesViewModel : ViewModelBase
|
2025-06-10 20:55:39 +08:00
|
|
|
|
{
|
2025-07-03 13:17:25 +08:00
|
|
|
|
private readonly DataServices _dataServices;
|
|
|
|
|
|
private readonly IDialogService _dialogService;
|
2025-06-26 19:36:27 +08:00
|
|
|
|
private readonly DeviceRepository _deviceRepository;
|
2025-07-06 19:51:53 +08:00
|
|
|
|
|
2025-06-26 19:36:27 +08:00
|
|
|
|
private readonly MenuRepository _menuRepository;
|
2025-07-02 22:07:16 +08:00
|
|
|
|
private readonly VarTableRepository _varTableRepository;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
|
2025-07-03 13:17:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设备列表。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private ObservableCollection<Device> _devices;
|
|
|
|
|
|
|
2025-07-16 19:37:13 +08:00
|
|
|
|
|
2025-07-03 13:17:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当前选中的设备。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private Device _selectedDevice;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化 <see cref="DevicesViewModel"/> 类的新实例。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="logger">日志记录器。</param>
|
|
|
|
|
|
/// <param name="dialogService">对话框服务。</param>
|
|
|
|
|
|
/// <param name="dataServices">数据服务。</param>
|
2025-06-26 19:36:27 +08:00
|
|
|
|
public DevicesViewModel(
|
2025-07-15 22:18:37 +08:00
|
|
|
|
IDialogService dialogService, DataServices dataServices,DeviceRepository deviceRepository,VarTableRepository varTableRepository,MenuRepository menuRepository
|
2025-06-23 17:01:06 +08:00
|
|
|
|
)
|
2025-06-12 13:15:55 +08:00
|
|
|
|
{
|
2025-07-15 22:18:37 +08:00
|
|
|
|
_deviceRepository = deviceRepository;
|
|
|
|
|
|
_varTableRepository = varTableRepository;
|
|
|
|
|
|
_menuRepository = menuRepository;
|
2025-07-06 19:51:53 +08:00
|
|
|
|
|
2025-06-26 19:36:27 +08:00
|
|
|
|
_dialogService = dialogService;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
_dataServices = dataServices;
|
2025-07-01 21:34:20 +08:00
|
|
|
|
|
2025-07-13 16:22:07 +08:00
|
|
|
|
_dataServices.OnDeviceListChanged += (devices) =>
|
2025-07-10 17:16:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
Devices = new ObservableCollection<Device>(devices);
|
|
|
|
|
|
};
|
2025-06-12 13:15:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-13 16:22:07 +08:00
|
|
|
|
public override void OnLoaded()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_dataServices.Devices!=null && _dataServices.Devices.Count>0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Devices=new ObservableCollection<Device>(_dataServices.Devices);
|
2025-07-16 19:37:13 +08:00
|
|
|
|
foreach (var device in Devices)
|
|
|
|
|
|
{
|
|
|
|
|
|
device.OnDeviceIsActiveChanged += HandleDeviceIsActiveChanged;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Task<bool> OnExitAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_dataServices.Devices!=null && _dataServices.Devices.Count>0)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var device in Devices)
|
|
|
|
|
|
{
|
|
|
|
|
|
device.OnDeviceIsActiveChanged -= HandleDeviceIsActiveChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async void HandleDeviceIsActiveChanged(Device device, bool isActive)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _deviceRepository.UpdateAsync(device);
|
|
|
|
|
|
NotificationHelper.ShowSuccess($"设备 {device.Name} 的激活状态已更新。");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError($"更新设备 {device.Name} 激活状态失败: {ex.Message}", ex);
|
2025-07-13 16:22:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
|
/// <summary>
|
2025-07-03 13:17:25 +08:00
|
|
|
|
/// 添加设备命令。
|
2025-07-02 22:07:16 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async void AddDevice()
|
2025-07-02 18:33:08 +08:00
|
|
|
|
{
|
2025-07-04 18:33:48 +08:00
|
|
|
|
try
|
2025-06-12 18:41:46 +08:00
|
|
|
|
{
|
2025-07-04 18:33:48 +08:00
|
|
|
|
// 1. 显示添加设备对话框
|
|
|
|
|
|
var device = await _dialogService.ShowAddDeviceDialog();
|
|
|
|
|
|
// 如果用户取消或对话框未返回设备,则直接返回
|
|
|
|
|
|
if (device == null)
|
|
|
|
|
|
{
|
2025-07-06 19:51:53 +08:00
|
|
|
|
NlogHelper.Info("用户取消了添加设备操作。");
|
2025-07-04 18:33:48 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-07-03 13:17:25 +08:00
|
|
|
|
|
2025-07-10 21:24:20 +08:00
|
|
|
|
if (device.ProtocolType == ProtocolType.OpcUA)
|
|
|
|
|
|
device.OpcUaEndpointUrl = $"opc.tcp://{device.Ip}:{device.Prot}";
|
|
|
|
|
|
|
2025-07-16 19:37:13 +08:00
|
|
|
|
await _deviceRepository.AddAsync(device);
|
2025-07-04 18:33:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"添加设备的过程中发生错误:{e.Message}", e);
|
2025-07-04 18:33:48 +08:00
|
|
|
|
}
|
2025-06-10 22:13:06 +08:00
|
|
|
|
}
|
2025-06-13 18:54:17 +08:00
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
|
/// <summary>
|
2025-07-03 13:17:25 +08:00
|
|
|
|
/// 删除设备命令。
|
2025-07-01 21:34:20 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
2025-07-03 13:17:25 +08:00
|
|
|
|
public async void DeleteDevice()
|
2025-07-01 21:34:20 +08:00
|
|
|
|
{
|
2025-07-04 18:33:48 +08:00
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedDevice == null)
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击删除设备");
|
2025-07-01 21:34:20 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-03 13:17:25 +08:00
|
|
|
|
string msg = $"确认要删除设备名为:{SelectedDevice.Name}";
|
|
|
|
|
|
var isDel = await _dialogService.ShowConfrimeDialog("删除设备", msg, "删除设备");
|
|
|
|
|
|
if (isDel)
|
2025-07-02 22:07:16 +08:00
|
|
|
|
{
|
2025-07-04 18:33:48 +08:00
|
|
|
|
|
2025-07-03 13:17:25 +08:00
|
|
|
|
// 删除设备
|
2025-07-16 19:37:13 +08:00
|
|
|
|
await _deviceRepository.DeleteAsync(SelectedDevice ,_dataServices.MenuTrees);
|
2025-07-01 21:34:20 +08:00
|
|
|
|
|
|
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
|
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Devices);
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"删除设备成功,设备名:{SelectedDevice.Name}");
|
2025-07-01 21:34:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"删除设备的过程中发生错误:{e.Message}", e);
|
2025-07-01 21:34:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-03 13:17:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 编辑设备命令。
|
|
|
|
|
|
/// </summary>
|
2025-07-01 21:34:20 +08:00
|
|
|
|
[RelayCommand]
|
2025-07-03 13:17:25 +08:00
|
|
|
|
public async void EditDevice()
|
2025-07-01 21:34:20 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedDevice == null)
|
|
|
|
|
|
{
|
2025-07-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击编辑设备");
|
2025-07-01 21:34:20 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-03 13:17:25 +08:00
|
|
|
|
var editDievce = await _dialogService.ShowEditDeviceDialog(SelectedDevice);
|
|
|
|
|
|
if (editDievce != null)
|
2025-07-01 21:34:20 +08:00
|
|
|
|
{
|
2025-07-03 13:17:25 +08:00
|
|
|
|
// 更新菜单
|
2025-07-16 19:37:13 +08:00
|
|
|
|
var res = await _deviceRepository.UpdateAsync(editDievce);
|
2025-07-03 13:17:25 +08:00
|
|
|
|
var menu = DataServicesHelper.FindMenusForDevice(editDievce, _dataServices.MenuTrees);
|
2025-07-02 12:01:20 +08:00
|
|
|
|
if (menu != null)
|
2025-07-16 19:37:13 +08:00
|
|
|
|
await _menuRepository.UpdateAsync(menu);
|
2025-07-02 22:07:16 +08:00
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
|
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Devices);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"编辑设备的过程中发生错误:{e.Message}", e);
|
2025-07-01 21:34:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-16 18:39:00 +08:00
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public void NavigateToDetail()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedDevice == null) return;
|
|
|
|
|
|
var deviceDetailVm = App.Current.Services.GetRequiredService<DeviceDetailViewModel>();
|
|
|
|
|
|
deviceDetailVm.CurrentDevice = SelectedDevice;
|
|
|
|
|
|
MessageHelper.SendNavgatorMessage(deviceDetailVm);
|
|
|
|
|
|
}
|
2025-06-10 20:55:39 +08:00
|
|
|
|
}
|