Merge branch 'clientmg' of s421801/Squirrowse into develop

This commit is contained in:
Daniel Grabowski 2019-11-06 10:52:00 +00:00 committed by Gogs
commit b21b2c3678
17 changed files with 190 additions and 95 deletions

View File

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

View File

@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using Squirrowse.Core.Models;
namespace Squirrowse.Client.Service
{
public class ActionDispatcher : IActionDispatcher
{
private readonly IConnectionManager connectionManager;
private readonly ILogger<ActionDispatcher> logger;
private readonly HubConnection session;
public ActionDispatcher(ILogger<ActionDispatcher> logger, IConnectionManager connectionManager)
{
this.connectionManager = connectionManager;
this.logger = logger;
session.On("Start",StartStream);
session.On("Stop",StopStream);
}
public Task StopStream()
{
throw new System.NotImplementedException();
}
public async Task SayHello()
{
await session.SendAsync("AddToGroup", Groups.debug);
}
public async Task SendStreamAsync(IAsyncEnumerable<byte[]> asb)
{
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
await session.SendAsync("UploadByteStream", asb);
logger.LogInformation($"{nameof(SendStreamAsync)} End stream");
}
public Task StartStream()
{
throw new System.NotImplementedException();
}
}
}

View File

@ -9,7 +9,7 @@ namespace Squirrowse.Client.Service
private readonly int Height; private readonly int Height;
private readonly int Width; private readonly int Width;
public Camera(int height = 480, int width = 640, double fps = 15f) public Camera(int height = 480, int width = 640, double fps = 30f)
{ {
Height = height; Height = height;
Width = width; Width = width;

View File

@ -1,6 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using OpenCvSharp; using OpenCvSharp;
using Squirrowse.Core.Services; using Squirrowse.Core.Services;
@ -10,6 +8,7 @@ namespace Squirrowse.Client.Service
public class CameraService : ICameraService public class CameraService : ICameraService
{ {
private readonly VideoCapture _videoCapture; private readonly VideoCapture _videoCapture;
public CameraService(CameraFactory cam) public CameraService(CameraFactory cam)
{ {
_videoCapture = cam.GetCamera(); _videoCapture = cam.GetCamera();
@ -17,18 +16,16 @@ namespace Squirrowse.Client.Service
public async Task<Mat> GetFrame() public async Task<Mat> GetFrame()
{ {
var video = _videoCapture.RetrieveMat(); var video = _videoCapture.RetrieveMat();
return video; return video;
} }
public async IAsyncEnumerable<byte[]> GetFramesAsyncEnumerator() public async IAsyncEnumerable<byte[]> GetFramesAsyncEnumerator()
{ {
while (true) using var fr = await GetFrame();
{ yield return fr.ConvertToJpgByte();
var fr = await GetFrame(); //fr.Dispose();
yield return fr.ConvertToJpgByte();
}
} }
} }
} }

View File

@ -1,4 +1,6 @@
using Microsoft.AspNetCore.SignalR.Client; using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace Squirrowse.Client.Service namespace Squirrowse.Client.Service
@ -6,6 +8,7 @@ namespace Squirrowse.Client.Service
public class ConnectionManager : IConnectionManager public class ConnectionManager : IConnectionManager
{ {
private readonly HubConnection _connection; private readonly HubConnection _connection;
private bool Connected;
public ConnectionManager(string url, int port) public ConnectionManager(string url, int port)
{ {
@ -14,11 +17,40 @@ namespace Squirrowse.Client.Service
.AddMessagePackProtocol() .AddMessagePackProtocol()
.WithAutomaticReconnect() .WithAutomaticReconnect()
.Build(); .Build();
} }
public HubConnection Connect() public async Task<HubConnection> GetConnection()
{ {
return _connection; if (Connected)
{
return _connection;
}
throw new Exception();
}
public async Task InitConnection()
{
if (_connection.State == HubConnectionState.Connected)
{
return;
}
await _connection.StartAsync();
await RegisterOnHub();
Connected = true;
}
public async Task Disconnect()
{
if (_connection.State == HubConnectionState.Disconnected) throw new Exception();
await _connection.StopAsync();
Connected = false;
}
private async Task RegisterOnHub()
{
await _connection.SendAsync("AddUser", Environment.UserName);
} }
} }
} }

View File

@ -3,9 +3,11 @@ using System.Threading.Tasks;
namespace Squirrowse.Client.Service namespace Squirrowse.Client.Service
{ {
public interface IStreamService public interface IActionDispatcher
{ {
Task SendStreamAsync(IAsyncEnumerable<byte[]> asb); Task SendStreamAsync(IAsyncEnumerable<byte[]> asb);
Task StartStream();
Task StopStream();
Task SayHello(); Task SayHello();
} }
} }

View File

@ -1,9 +1,12 @@
using Microsoft.AspNetCore.SignalR.Client; using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
namespace Squirrowse.Client.Service namespace Squirrowse.Client.Service
{ {
public interface IConnectionManager public interface IConnectionManager
{ {
HubConnection Connect(); Task<HubConnection> GetConnection();
Task InitConnection();
Task Disconnect();
} }
} }

View File

