Merge branch 'clientmg' of s421801/Squirrowse into develop
This commit is contained in:
commit
b21b2c3678
@ -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<IStreamService, StreamService>();
|
||||
services.AddSingleton<IActionDispatcher, ActionDispatcher>();
|
||||
|
||||
});
|
||||
}
|
||||
|
46
Squirrowse.Client/Service/ActionDispatcher.cs
Normal file
46
Squirrowse.Client/Service/ActionDispatcher.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ namespace Squirrowse.Client.Service
|
||||
private readonly int Height;
|
||||
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;
|
||||
Width = width;
|
||||
|
@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using OpenCvSharp;
|
||||
using Squirrowse.Core.Services;
|
||||
@ -10,6 +8,7 @@ namespace Squirrowse.Client.Service
|
||||
public class CameraService : ICameraService
|
||||
{
|
||||
private readonly VideoCapture _videoCapture;
|
||||
|
||||
public CameraService(CameraFactory cam)
|
||||
{
|
||||
_videoCapture = cam.GetCamera();
|
||||
@ -17,18 +16,16 @@ namespace Squirrowse.Client.Service
|
||||
|
||||
|
||||
public async Task<Mat> GetFrame()
|
||||
{
|
||||
{
|
||||
var video = _videoCapture.RetrieveMat();
|
||||
return video;
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<byte[]> GetFramesAsyncEnumerator()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var fr = await GetFrame();
|
||||
yield return fr.ConvertToJpgByte();
|
||||
}
|
||||
using var fr = await GetFrame();
|
||||
yield return fr.ConvertToJpgByte();
|
||||
//fr.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Squirrowse.Client.Service
|
||||
@ -6,6 +8,7 @@ namespace Squirrowse.Client.Service
|
||||
public class ConnectionManager : IConnectionManager
|
||||
{
|
||||
private readonly HubConnection _connection;
|
||||
private bool Connected;
|
||||
|
||||
public ConnectionManager(string url, int port)
|
||||
{
|
||||
@ -14,11 +17,40 @@ namespace Squirrowse.Client.Service
|
||||
.AddMessagePackProtocol()
|
||||
.WithAutomaticReconnect()
|
||||
.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);
|
||||
}
|
||||
}
|
||||
}
|
@ -3,9 +3,11 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Squirrowse.Client.Service
|
||||
{
|
||||
public interface IStreamService
|
||||
public interface IActionDispatcher
|
||||
{
|
||||
Task SendStreamAsync(IAsyncEnumerable<byte[]> asb);
|
||||
Task StartStream();
|
||||
Task StopStream();
|
||||
Task SayHello();
|
||||
}
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
|
||||
namespace Squirrowse.Client.Service
|
||||
{
|
||||
public interface IConnectionManager
|
||||
{
|
||||
HubConnection Connect();
|
||||
Task<HubConnection> GetConnection();
|
||||
Task InitConnection();
|
||||
Task Disconnect();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,35 +1,35 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenCvSharp;
|
||||
using Squirrowse.Client.Service;
|
||||
|
||||
namespace Squirrowse.Client
|
||||
{
|
||||
public class Worker : BackgroundService
|
||||
public class Worker : IHostedService
|
||||
{
|
||||
private readonly ILogger<Worker> logger;
|
||||
private readonly IStreamService streamService;
|
||||
private readonly IConnectionManager _connectionManager;
|
||||
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.streamService = streamService;
|
||||
this._connectionManager = connectionManager;
|
||||
this.camera = camera;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
await _connectionManager.InitConnection();
|
||||
}
|
||||
|
||||
|
||||
await streamService.SendStreamAsync(camera.GetFramesAsyncEnumerator());
|
||||
|
||||
// await Task.Delay(1000, stoppingToken);
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _connectionManager.Disconnect();
|
||||
}
|
||||
}
|
||||
}
|
14
Squirrowse.Core/Models/User.cs
Normal file
14
Squirrowse.Core/Models/User.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@ namespace Squirrowse.Core.Services
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ namespace Squirrowse.Service.Hubs
|
||||
{
|
||||
public interface IStreamHub
|
||||
{
|
||||
Task AddClient();
|
||||
Task AddUser(string username);
|
||||
Task UploadByteStream(IAsyncEnumerable<byte[]> stream);
|
||||
Task Startstream(string userId);
|
||||
Task StopStream(string userId);
|
||||
|
16
Squirrowse.Service/Hubs/IStreamManager.cs
Normal file
16
Squirrowse.Service/Hubs/IStreamManager.cs
Normal 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);
|
||||
|
||||
}
|
||||
}
|
@ -3,22 +3,25 @@ using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenCvSharp;
|
||||
using Squirrowse.Core.Models;
|
||||
using Squirrowse.Core.Services;
|
||||
|
||||
namespace Squirrowse.Service.Hubs
|
||||
{
|
||||
public class StreamHub : Hub, IStreamHub //fujka
|
||||
{
|
||||
private readonly ILogger<StreamHub> logger;
|
||||
|
||||
public StreamHub(ILogger<StreamHub> logger)
|
||||
private readonly IStreamManager manager;
|
||||
public StreamHub(ILogger<StreamHub> logger, IStreamManager manager)
|
||||
{
|
||||
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 = "")
|
||||
@ -59,13 +62,17 @@ namespace Squirrowse.Service.Hubs
|
||||
|
||||
await client.SendAsync("Stop");
|
||||
}
|
||||
|
||||
public Window okno = new Window("test");
|
||||
public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream)
|
||||
{
|
||||
|
||||
await foreach (var frame in stream)
|
||||
{
|
||||
logger.LogInformation($"Got frame size: {frame.Length} ");
|
||||
await Task.Delay(100); //leave some delay for debug purpose
|
||||
using var imgbuffer = frame.ConvertByteToMat();
|
||||
okno.ShowImage(imgbuffer);
|
||||
Cv2.WaitKey(1);
|
||||
// logger.LogInformation($"Got frame size: {frame.Length} ");
|
||||
// await Task.Delay(100); //leave some delay for debug purpose
|
||||
}
|
||||
}
|
||||
}
|
||||
|
35
Squirrowse.Service/Hubs/StreamManager.cs
Normal file
35
Squirrowse.Service/Hubs/StreamManager.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
namespace Squirrowse.Service
|
||||
{
|
||||
public interface IUserProvider
|
||||
{
|
||||
string GetUserId();
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Squirrowse.Service
|
||||
{
|
||||
public class UserProvider : IUserProvider
|
||||
{
|
||||
public string GetUserId()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user