using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading.Tasks; using System.Windows; using TeamAAS.Robot.Models; using TeamAAS.Robot.UI; using TeamAAS.Views; namespace TeamAAS.ViewModels { /// /// 设置页 - 机器人管理(列表 / 连接 / 属性)。 /// 调试操作(jog / 电机 / 复位 / 刹车 / Tool / 坐标)已下沉到库内 DefaultRobotDebugPage。 /// public partial class SettingViewModel { #region 机器人管理 public ObservableCollection Robots { get; } private RobotInfo _selectedRobot; public RobotInfo SelectedRobot { get { return _selectedRobot; } set { if (SetProperty(ref _selectedRobot, value)) RefreshRobotDebugHost(); RaisePropertyChanged(nameof(HasSelectedRobot)); } } private object _currentRobotDebugPage; /// 右侧调试页内容:RobotDebugPageFactory 创建(品牌专属页或库内默认页),选中机器人时恒非空。 public object CurrentRobotDebugPage { get { return _currentRobotDebugPage; } private set { SetProperty(ref _currentRobotDebugPage, value); } } /// 按选中的机器人刷新右侧调试页(工厂恒返回页面并绑定设备实例)。 private void RefreshRobotDebugHost() { (_currentRobotDebugPage as IRobotDebugPage)?.UnbindRobot(); CurrentRobotDebugPage = null; if (_selectedRobot == null) return; try { var robot = _robotManager.GetRobot(_selectedRobot.Id); if (robot == null) return; CurrentRobotDebugPage = RobotDebugPageFactory.Create(robot); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"加载机器人调试页失败: {ex.Message}"); } } public bool HasSelectedRobot => _selectedRobot != null; #endregion #region 机器人操作(列表管理 + 连接) private void AddRobot() { var dialog = new AddRobotDialog(Robots.Count) { Owner = Application.Current.MainWindow }; if (dialog.ShowDialog() == true && dialog.Result != null) { var robotInfo = dialog.Result; robotInfo.RobotNo = Robots.Count + 1; try { _robotManager.CreateRobot(robotInfo.Id, robotInfo); Robots.Add(robotInfo); SelectedRobot = robotInfo; DialogHelper.Success($"已添加 {robotInfo.RobotName}"); } catch (Exception ex) { DialogHelper.ShowError("添加机器人失败", ex); } } } private void DeleteRobot() { if (SelectedRobot == null) return; if (!DialogHelper.ConfirmYesNo( $"确认删除 \"{SelectedRobot.RobotName}\" 吗?", "确认删除")) { return; } var robotToRemove = SelectedRobot; try { _robotManager.RemoveRobot(robotToRemove.Id); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"释放机器人资源异常: {ex.Message}"); } Robots.Remove(robotToRemove); for (int i = 0; i < Robots.Count; i++) { Robots[i].RobotNo = i + 1; } SelectedRobot = null; } private async Task ConnectRobotAsync() { if (SelectedRobot == null) return; try { var robot = _robotManager.GetRobot(SelectedRobot.Id); if (robot == null) { _robotManager.CreateRobot(SelectedRobot.Id, SelectedRobot); robot = _robotManager.GetRobot(SelectedRobot.Id); } if (robot == null) { DialogHelper.ShowWarning("无法获取机器人实例"); return; } await robot.ConnectAsync(); if (robot.IsConnected) DialogHelper.Success("机器人连接成功"); else DialogHelper.ShowError("机器人连接失败"); } catch (Exception ex) { DialogHelper.ShowError("连接异常", ex); } } private void DisconnectRobot() { if (SelectedRobot == null) return; try { var robot = _robotManager.GetRobot(SelectedRobot.Id); if (robot == null) return; robot.Disconnect(); DialogHelper.Info("已断开机器人"); } catch (Exception ex) { DialogHelper.ShowError("断开异常", ex); } } #endregion #region 机器人列表刷新 & 配置保存 & 属性编辑 public void ApplyRobotEdit(RobotInfo edited) { try { var infos = _robotManager.GetAllRobotInfos(); var list = new List(infos); int idx = list.FindIndex(r => r.Id == edited.Id); if (idx >= 0) list[idx] = edited; _robotManager.InitializeAllRobots(list.ToArray()); _robotManager.SaveConfig(); RefreshRobotList(); DialogHelper.Success("机器人属性已更新并保存"); } catch (Exception ex) { DialogHelper.ShowError("应用机器人属性失败", ex); } } private void RefreshRobotList() { Robots.Clear(); var infos = _robotManager.GetAllRobotInfos(); foreach (var info in infos) { Robots.Add(info); } } private void SaveRobotConfig() { try { if (_robotManager.SaveConfig()) { RefreshRobotList(); DialogHelper.Success("机器人配置已保存"); } else DialogHelper.ShowError("机器人配置保存失败,请查看日志"); } catch (Exception ex) { DialogHelper.ShowError("保存机器人配置失败", ex); } } #endregion } }