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; namespace Tango.MachineStudio.UI.StudioApplication { /// /// Represents the default Machine Studio Application Manager. /// /// /// public class DefaultStudioApplicationManager : ExtendedObject, IStudioApplicationManager { private INavigationManager _navigationManager; private IStudioModuleLoader _moduleLoader; /// /// Initializes a new instance of the class. /// /// The navigation manager. public DefaultStudioApplicationManager(INavigationManager navigationManager, IStudioModuleLoader moduleLoader) { _moduleLoader = moduleLoader; _navigationManager = navigationManager; } /// /// 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; } } /// /// Shutdown the application. /// public async void ShutDown() { if (IsShuttingDown) return; IsShuttingDown = true; await Task.Factory.StartNew(async () => { try { if (ConnectedMachine != null) { ConnectedMachine.Disconnect().Wait(); } } catch (Exception ex) { LogManager.Log(ex, "Error disconnecting from machine."); } //Do Shutdown Procedures... foreach (var vm in ServiceLocator.Current.GetAllInstancesByBase()) { var result = await vm.OnShutdownRequest(); if (!result) { IsShuttingDown = false; return; } } _navigationManager.NavigateTo(NavigationView.ShutdownView); Thread.Sleep(3000); 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."); } } } }