diff options
| author | Avi Levkovich <avi@twine-s.com> | 2018-02-20 16:45:00 +0200 |
|---|---|---|
| committer | Avi Levkovich <avi@twine-s.com> | 2018-02-20 16:45:00 +0200 |
| commit | 6c208c90bc45aff4a7fa214356a42fe7757c5e6f (patch) | |
| tree | 0d77bc6a0ecfbb53cf42c5462ee19212197ee1bd /Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/StudioApplication/DefaultStudioApplicationManager.cs | |
| parent | b0823127f152fe97a6e8fce29e427c7f3db9cf5a (diff) | |
| parent | 1a573aaa346ec4b8bd58a0e35ab9df571a09b855 (diff) | |
| download | Tango-6c208c90bc45aff4a7fa214356a42fe7757c5e6f.tar.gz Tango-6c208c90bc45aff4a7fa214356a42fe7757c5e6f.zip | |
MERGE
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/StudioApplication/DefaultStudioApplicationManager.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/StudioApplication/DefaultStudioApplicationManager.cs | 112 |
1 files changed, 102 insertions, 10 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/StudioApplication/DefaultStudioApplicationManager.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/StudioApplication/DefaultStudioApplicationManager.cs index de8649c13..886985c92 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/StudioApplication/DefaultStudioApplicationManager.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/StudioApplication/DefaultStudioApplicationManager.cs @@ -14,22 +14,50 @@ using System.Collections; using Tango.Integration.Services; using Tango.Core; using Tango.Logging; +using Tango.MachineStudio.Common.Modules; +using Tango.MachineStudio.Common; +using Tango.Settings; namespace Tango.MachineStudio.UI.StudioApplication { + /// <summary> + /// Represents the default Machine Studio <see cref="IStudioApplicationManager">Application Manager</see>. + /// </summary> + /// <seealso cref="Tango.Core.ExtendedObject" /> + /// <seealso cref="Tango.MachineStudio.Common.StudioApplication.IStudioApplicationManager" /> public class DefaultStudioApplicationManager : ExtendedObject, IStudioApplicationManager { private INavigationManager _navigationManager; + private IStudioModuleLoader _moduleLoader; - public DefaultStudioApplicationManager(INavigationManager navigationManager) + /// <summary> + /// Initializes a new instance of the <see cref="DefaultStudioApplicationManager" /> class. + /// </summary> + /// <param name="navigationManager">The navigation manager.</param> + public DefaultStudioApplicationManager(INavigationManager navigationManager, IStudioModuleLoader moduleLoader) { + _moduleLoader = moduleLoader; _navigationManager = navigationManager; } + /// <summary> + /// Gets a value indicating whether Machine Studio is shutting down. + /// </summary> public bool IsShuttingDown { get; private set; } + /// <summary> + /// The connected machine + /// </summary> private IExternalBridgeClient _connectedMachine; + /// <summary> + /// Occurs when the connected machine property has changed. + /// </summary> + public event EventHandler<IExternalBridgeClient> ConnectedMachineChanged; + + /// <summary> + /// Gets or sets the currently connected machine if any. + /// </summary> public IExternalBridgeClient ConnectedMachine { get { return _connectedMachine; } @@ -45,14 +73,24 @@ namespace Tango.MachineStudio.UI.StudioApplication _connectedMachine.StateChanged -= ConnectedMachine_StateChanged; _connectedMachine.StateChanged += ConnectedMachine_StateChanged; } + + ConnectedMachineChanged?.Invoke(this, _connectedMachine); } } + /// <summary> + /// Gets a value indicating whether the <see cref="P:Tango.MachineStudio.Common.StudioApplication.IStudioApplicationManager.ConnectedMachine" /> is valid and connected through TCP/IP. + /// </summary> public bool IsMachineConnectedViaTCP { - get { return IsMachineConnected && ConnectedMachine.Type != ExternalBridgeClientType.USB; } + get { return IsMachineConnected && ConnectedMachine is ExternalBridgeTcpClient; } } + /// <summary> + /// Handles the <see cref="ConnectedMachine"/> state changed event. + /// </summary> + /// <param name="sender">The sender.</param> + /// <param name="e">The e.</param> private void ConnectedMachine_StateChanged(object sender, Transport.TransportComponentState e) { if (e == Transport.TransportComponentState.Disconnected || e == Transport.TransportComponentState.Failed) @@ -62,11 +100,17 @@ namespace Tango.MachineStudio.UI.StudioApplication } + /// <summary> + /// Gets a value indicating whether the <see cref="P:Tango.MachineStudio.Common.StudioApplication.IStudioApplicationManager.ConnectedMachine" /> is valid. + /// </summary> public bool IsMachineConnected { get { return ConnectedMachine != null; } } + /// <summary> + /// Shutdown the application. + /// </summary> public async void ShutDown() { if (IsShuttingDown) return; @@ -75,6 +119,24 @@ namespace Tango.MachineStudio.UI.StudioApplication await Task.Factory.StartNew(async () => { + //Do Shutdown Procedures... + foreach (var vm in ServiceLocator.Current.GetAllInstancesByBase<IShutdownRequestBlocker>()) + { + try + { + var result = await vm.OnShutdownRequest(); + if (!result) + { + IsShuttingDown = false; + return; + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error on shutdown request with " + vm.GetType().Name); + } + } + try { if (ConnectedMachine != null) @@ -87,22 +149,52 @@ namespace Tango.MachineStudio.UI.StudioApplication LogManager.Log(ex, "Error disconnecting from machine."); } - //Do Shutdown Procedures... - foreach (var vm in ServiceLocator.Current.GetAllInstancesByBase<IShutdownRequestBlocker>()) + foreach (var vm in ServiceLocator.Current.GetAllInstancesByBase<IShutdownListener>()) { - var result = await vm.OnShutdownRequest(); - if (!result) - { - IsShuttingDown = false; - return; - } + vm.OnShuttingDown(); + } + + try + { + SettingsManager.SaveDefaultSettings(); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error saving settings."); } _navigationManager.NavigateTo(NavigationView.ShutdownView); + Thread.Sleep(3000); + Environment.Exit(0); }); } + + /// <summary> + /// Loads the specified module if permitted. + /// </summary> + /// <param name="moduleName">Name of the module.</param> + /// <param name="args">The arguments.</param> + public void RequestModule(string moduleName, object args) + { + IStudioModule module = _moduleLoader.UserModules.SingleOrDefault(x => x.Name == moduleName); + + if (module != null) + { + ServiceLocator.Current.GetInstance<ViewModels.MainViewVM>().StartModule(module); + + //Notify request listeners. + foreach (var vm in ServiceLocator.Current.GetAllInstancesByBase<IModuleRequestListener>()) + { + vm.OnRequestModule(module, args); + } + } + else + { + throw new InvalidOperationException("The module was not found or you do not have sufficient privileges."); + } + } } } |
