diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs | 98 |
1 files changed, 96 insertions, 2 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs index 7ef47fabd..92c0afa21 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs @@ -7,18 +7,24 @@ 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<IMainView> { private IStudioModule _currentModule; + private bool _isDisconnecting; public IStudioModule CurrentModule { @@ -26,6 +32,15 @@ namespace Tango.MachineStudio.UI.ViewModels set { _currentModule = value; RaisePropertyChangedAuto(); } } + private bool _isModuleLoaded; + + public bool IsModuleLoaded + { + get { return _isModuleLoaded; } + set { _isModuleLoaded = value; RaisePropertyChangedAuto(); } + } + + private bool _isMenuOpened; public bool IsMenuOpened @@ -38,6 +53,10 @@ namespace Tango.MachineStudio.UI.ViewModels public RelayCommand HomeCommand { get; set; } + public RelayCommand ConnectCommand { get; set; } + + public RelayCommand DisconnectCommand { get; set; } + private IAuthenticationProvider _authenticationProvider; public IAuthenticationProvider AuthenticationProvider { @@ -61,20 +80,86 @@ namespace Tango.MachineStudio.UI.ViewModels 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) : base(view) + INotificationProvider notificationProvider, + IStudioApplicationManager applicationManager) : base(view) { AuthenticationProvider = authenticationProvider; StudioModuleLoader = studioModuleLoader; NotificationProvider = notificationProvider; + ApplicationManager = applicationManager; StartModuleCommand = new RelayCommand<IStudioModule>(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<MachineConnectionViewVM>((x) => + { + if (x.SelectedMachine != null) + { + _notificationProvider.ShowModalDialog<MachineLoginViewVM>(async (login) => + { + using (NotificationProvider.PushTaskItem("Connecting to machine " + x.SelectedMachine.SerialNumber + "...")) + { + try + { + await x.SelectedMachine.Connect(); + var authenticated = await x.SelectedMachine.Authenticate(login.Password); + if (!authenticated) + { + _notificationProvider.ShowError("It seems like you are not authorized to access the selected machine."); + } + else + { + ApplicationManager.ConnectedMachine = x.SelectedMachine; + } + } + catch (Exception ex) + { + LogManager.Log(ex); + _notificationProvider.ShowError(ex.Message); + } + + InvalidateRelayCommands(); + } + }); + + InvalidateRelayCommands(); + } + + InvalidateRelayCommands(); + }); } private void Home() @@ -85,7 +170,16 @@ namespace Tango.MachineStudio.UI.ViewModels private void StartModule(IStudioModule module) { IsMenuOpened = false; - CurrentModule = module; + + if (module != null) + { + CurrentModule = module; + IsModuleLoaded = true; + } + else + { + IsModuleLoaded = false; + } } protected override void OnViewAttached() |
