| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- 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.Motion.Motions;
- 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();
- ReloadMotionCardAxes(edited);
- RefreshMotionDebugHost();
- DialogHelper.Success("控制卡属性已更新并保存");
- }
- catch (Exception ex)
- {
- DialogHelper.ShowError("应用控制卡属性失败", ex);
- }
- }
- private void EditMotionAxes()
- {
- if (SelectedMotionCard == null) return;
- var current = MotionAxisCatalog.GetEffective(SelectedMotionCard);
- var dialog = new EditMotionAxesDialog(current)
- {
- Owner = Application.Current.MainWindow
- };
- if (dialog.ShowDialog() != true || dialog.Result == null)
- return;
- SelectedMotionCard.Axes = dialog.Result;
- SelectedMotionCard.AxisCount = dialog.Result.Count;
- _motionManager.SaveConfig();
- ReloadMotionCardAxes(SelectedMotionCard);
- RefreshMotionDebugHost();
- DialogHelper.Success("轴配置已保存");
- }
- private void EditMotionSafety()
- {
- if (SelectedMotionCard == null) return;
- var current = MotionAxisCatalog.GetEffective(SelectedMotionCard);
- var dialog = new EditMotionSafetyDialog(SelectedMotionCard, current)
- {
- Owner = Application.Current.MainWindow
- };
- if (dialog.ShowDialog() != true || dialog.Result == null)
- return;
- SelectedMotionCard.EstopDiIndex = dialog.EstopDiIndex;
- SelectedMotionCard.EstopDiBank = dialog.EstopDiBank;
- SelectedMotionCard.EstopActiveHigh = dialog.EstopActiveHigh;
- SelectedMotionCard.StopDiIndex = dialog.StopDiIndex;
- SelectedMotionCard.StopDiBank = dialog.StopDiBank;
- SelectedMotionCard.StopActiveHigh = dialog.StopActiveHigh;
- if (SelectedMotionCard.Axes == null || SelectedMotionCard.Axes.Count == 0)
- SelectedMotionCard.Axes = current;
- foreach (var row in dialog.Result)
- {
- var ax = SelectedMotionCard.Axes.FirstOrDefault(a => a != null && a.AxisNo == row.AxisNo);
- if (ax == null) continue;
- ax.SoftLimitEnabled = row.SoftLimitEnabled;
- ax.SoftLimitNeg = row.SoftLimitNeg;
- ax.SoftLimitPos = row.SoftLimitPos;
- ax.MaxVelocity = row.MaxVelocity;
- }
- _motionManager.SaveConfig();
- ReloadMotionCardAxes(SelectedMotionCard);
- RefreshMotionDebugHost();
- DialogHelper.Success("安全配置已保存");
- }
- private void ReloadMotionCardAxes(MotionDeviceConfig cfg)
- {
- var motion = _motionManager.GetMotion(cfg.Name);
- if (motion is InovanceMotionCard inovance)
- inovance.ReloadFromConfig();
- else if (motion is SimulatedMotionCard simulated)
- simulated.ReloadFromConfig();
- }
- 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
- }
- }
|