From f6e5152939f613029d80bcc1aec868aaa04b8f16 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 9 Oct 2020 16:58:39 +0200 Subject: [PATCH 1/2] connect new gui to vr engine --- ClientApp/Models/Info.cs | 2 ++ ClientApp/Utils/Client.cs | 22 +++++++++------------- ClientApp/ViewModels/LoginViewModel.cs | 1 + ClientApp/ViewModels/MainViewModel.cs | 24 ++++++++++++++++++++++++ ClientApp/Views/MainView.xaml | 2 +- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/ClientApp/Models/Info.cs b/ClientApp/Models/Info.cs index e976bda..350d6e8 100644 --- a/ClientApp/Models/Info.cs +++ b/ClientApp/Models/Info.cs @@ -10,5 +10,7 @@ namespace ClientApp.Models public bool ConnectedToServer { get; set; } public bool ConnectedToVREngine { get; set; } public bool DoctorConnected { get; set; } + + public bool CanConnectToVR { get; set; } } } diff --git a/ClientApp/Utils/Client.cs b/ClientApp/Utils/Client.cs index 0de2ba7..3d74c8b 100644 --- a/ClientApp/Utils/Client.cs +++ b/ClientApp/Utils/Client.cs @@ -8,15 +8,18 @@ using ProftaakRH; namespace ClientApp.Utils { + public delegate void EngineCallback(); public class Client : IDataReceiver { + public EngineCallback engineConnectFailed; + public EngineCallback engineConnectSuccess; private TcpClient client; private NetworkStream stream; private byte[] buffer = new byte[1024]; private bool connected; private byte[] totalBuffer = new byte[1024]; private int totalBufferReceived = 0; - private EngineConnection engineConnection; + public EngineConnection engineConnection; private bool sessionRunning = false; private IHandler handler = null; private LoginViewModel LoginViewModel; @@ -40,7 +43,7 @@ namespace ClientApp.Utils private void initEngine() { engineConnection = EngineConnection.INSTANCE; - engineConnection.OnNoTunnelId = retryEngineConnection; + engineConnection.OnNoTunnelId = RetryEngineConnection; engineConnection.OnSuccessFullConnection = engineConnected; if (!engineConnection.Connected) engineConnection.Connect(); } @@ -48,24 +51,16 @@ namespace ClientApp.Utils /// /// retries to connect to the VR engine if no tunnel id was found /// - private void retryEngineConnection() + public void RetryEngineConnection() { - Console.WriteLine("-- Could not connect to the VR engine. Please make sure you are running the simulation!"); - Console.WriteLine("-- Press ENTER to retry connecting to the VR engine."); - Console.WriteLine("-- Press 'q' and then ENTER to not connect to the VR engine"); - string input = Console.ReadLine(); - if (input == string.Empty) engineConnection.CreateConnection(); - else - { - Console.WriteLine("Skipping connecting to VR engine..."); - engineConnection.Stop(); - } + engineConnectFailed?.Invoke(); } private void engineConnected() { Console.WriteLine("successfully connected to VR engine"); + engineConnectSuccess?.Invoke(); engineConnection.initScene(); if (engineConnection.Connected && sessionRunning && !engineConnection.FollowingRoute) engineConnection.StartRouteFollow(); } @@ -126,6 +121,7 @@ namespace ClientApp.Utils this.LoginViewModel.setLoginStatus(true); this.connected = true; initEngine(); + } else { diff --git a/ClientApp/ViewModels/LoginViewModel.cs b/ClientApp/ViewModels/LoginViewModel.cs index a5e9d26..c9f02b9 100644 --- a/ClientApp/ViewModels/LoginViewModel.cs +++ b/ClientApp/ViewModels/LoginViewModel.cs @@ -15,6 +15,7 @@ namespace ClientApp.ViewModels public string Username { get; set; } public ICommand LoginCommand { get; set; } public bool LoginStatus { get; set; } + private MainWindowViewModel mainWindowViewModel; public LoginViewModel(MainWindowViewModel mainWindowViewModel) { diff --git a/ClientApp/ViewModels/MainViewModel.cs b/ClientApp/ViewModels/MainViewModel.cs index ee13d90..d609e9a 100644 --- a/ClientApp/ViewModels/MainViewModel.cs +++ b/ClientApp/ViewModels/MainViewModel.cs @@ -11,10 +11,14 @@ namespace ClientApp.ViewModels public ICommand RetryVREngineCommand { get; set; } public MainWindowViewModel MainWindowViewModel { get; set; } + private Client client; public MainViewModel(MainWindowViewModel mainWindowViewModel) { this.MainWindowViewModel = mainWindowViewModel; + client = this.MainWindowViewModel.client; + client.engineConnectFailed = retryEngineConnection; + client.engineConnectSuccess = succesEngineConnection; this.RetryServerCommand = new RelayCommand(() => { //try connect server @@ -23,8 +27,28 @@ namespace ClientApp.ViewModels this.RetryVREngineCommand = new RelayCommand(() => { //try connect vr-engine + this.MainWindowViewModel.InfoModel.ConnectedToVREngine = true; + this.MainWindowViewModel.InfoModel.CanConnectToVR = false; + client.engineConnection.CreateConnection(); + }); } + + private void retryEngineConnection() + { + this.MainWindowViewModel.InfoModel.ConnectedToVREngine = false; + this.MainWindowViewModel.InfoModel.CanConnectToVR = true; + client.engineConnection.CreateConnection(); + } + + private void succesEngineConnection() + { + this.MainWindowViewModel.InfoModel.ConnectedToVREngine = true; + this.MainWindowViewModel.InfoModel.CanConnectToVR = false; + + } + + } } diff --git a/ClientApp/Views/MainView.xaml b/ClientApp/Views/MainView.xaml index 72a0ff2..c0997f6 100644 --- a/ClientApp/Views/MainView.xaml +++ b/ClientApp/Views/MainView.xaml @@ -40,7 +40,7 @@ -