From 9103a5959f072279b73be0394cafa59a22a04bdd Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Mon, 14 Mar 2022 22:52:46 +0100 Subject: [PATCH] Update: API Connection in BFFHService --- Borepin/Borepin/Model/MachineVisualize.cs | 10 +-- .../Borepin/PageModel/MachineListPageModel.cs | 10 +-- Borepin/Borepin/PageModel/MachinePageModel.cs | 4 +- Borepin/Borepin/Service/BFFH/BFFHService.cs | 70 ++++++++----------- Borepin/Borepin/Service/BFFH/IBFFHService.cs | 4 +- 5 files changed, 41 insertions(+), 57 deletions(-) diff --git a/Borepin/Borepin/Model/MachineVisualize.cs b/Borepin/Borepin/Model/MachineVisualize.cs index d04dd9b..c1011ab 100644 --- a/Borepin/Borepin/Model/MachineVisualize.cs +++ b/Borepin/Borepin/Model/MachineVisualize.cs @@ -30,7 +30,7 @@ namespace Borepin.Model Manager = new UserVisualize(_Machine.Manager); Manager.LoadData(); - MachineInfoExtended machineInfoExtended = (await _Machine.Info.GetMachineInfoExtended().ConfigureAwait(false)).Item1; + MachineInfoExtended machineInfoExtended = await _Machine.Manage.GetMachineInfoExtended().ConfigureAwait(false); if(machineInfoExtended != null) { if (machineInfoExtended.CurrentUser == null || machineInfoExtended.CurrentUser.Username == null) @@ -43,13 +43,13 @@ namespace Borepin.Model CurrentUser.LoadData(); } - if (machineInfoExtended.TransferUser == null || machineInfoExtended.TransferUser.Username == null) + if (machineInfoExtended.LastUser == null || machineInfoExtended.LastUser.Username == null) { LastUser = null; } else { - LastUser = new UserVisualize(machineInfoExtended.TransferUser); + LastUser = new UserVisualize(machineInfoExtended.LastUser); LastUser.LoadData(); } } @@ -61,7 +61,7 @@ namespace Borepin.Model CanUse = !((UseInterface_Proxy)_Machine.Use).IsNull; CanInUse = !((InUseInterface_Proxy) _Machine.Inuse).IsNull; - CanTransfer = !((TransferInterface_Proxy) _Machine.Transfer).IsNull; + CanTakeOver = !((TakeoverInterface_Proxy) _Machine.Takeover).IsNull; CanCheck = !((CheckInterface_Proxy) _Machine.Check).IsNull; CanManage = !((ManageInterface_Proxy) _Machine.Manage).IsNull; CanAdmin = !((AdminInterface_Proxy) _Machine.Admin).IsNull; @@ -140,7 +140,7 @@ namespace Borepin.Model } private bool _CanTransfer; - public bool CanTransfer + public bool CanTakeOver { get => _CanTransfer; set => SetProperty(ref _CanTransfer, value); diff --git a/Borepin/Borepin/PageModel/MachineListPageModel.cs b/Borepin/Borepin/PageModel/MachineListPageModel.cs index 13ff559..d1231d0 100644 --- a/Borepin/Borepin/PageModel/MachineListPageModel.cs +++ b/Borepin/Borepin/PageModel/MachineListPageModel.cs @@ -49,10 +49,10 @@ namespace Borepin.PageModel IsConnected = true; - IMachineSystem machineSystem = await _BFFHService.GetMachineSystemInterface().ConfigureAwait(false); + IMachineSystem machineSystem = (await _BFFHService.GetSession().ConfigureAwait(false)).MachineSystem; MachineSystem.IInfoInterface machine_infoInterface = await machineSystem.Info().ConfigureAwait(false); - IUserSystem userSystem = await _BFFHService.GetUserSystemInterface().ConfigureAwait(false); + IUserSystem userSystem = (await _BFFHService.GetSession().ConfigureAwait(false)).UserSystem; UserSystem.IInfoInterface user_infoInterface = await userSystem.Info().ConfigureAwait(false); User user_self = (await user_infoInterface.GetUserSelf().ConfigureAwait(false)).Item1; @@ -66,7 +66,7 @@ namespace Borepin.PageModel { MachineListItemViewModel new_viewmodel = new MachineListItemViewModel(machine) { - IsUserAssigned = true + IsUserAssigned = true, }; viewmodel_list_user_assigned.Add(new_viewmodel); } @@ -156,10 +156,10 @@ namespace Borepin.PageModel return; } - IMachineSystem machineInterface = await _BFFHService.GetMachineSystemInterface().ConfigureAwait(false); + IMachineSystem machineInterface = (await _BFFHService.GetSession().ConfigureAwait(false)).MachineSystem; MachineSystem.IInfoInterface infoInterface = await machineInterface.Info().ConfigureAwait(false); - Machine machine = (await infoInterface.GetMachineURN(result.Parameters.GetValue("value")).ConfigureAwait(false)).Item1; + Machine machine = await infoInterface.GetMachineURN(result.Parameters.GetValue("value")).ConfigureAwait(false); if(machine == null) { diff --git a/Borepin/Borepin/PageModel/MachinePageModel.cs b/Borepin/Borepin/PageModel/MachinePageModel.cs index 169e495..8f73a4b 100644 --- a/Borepin/Borepin/PageModel/MachinePageModel.cs +++ b/Borepin/Borepin/PageModel/MachinePageModel.cs @@ -43,7 +43,7 @@ namespace Borepin.PageModel IMachineSystem machineSystem; try { - machineSystem = await _BFFHService.GetMachineSystemInterface().ConfigureAwait(false); + machineSystem = (await _BFFHService.GetSession().ConfigureAwait(false)).MachineSystem; } catch(ReconnectingFailedException) { @@ -54,7 +54,7 @@ namespace Borepin.PageModel IInfoInterface info = await machineSystem.Info().ConfigureAwait(false); - _Machine = (await info.GetMachine(_ID).ConfigureAwait(false)).Item1; + _Machine = await info.GetMachine(_ID).ConfigureAwait(false); MachineItem = new MachineVisualize(_Machine); MachineItem.LoadData(); diff --git a/Borepin/Borepin/Service/BFFH/BFFHService.cs b/Borepin/Borepin/Service/BFFH/BFFHService.cs index c7a79d1..8ee600f 100644 --- a/Borepin/Borepin/Service/BFFH/BFFHService.cs +++ b/Borepin/Borepin/Service/BFFH/BFFHService.cs @@ -9,6 +9,7 @@ using Borepin.Model.Storage; using Borepin.Service.BFFH.Exceptions; using Borepin.Service.Storage.Exceptions; using Capnp.Rpc; +using FabAccessAPI.Exceptions; namespace Borepin.Service.BFFH { @@ -60,12 +61,7 @@ namespace Borepin.Service.BFFH } #endregion - #region Methods - private void StateChanged(Object sender, ConnectionStateEventArgs e) - { - - } - + #region Method /// /// Get all known Connections from Storage /// @@ -110,17 +106,16 @@ namespace Borepin.Service.BFFH { try { - Capnp.Rpc.TcpRpcClient rpcClient = new Capnp.Rpc.TcpRpcClient(); - rpcClient.Connect(connection.Address.Host, connection.Address.Port); - await rpcClient.WhenConnected.ConfigureAwait(false); + + TcpRpcClient rpcClient = await _ConnectAsync(connection.Address.Host, connection.Address.Port).ConfigureAwait(false); rpcClient.Dispose(); + + return true; } catch { return false; } - - return true; } /// @@ -152,11 +147,11 @@ namespace Borepin.Service.BFFH try { - Capnp.Rpc.TcpRpcClient rpcClient = await _ConnectAsync(connection.Address.Host, connection.Address.Port).ConfigureAwait(false); + TcpRpcClient rpcClient = await _ConnectAsync(connection.Address.Host, connection.Address.Port).ConfigureAwait(false); _APIConnection = new FabAccessAPI.Connection(rpcClient); } - catch (Capnp.Rpc.RpcException exception) when (string.Equals(exception.Message, "TcpRpcClient is unable to connect", StringComparison.Ordinal)) + catch (RpcException exception) when (string.Equals(exception.Message, "TcpRpcClient is unable to connect", StringComparison.Ordinal)) { throw new ConnectingFailedException("Connecting failed", exception); } @@ -191,11 +186,11 @@ namespace Borepin.Service.BFFH try { - Capnp.Rpc.TcpRpcClient rpcClient = await _ConnectAsync(connection.Address.Host, connection.Address.Port).ConfigureAwait(false); + TcpRpcClient rpcClient = await _ConnectAsync(connection.Address.Host, connection.Address.Port).ConfigureAwait(false); _APIConnection = new FabAccessAPI.Connection(rpcClient); } - catch (Capnp.Rpc.RpcException exception) when (string.Equals(exception.Message, "TcpRpcClient is unable to connect", StringComparison.Ordinal)) + catch (RpcException exception) when (string.Equals(exception.Message, "TcpRpcClient is unable to connect", StringComparison.Ordinal)) { throw new ConnectingFailedException("Connecting failed", exception); } @@ -237,11 +232,11 @@ namespace Borepin.Service.BFFH try { - Capnp.Rpc.TcpRpcClient rpcClient = await _ConnectAsync(_CurrentConnection.Address.Host, _CurrentConnection.Address.Port).ConfigureAwait(false); + TcpRpcClient rpcClient = await _ConnectAsync(_CurrentConnection.Address.Host, _CurrentConnection.Address.Port).ConfigureAwait(false); _APIConnection = new FabAccessAPI.Connection(rpcClient); } - catch (Capnp.Rpc.RpcException exception) when (string.Equals(exception.Message, "TcpRpcClient is unable to connect", StringComparison.Ordinal)) + catch (RpcException exception) when (string.Equals(exception.Message, "TcpRpcClient is unable to connect", StringComparison.Ordinal)) { throw new ReconnectingFailedException("Connecting failed", new ConnectingFailedException("Connecting failed", exception)); } @@ -270,34 +265,14 @@ namespace Borepin.Service.BFFH } #region FabAccess API Systems - public async Task GetMachineSystemInterface() + public async Task GetSession() { if (!IsConnected) { await Reconnect().ConfigureAwait(false); } - return await _APIConnection.AccessMachineSystem().ConfigureAwait(false); - } - - public async Task GetUserSystemInterface() - { - if (!IsConnected) - { - await Reconnect().ConfigureAwait(false); - } - - return await _APIConnection.AccessUserSystem().ConfigureAwait(false); - } - - public async Task GetPermissionSystemInterface() - { - if (!IsConnected) - { - await Reconnect().ConfigureAwait(false); - } - - return await _APIConnection.AccessPermissionSystem().ConfigureAwait(false); + return _APIConnection.Session; } #endregion #endregion @@ -306,16 +281,27 @@ namespace Borepin.Service.BFFH private async Task _ConnectAsync(string host, int port) { TcpRpcClient rpcClient = new TcpRpcClient(); - rpcClient.ConnectionStateChanged += StateChanged; rpcClient.Connect(host, port); await rpcClient.WhenConnected.ConfigureAwait(false); return rpcClient; } - private Task _AuthenticatePlainAsync(string username, string password) + private async Task _AuthenticatePlainAsync(string username, string password) { - return _APIConnection.Auth("PLAIN", new Dictionary(StringComparer.Ordinal) { { "Username", username }, { "Password", password } }); + try + { + await _APIConnection.Auth("PLAIN", new Dictionary(StringComparer.Ordinal) { { "Username", username }, { "Password", password } }).ConfigureAwait(false); + return await Task.FromResult(true).ConfigureAwait(false); + } + catch(InvalidCredentialsException) + { + return await Task.FromResult(true).ConfigureAwait(false); + } + catch (AuthenticatingFailedException) + { + return await Task.FromResult(true).ConfigureAwait(false); + } } #endregion } diff --git a/Borepin/Borepin/Service/BFFH/IBFFHService.cs b/Borepin/Borepin/Service/BFFH/IBFFHService.cs index fc4027e..b05d45e 100644 --- a/Borepin/Borepin/Service/BFFH/IBFFHService.cs +++ b/Borepin/Borepin/Service/BFFH/IBFFHService.cs @@ -22,8 +22,6 @@ namespace Borepin.Service.BFFH Task Disconnect(); - Task GetMachineSystemInterface(); - Task GetPermissionSystemInterface(); - Task GetUserSystemInterface(); + Task GetSession(); } } \ No newline at end of file