Codecleamup

This commit is contained in:
danielgrabowski 2019-11-06 13:47:00 +01:00
parent b21b2c3678
commit 507e08ab19
13 changed files with 43 additions and 63 deletions

View File

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

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client; using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -11,18 +12,18 @@ namespace Squirrowse.Client.Service
private readonly IConnectionManager connectionManager; private readonly IConnectionManager connectionManager;
private readonly ILogger<ActionDispatcher> logger; private readonly ILogger<ActionDispatcher> logger;
private readonly HubConnection session; private readonly HubConnection session;
public ActionDispatcher(ILogger<ActionDispatcher> logger, IConnectionManager connectionManager) public ActionDispatcher(ILogger<ActionDispatcher> logger, IConnectionManager connectionManager)
{ {
this.connectionManager = connectionManager; this.connectionManager = connectionManager;
this.logger = logger; this.logger = logger;
session.On("Start",StartStream); session.On("Start", StartStream);
session.On("Stop",StopStream); session.On("Stop", StopStream);
} }
public Task StopStream() public Task StopStream()
{ {
throw new System.NotImplementedException(); throw new NotImplementedException();
} }
public async Task SayHello() public async Task SayHello()
@ -34,13 +35,13 @@ namespace Squirrowse.Client.Service
{ {
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream"); logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
await session.SendAsync("UploadByteStream", asb); await session.SendAsync("UploadByteStream", asb);
logger.LogInformation($"{nameof(SendStreamAsync)} End stream"); logger.LogInformation($"{nameof(SendStreamAsync)} End stream");
} }
public Task StartStream() public Task StartStream()
{ {
throw new System.NotImplementedException(); throw new NotImplementedException();
} }
} }
} }

View File

@ -1,12 +1,9 @@
using System; using OpenCvSharp;
using System.Collections.Generic;
using System.Text;
using OpenCvSharp;
namespace Squirrowse.Client.Service namespace Squirrowse.Client.Service
{ {
public abstract class CameraFactory public abstract class CameraFactory
{ {
public abstract VideoCapture GetCamera(); public abstract VideoCapture GetCamera();
} }
} }

View File

@ -17,25 +17,18 @@ namespace Squirrowse.Client.Service
.AddMessagePackProtocol() .AddMessagePackProtocol()
.WithAutomaticReconnect() .WithAutomaticReconnect()
.Build(); .Build();
} }
public async Task<HubConnection> GetConnection() public async Task<HubConnection> GetConnection()
{ {
if (Connected) if (Connected) return _connection;
{
return _connection;
}
throw new Exception(); throw new Exception();
} }
public async Task InitConnection() public async Task InitConnection()
{ {
if (_connection.State == HubConnectionState.Connected) if (_connection.State == HubConnectionState.Connected) return;
{
return;
}
await _connection.StartAsync(); await _connection.StartAsync();
await RegisterOnHub(); await RegisterOnHub();
Connected = true; Connected = true;
@ -50,7 +43,7 @@ namespace Squirrowse.Client.Service
private async Task RegisterOnHub() private async Task RegisterOnHub()
{ {
await _connection.SendAsync("AddUser", Environment.UserName); await _connection.SendAsync("AddUser", Environment.UserName);
} }
} }
} }

View File

@ -1,10 +1,6 @@
using System; namespace Squirrowse.Client.Service
using System.Collections.Generic;
using System.Text;
namespace Squirrowse.Client.Service
{ {
public interface ICameraFactory public interface ICameraFactory
{ {
} }
} }

View File

@ -1,5 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using OpenCvSharp; using OpenCvSharp;

View File

