From f444c9fbae8f292377fa1b24ebf1f87d99840425 Mon Sep 17 00:00:00 2001 From: danielgrabowski Date: Wed, 6 Nov 2019 11:49:39 +0100 Subject: [PATCH] Refactor hub/client --- Squirrowse.Client/Program.cs | 2 +- .../Service/ConnectionManager.cs | 19 +++++++++++++++++-- .../Service/IConnectionManager.cs | 2 ++ Squirrowse.Client/Worker.cs | 18 +++++++----------- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Squirrowse.Client/Program.cs b/Squirrowse.Client/Program.cs index 661b03a..3af0c5b 100644 --- a/Squirrowse.Client/Program.cs +++ b/Squirrowse.Client/Program.cs @@ -19,7 +19,7 @@ namespace Squirrowse.Client services.AddHostedService(); services.AddSingleton(x=> new ConnectionManager("http://localhost", 5000)); //keep as transient for now services.AddSingleton< ICameraService, CameraService >(x=>new CameraService(new Camera())); - services.AddTransient(); + services.AddSingleton(); }); } diff --git a/Squirrowse.Client/Service/ConnectionManager.cs b/Squirrowse.Client/Service/ConnectionManager.cs index 60dc8b1..642c289 100644 --- a/Squirrowse.Client/Service/ConnectionManager.cs +++ b/Squirrowse.Client/Service/ConnectionManager.cs @@ -26,11 +26,26 @@ namespace Squirrowse.Client.Service { return _connection; } - + + throw new Exception(); + } + + public async Task InitConnection() + { + if (_connection.State == HubConnectionState.Connected) + { + return; + } await _connection.StartAsync(); await RegisterOnHub(); Connected = true; - return _connection; + } + + public async Task Disconnect() + { + if (_connection.State == HubConnectionState.Disconnected) throw new Exception(); + await _connection.StopAsync(); + Connected = false; } private async Task RegisterOnHub() diff --git a/Squirrowse.Client/Service/IConnectionManager.cs b/Squirrowse.Client/Service/IConnectionManager.cs index 4edf008..5398613 100644 --- a/Squirrowse.Client/Service/IConnectionManager.cs +++ b/Squirrowse.Client/Service/IConnectionManager.cs @@ -6,5 +6,7 @@ namespace Squirrowse.Client.Service public interface IConnectionManager { Task GetConnection(); + Task InitConnection(); + Task Disconnect(); } } \ No newline at end of file diff --git a/Squirrowse.Client/Worker.cs b/Squirrowse.Client/Worker.cs index 68bb130..7c714e4 100644 --- a/Squirrowse.Client/Worker.cs +++ b/Squirrowse.Client/Worker.cs @@ -9,7 +9,7 @@ using Squirrowse.Client.Service; namespace Squirrowse.Client { - public class Worker : BackgroundService + public class Worker : IHostedService { private readonly ILogger logger; private readonly IConnectionManager _connectionManager; @@ -22,18 +22,14 @@ namespace Squirrowse.Client this.camera = camera; } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) + public async Task StartAsync(CancellationToken cancellationToken) { + await _connectionManager.InitConnection(); + } - while (!stoppingToken.IsCancellationRequested) - { - logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); - - await _connectionManager.GetConnection(); - //await _connectionManager.SendStreamAsync(camera.GetFramesAsyncEnumerator()); - - // await Task.Delay(50, stoppingToken); - } + public async Task StopAsync(CancellationToken cancellationToken) + { + await _connectionManager.Disconnect(); } } } \ No newline at end of file