diff --git a/ADP/App.xaml.cs b/ADP/App.xaml.cs index e41103a..0e50155 100644 --- a/ADP/App.xaml.cs +++ b/ADP/App.xaml.cs @@ -18,7 +18,7 @@ using static System.Runtime.InteropServices.JavaScript.JSType; using UIShare.GlobalVariable; using UIShare; using System; -using DeviceCommand.Device; +using DeviceCommand.Devices; using DeviceCommand.Base; using AutoMapper; using Microsoft.Extensions.Logging.Abstractions; diff --git a/ADP/ViewModels/ShellViewModel.cs b/ADP/ViewModels/ShellViewModel.cs index 71e3e72..0ad9680 100644 --- a/ADP/ViewModels/ShellViewModel.cs +++ b/ADP/ViewModels/ShellViewModel.cs @@ -1,5 +1,5 @@  -using DeviceCommand.Device; +using DeviceCommand.Devices; using Logger; using MaterialDesignThemes.Wpf; using Microsoft.Win32; diff --git a/BenchMovementModule/BenchMovementModule.cs b/BenchMovementModule/BenchMovementModule.cs index 8036819..6041442 100644 --- a/BenchMovementModule/BenchMovementModule.cs +++ b/BenchMovementModule/BenchMovementModule.cs @@ -1,7 +1,5 @@ using System.Reflection; using BenchMovementModule.Views; -using BenchMovementModule.Views.Dialogs; -using BenchMovementModule.ViewModels.Dialogs; namespace BenchMovementModule { @@ -16,9 +14,6 @@ namespace BenchMovementModule public void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation("BenchMovementView"); - - // 台架 TCP 连接配置弹窗 - containerRegistry.RegisterDialog("GantryConfigDialog"); } } } diff --git a/BenchMovementModule/HardwareDrive/GantryControlTcp.cs b/BenchMovementModule/HardwareDrive/GantryControlTcp.cs deleted file mode 100644 index dae9bd9..0000000 --- a/BenchMovementModule/HardwareDrive/GantryControlTcp.cs +++ /dev/null @@ -1,27 +0,0 @@ -using DeviceCommand.Base; -using Model.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace BenchMovementModule.HardwareDrive -{ - public class GantryControlTcp : GantryControlBase - { - // 暴露出底层的 ModbusTcp 实例以便修改网络配置 - public ModbusTcp TcpDevice => (ModbusTcp)_device; - - public GantryControlTcp(TcpConfig config, byte slaveAddress = 1) - : base(new ModbusTcp(config), slaveAddress) - { - } - - public GantryControlTcp(string ipAddress, int port = 502, byte slaveAddress = 1) - : base(new ModbusTcp(), slaveAddress) - { - TcpDevice.ConfigureDevice(ipAddress, port); - } - } -} diff --git a/BenchMovementModule/ViewModels/BenchMovementViewModel.cs b/BenchMovementModule/ViewModels/BenchMovementViewModel.cs index 21f87a7..d1690c3 100644 --- a/BenchMovementModule/ViewModels/BenchMovementViewModel.cs +++ b/BenchMovementModule/ViewModels/BenchMovementViewModel.cs @@ -1,10 +1,13 @@ using System; +using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Input; using System.Windows.Threading; -using BenchMovementModule.HardwareDrive; +using DeviceCommand.Devices; using Logger; +using Prism.Ioc; +using UIShare.GlobalVariable; using UIShare.ViewModelBase; namespace BenchMovementModule.ViewModels @@ -15,8 +18,10 @@ namespace BenchMovementModule.ViewModels public bool KeepAlive => true; - // ===== GantryControlTcp 实例 ===== + // ===== GantryControlTcp 实例(从 DeviceManager 获取)===== private GantryControlTcp? _gantry; + private DeviceManager? _deviceManager; + private readonly GlobalInfo _globalInfo; // ===== 连接状态 ===== private bool _isConnected; @@ -116,7 +121,6 @@ namespace BenchMovementModule.ViewModels #endregion #region 命令 - public ICommand OpenConfigCommand { get; } public ICommand ConnectCommand { get; } public ICommand DisconnectCommand { get; } public ICommand MoveXCommand { get; } @@ -140,10 +144,7 @@ namespace BenchMovementModule.ViewModels public BenchMovementViewModel(IContainerProvider containerProvider) : base(containerProvider) { - // 创建 GantryControlTcp 实例(默认配置,用户可通过弹窗修改) - _gantry = new GantryControlTcp("192.168.0.30", 502); - - OpenConfigCommand = new DelegateCommand(OnOpenConfig); + _globalInfo = containerProvider.Resolve(); ConnectCommand = new DelegateCommand(OnConnect); DisconnectCommand = new DelegateCommand(OnDisconnect); MoveXCommand = new DelegateCommand(OnMoveX); @@ -169,16 +170,6 @@ namespace BenchMovementModule.ViewModels #region 命令处理 - private void OnOpenConfig() - { - var param = new DialogParameters(); - param.Add("Gantry", _gantry); - _dialogService.ShowDialog("GantryConfigDialog", param, result => - { - UpdateConnectionStatus(); - }); - } - private async void OnConnect() { if (_gantry == null) return; @@ -470,14 +461,60 @@ namespace BenchMovementModule.ViewModels if (!_isInitiated) { _isInitiated = true; + InitGantryFromDeviceManager(); + } + } + + /// + /// 从 DeviceManager.DeviceMap 中查找 GantryControlTcp 实例, + /// 优先使用当前打开的作用域,找不到则取任意已注册作用域。 + /// + private void InitGantryFromDeviceManager() + { + try + { + // 优先使用当前打开的作用域 + IScopedProvider? scope = null; + if (!string.IsNullOrEmpty(_globalInfo.CurrentOpeningScope) + && _globalInfo.ScopeDic.TryGetValue(_globalInfo.CurrentOpeningScope, out scope)) + { + // 找到了 + } + else + { + // 退而求其次,取任意可用作用域 + scope = _globalInfo.ScopeDic.Values.FirstOrDefault(); + } + + if (scope == null) + { + LoggerHelper.Warn("[BenchMovement] 无可用的作用域,无法解析 DeviceManager。"); + return; + } + + _deviceManager = scope.Resolve(); + _gantry = _deviceManager.DeviceMap.Values.OfType().FirstOrDefault(); + + if (_gantry != null) + { + LoggerHelper.Info($"[BenchMovement] 已从 DeviceManager 获取 GantryControlTcp 实例。"); + UpdateConnectionStatus(); + } + else + { + LoggerHelper.Warn("[BenchMovement] DeviceManager 中未找到 GantryControlTcp 设备,请检查设备配置。"); + } + } + catch (Exception ex) + { + LoggerHelper.Error($"[BenchMovement] 初始化 GantryControlTcp 失败: {ex.Message}"); } } public void Dispose() { _pollTimer.Stop(); - _gantry?.Close(); - _gantry?.TcpDevice?.Dispose(); + // GantryControlTcp 由 DeviceManager 统一管理生命周期,此处仅停止轮询 } #endregion diff --git a/BenchMovementModule/ViewModels/Dialogs/GantryConfigDialogViewModel.cs b/BenchMovementModule/ViewModels/Dialogs/GantryConfigDialogViewModel.cs deleted file mode 100644 index eb2d1f6..0000000 --- a/BenchMovementModule/ViewModels/Dialogs/GantryConfigDialogViewModel.cs +++ /dev/null @@ -1,202 +0,0 @@ -using System.Collections.ObjectModel; -using System.Windows.Input; -using BenchMovementModule.HardwareDrive; -using Logger; -using UIShare.ViewModelBase; - -namespace BenchMovementModule.ViewModels.Dialogs -{ - public class GantryConfigDialogViewModel : DialogViewModelBase - { - #region 属性 - - private string _title = "台架 TCP 连接配置"; - public string Title - { - get => _title; - set => SetProperty(ref _title, value); - } - - private string _ipAddress = "192.168.0.30"; - public string IpAddress - { - get => _ipAddress; - set => SetProperty(ref _ipAddress, value); - } - - private int _port = 502; - public int Port - { - get => _port; - set => SetProperty(ref _port, value); - } - - private int _sendTimeout = 3000; - public int SendTimeout - { - get => _sendTimeout; - set => SetProperty(ref _sendTimeout, value); - } - - private int _receiveTimeout = 3000; - public int ReceiveTimeout - { - get => _receiveTimeout; - set => SetProperty(ref _receiveTimeout, value); - } - - private bool _isConnected; - public bool IsConnected - { - get => _isConnected; - set => SetProperty(ref _isConnected, value); - } - - private string _connectionStatus = "未连接"; - public string ConnectionStatus - { - get => _connectionStatus; - set => SetProperty(ref _connectionStatus, value); - } - - private string _errorMessage = string.Empty; - public string ErrorMessage - { - get => _errorMessage; - set => SetProperty(ref _errorMessage, value); - } - - public ObservableCollection CommonPorts { get; } = new() - { - 502, 102, 80, 8080, 5020, 4840 - }; - - public ObservableCollection CommonTimeouts { get; } = new() - { - 500, 1000, 2000, 3000, 5000, 10000 - }; - - #endregion - - #region 命令 - public ICommand ConnectCommand { get; } - public ICommand DisconnectCommand { get; } - public ICommand CloseCommand { get; } - #endregion - - private GantryControlTcp? _gantry; - - public GantryConfigDialogViewModel(IContainerProvider containerProvider) : base(containerProvider) - { - ConnectCommand = new DelegateCommand(OnConnect); - DisconnectCommand = new DelegateCommand(OnDisconnect); - CloseCommand = new DelegateCommand(OnClose); - } - - private void ApplyConfigToDevice() - { - if (_gantry?.TcpDevice == null) return; - _gantry.TcpDevice.ConfigureDevice(IpAddress, Port, SendTimeout, ReceiveTimeout); - } - - private async void OnConnect() - { - ErrorMessage = string.Empty; - - if (string.IsNullOrWhiteSpace(IpAddress)) - { - ErrorMessage = "IP 地址不能为空"; - return; - } - if (!System.Net.IPAddress.TryParse(IpAddress, out _)) - { - ErrorMessage = "IP 地址格式不正确"; - return; - } - if (Port <= 0 || Port > 65535) - { - ErrorMessage = "端口范围应在 1 - 65535"; - return; - } - - try - { - ConnectionStatus = "连接中..."; - ApplyConfigToDevice(); - bool ok = await _gantry!.ConnectAsync(); - if (ok) - { - IsConnected = true; - ConnectionStatus = "已连接"; - LoggerHelper.Info($"[BenchMovement] 台架连接成功: {IpAddress}:{Port}"); - } - else - { - ConnectionStatus = "连接失败"; - ErrorMessage = "连接失败,请检查网络和设备状态"; - } - } - catch (Exception ex) - { - ConnectionStatus = "连接失败"; - ErrorMessage = $"连接异常: {ex.Message}"; - LoggerHelper.Error($"[BenchMovement] 连接异常: {ex.Message}"); - } - } - - private void OnDisconnect() - { - if (_gantry == null) return; - try - { - _gantry.Close(); - IsConnected = false; - ConnectionStatus = "未连接"; - ErrorMessage = string.Empty; - LoggerHelper.Info("[BenchMovement] 台架已断开"); - } - catch (Exception ex) - { - ErrorMessage = $"断开失败: {ex.Message}"; - LoggerHelper.Error($"[BenchMovement] 断开失败: {ex.Message}"); - } - } - - private void OnClose() - { - RequestClose.Invoke(ButtonResult.OK); - } - - #region Prism Dialog 规范 - - public override void OnDialogOpened(IDialogParameters parameters) - { - _eventAggregator.GetEvent().Publish(true); - - if (parameters.ContainsKey("Gantry")) - { - _gantry = parameters.GetValue("Gantry"); - - // 从现有设备读取当前配置 - if (_gantry?.TcpDevice != null) - { - IpAddress = _gantry.TcpDevice.IPAddress; - Port = _gantry.TcpDevice.Port; - SendTimeout = _gantry.TcpDevice.SendTimeout; - ReceiveTimeout = _gantry.TcpDevice.ReceiveTimeout; - } - - // 同步当前连接状态 - IsConnected = _gantry?.IsConnected ?? false; - ConnectionStatus = IsConnected ? "已连接" : "未连接"; - } - } - - public override void OnDialogClosed() - { - _eventAggregator.GetEvent().Publish(false); - } - - #endregion - } -} diff --git a/BenchMovementModule/Views/BenchMovementView.xaml b/BenchMovementModule/Views/BenchMovementView.xaml index d4aadf4..0b57a9b 100644 --- a/BenchMovementModule/Views/BenchMovementView.xaml +++ b/BenchMovementModule/Views/BenchMovementView.xaml @@ -195,8 +195,7 @@ -