Refactor hub/client

This commit is contained in:
danielgrabowski 2019-11-06 11:49:39 +01:00
parent 2e624d9b7a
commit f444c9fbae
4 changed files with 27 additions and 14 deletions

View File

@ -19,7 +19,7 @@ namespace Squirrowse.Client
services.AddHostedService<Worker>();
services.AddSingleton<IConnectionManager>(x=> new ConnectionManager("http://localhost", 5000)); //keep as transient for now
services.AddSingleton< ICameraService, CameraService >(x=>new CameraService(new Camera()));
services.AddTransient<IActionDispatcher, ActionDispatcher>();
services.AddSingleton<IActionDispatcher, ActionDispatcher>();
});
}

View File

@ -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()

View File

@ -6,5 +6,7 @@ namespace Squirrowse.Client.Service
public interface IConnectionManager
{
Task<HubConnection> GetConnection();
Task InitConnection();
Task Disconnect();
}
}

View File

@ -9,7 +9,7 @@ using Squirrowse.Client.Service;
namespace Squirrowse.Client
{
public class Worker : BackgroundService
public class Worker : IHostedService
{
private readonly ILogger<Worker> 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();
}
}
}