using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Tango.Core.Commands; using Tango.Logging; using Tango.MachineStudio.Common; using Tango.MachineStudio.Common.Authentication; using Tango.MachineStudio.Common.Modules; using Tango.MachineStudio.Common.Notifications; using Tango.MachineStudio.Common.StudioApplication; using Tango.MachineStudio.UI.SupervisingController; using Tango.MachineStudio.UI.Views; using Tango.PMR.Stubs; using Tango.SharedUI; using Tango.Transport.Adapters; namespace Tango.MachineStudio.UI.ViewModels { public class MainViewVM : ViewModel { private IStudioModule _currentModule; private bool _isDisconnecting; public IStudioModule CurrentModule { get { return _currentModule; } set { _currentModule = value; RaisePropertyChangedAuto(); } } private bool _isModuleLoaded; public bool IsModuleLoaded { get { return _isModuleLoaded; } set { _isModuleLoaded = value; RaisePropertyChangedAuto(); } } private bool _isMenuOpened; public bool IsMenuOpened { get { return _isMenuOpened; } set { _isMenuOpened = value; RaisePropertyChangedAuto(); } } public RelayCommand StartModuleCommand { get; set; } public RelayCommand HomeCommand { get; set; } public RelayCommand ConnectCommand { get; set; } public RelayCommand DisconnectCommand { get; set; } private IAuthenticationProvider _authenticationProvider; public IAuthenticationProvider AuthenticationProvider { get { return _authenticationProvider; } set { _authenticationProvider = value; RaisePropertyChangedAuto(); } } private IStudioModuleLoader _studioModuleLoader; public IStudioModuleLoader StudioModuleLoader { get { return _studioModuleLoader; } set { _studioModuleLoader = value; RaisePropertyChangedAuto(); } } private INotificationProvider _notificationProvider; public INotificationProvider NotificationProvider { get { return _notificationProvider; } set { _notificationProvider = value; RaisePropertyChangedAuto(); } } private IStudioApplicationManager _applicationManager; public IStudioApplicationManager ApplicationManager { get { return _applicationManager; } set { _applicationManager = value; RaisePropertyChangedAuto(); } } public MainViewVM( IMainView view, IAuthenticationProvider authenticationProvider, IStudioModuleLoader studioModuleLoader, INotificationProvider notificationProvider, IStudioApplicationManager applicationManager) : base(view) { AuthenticationProvider = authenticationProvider; StudioModuleLoader = studioModuleLoader; NotificationProvider = notificationProvider; ApplicationManager = applicationManager; StartModuleCommand = new RelayCommand(StartModule); HomeCommand = new RelayCommand(Home); ConnectCommand = new RelayCommand(ConnectToMachine); DisconnectCommand = new RelayCommand(DisconnectFromMachine,(x) => ApplicationManager.IsMachineConnected && !_isDisconnecting); } private async void DisconnectFromMachine() { using (_notificationProvider.PushTaskItem("Disconnecting from machine...")) { _isDisconnecting = true; InvalidateRelayCommands(); await ApplicationManager.ConnectedMachine.Disconnect(); ApplicationManager.ConnectedMachine = null; _isDisconnecting = false; InvalidateRelayCommands(); } } private void ConnectToMachine() { _notificationProvider.ShowModalDialog(async (x) => { if (x.SelectedMachine != null) { using (NotificationProvider.PushTaskItem("Connecting to machine " + x.SelectedMachine.SerialNumber + "...")) { try { await x.SelectedMachine.Connect(); var authenticated = await x.SelectedMachine.Authenticate(); if (!authenticated) { _notificationProvider.ShowError("It seems like you are not authorized to access the selected machine."); } else { ApplicationManager.ConnectedMachine = x.SelectedMachine; var response = await x.SelectedMachine.SendRequest(new CalculateRequest() { A = 10, B = 5 }); _notificationProvider.ShowInfo(response.ToString()); } } catch (Exception ex) { LogManager.Log(ex); _notificationProvider.ShowError(ex.Message); } } } InvalidateRelayCommands(); }); } private void Home() { StartModule(null); } private void StartModule(IStudioModule module) { IsMenuOpened = false; if (module != null) { CurrentModule = module; IsModuleLoaded = true; } else { IsModuleLoaded = false; } } protected override void OnViewAttached() { base.OnViewAttached(); } } }