| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- 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
- {
- /// <summary>
- /// 设置页 - 控制卡管理(列表 / 连接 / 属性)。
- /// 调试操作已下沉到库内 MotionDebugView。
- /// </summary>
- public partial class SettingViewModel
- {
- #region 控制卡管理
- private readonly MotionDebugPageFactory _motionDebugFactory = new MotionDebugPageFactory();
- public ObservableCollection<MotionDeviceConfig> 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
- }
- }
|