using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using TeamAAS.Motion;
using TeamAAS.Motion.Models;
using TeamAAS.Views;
namespace TeamAAS.ViewModels
{
///
/// 设置页 - 控制卡管理(列表 / 连接 / 属性)。
/// 调试操作(jog / 复位)已下沉到库内 MotionDebugView。
///
public partial class SettingViewModel
{
#region 控制卡管理
MotionDebugPageFactory Factory=new MotionDebugPageFactory();
public ObservableCollection MotionCards { get; }
private MotionDeviceConfig _selectedMotionCard;
public MotionDeviceConfig SelectedMotionCard
{
get { return _selectedMotionCard; }
set
{
if ( SetProperty(ref _selectedMotionCard, value) )
RefreshMotionDebugHost();
RaisePropertyChanged(nameof(HasSelectedMotionCard));
}
}
private object _currentMotionDebugPage;
/// 右侧调试页内容:RobotDebugPageFactory 创建(品牌专属页或库内默认页),选中机器人时恒非空。
public object CurrentMotionDebugPage
{
get { return _currentMotionDebugPage; }
private set { SetProperty(ref _currentMotionDebugPage, value); }
}
/// 按选中的机器人刷新右侧调试页(工厂恒返回页面并绑定设备实例)。
private void RefreshMotionDebugHost()
{
if ( _selectedMotionCard == null ) return;
try
{
var motionCard = _motionManager.GetMotion(_selectedMotionCard.Name);
if ( motionCard == null ) return;
CurrentMotionDebugPage = Factory.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 )
{
var montionInfo = dialog.Result;
try
{
_motionManager.CreateDevice (montionInfo);
MotionCards.Add(montionInfo);
SelectedMotionCard = montionInfo;
DialogHelper.Success($"已添加 {montionInfo.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;
}
catch ( Exception ex )
{
System.Diagnostics.Debug.WriteLine($"释放控制卡资源异常: {ex.Message}");
}
MotionCards.Remove(CardToRemove);
SelectedRobot = null;
}
private async Task ConnectMotionCardAsync()
{
if ( SelectedMotionCard == null ) return;
try
{
var motion = _motionManager.GetMotion(SelectedMotionCard.Name);
if ( motion == null )
{
_motionManager.CreateDevice(SelectedMotionCard);
motion = _motionManager.GetMotion(SelectedMotionCard.Name);
}
if ( motion == null )
{
DialogHelper.ShowWarning("无法获取控制卡实例");
return;
}
motion.Connect(SelectedMotionCard.ConnectionString);
if ( motion.IsConnected ) DialogHelper.Success("控制卡连接成功");
else DialogHelper.ShowError("控制卡连接失败");
}
catch ( Exception ex )
{
DialogHelper.ShowError("连接异常", ex);
}
}
private void DisconnectMotionCard()
{
if ( SelectedMotionCard == null ) return;
try
{
var motion = _motionManager.GetMotion(SelectedMotionCard.Name);
if ( motion == null ) return;
motion.Disconnect();
DialogHelper.Info("已断开控制卡");
}
catch ( Exception ex )
{
DialogHelper.ShowError("断开异常", ex);
}
}
#endregion
#region 机器人列表刷新 & 配置保存 & 属性编辑
public void ApplyMotionCardEdit(MotionDeviceConfig edited)
{
try
{
DialogHelper.Success("控制卡属性已更新并保存");
}
catch ( Exception ex )
{
DialogHelper.ShowError("应用控制卡属性失败", ex);
}
}
private void RefreshMotionCardList()
{
MotionCards.Clear();
var infos = _motionManager.Devices;
foreach ( var info in infos )
{
MotionCards.Add(( MotionDeviceConfig ) info);
}
}
private void SaveMotionCardConfig()
{
try
{
if ( _motionManager.SaveConfig() )
{
DialogHelper.Success("控制卡配置已保存");
}
else
DialogHelper.ShowError("控制卡配置保存失败,请查看日志");
}
catch ( Exception ex )
{
DialogHelper.ShowError("保存控制卡配置失败", ex);
}
}
#endregion
}
}