From 0ee3dc849749dfcfea31e638af51075e56d9c32e Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Mon, 16 May 2022 22:41:29 +0200 Subject: [PATCH] Added more new API Class --- .../Borepin.Android/Borepin.Android.csproj | 4 + .../Borepin.Android/PlatformInitializer.cs | 4 +- .../Services/APIBindedService.cs | 50 +++++++ Borepin/Borepin.Android/Services/APIBinder.cs | 18 +++ .../Borepin.Android/Services/APIService.cs | 28 ++++ .../Services/APIServiceConnection.cs | 32 +++++ Borepin/Borepin.UWP/Borepin.UWP.csproj | 1 + Borepin/Borepin.UWP/PlatformInitializer.cs | 4 +- Borepin/Borepin.UWP/Services/APIService.cs | 23 +++ Borepin/Borepin.iOS/Borepin.iOS.csproj | 1 + Borepin/Borepin.iOS/PlatformInitializer.cs | 4 +- Borepin/Borepin.iOS/Services/APIService.cs | 23 +++ Borepin/Borepin/App.xaml.cs | 7 +- Borepin/Borepin/Base/ConnectionModelBase.cs | 98 +++++++++---- .../Exceptions/InstanceIncorrectException.cs | 22 +++ Borepin/Borepin/Base/PageModelBase.cs | 16 +-- Borepin/Borepin/Borepin.csproj | 1 + .../AddServerProcess/AuthPlainPageModel.cs | 80 +++++------ .../AddServerProcess/SelectServerPageModel.cs | 136 ++++++------------ .../Borepin/PageModel/MachineListPageModel.cs | 4 +- Borepin/Borepin/PageModel/MachinePageModel.cs | 97 +++++-------- Borepin/Borepin/PageModel/ScanURNPageModel.cs | 68 +++------ .../Borepin/PageModel/ServerListPageModel.cs | 23 +-- Borepin/Borepin/PageModel/ServerPageModel.cs | 54 ++----- Borepin/Borepin/Service/IAPIService.cs | 9 ++ .../ViewModel/ServerListItemViewModel.cs | 7 +- FabAccessAPI/API.cs | 18 ++- FabAccessAPI/FabAccessAPI.csproj | 2 +- FabAccessAPI/IAPI.cs | 4 +- 29 files changed, 468 insertions(+), 370 deletions(-) create mode 100644 Borepin/Borepin.Android/Services/APIBindedService.cs create mode 100644 Borepin/Borepin.Android/Services/APIBinder.cs create mode 100644 Borepin/Borepin.Android/Services/APIService.cs create mode 100644 Borepin/Borepin.Android/Services/APIServiceConnection.cs create mode 100644 Borepin/Borepin.UWP/Services/APIService.cs create mode 100644 Borepin/Borepin.iOS/Services/APIService.cs create mode 100644 Borepin/Borepin/Base/Exceptions/InstanceIncorrectException.cs create mode 100644 Borepin/Borepin/Service/IAPIService.cs diff --git a/Borepin/Borepin.Android/Borepin.Android.csproj b/Borepin/Borepin.Android/Borepin.Android.csproj index aca6837..339d783 100644 --- a/Borepin/Borepin.Android/Borepin.Android.csproj +++ b/Borepin/Borepin.Android/Borepin.Android.csproj @@ -76,6 +76,10 @@ + + + + diff --git a/Borepin/Borepin.Android/PlatformInitializer.cs b/Borepin/Borepin.Android/PlatformInitializer.cs index b26b6c6..ac5fbc0 100644 --- a/Borepin/Borepin.Android/PlatformInitializer.cs +++ b/Borepin/Borepin.Android/PlatformInitializer.cs @@ -1,7 +1,7 @@ using Borepin.Droid.Services; +using Borepin.Service; using Borepin.Service.Storage; using Borepin.Service.Versioning; -using FabAccessAPI; using Prism; using Prism.Ioc; @@ -15,7 +15,7 @@ namespace Borepin.Droid containerRegistry.Register(); containerRegistry.Register(); - containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); } } } \ No newline at end of file diff --git a/Borepin/Borepin.Android/Services/APIBindedService.cs b/Borepin/Borepin.Android/Services/APIBindedService.cs new file mode 100644 index 0000000..e84859c --- /dev/null +++ b/Borepin/Borepin.Android/Services/APIBindedService.cs @@ -0,0 +1,50 @@ +using Android.App; +using Android.Content; +using Android.OS; +using FabAccessAPI; + +namespace Borepin.Droid.Services +{ + [Service(Name= "org.fab_infra.fabaccess.APIService")] + public class APIBindedService : Android.App.Service + { + #region Private Members + private IAPI _API; + #endregion + + #region Members + public IBinder Binder { get; private set; } + #endregion + + #region Methods + public IAPI GetAPI() + { + return _API; + } + + public override void OnCreate() + { + base.OnCreate(); + _API = new API(); + } + + public override IBinder OnBind(Intent intent) + { + Binder = new APIBinder(this); + return Binder; + } + + public override bool OnUnbind(Intent intent) + { + return base.OnUnbind(intent); + } + + public override void OnDestroy() + { + Binder = null; + _API = null; + base.OnDestroy(); + } + #endregion + } +} \ No newline at end of file diff --git a/Borepin/Borepin.Android/Services/APIBinder.cs b/Borepin/Borepin.Android/Services/APIBinder.cs new file mode 100644 index 0000000..5deeb3b --- /dev/null +++ b/Borepin/Borepin.Android/Services/APIBinder.cs @@ -0,0 +1,18 @@ +using Android.OS; + +namespace Borepin.Droid.Services +{ + public class APIBinder : Binder + { + #region Constructors + public APIBinder(APIBindedService service) + { + Service = service; + } + #endregion + + #region Members + public APIBindedService Service { get; private set; } + #endregion + } +} \ No newline at end of file diff --git a/Borepin/Borepin.Android/Services/APIService.cs b/Borepin/Borepin.Android/Services/APIService.cs new file mode 100644 index 0000000..815d895 --- /dev/null +++ b/Borepin/Borepin.Android/Services/APIService.cs @@ -0,0 +1,28 @@ +using Android.App; +using Android.Content; +using Borepin.Service; +using FabAccessAPI; + +namespace Borepin.Droid.Services +{ + public class APIService : IAPIService + { + #region Private Members + private readonly APIServiceConnection _APIServiceConnection; + #endregion + + #region Constructors + public APIService() + { + Context context = Application.Context; + Intent service = new Intent(context, typeof(APIBindedService)); + context.BindService(service, _APIServiceConnection, Bind.AutoCreate); + } + #endregion + + public IAPI GetAPI() + { + return _APIServiceConnection?.Binder?.Service?.GetAPI(); + } + } +} \ No newline at end of file diff --git a/Borepin/Borepin.Android/Services/APIServiceConnection.cs b/Borepin/Borepin.Android/Services/APIServiceConnection.cs new file mode 100644 index 0000000..319d008 --- /dev/null +++ b/Borepin/Borepin.Android/Services/APIServiceConnection.cs @@ -0,0 +1,32 @@ +using Android.Content; +using Android.OS; +using FabAccessAPI; + +namespace Borepin.Droid.Services +{ + class APIServiceConnection : Java.Lang.Object, IServiceConnection + { + #region Members + public bool IsConnected + { + get + { + return Binder != null; + } + } + public APIBinder Binder { get; private set; } = null; + #endregion + + #region Methods + public void OnServiceConnected(ComponentName name, IBinder service) + { + Binder = service as APIBinder; + } + + public void OnServiceDisconnected(ComponentName name) + { + Binder = null; + } + #endregion + } +} \ No newline at end of file diff --git a/Borepin/Borepin.UWP/Borepin.UWP.csproj b/Borepin/Borepin.UWP/Borepin.UWP.csproj index ce20094..2f9fcd5 100644 --- a/Borepin/Borepin.UWP/Borepin.UWP.csproj +++ b/Borepin/Borepin.UWP/Borepin.UWP.csproj @@ -98,6 +98,7 @@ + diff --git a/Borepin/Borepin.UWP/PlatformInitializer.cs b/Borepin/Borepin.UWP/PlatformInitializer.cs index 4fc4a5e..9eacb3c 100644 --- a/Borepin/Borepin.UWP/PlatformInitializer.cs +++ b/Borepin/Borepin.UWP/PlatformInitializer.cs @@ -3,7 +3,7 @@ using Prism; using Prism.Ioc; using Borepin.Service.Storage; using Borepin.Service.Versioning; -using FabAccessAPI; +using Borepin.Service; namespace Borepin.UWP { @@ -15,7 +15,7 @@ namespace Borepin.UWP containerRegistry.Register(); containerRegistry.Register(); - containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); } } } diff --git a/Borepin/Borepin.UWP/Services/APIService.cs b/Borepin/Borepin.UWP/Services/APIService.cs new file mode 100644 index 0000000..f985225 --- /dev/null +++ b/Borepin/Borepin.UWP/Services/APIService.cs @@ -0,0 +1,23 @@ +using Borepin.Service; +using FabAccessAPI; + +namespace Borepin.UWP.Services +{ + public class APIService : IAPIService + { + #region Private Members + private readonly IAPI _API; + #endregion + + #region Constructors + public APIService() + { + _API = new API(); + } + #endregion + public IAPI GetAPI() + { + return _API; + } + } +} diff --git a/Borepin/Borepin.iOS/Borepin.iOS.csproj b/Borepin/Borepin.iOS/Borepin.iOS.csproj index 9af051d..49518ba 100644 --- a/Borepin/Borepin.iOS/Borepin.iOS.csproj +++ b/Borepin/Borepin.iOS/Borepin.iOS.csproj @@ -81,6 +81,7 @@ + diff --git a/Borepin/Borepin.iOS/PlatformInitializer.cs b/Borepin/Borepin.iOS/PlatformInitializer.cs index 4827500..26a09b8 100644 --- a/Borepin/Borepin.iOS/PlatformInitializer.cs +++ b/Borepin/Borepin.iOS/PlatformInitializer.cs @@ -1,7 +1,7 @@ using Borepin.iOS.Services; +using Borepin.Service; using Borepin.Service.Storage; using Borepin.Service.Versioning; -using FabAccessAPI; using Prism; using Prism.Ioc; @@ -15,7 +15,7 @@ namespace Borepin.iOS containerRegistry.Register(); containerRegistry.Register(); - containerRegistry.RegisterSingleton(); + containerRegistry.RegisterSingleton(); } } } \ No newline at end of file diff --git a/Borepin/Borepin.iOS/Services/APIService.cs b/Borepin/Borepin.iOS/Services/APIService.cs new file mode 100644 index 0000000..f311dab --- /dev/null +++ b/Borepin/Borepin.iOS/Services/APIService.cs @@ -0,0 +1,23 @@ +using Borepin.Service; +using FabAccessAPI; + +namespace Borepin.iOS.Services +{ + public class APIService : IAPIService + { + #region Private Members + private readonly IAPI _API; + #endregion + + #region Constructors + public APIService() + { + _API = new API(); + } + #endregion + public IAPI GetAPI() + { + return _API; + } + } +} \ No newline at end of file diff --git a/Borepin/Borepin/App.xaml.cs b/Borepin/Borepin/App.xaml.cs index 702aad5..4fc87c3 100644 --- a/Borepin/Borepin/App.xaml.cs +++ b/Borepin/Borepin/App.xaml.cs @@ -26,11 +26,6 @@ namespace Borepin { InitializeComponent(); - INavigationParameters parameters = new NavigationParameters() - { - { "instance", 0 }, - }; - await NavigationService.NavigateAsync(new Uri("https://borepin.fab-access.org/StartPage")).ConfigureAwait(false); } @@ -71,7 +66,7 @@ namespace Borepin // IPreferenceStorageService // ISecretStorageService // IVersioningService - // IAPI + // IAPIService #endregion } } diff --git a/Borepin/Borepin/Base/ConnectionModelBase.cs b/Borepin/Borepin/Base/ConnectionModelBase.cs index e999e7d..87b67f0 100644 --- a/Borepin/Borepin/Base/ConnectionModelBase.cs +++ b/Borepin/Borepin/Base/ConnectionModelBase.cs @@ -1,9 +1,8 @@ -using Borepin.Service.BFFH; -using Borepin.Service.BFFH.Exceptions; +using Borepin.Service; +using FabAccessAPI; using Prism.Navigation; using Prism.Services; using System.Threading.Tasks; -using Xamarin.Forms; namespace Borepin.Base { @@ -14,14 +13,56 @@ namespace Borepin.Base { #region Private Fields protected readonly IPageDialogService _PageDialogService; - protected readonly IBFFHService _BFFHService; + protected readonly IAPI _API; #endregion #region Constructors - protected ConnectionModelBase(INavigationService navigationService, IPageDialogService pageDialogService, IBFFHService bFFHService) : base(navigationService) + protected ConnectionModelBase(INavigationService navigationService, IPageDialogService pageDialogService, IAPIService apiService) : base(navigationService) { _PageDialogService = pageDialogService; - _BFFHService = bFFHService; + _API = apiService.GetAPI(); + _API.ConnectionStatusChanged += OnConnectionStatusChanged; + + IsConnected = _API.IsConnected; + } + #endregion + + #region Methods + public void OnConnectionStatusChanged(object sender, ConnectionStatusChange args) + { + switch(args) + { + case ConnectionStatusChange.Connected: + IsConnected = true; + LoadAPIData(); + break; + case ConnectionStatusChange.Reconnected: + ReloadAPIData(); + break; + case ConnectionStatusChange.ConnectionLoss: + case ConnectionStatusChange.Disconnected: + IsConnected = false; + break; + } + } + + public virtual Task LoadAPIData() + { + return Task.CompletedTask; + } + public virtual Task ReloadAPIData() + { + return Task.CompletedTask; + } + + public virtual Task LoadInstance(object instance) + { + return Task.CompletedTask; + } + + public virtual Task CreateInstance() + { + return Task.FromResult(null); } #endregion @@ -37,36 +78,31 @@ namespace Borepin.Base } #endregion - #region Methods - /// - /// Checks connection to Server. - /// Display message if Connection is lost. - /// - /// True if connection is ok. - public async Task CheckConnection() + #region INavigationAware + public override void OnNavigatedTo(INavigationParameters parameters) { - try + if(parameters.ContainsKey("instance")) { - if(_BFFHService.CurrentConnection != null && !_BFFHService.IsConnected) - { - await _BFFHService.Reconnect().ConfigureAwait(false); - } - else if(_BFFHService.CurrentConnection == null) - { - return false; - } - - return true; + LoadInstance(parameters.GetValue("instance")); } - catch (ReconnectingFailedException) + else { - Device.BeginInvokeOnMainThread(async () => - { - await _PageDialogService.DisplayAlertAsync("Connection failed", "Lost connection to server.", "Ok").ConfigureAwait(false); - }); + Log.Trace("No instance"); + LoadInstance(null); + } - IsConnected = false; - return false; + if(_API.IsConnected) + { + LoadAPIData(); + } + } + + public override void OnNavigatedFrom(INavigationParameters parameters) + { + object instance = CreateInstance(); + if(instance != null) + { + parameters.Add("instance", instance); } } #endregion diff --git a/Borepin/Borepin/Base/Exceptions/InstanceIncorrectException.cs b/Borepin/Borepin/Base/Exceptions/InstanceIncorrectException.cs new file mode 100644 index 0000000..632b89e --- /dev/null +++ b/Borepin/Borepin/Base/Exceptions/InstanceIncorrectException.cs @@ -0,0 +1,22 @@ +using System; + +namespace Borepin.Base.Exceptions +{ + public class InstanceIncorrectException : Exception + { + public InstanceIncorrectException() + { + + } + + public InstanceIncorrectException(string message) : base(message) + { + + } + + public InstanceIncorrectException(string message, Exception inner) : base(message, inner) + { + + } + } +} diff --git a/Borepin/Borepin/Base/PageModelBase.cs b/Borepin/Borepin/Base/PageModelBase.cs index 8e51506..f4cf05d 100644 --- a/Borepin/Borepin/Base/PageModelBase.cs +++ b/Borepin/Borepin/Base/PageModelBase.cs @@ -1,6 +1,6 @@ -using Prism.Mvvm; +using NLog; +using Prism.Mvvm; using Prism.Navigation; -using System.Threading.Tasks; namespace Borepin.Base { @@ -9,6 +9,10 @@ namespace Borepin.Base /// public abstract class PageModelBase : BindableBase, INavigationAware { + #region Logger + protected static readonly Logger Log = LogManager.GetCurrentClassLogger(); + #endregion + #region Private Fields protected readonly INavigationService _NavigationService; @@ -30,14 +34,6 @@ namespace Borepin.Base } #endregion - #region Data - /// - /// Load Data async - /// - /// - public abstract Task LoadData(); - #endregion - #region INavigationAware public abstract void OnNavigatedTo(INavigationParameters parameters); public abstract void OnNavigatedFrom(INavigationParameters parameters); diff --git a/Borepin/Borepin/Borepin.csproj b/Borepin/Borepin/Borepin.csproj index 6f5458e..33c442f 100644 --- a/Borepin/Borepin/Borepin.csproj +++ b/Borepin/Borepin/Borepin.csproj @@ -31,6 +31,7 @@ + diff --git a/Borepin/Borepin/PageModel/AddServerProcess/AuthPlainPageModel.cs b/Borepin/Borepin/PageModel/AddServerProcess/AuthPlainPageModel.cs index 656c023..e494e2e 100644 --- a/Borepin/Borepin/PageModel/AddServerProcess/AuthPlainPageModel.cs +++ b/Borepin/Borepin/PageModel/AddServerProcess/AuthPlainPageModel.cs @@ -1,10 +1,11 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using System.Windows.Input; using Borepin.Base; -using Borepin.Model; -using Borepin.Service.BFFH; -using Borepin.Service.BFFH.Exceptions; +using Borepin.Base.Exceptions; +using Borepin.Service; +using FabAccessAPI; using FabAccessAPI.Exceptions; using Prism.Commands; using Prism.Navigation; @@ -12,31 +13,32 @@ using Prism.Services; namespace Borepin.PageModel.AddServerProcess { - public class AuthPlainPageModel : PageModelBase + public class AuthPlainPageModel : ConnectionModelBase { #region Private Fields - private readonly IBFFHService _BFFHService; - private readonly IPageDialogService _PageDialogService; - - private Connection _Connection; + private ConnectionData _ConnectionData; #endregion #region Constructors - public AuthPlainPageModel(INavigationService navigationService, IBFFHService bffhService, IPageDialogService pageDialogService) : base(navigationService) + public AuthPlainPageModel(INavigationService navigationService, IAPIService apiService, IPageDialogService pageDialogService) : base(navigationService, pageDialogService, apiService) { - _BFFHService = bffhService; - _PageDialogService = pageDialogService; - AuthenticateCommand = new DelegateCommand(async () => await AuthenticateCommandExecute().ConfigureAwait(false)); } #endregion #region LoadData - public override Task LoadData() + public override Task LoadInstance(object instance) { - Username = _Connection.Username; - - IsBusy = false; + if(instance is ConnectionData) + { + _ConnectionData = instance as ConnectionData; + Username = _ConnectionData.Username; + } + else + { + throw new InstanceIncorrectException(); + } + return Task.CompletedTask; } #endregion @@ -68,16 +70,26 @@ namespace Borepin.PageModel.AddServerProcess { IsBusy = true; - _Connection.Username = Username; - - if (_BFFHService.IsConnected) + _ConnectionData = new ConnectionData() { - await _BFFHService.Disconnect().ConfigureAwait(true); + Host = _ConnectionData.Host, + Mechanism = Mechanism.PLAIN, + Username = Username, + Properties = new Dictionary(StringComparer.Ordinal) + { + { "username", Username }, + { "password", Password } + }, + }; + + if (_API.IsConnected) + { + await _API.Disconnect().ConfigureAwait(true); } try { - await _BFFHService.Connect(_Connection, Password).ConfigureAwait(true); + await _API.Connect(_ConnectionData).ConfigureAwait(true); } catch (ConnectingFailedException) { @@ -101,30 +113,10 @@ namespace Borepin.PageModel.AddServerProcess return; } - await _NavigationService.NavigateAsync("/MainPage/NavigationPage/MachineListPage").ConfigureAwait(false); - } - #endregion - - #region INavigationAware - public override void OnNavigatedTo(INavigationParameters parameters) - { - if (parameters.ContainsKey("instance") && parameters["instance"] is Connection) + INavigationResult result = await _NavigationService.NavigateAsync("/MainPage/NavigationPage/MachineListPage").ConfigureAwait(false); + if(result.Exception != null) { - _Connection = parameters["instance"] as Connection; - } - else - { - _Connection = new Connection(); - } - - LoadData(); - } - - public override void OnNavigatedFrom(INavigationParameters parameters) - { - if(parameters.GetNavigationMode() == NavigationMode.Back) - { - parameters.Add("instance", _Connection); + Log.Fatal(result.Exception, "Navigating failed"); } } #endregion diff --git a/Borepin/Borepin/PageModel/AddServerProcess/SelectServerPageModel.cs b/Borepin/Borepin/PageModel/AddServerProcess/SelectServerPageModel.cs index c02997d..a8133d6 100644 --- a/Borepin/Borepin/PageModel/AddServerProcess/SelectServerPageModel.cs +++ b/Borepin/Borepin/PageModel/AddServerProcess/SelectServerPageModel.cs @@ -1,59 +1,56 @@ using Borepin.Base; -using Borepin.Model; -using Borepin.Service.BFFH; -using Prism.AppModel; +using Borepin.Service; +using FabAccessAPI; +using FabAccessAPI.Exceptions; +using Prism.Commands; using Prism.Navigation; using Prism.Services; -using Prism.Services.Dialogs; using System; -using System.Globalization; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Input; -using Xamarin.Forms; namespace Borepin.PageModel.AddServerProcess { - public class SelectServerPageModel : PageModelBase, IPageLifecycleAware + public class SelectServerPageModel : ConnectionModelBase { #region Private Fields - private readonly IBFFHService _BFFHService; - private readonly IPageDialogService _PageDialogService; - private readonly IDialogService _DialogService; - - private Connection _Connection; - private bool _Scanned; + private ConnectionData _ConnectionData; #endregion #region Constructors - public SelectServerPageModel(INavigationService navigationService, IBFFHService bffhService, IPageDialogService pageDialogService, IDialogService dialogService) : base(navigationService) + public SelectServerPageModel(INavigationService navigationService, IPageDialogService pageDialogService, IAPIService apiService) : base(navigationService, pageDialogService, apiService) { - _BFFHService = bffhService; - _PageDialogService = pageDialogService; - _DialogService = dialogService; - - ConnectToServerCommand = new Command(async ()=> await ConnectToServerExecute().ConfigureAwait(false)); - DetectLocalServerCommand = new Command(DetectHostCommandExecute); - ScanCodeCommand = new Command(ScanCodeCommandExecute); + ConnectToServerCommand = new DelegateCommand(async () => await ConnectToServerExecute().ConfigureAwait(false)); + DetectLocalServerCommand = new DelegateCommand(DetectHostCommandExecute); + ScanCodeCommand = new DelegateCommand(ScanCodeCommandExecute); } #endregion #region LoadData - public override Task LoadData() + public override Task LoadInstance(object instance) { - Server = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", _Connection.Address.Host, _Connection.Address.Port); + if(instance is ConnectionData) + { + _ConnectionData = instance as ConnectionData; + Host = _ConnectionData.Host.ToString(); + } - IsBusy = false; return Task.CompletedTask; } + + public override Task CreateInstance() + { + return Task.FromResult(_ConnectionData); + } #endregion #region Fields - private string _Server; - public string Server + private string _Host; + public string Host { - get => _Server; - set => SetProperty(ref _Server, value); + get => _Host; + set => SetProperty(ref _Host, value); } #endregion @@ -71,13 +68,13 @@ namespace Borepin.PageModel.AddServerProcess try { string server_address = ""; - if(Regex.IsMatch(Server, "([a-zA-Z]{2,20}):\\/\\/", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture, TimeSpan.FromSeconds(1))) + if(Regex.IsMatch(Host, "([a-zA-Z]{2,20}):\\/\\/", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture, TimeSpan.FromSeconds(1))) { - server_address = Server; + server_address = Host; } else { - server_address = "http://" + Server; + server_address = "http://" + Host; } UriBuilder builder = new UriBuilder(server_address); @@ -86,9 +83,9 @@ namespace Borepin.PageModel.AddServerProcess builder.Port = 59661; } - _Connection = new Connection() + _ConnectionData = new ConnectionData() { - Address = builder.Uri, + Host = builder.Uri, }; } catch (UriFormatException) @@ -99,7 +96,11 @@ namespace Borepin.PageModel.AddServerProcess return; } - if (!await _BFFHService.TestConnection(_Connection).ConfigureAwait(true)) + try + { + await _API.TestConnection(_ConnectionData).ConfigureAwait(true); + } + catch(ConnectingFailedException) { await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to connect to server.", "Ok").ConfigureAwait(false); @@ -107,12 +108,11 @@ namespace Borepin.PageModel.AddServerProcess return; } - INavigationParameters parameters = new NavigationParameters() + INavigationResult result = await _NavigationService.NavigateAsync("AddServerProcess_ChooseAuthTypePage").ConfigureAwait(false); + if (result.Exception != null) { - {"instance", _Connection }, - }; - - await _NavigationService.NavigateAsync("AddServerProcess_ChooseAuthTypePage", parameters).ConfigureAwait(false); + Log.Fatal(result.Exception, "Navigating failed"); + } } private ICommand _ScanCodeCommand; @@ -123,16 +123,7 @@ namespace Borepin.PageModel.AddServerProcess } public void ScanCodeCommandExecute() { - _DialogService.ShowDialog("ScanDialog", ScanCodeCommandExecute_Dialog); - } - - public void ScanCodeCommandExecute_Dialog(IDialogResult result) - { - if (string.Equals(result.Parameters.GetValue("result"), "scanned", StringComparison.Ordinal)) - { - Server = result.Parameters["value"] as string; - _Scanned = true; - } + } private ICommand _DetectLocalServerCommand; @@ -144,52 +135,7 @@ namespace Borepin.PageModel.AddServerProcess public void DetectHostCommandExecute() { // Get Example Server Address - Server = "127.0.0.1:59661"; - } - #endregion - - #region IPageLifecycleAware - public async void OnAppearing() - { - if (_Scanned) - { - _Scanned = false; - - await ConnectToServerExecute().ConfigureAwait(false); - } - } - - public void OnDisappearing() - { - - } - #endregion - - #region INavigationAware - public override void OnNavigatedTo(INavigationParameters parameters) - { - if(_Connection == null) - { - if (parameters.ContainsKey("instance") && parameters["instance"] is Connection) - { - _Connection = parameters["instance"] as Connection; - } - else - { - _Connection = new Connection(); - } - - LoadData(); - } - else - { - IsBusy = false; - } - } - - public override void OnNavigatedFrom(INavigationParameters parameters) - { - + Host = "127.0.0.1:59661"; } #endregion } diff --git a/Borepin/Borepin/PageModel/MachineListPageModel.cs b/Borepin/Borepin/PageModel/MachineListPageModel.cs index ddf20a6..50c7dcb 100644 --- a/Borepin/Borepin/PageModel/MachineListPageModel.cs +++ b/Borepin/Borepin/PageModel/MachineListPageModel.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using System.Windows.Input; using Prism.Commands; using Prism.Navigation; -using Borepin.Service.BFFH; using Borepin.Base; using FabAccessAPI.Schema; using Prism.Services; @@ -12,6 +11,7 @@ using static FabAccessAPI.Schema.Machine; using System; using NaturalSort.Extension; using System.Linq; +using Borepin.Service; namespace Borepin.PageModel { @@ -22,7 +22,7 @@ namespace Borepin.PageModel #endregion #region Constructors - public MachineListPageModel(INavigationService navigationService, IPageDialogService pageDialogService, IBFFHService bFFHService) : base(navigationService, pageDialogService, bFFHService) + public MachineListPageModel(INavigationService navigationService, IPageDialogService pageDialogService, IAPIService apiService) : base(navigationService, pageDialogService, apiService) { SelectInstanceCommand = new DelegateCommand(SelectInstanceCommandExecute); ScanCodeCommand = new DelegateCommand(async () => await ScanCodeCommandExecute().ConfigureAwait(false)); diff --git a/Borepin/Borepin/PageModel/MachinePageModel.cs b/Borepin/Borepin/PageModel/MachinePageModel.cs index 78337c9..19b6f91 100644 --- a/Borepin/Borepin/PageModel/MachinePageModel.cs +++ b/Borepin/Borepin/PageModel/MachinePageModel.cs @@ -4,11 +4,10 @@ using Prism.Navigation; using System.Threading.Tasks; using System.Windows.Input; using FabAccessAPI.Schema; -using Borepin.Service.BFFH; -using static FabAccessAPI.Schema.MachineSystem; using Borepin.Model; using Prism.Services; -using Borepin.Service.BFFH.Exceptions; +using Borepin.Service; +using Borepin.Base.Exceptions; namespace Borepin.PageModel { @@ -20,7 +19,7 @@ namespace Borepin.PageModel #endregion #region Contructors - public MachinePageModel(INavigationService navigationService, IPageDialogService pageDialogService, IBFFHService bffhService) : base(navigationService, pageDialogService, bffhService) + public MachinePageModel(INavigationService navigationService, IPageDialogService pageDialogService, IAPIService apiService) : base(navigationService, pageDialogService, apiService) { UseMachineCommand = new DelegateCommand(UseMachineCommandExecute); GiveBackMachineCommand = new DelegateCommand(GiveBackMachineCommandExecute); @@ -29,34 +28,29 @@ namespace Borepin.PageModel #endregion #region Data - public override async Task LoadData() + public override Task LoadInstance(object instance) { IsBusy = true; - if (!await CheckConnection().ConfigureAwait(false)) - { - IsConnected = false; - IsBusy = false; - return; - } - - MachineSystem machineSystem; - try + if(instance is string) { - machineSystem = (await _BFFHService.GetSession().ConfigureAwait(false)).MachineSystem; + _ID = instance as string; } - catch(ReconnectingFailedException) + else { - await _PageDialogService.DisplayAlertAsync("Connection failed", "Lost connection to server.", "Ok").ConfigureAwait(false); - - return; + throw new InstanceIncorrectException(); } - _Machine = (await machineSystem.Info.GetMachine(_ID).ConfigureAwait(false)).Just; - MachineItem = new MachineVisualize(_Machine); - MachineItem.LoadData(); - IsBusy = false; + + return Task.CompletedTask; + } + + public override async Task LoadAPIData() + { + _Machine = (await _API.Session.MachineSystem.Info.GetMachine(_ID).ConfigureAwait(false)).Just; + MachineItem = new MachineVisualize(_Machine); + MachineItem.LoadData(); } #endregion @@ -81,19 +75,15 @@ namespace Borepin.PageModel public async void UseMachineCommandExecute() { IsBusy = true; - if (!await CheckConnection().ConfigureAwait(false)) + + if(_API.IsConnected) { - IsConnected = false; + Machine.IUseInterface useInterface = _Machine.Use; - IsBusy = false; - return; + await useInterface.Use().ConfigureAwait(false); + await LoadAPIData().ConfigureAwait(false); } - Machine.IUseInterface useInterface = _Machine.Use; - - await useInterface.Use().ConfigureAwait(false); - await LoadData().ConfigureAwait(false); - IsBusy = false; } @@ -107,19 +97,15 @@ namespace Borepin.PageModel public async void GiveBackMachineCommandExecute() { IsBusy = true; - if (!await CheckConnection().ConfigureAwait(false)) + + if (_API.IsConnected) { - IsConnected = false; + Machine.IInUseInterface inUseInterface = _Machine.Inuse; - IsBusy = false; - return; + await inUseInterface.GiveBack().ConfigureAwait(false); + await LoadAPIData().ConfigureAwait(false); } - Machine.IInUseInterface inUseInterface = _Machine.Inuse; - - await inUseInterface.GiveBack().ConfigureAwait(false); - await LoadData().ConfigureAwait(false); - IsBusy = false; } @@ -133,35 +119,18 @@ namespace Borepin.PageModel public async void ForceFreeMachineCommandExecute() { IsBusy = true; - if (!await CheckConnection().ConfigureAwait(false)) + + if (_API.IsConnected) { - IsConnected = false; - IsBusy = false; - return; + Machine.IManageInterface manageInterface = _Machine.Manage; + + await manageInterface.ForceFree().ConfigureAwait(false); + await LoadAPIData().ConfigureAwait(false); } - Machine.IManageInterface manageInterface = _Machine.Manage; - - await manageInterface.ForceFree().ConfigureAwait(false); - await LoadData().ConfigureAwait(false); - IsBusy = false; } #endregion - - #region INavigationService - public override void OnNavigatedFrom(INavigationParameters parameters) - { - - } - - public override async void OnNavigatedTo(INavigationParameters parameters) - { - _ID = parameters["id"] as string; - - await LoadData().ConfigureAwait(false); - } - #endregion } } diff --git a/Borepin/Borepin/PageModel/ScanURNPageModel.cs b/Borepin/Borepin/PageModel/ScanURNPageModel.cs index 52bdeae..18d547f 100644 --- a/Borepin/Borepin/PageModel/ScanURNPageModel.cs +++ b/Borepin/Borepin/PageModel/ScanURNPageModel.cs @@ -1,6 +1,5 @@ using Borepin.Base; -using Borepin.Service.BFFH; -using Borepin.Service.BFFH.Exceptions; +using Borepin.Service; using FabAccessAPI.Schema; using Prism.Commands; using Prism.Navigation; @@ -17,7 +16,7 @@ namespace Borepin.PageModel class ScanURNPageModel : ConnectionModelBase { #region Contructors - public ScanURNPageModel(INavigationService navigationService, IPageDialogService pageDialogService, IBFFHService bffhService) : base(navigationService, pageDialogService, bffhService) + public ScanURNPageModel(INavigationService navigationService, IPageDialogService pageDialogService, IAPIService apiService) : base(navigationService, pageDialogService, apiService) { AbortCommand = new DelegateCommand(async () => await AbortCommandExecute().ConfigureAwait(true)); ScannedCommand = new DelegateCommand(async () => await ScannedCommandExecuteAsync().ConfigureAwait(false)); @@ -27,13 +26,6 @@ namespace Borepin.PageModel } #endregion - #region Data - public override async Task LoadData() - { - await Task.CompletedTask.ConfigureAwait(false); - } - #endregion - #region Fields public MobileBarcodeScanningOptions ScanOptions { @@ -98,28 +90,15 @@ namespace Borepin.PageModel return; } // END HACK - string id = null; - try - { - id = await QRToID(ScanResult.Text).ConfigureAwait(false); - } - catch(ReconnectingFailedException) - { - Device.BeginInvokeOnMainThread(async () => - { - await _PageDialogService.DisplayAlertAsync("Connection failed", "Please reconnect to the server.", "OK").ConfigureAwait(false); - IsScanning = true; - }); - return; - } + string id = await QRToID(ScanResult.Text).ConfigureAwait(false); if (id != null) { NavigationParameters parameters = new NavigationParameters - { - { "id", id }, - }; + { + { "id", id }, + }; Device.BeginInvokeOnMainThread(async () => { @@ -141,24 +120,21 @@ namespace Borepin.PageModel public async Task QRToID(string value) { - if (!_BFFHService.IsConnected) + if (_API.IsConnected) { - await _BFFHService.Reconnect().ConfigureAwait(false); - - IsConnected = false; - return null; + Optional optional = await _API.Session.MachineSystem.Info.GetMachineURN(value).ConfigureAwait(false); + + if (optional.Just == null) + { + return null; + } + + return optional.Just.Id; } - - MachineSystem machineSystem = (await _BFFHService.GetSession().ConfigureAwait(false)).MachineSystem; - - Optional optional = await machineSystem.Info.GetMachineURN(value).ConfigureAwait(false); - - if (optional.Just == null) + else { return null; } - - return optional.Just.Id; } private ICommand _AbortCommand; @@ -175,17 +151,5 @@ namespace Borepin.PageModel await _NavigationService.GoBackAsync().ConfigureAwait(false); } #endregion - - #region INavigationService - public override void OnNavigatedFrom(INavigationParameters parameters) - { - - } - - public override void OnNavigatedTo(INavigationParameters parameters) - { - - } - #endregion } } diff --git a/Borepin/Borepin/PageModel/ServerListPageModel.cs b/Borepin/Borepin/PageModel/ServerListPageModel.cs index 286f27b..b030e80 100644 --- a/Borepin/Borepin/PageModel/ServerListPageModel.cs +++ b/Borepin/Borepin/PageModel/ServerListPageModel.cs @@ -2,25 +2,28 @@ using System.Collections.Generic; using System.Threading.Tasks; using System.Linq; -using Borepin.Model; using System.Windows.Input; using Prism.Commands; using Prism.Navigation; -using Borepin.Service.BFFH; using Borepin.Base; +using Borepin.Service.Storage; +using FabAccessAPI; +using Borepin.Service; namespace Borepin.PageModel { public class ServerListPageModel : PageModelBase { #region Private Fields - private readonly IBFFHService _BFFHService; + private readonly ILoginStorageService _LoginStorageService; + private readonly IAPI _API; #endregion #region Constructors - public ServerListPageModel(INavigationService navigationService, IBFFHService bffhService) : base(navigationService) + public ServerListPageModel(INavigationService navigationService, ILoginStorageService loginStorageService, IAPIService apiService) : base(navigationService) { - _BFFHService = bffhService; + _LoginStorageService = loginStorageService; + _API = apiService.GetAPI(); AddInstancesCommand = new DelegateCommand(AddInstancesCommandExecute); SelectInstanceCommand = new DelegateCommand(SelectInstanceCommandExecute); @@ -28,14 +31,14 @@ namespace Borepin.PageModel #endregion #region Data - public override async Task LoadData() + public async Task LoadData() { - IList list = await _BFFHService.GetConnections().ConfigureAwait(false); - if (_BFFHService.IsConnected) + IList list = await _LoginStorageService.GetList().ConfigureAwait(false); + if (_API.IsConnected) { - ActiveConnection = new ServerListItemViewModel(_BFFHService.CurrentConnection); + ActiveConnection = new ServerListItemViewModel(_API.ConnectionData); - list.Remove(_BFFHService.CurrentConnection); + list.Remove(_API.ConnectionData); HasActiveConnection = true; } diff --git a/Borepin/Borepin/PageModel/ServerPageModel.cs b/Borepin/Borepin/PageModel/ServerPageModel.cs index 04ad232..a268796 100644 --- a/Borepin/Borepin/PageModel/ServerPageModel.cs +++ b/Borepin/Borepin/PageModel/ServerPageModel.cs @@ -1,7 +1,7 @@ using Borepin.Base; -using Borepin.Model; -using Borepin.Service.BFFH; -using Borepin.Service.BFFH.Exceptions; +using Borepin.Service; +using FabAccessAPI; +using FabAccessAPI.Exceptions; using Prism.Commands; using Prism.Navigation; using Prism.Services; @@ -13,22 +13,17 @@ using System.Windows.Input; namespace Borepin.PageModel { - public class ServerPageModel : PageModelBase + public class ServerPageModel : ConnectionModelBase { #region Private Fields private readonly IDialogService _DialogService; - private readonly IBFFHService _BFFHService; - private readonly IPageDialogService _PageDialogService; #endregion #region Constructors - public ServerPageModel(INavigationService navigationService, IDialogService dialogService, IBFFHService bffhService, IPageDialogService pageDialogService) : base(navigationService) + public ServerPageModel(INavigationService navigationService, IPageDialogService pageDialogService, IAPIService apiService, IDialogService dialogService,) : base(navigationService, pageDialogService, apiService) { _DialogService = dialogService; - _BFFHService = bffhService; - _PageDialogService = pageDialogService; - ConnectCommand = new DelegateCommand(async () => await ConnectCommandExecute().ConfigureAwait(false)); DisconnectCommand = new DelegateCommand(async () => await DisonnectCommandExecute().ConfigureAwait(false)); DeleteCommand = new DelegateCommand(DeleteCommandExecute); @@ -58,8 +53,8 @@ namespace Borepin.PageModel #endregion #region Fields - private Connection _Connection_Item; - public Connection Connection_Item + private ConnectionData _Connection_Item; + public ConnectionData Connection_Item { get => _Connection_Item; set => SetProperty(ref _Connection_Item, value); @@ -91,14 +86,14 @@ namespace Borepin.PageModel { IsBusy = true; - if(_BFFHService.IsConnected) + if(_API.IsConnected) { - await _BFFHService.Disconnect().ConfigureAwait(true); + await _API.Disconnect().ConfigureAwait(true); } try { - await _BFFHService.Connect(Connection_Item).ConfigureAwait(true); + await _API.Connect(Connection_Item).ConfigureAwait(true); } catch(ConnectingFailedException) { @@ -107,21 +102,13 @@ namespace Borepin.PageModel IsBusy = false; return; } - catch(AuthenticatingFailedException) + catch(AuthenticationFailedException) { await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to authenticate to server.", "Ok").ConfigureAwait(false); IsBusy = false; return; } - catch(MissingCredentialsException) - { - await _PageDialogService.DisplayAlertAsync("Secure Storage failed", "Unable to load connections from secure storage.\n Please recreate the connection.", "Ok").ConfigureAwait(false); - - await _NavigationService.NavigateAsync("/MainPage/NavigationPage/ServerListPage").ConfigureAwait(false); - IsBusy = false; - return; - } await _NavigationService.NavigateAsync("/MainPage/NavigationPage/MachineListPage").ConfigureAwait(false); } @@ -134,7 +121,7 @@ namespace Borepin.PageModel } public async Task DisonnectCommandExecute() { - await _BFFHService.Disconnect().ConfigureAwait(false); + await _API.Disconnect().ConfigureAwait(false); await LoadData().ConfigureAwait(false); } @@ -170,22 +157,5 @@ namespace Borepin.PageModel } } #endregion - - #region INavigationAware - public override void OnNavigatedFrom(INavigationParameters parameters) - { - - } - - public override async void OnNavigatedTo(INavigationParameters parameters) - { - if(Connection_Item == null) - { - Connection_Item = parameters["instance"] as Connection; - - await LoadData().ConfigureAwait(false); - } - } - #endregion } } diff --git a/Borepin/Borepin/Service/IAPIService.cs b/Borepin/Borepin/Service/IAPIService.cs new file mode 100644 index 0000000..4937beb --- /dev/null +++ b/Borepin/Borepin/Service/IAPIService.cs @@ -0,0 +1,9 @@ +using FabAccessAPI; + +namespace Borepin.Service +{ + public interface IAPIService + { + IAPI GetAPI(); + } +} diff --git a/Borepin/Borepin/ViewModel/ServerListItemViewModel.cs b/Borepin/Borepin/ViewModel/ServerListItemViewModel.cs index 4d6b33f..4de8be4 100644 --- a/Borepin/Borepin/ViewModel/ServerListItemViewModel.cs +++ b/Borepin/Borepin/ViewModel/ServerListItemViewModel.cs @@ -1,4 +1,5 @@ using Borepin.Model; +using FabAccessAPI; using Prism.Mvvm; namespace Borepin.ViewModel @@ -6,16 +7,16 @@ namespace Borepin.ViewModel public class ServerListItemViewModel : BindableBase { #region Constructors - public ServerListItemViewModel(Connection instance) + public ServerListItemViewModel(ConnectionData instance) { Instance = instance; - Address = instance.Address.Host; + Address = instance.Host.Host; Username = instance.Username; } #endregion #region Fields - public readonly Connection Instance; + public readonly ConnectionData Instance; private string _Address; public string Address diff --git a/FabAccessAPI/API.cs b/FabAccessAPI/API.cs index d2e375d..d903c7f 100644 --- a/FabAccessAPI/API.cs +++ b/FabAccessAPI/API.cs @@ -1,6 +1,7 @@ using Capnp.Rpc; using FabAccessAPI.Exceptions; using FabAccessAPI.Schema; +using NLog; using S22.Sasl; using System; using System.Collections.Generic; @@ -14,6 +15,10 @@ namespace FabAccessAPI { public class API : IAPI { + #region Logger + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); + #endregion + #region Private Members private TcpRpcClient _TcpRpcClient; private IBootstrap _Bootstrap; @@ -40,11 +45,14 @@ namespace FabAccessAPI if (args.LastState == ConnectionState.Initializing && args.NewState == ConnectionState.Active) { + Log.Trace("TcpRpcClient Event Connected"); eventHandler(this, ConnectionStatusChange.Connected); } if (args.LastState == ConnectionState.Active && args.NewState == ConnectionState.Down) { + Log.Trace("TcpRpcClient Event ConnectionLoss"); eventHandler(this, ConnectionStatusChange.ConnectionLoss); + _TcpRpcClient = null; } } #endregion @@ -98,11 +106,14 @@ namespace FabAccessAPI Session = await _Authenticate(connectionData).ConfigureAwait(false); ConnectionData = connectionData; + + Log.Info("API connected"); } - catch(System.Exception) + catch(System.Exception ex) { await Disconnect().ConfigureAwait(false); - throw; + Log.Warn(ex, "API connecting failed"); + throw ex; } } @@ -121,6 +132,8 @@ namespace FabAccessAPI ConnectionStatusChanged?.Invoke(this, ConnectionStatusChange.Disconnected); + Log.Info("API disconnected"); + return Task.CompletedTask; } @@ -132,6 +145,7 @@ namespace FabAccessAPI } ConnectionStatusChanged?.Invoke(this, ConnectionStatusChange.Reconnected); + Log.Info("API reconnected"); } public async Task TestConnection(ConnectionData connectionData, TcpRpcClient tcpRpcClient = null) diff --git a/FabAccessAPI/FabAccessAPI.csproj b/FabAccessAPI/FabAccessAPI.csproj index efab474..ba24ff4 100644 --- a/FabAccessAPI/FabAccessAPI.csproj +++ b/FabAccessAPI/FabAccessAPI.csproj @@ -12,8 +12,8 @@ - + diff --git a/FabAccessAPI/IAPI.cs b/FabAccessAPI/IAPI.cs index 5818e34..b2177e4 100644 --- a/FabAccessAPI/IAPI.cs +++ b/FabAccessAPI/IAPI.cs @@ -37,7 +37,7 @@ namespace FabAccessAPI /// Connect to BFFH Server /// /// - Task Connect(ConnectionData connectionData, TcpRpcClient tcpRpcClient); + Task Connect(ConnectionData connectionData, TcpRpcClient tcpRpcClient = null); /// /// Disconnect from BFFH Server @@ -53,6 +53,6 @@ namespace FabAccessAPI /// Connect to Server and get ConnectionInfo. /// The Connection is not maintained. /// - Task TestConnection(ConnectionData connectionData, TcpRpcClient tcpRpcClient); + Task TestConnection(ConnectionData connectionData, TcpRpcClient tcpRpcClient = null); } }