2025-10-06 18:17:56 +08:00
|
|
|
|
// 文件: DMS.WPF/ViewModels/Items/DeviceItem.cs
|
2025-07-27 21:58:50 +08:00
|
|
|
|
|
|
|
|
|
|
using System.Collections.ObjectModel;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2025-10-01 19:16:47 +08:00
|
|
|
|
using DMS.Application.Events;
|
2025-09-12 14:59:32 +08:00
|
|
|
|
using DMS.Application.Interfaces;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
using DMS.Core.Enums;
|
2025-09-16 14:42:23 +08:00
|
|
|
|
using DMS.Core.Events;
|
2025-09-12 13:25:39 +08:00
|
|
|
|
using DMS.WPF.Interfaces;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
2025-10-06 18:17:56 +08:00
|
|
|
|
namespace DMS.WPF.ItemViewModel;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 代表设备列表中的单个设备项的ViewModel。
|
|
|
|
|
|
/// 实现了INotifyPropertyChanged,其任何属性变化都会自动通知UI。
|
|
|
|
|
|
/// </summary>
|
2025-10-06 18:17:56 +08:00
|
|
|
|
public partial class DeviceItem : ObservableObject
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
2025-09-12 13:25:39 +08:00
|
|
|
|
// 用于访问事件服务的静态属性
|
|
|
|
|
|
public static IEventService EventService { get; set; }
|
|
|
|
|
|
|
2025-07-28 13:06:36 +08:00
|
|
|
|
public int Id { get; set; }
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private string _name;
|
|
|
|
|
|
|
2025-07-27 21:08:58 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private string _description;
|
|
|
|
|
|
|
2025-07-26 10:05:43 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private ProtocolType _protocol;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private string _ipAddress;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-08-24 11:31:07 +08:00
|
|
|
|
private int _port=102;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-09-05 15:59:14 +08:00
|
|
|
|
private int _rack;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-09-05 15:59:14 +08:00
|
|
|
|
private int _slot=1;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
2025-07-27 21:08:58 +08:00
|
|
|
|
[ObservableProperty]
|
2025-07-27 21:58:50 +08:00
|
|
|
|
private CpuType _cpuType;
|
2025-07-27 21:08:58 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private DeviceType _deviceType;
|
|
|
|
|
|
|
2025-07-26 10:05:43 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private string _opcUaServerUrl;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-08-24 11:31:07 +08:00
|
|
|
|
private bool _isActive =true;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
2025-07-27 21:08:58 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private bool _isRunning;
|
|
|
|
|
|
|
2025-07-26 10:05:43 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private string _status;
|
2025-07-28 11:08:56 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-08-24 11:31:07 +08:00
|
|
|
|
private bool _isAddDefVarTable=true;
|
2025-07-27 21:58:50 +08:00
|
|
|
|
|
2025-07-28 11:08:56 +08:00
|
|
|
|
partial void OnIpAddressChanged(string newIpAddress)
|
2025-07-27 22:35:07 +08:00
|
|
|
|
{
|
2025-07-28 11:08:56 +08:00
|
|
|
|
if (Protocol == ProtocolType.OpcUa)
|
|
|
|
|
|
{
|
|
|
|
|
|
OpcUaServerUrl="opc.tcp://" + IpAddress+":"+Port;
|
|
|
|
|
|
}
|
2025-07-27 22:35:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-28 11:08:56 +08:00
|
|
|
|
partial void OnPortChanged(int newPort)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
2025-07-28 11:08:56 +08:00
|
|
|
|
if (Protocol == ProtocolType.OpcUa)
|
|
|
|
|
|
{
|
|
|
|
|
|
OpcUaServerUrl="opc.tcp://" + IpAddress+":"+Port;
|
|
|
|
|
|
}
|
2025-07-26 10:05:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-12 17:22:15 +08:00
|
|
|
|
partial void OnIsRunningChanged(bool oldValue, bool newValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
System.Console.WriteLine($"IsRunning changed from {oldValue} to {newValue} for device {Name}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-12 13:25:39 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当IsActive属性改变时调用,用于发布设备状态改变事件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
partial void OnIsActiveChanged(bool oldValue, bool newValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 只有当设备ID有效且事件服务已初始化时才发布事件
|
2025-09-12 14:59:32 +08:00
|
|
|
|
if (Id > 0 && EventService != null )
|
2025-09-12 13:25:39 +08:00
|
|
|
|
{
|
2025-10-01 18:41:05 +08:00
|
|
|
|
// 发布设备状态改变事件(使用统一的事件类型)
|
|
|
|
|
|
EventService.RaiseDeviceStateChanged(this, new DeviceStateChangedEventArgs(Id, Name, newValue, Core.Enums.DeviceStateType.Active));
|
2025-09-12 13:25:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-06 18:17:56 +08:00
|
|
|
|
public ObservableCollection<VariableTableItem> VariableTables { get; set; } = new();
|
2025-09-14 20:46:31 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private bool _isConnected;
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private string _connectionStatus = "未连接";
|
2025-07-27 21:08:58 +08:00
|
|
|
|
|
2025-07-26 10:05:43 +08:00
|
|
|
|
}
|