299 lines
8.0 KiB
C#
299 lines
8.0 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using Heisig.HMI.AdsManager;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using TcEventLoggerAdsProxyLib;
|
|
using InfineonHMI.Pages.Views;
|
|
|
|
namespace InfineonHMI;
|
|
|
|
public sealed partial class MainWindowVM : ObservableObject, IRecipient<NavigateMessage>, IDisposable
|
|
{
|
|
|
|
[ObservableProperty] private StringControlButtonVM dummyStringVM;
|
|
|
|
[ObservableProperty] private Page currentPage;
|
|
|
|
[ObservableProperty] private Visibility statusBarVisible;
|
|
|
|
[ObservableProperty] private string breadcrumb;
|
|
|
|
[ObservableProperty] private string actualUser;
|
|
|
|
private const string _actualUserPrefix = "Aktueller Benutzer: \n";
|
|
|
|
private readonly IAdsManager _adsManager;
|
|
private readonly IConfiguration _config;
|
|
private readonly TcEventLogger _eventlogger;
|
|
|
|
// Last active event
|
|
[ObservableProperty] private string currentActiveEvent = "";
|
|
|
|
private readonly object _lock = new();
|
|
|
|
// Empty page
|
|
private readonly Page _emptyPage;
|
|
|
|
// Last navigate message
|
|
private readonly Stack<NavigateMessage> _messageStack = new();
|
|
NavigateMessage? _currentMessage;
|
|
|
|
// Events page view model
|
|
[ObservableProperty] EventsPageVM _eventsPageVM;
|
|
|
|
// Settings page viem model
|
|
SettingsPageVM? _settingsPageVM;
|
|
|
|
ProductionOverviewPageVM? _productionOverviewPageVM;
|
|
|
|
private MachineOverviewPageVM? _machineOverviewPageVM;
|
|
|
|
// Hot Coolplate page view model
|
|
HotCoolPlatePageVM? _hotCoolplatePageVM;
|
|
|
|
|
|
// Kuka Robot page view model
|
|
ReceipePageVM? _receipePageVM;
|
|
|
|
public MainWindowVM(IAdsManager adsManager, IConfiguration config, TcEventLogger eventLogger)
|
|
{
|
|
_adsManager = adsManager;
|
|
_config = config;
|
|
|
|
ActualUser = _actualUserPrefix + "---------";
|
|
// Create dummy string
|
|
DummyStringVM = new StringControlButtonVM();
|
|
|
|
// Create empty page
|
|
_emptyPage = new();
|
|
|
|
// Create events page viewmodel
|
|
_eventlogger = eventLogger;
|
|
_eventsPageVM = new(_eventlogger);
|
|
|
|
CurrentPage = _emptyPage;
|
|
_currentMessage = new NavigateMessage("", typeof(Page));
|
|
_messageStack.Push(_currentMessage);
|
|
|
|
WeakReferenceMessenger.Default.Register<NavigateMessage>(this);
|
|
|
|
breadcrumb = "";
|
|
}
|
|
|
|
public void NavigateFromOuterPage(NavigateMessage message, NavigateMessage nextMessage)
|
|
{
|
|
_currentMessage = message;
|
|
|
|
Navigate(message, nextMessage);
|
|
}
|
|
|
|
|
|
[RelayCommand]
|
|
private void SettingsWindow()
|
|
{
|
|
StatusBarVisible = Visibility.Visible;
|
|
_messageStack.Clear();
|
|
NavigateMessage message = new("", typeof(SettingsPage));
|
|
Receive(message);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void ChangeUserClicked()
|
|
{
|
|
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void WorkingModeSelectionClicked()
|
|
{
|
|
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void OverviewWindowClicked()
|
|
{
|
|
_messageStack.Clear();
|
|
_currentMessage = new NavigateMessage("", typeof(Page));
|
|
NavigateMessage message = new(_config[""]!, typeof(MachineOverviewPage));
|
|
Receive(message);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void ProductionWindowClicked()
|
|
{
|
|
StatusBarVisible = Visibility.Visible;
|
|
_messageStack.Clear();
|
|
_currentMessage = new NavigateMessage("", typeof(Page));
|
|
NavigateMessage message = new(_config[""]!, typeof(ProductionOverviewPage));
|
|
Receive(message);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void ProtocolWindowClicked()
|
|
{
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void ReceipesWindowClicked()
|
|
{
|
|
StatusBarVisible = Visibility.Visible;
|
|
_messageStack.Clear();
|
|
_currentMessage = new NavigateMessage("", typeof(Page));
|
|
NavigateMessage message = new(_config[""]!, typeof(ReceipePage));
|
|
Receive(message);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void TrendWindowClicked()
|
|
{
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void ComponentsWindowClicked()
|
|
{
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void SettingsWindowClicked()
|
|
{
|
|
_messageStack.Clear();
|
|
NavigateMessage message = new("", typeof(SettingsPage));
|
|
Receive(message);
|
|
}
|
|
|
|
|
|
[RelayCommand]
|
|
public void EventsListClicked()
|
|
{
|
|
StatusBarVisible = Visibility.Collapsed;
|
|
_messageStack.Clear();
|
|
_currentMessage = new NavigateMessage("", typeof(Page));
|
|
NavigateMessage message = new("", typeof(EventsPage));
|
|
Receive(message);
|
|
}
|
|
|
|
|
|
|
|
// Only use for forward traversal!
|
|
public void Receive(NavigateMessage message)
|
|
{
|
|
// Only change page if its a new page type
|
|
if (CurrentPage.GetType() == message.type)
|
|
return;
|
|
|
|
// Push current message
|
|
if (_currentMessage != null)
|
|
_messageStack.Push(_currentMessage);
|
|
|
|
// Save current message for later push
|
|
_currentMessage = message;
|
|
|
|
|
|
Navigate(message);
|
|
}
|
|
|
|
private void Navigate(NavigateMessage message, NavigateMessage? nextMessage = null)
|
|
{
|
|
// Dispose current pages viewmodel
|
|
if (CurrentPage.DataContext is IDisposable viewModel)
|
|
{
|
|
CurrentPage.DataContext = null;
|
|
viewModel.Dispose();
|
|
}
|
|
|
|
// Create new page
|
|
switch (message.type.Name)
|
|
{
|
|
case nameof(ProductionOverviewPage):
|
|
|
|
if (_productionOverviewPageVM == null || nextMessage != null)
|
|
_productionOverviewPageVM = new ProductionOverviewPageVM(_adsManager, _config, _eventlogger, nextMessage);
|
|
ProductionOverviewPage productionOverviewPage = new() { DataContext = _productionOverviewPageVM };
|
|
CurrentPage = productionOverviewPage;
|
|
break;
|
|
|
|
case nameof(MachineOverviewPage):
|
|
_machineOverviewPageVM?.Dispose();
|
|
|
|
_machineOverviewPageVM = new MachineOverviewPageVM(_adsManager, _config,this, new ProductionOverviewPageVM(_adsManager, _config, _eventlogger), _eventlogger);
|
|
MachineOverviewPage machineOverviewPage = new() { DataContext = _machineOverviewPageVM };
|
|
CurrentPage = machineOverviewPage;
|
|
break;
|
|
|
|
case nameof(EventsPage):
|
|
#pragma warning disable MVVMTK0034 // Direct field reference to [ObservableProperty] backing field
|
|
EventsPage eventsPage = new() { DataContext = _eventsPageVM };
|
|
#pragma warning restore MVVMTK0034 // Direct field reference to [ObservableProperty] backing field
|
|
CurrentPage = eventsPage;
|
|
Breadcrumb = " > Events";
|
|
break;
|
|
|
|
case nameof(SettingsPage):
|
|
// Create seetings page view model only once
|
|
if (_settingsPageVM == null)
|
|
_settingsPageVM = new(_adsManager, "GVL_CONFIG.stUnitConfig");
|
|
|
|
SettingsPage settingsPage = new() { DataContext = _settingsPageVM };
|
|
CurrentPage = settingsPage;
|
|
Breadcrumb = " > Settings";
|
|
break;
|
|
|
|
case nameof(ReceipePage):
|
|
if (_receipePageVM == null)
|
|
_receipePageVM = new();
|
|
|
|
ReceipePage receipePage = new() { DataContext = _receipePageVM };
|
|
CurrentPage = receipePage;
|
|
Breadcrumb = " > Kuka Roboter";
|
|
break;
|
|
|
|
|
|
|
|
case nameof(HotCoolPlatePage):
|
|
if (_hotCoolplatePageVM == null)
|
|
_hotCoolplatePageVM = new(_adsManager, "GVL_Config.stHotCoolplateConfig");
|
|
|
|
HotCoolPlatePage hotCoolPlatePage = new() {DataContext = _hotCoolplatePageVM };
|
|
CurrentPage = hotCoolPlatePage;
|
|
Breadcrumb = " > Heiz- /Kühlplatte";
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
CurrentPage = new Page();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void AppendBreadcrumb(string path)
|
|
{
|
|
if (Breadcrumb[^1] != ' ')
|
|
Breadcrumb += " > " + path;
|
|
else
|
|
Breadcrumb += "> " + path;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void AckAlarms()
|
|
{
|
|
_adsManager.WriteValue("GVL_SCADA.stConfirmAlarmsBtn.xRequest", true);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Dispose current pages viewmodel
|
|
if (CurrentPage.DataContext is IDisposable viewModel)
|
|
{
|
|
CurrentPage.DataContext = null;
|
|
viewModel.Dispose();
|
|
}
|
|
|
|
DummyStringVM.Dispose();
|
|
}
|
|
}
|