台架程序tp化
This commit is contained in:
@@ -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<GlobalInfo>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从 DeviceManager.DeviceMap 中查找 GantryControlTcp 实例,
|
||||
/// 优先使用当前打开的作用域,找不到则取任意已注册作用域。
|
||||
/// </summary>
|
||||
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<DeviceManager>();
|
||||
_gantry = _deviceManager.DeviceMap.Values.OfType<GantryControlTcp>().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
|
||||
|
||||
@@ -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<int> CommonPorts { get; } = new()
|
||||
{
|
||||
502, 102, 80, 8080, 5020, 4840
|
||||
};
|
||||
|
||||
public ObservableCollection<int> 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<UIShare.PubEvent.OverlayEvent>().Publish(true);
|
||||
|
||||
if (parameters.ContainsKey("Gantry"))
|
||||
{
|
||||
_gantry = parameters.GetValue<GantryControlTcp>("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<UIShare.PubEvent.OverlayEvent>().Publish(false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user