@ -1,24 +1,21 @@
using System;
using System.Drawing;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using OpenCvSharp;
using Squirrowse.Client.Service; using Squirrowse.Client.Service;
namespace Squirrowse.Client namespace Squirrowse.Client
{ {
public class Worker : IHostedService public class Worker : IHostedService
{ {
private readonly ILogger<Worker> logger;
private readonly IConnectionManager _connectionManager; private readonly IConnectionManager _connectionManager;
private readonly ICameraService camera; private readonly ICameraService camera;
private readonly ILogger<Worker> logger;
public Worker(ILogger<Worker> logger, IConnectionManager connectionManager, ICameraService camera) public Worker(ILogger<Worker> logger, IConnectionManager connectionManager, ICameraService camera)
{ {
this.logger = logger; this.logger = logger;
this._connectionManager = connectionManager; _connectionManager = connectionManager;
this.camera = camera; this.camera = camera;
} }

View File

@ -2,13 +2,13 @@
{ {
public class User public class User
{ {
public string ConnectionId { get; set; }
public string AgentName { get; set; }
public User(string connectionId, string agentName) public User(string connectionId, string agentName)
{ {
ConnectionId = connectionId; ConnectionId = connectionId;
AgentName = agentName; AgentName = agentName;
} }
public string ConnectionId { get; set; }
public string AgentName { get; set; }
} }
} }

View File

@ -13,7 +13,7 @@ namespace Squirrowse.Core.Services
public static Mat ConvertByteToMat(this byte[] bytearr) public static Mat ConvertByteToMat(this byte[] bytearr)
{ {
var tempMat = Cv2.ImDecode(bytearr, ImreadModes.Unchanged); //keep as disposable var tempMat = Cv2.ImDecode(bytearr, ImreadModes.Unchanged); //keep as disposable
return tempMat ?? new Mat(); return tempMat ?? new Mat();
} }
} }

View File

@ -8,4 +8,4 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" />
<PackageReference Include="OpenCvSharp4.Windows" Version="4.1.1.20191021" /> <PackageReference Include="OpenCvSharp4.Windows" Version="4.1.1.20191021" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,16 +1,12 @@
using System; using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Squirrowse.Service.Hubs namespace Squirrowse.Service.Hubs
{ {
public interface IStreamManager public interface IStreamManager
{ {
Task AddUser(string connectionId, string agentName); Task AddUser(string connectionId, string agentName);
Task RemoveUserbyConnectionId(string connectionId); Task RemoveUserbyConnectionId(string connectionId);
Task RemoveUserByUserName(string agentName); Task RemoveUserByUserName(string agentName);
bool CheckUser(string agentName); bool CheckUser(string agentName);
} }
} }

View File

@ -1,5 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -13,6 +12,8 @@ namespace Squirrowse.Service.Hubs
{ {
private readonly ILogger<StreamHub> logger; private readonly ILogger<StreamHub> logger;
private readonly IStreamManager manager; private readonly IStreamManager manager;
public Window okno = new Window("test");
public StreamHub(ILogger<StreamHub> logger, IStreamManager manager) public StreamHub(ILogger<StreamHub> logger, IStreamManager manager)
{ {
this.logger = logger; this.logger = logger;
@ -21,7 +22,7 @@ namespace Squirrowse.Service.Hubs
public async Task AddUser(string UserName) public async Task AddUser(string UserName)
{ {
await manager.AddUser(Context.ConnectionId, UserName); await manager.AddUser(Context.ConnectionId, UserName);
} }
public async Task AddToGroup(Groups group, string user = "") public async Task AddToGroup(Groups group, string user = "")
@ -62,16 +63,15 @@ namespace Squirrowse.Service.Hubs
await client.SendAsync("Stop"); await client.SendAsync("Stop");
} }
public Window okno = new Window("test");
public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream) public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream)
{ {
await foreach (var frame in stream) await foreach (var frame in stream)
{ {
using var imgbuffer = frame.ConvertByteToMat(); using var imgbuffer = frame.ConvertByteToMat();
okno.ShowImage(imgbuffer); okno.ShowImage(imgbuffer);
Cv2.WaitKey(1); Cv2.WaitKey(1);
// logger.LogInformation($"Got frame size: {frame.Length} "); // logger.LogInformation($"Got frame size: {frame.Length} ");
// await Task.Delay(100); //leave some delay for debug purpose // await Task.Delay(100); //leave some delay for debug purpose
} }
} }

View File

@ -25,6 +25,7 @@ namespace Squirrowse.Service
services.AddControllers(); services.AddControllers();
services.AddMediatR(Assembly.GetAssembly(typeof(Startup))); services.AddMediatR(Assembly.GetAssembly(typeof(Startup)));
services.AddTransient<IStreamHub, StreamHub>(); services.AddTransient<IStreamHub, StreamHub>();
services.AddTransient<IStreamManager, StreamManager>();
services.AddCoreModule(); services.AddCoreModule();
services.AddSignalR() services.AddSignalR()
.AddHubOptions<StreamHub .AddHubOptions<StreamHub