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; namespace Tango.MachineStudio.UI.StudioApplication { public class DefaultStudioApplicationManager : ExtendedObject, IStudioApplicationManager { private INavigationManager _navigationManager; public DefaultStudioApplicationManager(INavigationManager navigationManager) { _navigationManager = navigationManager; } public bool IsShuttingDown { get; private set; } private IExternalBridgeClient _connectedMachine; 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; } } } public bool IsMachineConnectedViaTCP { get { return IsMachineConnected && ConnectedMachine.Type != ExternalBridgeClientType.USB; } } private void ConnectedMachine_StateChanged(object sender, Transport.TransportComponentState e) { if (e == Transport.TransportComponentState.Disconnected || e == Transport.TransportComponentState.Failed) { ConnectedMachine = null; } } public bool IsMachineConnected { get { return ConnectedMachine != null; } } 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); }); } } }