using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using TeamAAS.Motion;
using TeamAAS.Motion.Models;
using TeamAAS.Views;
namespace TeamAAS.ViewModels
{
///
/// 设置页 - 控制卡管理(列表 / 连接 / 属性)。
/// 调试操作已下沉到库内 MotionDebugView。
///
public partial class SettingViewModel
{
#region 控制卡管理
private readonly MotionDebugPageFactory _motionDebugFactory = new MotionDebugPageFactory();
public ObservableCollection MotionCards { get; }
private MotionDeviceConfig _selectedMotionCard;
public MotionDeviceConfig SelectedMotionCard
{
get => _selectedMotionCard;
set
{
if (SetProperty(ref _selectedMotionCard, value))
RefreshMotionDebugHost();
RaisePropertyChanged(nameof(HasSelectedMotionCard));
}
}
private object _currentMotionDebugPage;
public object CurrentMotionDebugPage
{
get => _currentMotionDebugPage;
private set => SetProperty(ref _currentMotionDebugPage, value);
}
private void RefreshMotionDebugHost()
{
if (_selectedMotionCard == null)
{
CurrentMotionDebugPage = null;
return;
}
try
{
var motionCard = _motionManager.GetMotion(_selectedMotionCard.Name);
if (motionCard == null) return;
CurrentMotionDebugPage = _motionDebugFactory.CreatePage(motionCard);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"加载控制卡调试页失败: {ex.Message}");
}
}
public bool HasSelectedMotionCard => _selectedMotionCard != null;
#endregion
#region 控制卡操作
private void AddMotionCard()
{
var dialog = new AddMotionCardDialog(_motionManager)
{
Owner = Application.Current.MainWindow
};
if (dialog.ShowDialog() != true || dialog.Result == null)
return;
var motionInfo = dialog.Result;
try
{
if (!_motionManager.AddDevice(motionInfo))
{
DialogHelper.ShowError("添加控制卡失败,名称可能重复或适配器不可用");
return;
}
MotionCards.Add(motionInfo);
SelectedMotionCard = motionInfo;
DialogHelper.Success($"已添加 {motionInfo.Name}");
}
catch (Exception ex)
{
DialogHelper.ShowError("添加控制卡失败", ex);
}
}
private void DeleteMotionCard()
{
if (SelectedMotionCard == null) return;
if (!DialogHelper.ConfirmYesNo(
$"确认删除 \"{SelectedMotionCard.Name}\" 吗?",
"确认删除"))
return;
var cardToRemove = SelectedMotionCard;
try
{
_motionManager.RemoveDevice(cardToRemove);
MotionCards.Remove(cardToRemove);
SelectedMotionCard = null;
CurrentMotionDebugPage = null;
}
catch (Exception ex)
{
DialogHelper.ShowError("删除控制卡失败", ex);
}
}
private Task ConnectMotionCardAsync()
{
if (SelectedMotionCard == null) return Task.CompletedTask;
try
{
var motion = _motionManager.GetMotion(SelectedMotionCard.Name);
if (motion == null)
{
var dev = _motionManager.CreateDevice(SelectedMotionCard);
if (dev != null)
_motionManager.Devices.Add(dev);
motion = _motionManager.GetMotion(SelectedMotionCard.Name);
}
if (motion == null)
{
DialogHelper.ShowWarning("无法获取控制卡实例");
return Task.CompletedTask;
}
if (motion.Open())
{
DialogHelper.Success("控制卡连接成功");
RefreshMotionDebugHost();
}
else
{
DialogHelper.ShowError("控制卡连接失败");
}
}
catch (Exception ex)
{
DialogHelper.ShowError("连接异常", ex);
}
return Task.CompletedTask;
}
private void DisconnectMotionCard()
{
if (SelectedMotionCard == null) return;
try
{
var motion = _motionManager.GetMotion(SelectedMotionCard.Name);
motion?.Close();
RefreshMotionDebugHost();
DialogHelper.Info("已断开控制卡");
}
catch (Exception ex)
{
DialogHelper.ShowError("断开异常", ex);
}
}
#endregion
#region 配置
public void ApplyMotionCardEdit(MotionDeviceConfig edited)
{
try
{
if (edited == null) return;
_motionManager.SaveConfig();
RefreshMotionDebugHost();
DialogHelper.Success("控制卡属性已更新并保存");
}
catch (Exception ex)
{
DialogHelper.ShowError("应用控制卡属性失败", ex);
}
}
private void RefreshMotionCardList()
{
MotionCards.Clear();
foreach (var cfg in _motionManager.Configs)
MotionCards.Add(cfg);
SelectedMotionCard = MotionCards.FirstOrDefault();
}
private void SaveMotionCardConfig()
{
try
{
if (_motionManager.SaveConfig())
DialogHelper.Success("控制卡配置已保存");
else
DialogHelper.ShowError("控制卡配置保存失败,请查看日志");
}
catch (Exception ex)
{
DialogHelper.ShowError("保存控制卡配置失败", ex);
}
}
#endregion
}
}