台架驱动
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
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