using CommunityToolkit.Mvvm.ComponentModel;
using HMI.Base;
using HMI.Core.Util;
using HMI.UI.Util;
using Plugin.DeviceConfig.Entity;
using Plugin.DeviceConfig.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace Plugin.DeviceConfig
{
///
/// DeviceConfig.xaml 的交互逻辑
///
[ObservableObject]
public partial class DeviceConfig : SimpleMaskControl
{
private DeviceConfigModel _model;
[ObservableProperty]
private ObservableCollection devices = new();
[ObservableProperty]
private ObservableCollection channels = new();
[ObservableProperty]
private ObservableCollection slotMappings = new();
[ObservableProperty]
private DeviceDefinition selectedDevice;
[ObservableProperty]
private string currentVersionStatus = "Draft";
[ObservableProperty]
private ObservableCollection treeCategories = new();
public DeviceConfig()
{
DataContext = this;
InitializeComponent();
}
///
/// 强制刷新数据(供集成测试预置设备后调用)
///
public void ForceRefreshForTest()
{
try { RefreshAllData(); }
catch (Exception ex) { log.Error("ForceRefreshForTest Error.", ex); }
}
public override void InitFinish()
{
base.InitFinish();
try
{
_model = new DeviceConfigModel();
var dbPath = Path.Combine(BaseFolder, "data.db");
_model.InitDatabase(dbPath);
LoadTreeCategories();
RefreshAllData();
}
catch (Exception ex)
{
log.Error("InitFinish Error.", ex);
ToastHelper.Error($"初始化失败: {ex.Message}");
}
}
#region 树分类加载
private void LoadTreeCategories()
{
TreeCategories = new ObservableCollection
{
new TreeNode { Name = "全部设备", Kind = null, Children = new ObservableCollection() },
new TreeNode { Name = "电源系统", Kind = DeviceKind.PowerSupply, Children = new ObservableCollection() },
new TreeNode { Name = "CAN/LIN", Kind = DeviceKind.CanDevice, Children = new ObservableCollection() },
new TreeNode { Name = "PLC/IO", Kind = DeviceKind.PlcDevice, Children = new ObservableCollection() },
new TreeNode { Name = "温箱/DAQ", Kind = null, Children = new ObservableCollection(),
FilterKinds = new[] { DeviceKind.Chamber, DeviceKind.DAQ } },
};
deviceTree.ItemsSource = TreeCategories;
}
#endregion
#region 数据加载
private void RefreshAllData()
{
try
{
var deviceList = _model.GetAllDevices();
Devices = new ObservableCollection(deviceList);
SlotMappings = new ObservableCollection(_model.GetSlotMappings());
RefreshTreeDevices();
LoadLatestVersionStatus();
}
catch (Exception ex)
{
log.Error("RefreshAllData Error.", ex);
ToastHelper.Error($"加载数据失败: {ex.Message}");
}
}
private void RefreshTreeDevices()
{
foreach (var cat in TreeCategories)
{
cat.Children.Clear();
IEnumerable filtered = Devices;
if (cat.Kind.HasValue)
{
filtered = Devices.Where(d => d.Kind == cat.Kind.Value);
}
else if (cat.FilterKinds != null)
{
filtered = Devices.Where(d => cat.FilterKinds.Contains(d.Kind));
}
foreach (var dev in filtered)
{
cat.Children.Add(new TreeNode { Name = $"{dev.DeviceId} ({dev.Endpoint})", Tag = dev });
}
}
}
private void LoadLatestVersionStatus()
{
try
{
var versions = _model.GetConfigVersions();
var effective = versions.FirstOrDefault(v => v.Status == VersionStatus.Effective);
if (effective != null)
{
CurrentVersionStatus = "Effective";
}
else if (versions.Any(v => v.Status == VersionStatus.Published))
{
CurrentVersionStatus = "Published";
}
else
{
CurrentVersionStatus = "Draft";
}
}
catch (Exception ex)
{
log.Error("LoadLatestVersionStatus Error.", ex);
}
}
#endregion
#region 事件处理
private void SimpleMaskControl_Loaded(object sender, RoutedEventArgs e)
{
// InitFinish 已加载数据,此处仅做一次性 UI 初始化
Loaded -= SimpleMaskControl_Loaded;
}
private void DeviceTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs