Files
DMS/DMS.WPF/ItemViewModel/DeviceItem.cs

109 lines
2.8 KiB
C#
Raw Normal View History

// 文件: 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;
using DMS.Application.Events;
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
namespace DMS.WPF.ItemViewModel;
2025-07-26 10:05:43 +08:00
/// <summary>
/// 代表设备列表中的单个设备项的ViewModel。
/// 实现了INotifyPropertyChanged其任何属性变化都会自动通知UI。
/// </summary>
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;
[ObservableProperty]
private string _description;
2025-07-26 10:05:43 +08:00
[ObservableProperty]
private ProtocolType _protocol;
[ObservableProperty]
private string _ipAddress;
[ObservableProperty]
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
[ObservableProperty]
2025-07-27 21:58:50 +08:00
private CpuType _cpuType;
[ObservableProperty]
private DeviceType _deviceType;
2025-07-26 10:05:43 +08:00
[ObservableProperty]
private string _opcUaServerUrl;
[ObservableProperty]
private bool _isActive =true;
2025-07-26 10:05:43 +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]
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-28 11:08:56 +08:00
if (Protocol == ProtocolType.OpcUa)
{
OpcUaServerUrl="opc.tcp://" + IpAddress+":"+Port;
}
}
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
}
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有效且事件服务已初始化时才发布事件
if (Id > 0 && EventService != null )
2025-09-12 13:25:39 +08:00
{
// 发布设备状态改变事件(使用统一的事件类型)
EventService.RaiseDeviceStateChanged(this, new DeviceStateChangedEventArgs(Id, Name, newValue, Core.Enums.DeviceStateType.Active));
2025-09-12 13:25:39 +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-26 10:05:43 +08:00
}