using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using System.Collections.ObjectModel; using VisionBuilder.UI.Common.Commands; using VisionBuilder.UI.Common.Commands.Interfaces; using VisionBuilder.UI.Common.RecipeProcessing; using VisionBuilder.UI.Common.ViewModel; using VisionBuilder.UI.Common.ViewModel.Classes; using VisionBuilder.UI.Common.ViewModel.Interfaces.UI; namespace VisionBuilder.UI.Common { public partial class SingleCameraVM : ObservableObject, IEventHandler { private readonly IRecipeSelectionDialogService _recipeSelectionDialogService; private readonly IRecognitionControl _recognitionControl; public ErrorsVM ErrorsVm { get; private set; } public StatisticsVM StatisticsVm { get; private set; } public PreviewVM PreviewVm { get; private set; } [ObservableProperty] private string _currentRecipeName = "Select recipe"; [ObservableProperty] [NotifyPropertyChangedFor(nameof(CanStart))] [NotifyPropertyChangedFor(nameof(CanStop))] [NotifyCanExecuteChangedFor(nameof(SelectRecipeCommand))] [NotifyCanExecuteChangedFor(nameof(StartCommand))] [NotifyCanExecuteChangedFor(nameof(StopCommand))] private bool _isRunning; [ObservableProperty] private string _cameraLabel; public bool CanStop => IsRunning; public bool CanStart => !IsRunning && SelectedRecipe!=null; public bool IsNotRunning => !IsRunning; private bool _handlingEnabled = true; [ObservableProperty] [NotifyPropertyChangedFor(nameof(CanStop))] [NotifyPropertyChangedFor(nameof(CanStart))] [NotifyCanExecuteChangedFor(nameof(SelectRecipeCommand))] [NotifyCanExecuteChangedFor(nameof(StartCommand))] [NotifyCanExecuteChangedFor(nameof(StopCommand))] private RecipeData? _selectedRecipe; public SingleCameraVM(CameraSettings cameraSettings, UIConfiguration uiConfiguration, IRecipeSelectionDialogService recipeSelectionDialogService, IRecognitionControl recognitionControl, IImagePreviewService imagePreviewService, ILearningTool learningTool) { _recipeSelectionDialogService = recipeSelectionDialogService; _recognitionControl = recognitionControl; ErrorsVm = new ErrorsVM(uiConfiguration, imagePreviewService, learningTool); StatisticsVm = new StatisticsVM(); PreviewVm = new PreviewVM(); CameraLabel = cameraSettings.CameraLabel; recognitionControl.ImageProcessed += Handle; recognitionControl.SessionStarted += StatisticsVm.Handle; recognitionControl.SessionStarted += ErrorsVm.Handle; } [RelayCommand(CanExecute = nameof(IsNotRunning))] public void SelectRecipe() { var vm = new RecipeSelectionVM(); vm.Recipes = new ObservableCollection(_recognitionControl.GetRecipesData()); if (!_recipeSelectionDialogService.SelectRecipe(vm))return; SelectedRecipe = vm.SelectedRecipe; _recognitionControl.SetRecipe(SelectedRecipe); CurrentRecipeName = SelectedRecipe?.RecipeName ?? "Select recipe"; } [RelayCommand(CanExecute = nameof(CanStart))] public void Start() { _handlingEnabled = true; _recognitionControl.Start(); IsRunning = true; } [RelayCommand(CanExecute = nameof(CanStop))] public void Stop() { _handlingEnabled = false; _recognitionControl.Stop(); IsRunning = false; } public void Handle(ImageProcessedEvent @event) { if (!_handlingEnabled) return; try { PreviewVm.Handle(@event); StatisticsVm.Handle(@event); ErrorsVm.Handle(@event); } catch(Exception e) { } } } }