修复CNC运行 与算子编辑
This commit is contained in:
@@ -8,11 +8,17 @@ using System.ComponentModel;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Media.Imaging;
|
||||||
|
using XplorePlane.Events;
|
||||||
using XplorePlane.Models;
|
using XplorePlane.Models;
|
||||||
using XplorePlane.Services;
|
using XplorePlane.Services;
|
||||||
|
using XplorePlane.Services.MainViewport;
|
||||||
using XP.Common.Logging.Interfaces;
|
using XP.Common.Logging.Interfaces;
|
||||||
|
using Prism.Events;
|
||||||
|
|
||||||
namespace XplorePlane.ViewModels.Cnc
|
namespace XplorePlane.ViewModels.Cnc
|
||||||
{
|
{
|
||||||
@@ -21,6 +27,9 @@ namespace XplorePlane.ViewModels.Cnc
|
|||||||
private readonly CncEditorViewModel _editorViewModel;
|
private readonly CncEditorViewModel _editorViewModel;
|
||||||
private readonly IImageProcessingService _imageProcessingService;
|
private readonly IImageProcessingService _imageProcessingService;
|
||||||
private readonly IPipelinePersistenceService _persistenceService;
|
private readonly IPipelinePersistenceService _persistenceService;
|
||||||
|
private readonly IPipelineExecutionService _executionService;
|
||||||
|
private readonly IMainViewportService _mainViewportService;
|
||||||
|
private readonly IEventAggregator _eventAggregator;
|
||||||
private readonly ILoggerService _logger;
|
private readonly ILoggerService _logger;
|
||||||
|
|
||||||
private CncNodeViewModel _activeModuleNode;
|
private CncNodeViewModel _activeModuleNode;
|
||||||
@@ -29,17 +38,26 @@ namespace XplorePlane.ViewModels.Cnc
|
|||||||
private string _pipelineFileDisplayName = "未命名模块.xpm";
|
private string _pipelineFileDisplayName = "未命名模块.xpm";
|
||||||
private string _currentFilePath;
|
private string _currentFilePath;
|
||||||
private bool _isSynchronizing;
|
private bool _isSynchronizing;
|
||||||
|
private CancellationTokenSource _debounceCts;
|
||||||
|
|
||||||
|
private const int DebounceDelayMs = 300;
|
||||||
|
|
||||||
public CncInspectionModulePipelineViewModel(
|
public CncInspectionModulePipelineViewModel(
|
||||||
CncEditorViewModel editorViewModel,
|
CncEditorViewModel editorViewModel,
|
||||||
IImageProcessingService imageProcessingService,
|
IImageProcessingService imageProcessingService,
|
||||||
IPipelinePersistenceService persistenceService,
|
IPipelinePersistenceService persistenceService,
|
||||||
ILoggerService logger)
|
ILoggerService logger,
|
||||||
|
IPipelineExecutionService executionService = null,
|
||||||
|
IMainViewportService mainViewportService = null,
|
||||||
|
IEventAggregator eventAggregator = null)
|
||||||
{
|
{
|
||||||
_editorViewModel = editorViewModel ?? throw new ArgumentNullException(nameof(editorViewModel));
|
_editorViewModel = editorViewModel ?? throw new ArgumentNullException(nameof(editorViewModel));
|
||||||
_imageProcessingService = imageProcessingService ?? throw new ArgumentNullException(nameof(imageProcessingService));
|
_imageProcessingService = imageProcessingService ?? throw new ArgumentNullException(nameof(imageProcessingService));
|
||||||
_persistenceService = persistenceService ?? throw new ArgumentNullException(nameof(persistenceService));
|
_persistenceService = persistenceService ?? throw new ArgumentNullException(nameof(persistenceService));
|
||||||
_logger = (logger ?? throw new ArgumentNullException(nameof(logger))).ForModule<CncInspectionModulePipelineViewModel>();
|
_logger = (logger ?? throw new ArgumentNullException(nameof(logger))).ForModule<CncInspectionModulePipelineViewModel>();
|
||||||
|
_executionService = executionService;
|
||||||
|
_mainViewportService = mainViewportService;
|
||||||
|
_eventAggregator = eventAggregator;
|
||||||
|
|
||||||
PipelineNodes = new ObservableCollection<PipelineNodeViewModel>();
|
PipelineNodes = new ObservableCollection<PipelineNodeViewModel>();
|
||||||
|
|
||||||
@@ -373,6 +391,52 @@ namespace XplorePlane.ViewModels.Cnc
|
|||||||
|
|
||||||
_activeModuleNode.Pipeline = BuildPipelineModel();
|
_activeModuleNode.Pipeline = BuildPipelineModel();
|
||||||
StatusMessage = statusMessage;
|
StatusMessage = statusMessage;
|
||||||
|
TriggerDebouncedPreview();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TriggerDebouncedPreview()
|
||||||
|
{
|
||||||
|
if (_executionService == null || _mainViewportService == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var sourceImage = _mainViewportService.CurrentDisplayImage as BitmapSource
|
||||||
|
?? _mainViewportService.LatestManualImage as BitmapSource;
|
||||||
|
if (sourceImage == null)
|
||||||
|
{
|
||||||
|
_logger.Debug("[图像链路][CNC] TriggerDebouncedPreview:无可用源图像,跳过");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_debounceCts?.Cancel();
|
||||||
|
_debounceCts = new CancellationTokenSource();
|
||||||
|
var token = _debounceCts.Token;
|
||||||
|
|
||||||
|
Task.Delay(DebounceDelayMs, token).ContinueWith(t =>
|
||||||
|
{
|
||||||
|
if (!t.IsCanceled)
|
||||||
|
_ = ExecutePreviewAsync(sourceImage, token);
|
||||||
|
}, TaskScheduler.FromCurrentSynchronizationContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ExecutePreviewAsync(BitmapSource sourceImage, CancellationToken token)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_logger.Info("[图像链路][CNC] ExecutePreviewAsync:开始执行,节点数={Count}", PipelineNodes.Count);
|
||||||
|
var result = await _executionService.ExecutePipelineAsync(PipelineNodes, sourceImage, null, token);
|
||||||
|
_logger.Info("[图像链路][CNC] ExecutePreviewAsync:执行完成,推送结果图像");
|
||||||
|
_mainViewportService.SetManualImage(result, string.Empty);
|
||||||
|
_eventAggregator?.GetEvent<PipelinePreviewUpdatedEvent>()
|
||||||
|
.Publish(new PipelinePreviewUpdatedPayload(result, StatusMessage));
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
_logger.Debug("[图像链路][CNC] ExecutePreviewAsync:已取消");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error(ex, "[图像链路][CNC] ExecutePreviewAsync:执行失败");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private PipelineModel BuildPipelineModel()
|
private PipelineModel BuildPipelineModel()
|
||||||
|
|||||||
@@ -61,12 +61,18 @@ namespace XplorePlane.Views.Cnc
|
|||||||
var imageProcessingService = ContainerLocator.Current.Resolve<IImageProcessingService>();
|
var imageProcessingService = ContainerLocator.Current.Resolve<IImageProcessingService>();
|
||||||
var persistenceService = ContainerLocator.Current.Resolve<IPipelinePersistenceService>();
|
var persistenceService = ContainerLocator.Current.Resolve<IPipelinePersistenceService>();
|
||||||
var logger = ContainerLocator.Current.Resolve<ILoggerService>();
|
var logger = ContainerLocator.Current.Resolve<ILoggerService>();
|
||||||
|
var executionService = ContainerLocator.Current.Resolve<IPipelineExecutionService>();
|
||||||
|
var mainViewportService = ContainerLocator.Current.Resolve<XplorePlane.Services.MainViewport.IMainViewportService>();
|
||||||
|
var eventAggregator = ContainerLocator.Current.Resolve<Prism.Events.IEventAggregator>();
|
||||||
|
|
||||||
_inspectionModulePipelineViewModel = new CncInspectionModulePipelineViewModel(
|
_inspectionModulePipelineViewModel = new CncInspectionModulePipelineViewModel(
|
||||||
editorViewModel,
|
editorViewModel,
|
||||||
imageProcessingService,
|
imageProcessingService,
|
||||||
persistenceService,
|
persistenceService,
|
||||||
logger);
|
logger,
|
||||||
|
executionService,
|
||||||
|
mainViewportService,
|
||||||
|
eventAggregator);
|
||||||
|
|
||||||
InspectionModulePipelineEditor.DataContext = _inspectionModulePipelineViewModel;
|
InspectionModulePipelineEditor.DataContext = _inspectionModulePipelineViewModel;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user