using Microsoft.Practices.ServiceLocation; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Tango.Core.Helpers; using Tango.MachineStudio.Common.StudioApplication; using Tango.MachineStudio.Common.Navigation; using GalaSoft.MvvmLight.Ioc; using System.Reflection; 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; using System.Windows; namespace Tango.MachineStudio.UI.StudioApplication { /// /// Represents the default Machine Studio Application Manager. /// /// /// public class DefaultStudioApplicationManager : ExtendedObject, IStudioApplicationManager { private INavigationManager _navigationManager; private IStudioModuleLoader _moduleLoader; private List _openedWindows; /// /// Initializes a new instance of the class. /// /// The navigation manager. public DefaultStudioApplicationManager(INavigationManager navigationManager, IStudioModuleLoader moduleLoader) { _moduleLoader = moduleLoader; _navigationManager = navigationManager; _openedWindows = new List(); } /// /// Gets a value indicating whether Machine Studio is shutting down. /// public bool IsShuttingDown { get; private set; } /// /// The connected machine /// private IExternalBridgeClient _connectedMachine; /// /// Occurs when the connected machine property has changed. /// public event EventHandler ConnectedMachineChanged; /// /// Gets or sets the currently connected machine if any. /// public IExternalBridgeClient ConnectedMachine { get { return _connectedMachine; } set { _connectedMachine = value; RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(IsMachineConnected)); RaisePropertyChanged(nameof(IsMachineConnectedViaTCP)); if (_connectedMachine != null) { _connectedMachine.StateChanged -= ConnectedMachine_StateChanged; _connectedMachine.StateChanged += ConnectedMachine_StateChanged; } ConnectedMachineChanged?.Invoke(this, _connectedMachine); } } /// /// Gets a value indicating whether the is valid and connected through TCP/IP. /// public bool IsMachineConnectedViaTCP { get { return IsMachineConnected && ConnectedMachine is ExternalBridgeTcpClient; } } /// /// Handles the state changed event. /// /// The sender. /// The e. private void ConnectedMachine_StateChanged(object sender, Transport.TransportComponentState e) { if (e == Transport.TransportComponentState.Disconnected || e == Transport.TransportComponentState.Failed) { ConnectedMachine = null; } } /// /// Gets a value indicating whether the is valid. /// public bool IsMachineConnected { get { return ConnectedMachine != null; } } /// /// Gets the machine studio application version. /// public string Version { get { return typeof(DefaultStudioApplicationManager).Assembly.GetName().Version.ToString(); } } /// /// Shutdown the application. /// public async void ShutDown() { if (IsShuttingDown) return; IsShuttingDown = true; await Task.Factory.StartNew(async () => { //Do Shutdown Procedures... foreach (var vm in ServiceLocator.Current.GetAllInstancesByBase()) { 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); } } foreach (var vm in ServiceLocator.Current.GetAllInstancesByBase()) { vm.OnShuttingDown(); } try { SettingsManager.SaveDefaultSettings(); } catch (Exception ex) { LogManager.Log(ex, "Error saving settings."); } _navigationManager.NavigateTo(NavigationView.ShutdownView); Thread.Sleep(1500); foreach (var window in _openedWindows) { ThreadsHelper.InvokeUI(() => { window.Close(); }); } try { if (ConnectedMachine != null) { ConnectedMachine.Disconnect().Wait(); } } catch (Exception ex) { LogManager.Log(ex, "Error disconnecting from machine."); } Thread.Sleep(1500); Environment.Exit(0); }); } /// /// Loads the specified module if permitted. /// /// Name of the module. /// The arguments. public void RequestModule(string moduleName, object args) { IStudioModule module = _moduleLoader.UserModules.SingleOrDefault(x => x.Name == moduleName); if (module != null) { ServiceLocator.Current.GetInstance().StartModule(module); //Notify request listeners. foreach (var vm in ServiceLocator.Current.GetAllInstancesByBase()) { vm.OnRequestModule(module, args); } } else { throw new InvalidOperationException("The module was not found or you do not have sufficient privileges."); } } public void RegisterOpenedWindow(Window window) { _openedWindows.Add(window); window.Closed += (x, y) => { _openedWindows.Remove(window); }; } } }