리크 테스트 gui
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

103 lines
3.7 KiB

using System;
using System.Windows.Input;
using leak_test_project.Infrastructure;
using leak_test_project.Services;
using leak_test_project.ViewModels.Core;
namespace leak_test_project.ViewModels
{
public class MainViewModel : ObservableObject, IDisposable
{
private ObservableObject _currentViewModel;
private readonly IoBoardService _ioService;
public HomeViewModel HomeVM { get; }
public InOutViewModel InOutVM { get; }
public DataViewModel DataVM { get; }
public ParametersViewModel ParametersVM { get; }
public ObservableObject CurrentViewModel
{
get => _currentViewModel;
set => SetProperty(ref _currentViewModel, value);
}
public ICommand NavigateHomeCommand { get; }
public ICommand NavigateInOutCommand { get; }
public ICommand NavigateDataCommand { get; }
public ICommand NavigateParametersCommand { get; }
public ICommand NavigateCommunicationCommand { get; }
public ICommand ReadIdTestCommand { get; }
public ICommand ExitCommand { get; }
public MainViewModel()
{
// 팩토리를 통해 설정에 맞는 DIO 보드 생성 (Simulation 여부 자동 판단)
var dioBoard = DioBoardFactory.CreateBoard();
_ioService = new IoBoardService(dioBoard);
_ioService.SetOutput("PC_ON", true);
// ViewModel 생성 시 DIO 보드 공유
HomeVM = new HomeViewModel(dioBoard);
InOutVM = new InOutViewModel(() => CurrentViewModel = HomeVM, _ioService);
DataVM = new DataViewModel(() => CurrentViewModel = HomeVM);
ParametersVM = new ParametersViewModel();
// 기본 View 설정
CurrentViewModel = HomeVM;
NavigateHomeCommand = new RelayCommand(o => CurrentViewModel = HomeVM);
NavigateInOutCommand = new RelayCommand(o =>
{
if (CurrentViewModel is InOutViewModel)
{
CurrentViewModel = HomeVM;
}
else
{
// 매 진입 시 새 인스턴스 생성하여 타이머가 항상 활성화되도록 보장
CurrentViewModel = new InOutViewModel(() => CurrentViewModel = HomeVM, _ioService);
}
});
NavigateDataCommand = new RelayCommand(o =>
{
if (CurrentViewModel == DataVM) CurrentViewModel = HomeVM;
else CurrentViewModel = DataVM;
});
NavigateParametersCommand = new RelayCommand(o =>
{
var win = new leak_test_project.Views.ParametersWindow();
win.Owner = System.Windows.Application.Current.MainWindow;
win.ShowDialog();
});
NavigateCommunicationCommand = new RelayCommand(o =>
{
var win = new leak_test_project.Views.CommunicationWindow();
win.Owner = System.Windows.Application.Current.MainWindow;
win.ShowDialog();
});
ReadIdTestCommand = new RelayCommand(async o =>
{
if (int.TryParse(o?.ToString(), out int index))
{
await HomeVM.TestReadIdAsync(index);
}
});
ExitCommand = new RelayCommand(o =>
{
Dispose();
System.Windows.Application.Current.MainWindow?.Close();
});
}
public void Dispose()
{
_ioService?.SetOutput("PC_ON", false);
HomeVM?.Dispose();
}
}
}