@ -1,38 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using Squirrowse.Core.Models;
namespace Squirrowse.Client.Service
{
public class StreamService : IStreamService
{
private readonly IConnectionManager connectionManager;
private readonly ILogger<StreamService> logger;
private readonly HubConnection session;
public StreamService(ILogger<StreamService> logger, IConnectionManager connectionManager)
{
this.connectionManager = connectionManager;
this.logger = logger;
session = connectionManager.Connect();
session.StartAsync();
}
public async Task SayHello()
{
await session.SendAsync("AddToGroup", Groups.debug);
}
public async Task SendStreamAsync(IAsyncEnumerable<byte[]> asb)
{
while (true)
{
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
await session.SendAsync("UploadByteStream", asb);
}
}
}
}

View File

@ -1,35 +1,35 @@
using System; 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 : BackgroundService public class Worker : IHostedService
{ {
private readonly ILogger<Worker> logger; private readonly ILogger<Worker> logger;
private readonly IStreamService streamService; private readonly IConnectionManager _connectionManager;
private readonly ICameraService camera; private readonly ICameraService camera;
public Worker(ILogger<Worker> logger, IStreamService streamService, ICameraService camera) public Worker(ILogger<Worker> logger, IConnectionManager connectionManager, ICameraService camera)
{ {
this.logger = logger; this.logger = logger;
this.streamService = streamService; this._connectionManager = connectionManager;
this.camera = camera; this.camera = camera;
} }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) public async Task StartAsync(CancellationToken cancellationToken)
{ {
await _connectionManager.InitConnection();
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); }
public async Task StopAsync(CancellationToken cancellationToken)
await streamService.SendStreamAsync(camera.GetFramesAsyncEnumerator()); {
await _connectionManager.Disconnect();
// await Task.Delay(1000, stoppingToken);
} }
} }
} }

View File

@ -0,0 +1,14 @@
namespace Squirrowse.Core.Models
{
public class User
{
public string ConnectionId { get; set; }
public string AgentName { get; set; }
public User(string connectionId, string agentName)
{
ConnectionId = connectionId;
AgentName = agentName;
}
}
}

View File

@ -13,7 +13,7 @@ namespace Squirrowse.Core.Services
public static Mat ConvertByteToMat(this byte[] bytearr) public static Mat ConvertByteToMat(this byte[] bytearr)
{ {
using 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

@ -6,7 +6,7 @@ namespace Squirrowse.Service.Hubs
{ {
public interface IStreamHub public interface IStreamHub
{ {
Task AddClient(); Task AddUser(string username);
Task UploadByteStream(IAsyncEnumerable<byte[]> stream); Task UploadByteStream(IAsyncEnumerable<byte[]> stream);
Task Startstream(string userId); Task Startstream(string userId);
Task StopStream(string userId); Task StopStream(string userId);

View File

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

View File

@ -3,22 +3,25 @@ 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;
using OpenCvSharp;
using Squirrowse.Core.Models; using Squirrowse.Core.Models;
using Squirrowse.Core.Services;
namespace Squirrowse.Service.Hubs namespace Squirrowse.Service.Hubs
{ {
public class StreamHub : Hub, IStreamHub //fujka public class StreamHub : Hub, IStreamHub //fujka
{ {
private readonly ILogger<StreamHub> logger; private readonly ILogger<StreamHub> logger;
private readonly IStreamManager manager;
public StreamHub(ILogger<StreamHub> logger) public StreamHub(ILogger<StreamHub> logger, IStreamManager manager)
{ {
this.logger = logger; this.logger = logger;
this.manager = manager;
} }
public async Task AddClient() public async Task AddUser(string UserName)
{ {
throw new NotImplementedException(); await manager.AddUser(Context.ConnectionId, UserName);
} }
public async Task AddToGroup(Groups group, string user = "") public async Task AddToGroup(Groups group, string user = "")
@ -59,13 +62,17 @@ 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)
{ {
logger.LogInformation($"Got frame size: {frame.Length} "); using var imgbuffer = frame.ConvertByteToMat();
await Task.Delay(100); //leave some delay for debug purpose okno.ShowImage(imgbuffer);
Cv2.WaitKey(1);
// logger.LogInformation($"Got frame size: {frame.Length} ");
// await Task.Delay(100); //leave some delay for debug purpose
} }
} }
} }

View File

@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Squirrowse.Core.Models;
namespace Squirrowse.Service.Hubs
{
public class StreamManager : IStreamManager
{
private readonly List<User> _users = new List<User>(); //temporary
public Task AddUser(string connectionId, string userName)
{
_users.Add(new User(connectionId, userName));
return Task.CompletedTask;
}
public Task RemoveUserbyConnectionId(string connectionId)
{
_users.Remove(_users.First(user => user.ConnectionId == connectionId));
return Task.CompletedTask;
}
public Task RemoveUserByUserName(string userName)
{
_users.RemoveAll(user => user.AgentName == userName);
return Task.CompletedTask;
}
public bool CheckUser(string userName)
{
return _users.Any(user => user.AgentName == userName);
}
}
}

View File

@ -1,7 +0,0 @@
namespace Squirrowse.Service
{
public interface IUserProvider
{
string GetUserId();
}
}

View File

@ -1,12 +0,0 @@
using System;
namespace Squirrowse.Service
{
public class UserProvider : IUserProvider
{
public string GetUserId()
{
throw new NotImplementedException();
}
}
}