台架程序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
|
||||
|
||||
Reference in New Issue
Block a user