| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- 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
- {
- /// <summary>
- /// 设置页 - 机器人管理(列表 / 连接 / 属性)。
- /// 调试操作(jog / 电机 / 复位 / 刹车 / Tool / 坐标)已下沉到库内 DefaultRobotDebugPage。
- /// </summary>
- public partial class SettingViewModel
- {
- #region 机器人管理
- public ObservableCollection<RobotInfo> Robots { get; }
- private RobotInfo _selectedRobot;
- public RobotInfo SelectedRobot
- {
- get { return _selectedRobot; }
- set
- {
- if (SetProperty(ref _selectedRobot, value))
- RefreshRobotDebugHost();
- RaisePropertyChanged(nameof(HasSelectedRobot));
- }
- }
- private object _currentRobotDebugPage;
- /// <summary>右侧调试页内容:RobotDebugPageFactory 创建(品牌专属页或库内默认页),选中机器人时恒非空。</summary>
- public object CurrentRobotDebugPage
- {
- get { return _currentRobotDebugPage; }
- private set { SetProperty(ref _currentRobotDebugPage, value); }
- }
- /// <summary>按选中的机器人刷新右侧调试页(工厂恒返回页面并绑定设备实例)。</summary>
- 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<RobotInfo>(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
- }
- }
|