From 6a64a71a5197897cfa6d904274399bad3fb0b2da Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Sat, 18 Jan 2020 17:23:37 +0200 Subject: Implemented Machine Studio => PPC auto reconnection. Implemented SignalR Adapter reconnection. --- .../StudioApplication/IStudioApplicationManager.cs | 5 + .../DefaultStudioApplicationManager.cs | 15 ++- .../ViewModels/ConnectionLostViewVM.cs | 64 +++++++++ .../ViewModels/MainViewVM.cs | 145 ++++++++++++--------- .../Views/ConnectionLostView.xaml | 9 +- 5 files changed, 174 insertions(+), 64 deletions(-) (limited to 'Software/Visual_Studio/MachineStudio') diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/StudioApplication/IStudioApplicationManager.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/StudioApplication/IStudioApplicationManager.cs index 3e54a327b..fabe3e02f 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/StudioApplication/IStudioApplicationManager.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/StudioApplication/IStudioApplicationManager.cs @@ -20,6 +20,11 @@ namespace Tango.MachineStudio.Common.StudioApplication /// event EventHandler ApplicationReady; + /// + /// Occurs when the connected machine session has been lost and an automatic reconnection with the last machine is required. + /// + event EventHandler ReconnectionRequired; + /// /// Occurs when the connected machine property has changed. /// 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 0235f8cca..2c3c9ccb9 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/StudioApplication/DefaultStudioApplicationManager.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/StudioApplication/DefaultStudioApplicationManager.cs @@ -48,6 +48,11 @@ namespace Tango.MachineStudio.UI.StudioApplication /// public event EventHandler ApplicationReady; + /// + /// Occurs when the connected machine session has been lost and an automatic reconnection with the last machine is required. + /// + public event EventHandler ReconnectionRequired; + /// /// Initializes a new instance of the class. /// @@ -126,6 +131,8 @@ namespace Tango.MachineStudio.UI.StudioApplication { if (e == Transport.TransportComponentState.Disconnected || e == Transport.TransportComponentState.Failed) { + bool reconnect = ConnectedMachine is IExternalBridgeSecureClient; + ConnectedMachine = null; if (e == Transport.TransportComponentState.Failed) @@ -134,12 +141,16 @@ namespace Tango.MachineStudio.UI.StudioApplication ConnectionLostViewVM vm = new ConnectionLostViewVM() { - Exception = failed_reason + Exception = failed_reason, + AutoReconnect = reconnect, }; InvokeUI(() => { - _notification.ShowModalDialog(vm, (x) => { }, () => { }); + _notification.ShowModalDialog(vm, (x) => + { + ReconnectionRequired?.Invoke(this, new EventArgs()); + }, () => { }); }); } } diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/ConnectionLostViewVM.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/ConnectionLostViewVM.cs index f1f4f69c0..e96f0ab62 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/ConnectionLostViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/ConnectionLostViewVM.cs @@ -3,12 +3,76 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Timers; +using System.Windows.Threading; using Tango.SharedUI; namespace Tango.MachineStudio.UI.ViewModels { public class ConnectionLostViewVM : DialogViewVM { + private Timer _reconnectTimer; + public String Exception { get; set; } + + private int _reconnectinSeconds; + public int ReconnectinSeconds + { + get { return _reconnectinSeconds; } + set { _reconnectinSeconds = value; RaisePropertyChangedAuto(); } + } + + private bool _autoReconnect; + public bool AutoReconnect + { + get { return _autoReconnect; } + set { _autoReconnect = value; RaisePropertyChangedAuto(); } + } + + + public ConnectionLostViewVM() : base() + { + ReconnectinSeconds = 10; + _reconnectTimer = new Timer(); + _reconnectTimer.Interval = TimeSpan.FromSeconds(1).TotalMilliseconds; + _reconnectTimer.Elapsed += _reconnectTimer_Elapsed; + } + + private void _reconnectTimer_Elapsed(object sender, ElapsedEventArgs e) + { + ReconnectinSeconds--; + + if (ReconnectinSeconds == -1) + { + _reconnectTimer.Stop(); + + InvokeUI(() => + { + Accept(); + }); + } + } + + public override void OnShow() + { + base.OnShow(); + + if (AutoReconnect) + { + _reconnectTimer.Start(); + } + } + + protected override void Accept() + { + _reconnectTimer.Stop(); + base.Accept(); + } + + protected override void Cancel() + { + base.Cancel(); + _reconnectTimer.Stop(); + } } } 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 a7397835b..ce3b825c8 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/MainViewVM.cs @@ -58,6 +58,8 @@ namespace Tango.MachineStudio.UI.ViewModels private IEventLogger _eventLogger; private MachineStudioSettings _settings; private MachineStudioWebClient _machineStudioWebClient; + private IExternalBridgeSecureClient _reconnectionMachine; + private MachineLoginViewVM _reconnectionMachineConfig; /// /// Gets or sets the current loaded module. @@ -344,6 +346,13 @@ namespace Tango.MachineStudio.UI.ViewModels AboutCommand = new RelayCommand(ShowAboutDialog); ChangeAppThemeCommand = new RelayCommand(ChangeTheme); + + ApplicationManager.ReconnectionRequired += ApplicationManager_ReconnectionRequired; + } + + private void ApplicationManager_ReconnectionRequired(object sender, EventArgs e) + { + ConnectToMachineSecure(_reconnectionMachine, _reconnectionMachineConfig); } private void MachineEventsStateProvider_EventsResolved(object sender, IEnumerable e) @@ -465,62 +474,7 @@ namespace Tango.MachineStudio.UI.ViewModels if (x.SelectedMachine.RequiresAuthentication) { - //Check machine exist on my database first - if (x.SelectedMachine.Machine == null) - { - _notificationProvider.ShowError($"The specified machine '{x.SelectedMachine.SerialNumber}' could not be found on the database. Aborting connection."); - return; - } - - _notificationProvider.ShowModalDialog(async (login) => - { - using (NotificationProvider.PushTaskItem("Connecting to machine " + x.SelectedMachine.ToString() + "...")) - { - try - { - await x.SelectedMachine.As().Connect(new PMR.Integration.ExternalBridgeLoginRequest() - { - AppID = "Machine Studio", - HostName = Environment.MachineName, - Password = login.Password, - UserGuid = AuthenticationProvider.CurrentUser.Guid, - Intent = login.Intent, - }); - - ApplicationManager.SetConnectedMachine(x.SelectedMachine); - (x.SelectedMachine as IExternalBridgeSecureClient).SessionClosed += (_, __) => - { - InvokeUI(() => - { - _notificationProvider.ShowError("The remote machine has closed the current session. Machine disconnected."); - ApplicationManager.SetConnectedMachine(null); - }); - }; - PostMessage(new MachineConnectionChangedMessage() { Machine = x.SelectedMachine }); - _eventLogger.Log(String.Format("Successfully connected to machine {0} via TCP", x.SelectedMachine.SerialNumber)); - - //if (x.UploadHardwareConfiguration) - //{ - // UploadHardwareConfiguration(false); - //} - - } - catch (ResponseErrorException ex) - { - LogManager.Log(ex); - _eventLogger.Log(ex, "Error connecting to machine " + x.SelectedMachine.SerialNumber); - _notificationProvider.ShowError("Could not connect to the selected machine." + Environment.NewLine + ex.Container.ErrorMessage); - } - catch (Exception ex) - { - LogManager.Log(ex); - _eventLogger.Log(ex, "Error connecting to machine " + x.SelectedMachine.SerialNumber); - _notificationProvider.ShowError("Could not connect to the selected machine." + Environment.NewLine + ex.Message); - } - - InvalidateRelayCommands(); - } - }); + ConnectToMachineSecure(x.SelectedMachine as IExternalBridgeSecureClient); } else { @@ -633,6 +587,77 @@ namespace Tango.MachineStudio.UI.ViewModels InvalidateRelayCommands(); } + private async void ConnectToMachineSecure(IExternalBridgeSecureClient machine, MachineLoginViewVM config) + { + using (NotificationProvider.PushTaskItem("Connecting to machine " + machine.ToString() + "...")) + { + try + { + await machine.Connect(new PMR.Integration.ExternalBridgeLoginRequest() + { + AppID = "Machine Studio", + HostName = Environment.MachineName, + Password = config.Password, + UserGuid = AuthenticationProvider.CurrentUser.Guid, + Intent = config.Intent, + }); + + _reconnectionMachine = machine; + _reconnectionMachineConfig = config; + + ApplicationManager.SetConnectedMachine(machine); + machine.SessionClosed -= Machine_SessionClosed; + machine.SessionClosed += Machine_SessionClosed; + PostMessage(new MachineConnectionChangedMessage() { Machine = machine }); + _eventLogger.Log(String.Format("Successfully connected to machine {0} via TCP", machine.SerialNumber)); + + //if (x.UploadHardwareConfiguration) + //{ + // UploadHardwareConfiguration(false); + //} + + } + catch (ResponseErrorException ex) + { + LogManager.Log(ex); + _eventLogger.Log(ex, "Error connecting to machine " + machine.SerialNumber); + _notificationProvider.ShowError("Could not connect to the selected machine." + Environment.NewLine + ex.Container.ErrorMessage); + } + catch (Exception ex) + { + LogManager.Log(ex); + _eventLogger.Log(ex, "Error connecting to machine " + machine.SerialNumber); + _notificationProvider.ShowError("Could not connect to the selected machine." + Environment.NewLine + ex.Message); + } + + InvalidateRelayCommands(); + } + } + + private void ConnectToMachineSecure(IExternalBridgeSecureClient machine) + { + //Check machine exist on my database first + if (machine.Machine == null) + { + _notificationProvider.ShowError($"The specified machine '{machine.SerialNumber}' could not be found on the database. Aborting connection."); + return; + } + + _notificationProvider.ShowModalDialog((login) => + { + ConnectToMachineSecure(machine, login); + }); + } + + private void Machine_SessionClosed(object sender, EventArgs e) + { + InvokeUI(() => + { + _notificationProvider.ShowError("The remote machine has closed the current session. Machine disconnected."); + ApplicationManager.SetConnectedMachine(null); + }); + } + private async void UploadHardwareConfiguration(bool showSuccessMessage = true) { try @@ -706,10 +731,10 @@ namespace Tango.MachineStudio.UI.ViewModels { IsModuleLoaded = false; LogManager.Log(String.Format("Navigating to Home...")); - (MainView.Self as MainView).NavigationControl.NavigateTo("Home",() => - { - CurrentModule = module; - }); + (MainView.Self as MainView).NavigationControl.NavigateTo("Home", () => + { + CurrentModule = module; + }); } } diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Views/ConnectionLostView.xaml b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Views/ConnectionLostView.xaml index 28937a438..ba5624592 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Views/ConnectionLostView.xaml +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Views/ConnectionLostView.xaml @@ -7,7 +7,7 @@ xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:local="clr-namespace:Tango.MachineStudio.UI.Views" mc:Ignorable="d" - Width="559" Height="266" Background="{StaticResource Dialog.Background}" d:DataContext="{d:DesignInstance Type=vm:ConnectionLostViewVM, IsDesignTimeCreatable=False}" Foreground="{StaticResource MainWindow.Foreground}"> + Width="559" Height="350" Background="{StaticResource Dialog.Background}" d:DataContext="{d:DesignInstance Type=vm:ConnectionLostViewVM, IsDesignTimeCreatable=False}" Foreground="{StaticResource MainWindow.Foreground}"> @@ -50,7 +50,12 @@ - + + Reconnecting in + + seconds... + + -- cgit v1.3.1