| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- 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
- {
- /// <summary>
- /// 设置页 - 控制卡管理(列表 / 连接 / 属性)。
- /// 调试操作(jog / 复位)已下沉到库内 MotionDebugView。
- /// </summary>
- public partial class SettingViewModel
- {
- #region 控制卡管理
- MotionDebugPageFactory Factory=new MotionDebugPageFactory();
- public ObservableCollection<MotionDeviceConfig> MotionCards { get; }
- private MotionDeviceConfig _selectedMotionCard;
- public MotionDeviceConfig SelectedMotionCard
- {
- get { return _selectedMotionCard; }
- set
- {
- if ( SetProperty(ref _selectedMotionCard, value) )
- RefreshMotionDebugHost();
- RaisePropertyChanged(nameof(HasSelectedMotionCard));
- }
- }
- private object _currentMotionDebugPage;
- /// <summary>右侧调试页内容:RobotDebugPageFactory 创建(品牌专属页或库内默认页),选中机器人时恒非空。</summary>
- public object CurrentMotionDebugPage
- {
- get { return _currentMotionDebugPage; }
- private set { SetProperty(ref _currentMotionDebugPage, value); }
- }
- /// <summary>按选中的机器人刷新右侧调试页(工厂恒返回页面并绑定设备实例)。</summary>
- 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
- }
- }
|