diff --git a/Squirrowse.Client/Service/CameraService.cs b/Squirrowse.Client/Service/CameraService.cs new file mode 100644 index 0000000..5cdbe0c --- /dev/null +++ b/Squirrowse.Client/Service/CameraService.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using OpenCvSharp; + +namespace Squirrowse.Client.Service +{ + public class CameraService : ICameraService + { + public VideoCapture GetCamera(int height=480,int widght=640,double fps=15f,bool disposable = false) + { + + var cam = new VideoCapture(CaptureDevice.Any); + cam.Fps = fps; + cam.FrameHeight = height; + cam.FrameWidth = widght; + cam.IsEnabledDispose = disposable; + + return cam; + } + + public Task GetFrame(VideoCapture video) + { + throw new NotImplementedException(); + } + } +} diff --git a/Squirrowse.Client/Service/ConnectionManager.cs b/Squirrowse.Client/Service/ConnectionManager.cs index 5ce3ae9..e2bf587 100644 --- a/Squirrowse.Client/Service/ConnectionManager.cs +++ b/Squirrowse.Client/Service/ConnectionManager.cs @@ -14,8 +14,9 @@ namespace Squirrowse.Client.Service .AddMessagePackProtocol() .WithAutomaticReconnect() .Build(); + } - public HubConnection EstablishHubConnection() => _connection; + public HubConnection Connect() => _connection; } } \ No newline at end of file diff --git a/Squirrowse.Client/Service/ICameraService.cs b/Squirrowse.Client/Service/ICameraService.cs new file mode 100644 index 0000000..3849785 --- /dev/null +++ b/Squirrowse.Client/Service/ICameraService.cs @@ -0,0 +1,14 @@ +using OpenCvSharp; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Squirrowse.Client.Service +{ + public interface ICameraService + { + public VideoCapture GetCamera(int height = 480, int widght = 640, double fps = 15f, bool disposable = false); + public Task GetFrame(VideoCapture video); + } +} diff --git a/Squirrowse.Client/Service/IConnectionManager.cs b/Squirrowse.Client/Service/IConnectionManager.cs index f5d208f..d014347 100644 --- a/Squirrowse.Client/Service/IConnectionManager.cs +++ b/Squirrowse.Client/Service/IConnectionManager.cs @@ -4,7 +4,7 @@ namespace Squirrowse.Client.Service { public interface IConnectionManager { - HubConnection EstablishHubConnection(); + HubConnection Connect(); } } \ No newline at end of file diff --git a/Squirrowse.Client/Service/IStreamService.cs b/Squirrowse.Client/Service/IStreamService.cs new file mode 100644 index 0000000..a56662e --- /dev/null +++ b/Squirrowse.Client/Service/IStreamService.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Squirrowse.Client.Service +{ + public interface IStreamService + { + Task SendStreamAsync(IAsyncEnumerable asb); + Task SayHello(); + + } +} diff --git a/Squirrowse.Client/Service/StreamService.cs b/Squirrowse.Client/Service/StreamService.cs new file mode 100644 index 0000000..c9292f6 --- /dev/null +++ b/Squirrowse.Client/Service/StreamService.cs @@ -0,0 +1,50 @@ +using Microsoft.AspNetCore.SignalR.Client; +using Microsoft.Extensions.Logging; +using Squirrowse.Core.Models; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Squirrowse.Client.Service +{ + public class StreamService : IStreamService + { + private readonly IConnectionManager connectionManager; + private readonly ILogger logger; + private readonly HubConnection session; + public StreamService(ILogger logger, IConnectionManager connectionManager) + { + this.connectionManager = connectionManager; + this.logger = logger; + session = connectionManager.Connect(); + session.StartAsync().ConfigureAwait(false); + } + + public async Task SayHello() + { + try + { + await session.SendAsync("AddToGroup",Groups.debug); + } + finally + { + + } + } + + public async Task SendStreamAsync(IAsyncEnumerable asb) + { + try + { + logger.LogInformation($"{nameof(SendStreamAsync)} Start stream"); + await session.SendAsync("UploadByteStream", asb, default); + + } + finally + { + logger.LogInformation($"{nameof(SendStreamAsync)} End stream"); + } + } + } +} diff --git a/Squirrowse.Client/Squirrowse.Client.csproj b/Squirrowse.Client/Squirrowse.Client.csproj index 81c0779..a134225 100644 --- a/Squirrowse.Client/Squirrowse.Client.csproj +++ b/Squirrowse.Client/Squirrowse.Client.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/Squirrowse.Client/Worker.cs b/Squirrowse.Client/Worker.cs index 33a59d9..ac2a609 100644 --- a/Squirrowse.Client/Worker.cs +++ b/Squirrowse.Client/Worker.cs @@ -5,16 +5,19 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Squirrowse.Client.Service; namespace Squirrowse.Client { public class Worker : BackgroundService { private readonly ILogger _logger; + private readonly IStreamService _streamService; - public Worker(ILogger logger) + public Worker(ILogger logger, IStreamService streamService) { _logger = logger; + _streamService = streamService; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) @@ -22,6 +25,7 @@ namespace Squirrowse.Client while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); + await Task.Delay(1000, stoppingToken); } } diff --git a/Squirrowse.Core/Models/Groups.cs b/Squirrowse.Core/Models/Groups.cs new file mode 100644 index 0000000..8285440 --- /dev/null +++ b/Squirrowse.Core/Models/Groups.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Squirrowse.Core.Models +{ + public enum Groups + { + debug=-1, + normal, + superUser + } +} diff --git a/Squirrowse.Service/Hubs/IStreamHub.cs b/Squirrowse.Service/Hubs/IStreamHub.cs index b73fa0b..81fb63c 100644 --- a/Squirrowse.Service/Hubs/IStreamHub.cs +++ b/Squirrowse.Service/Hubs/IStreamHub.cs @@ -1,10 +1,19 @@ -using System.Collections.Generic; +using Squirrowse.Core.Models; +using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace Squirrowse.Service.Hubs { public interface IStreamHub { + Task AddClient(); Task UploadByteStream(IAsyncEnumerable stream); + Task Startstream(string userId); + Task StopStream(string userId); + Task ExecCommandOnAll(string command,object[] act);//gni + Task AddToGroup(Groups group,string user=""); + Task RemoveFromGroup(Groups group,string user=""); + } } \ No newline at end of file diff --git a/Squirrowse.Service/Hubs/StreamHub.cs b/Squirrowse.Service/Hubs/StreamHub.cs index 0db5054..fab0661 100644 --- a/Squirrowse.Service/Hubs/StreamHub.cs +++ b/Squirrowse.Service/Hubs/StreamHub.cs @@ -6,17 +6,63 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; using NLog; +using Squirrowse.Core.Models; namespace Squirrowse.Service.Hubs { - public class StreamHub : Hub, IStreamHub + public class StreamHub : Hub, IStreamHub //fujka { private readonly ILogger logger; - + public StreamHub(ILogger logger) { this.logger = logger; } + + public async Task AddClient() + { + throw new NotImplementedException(); + } + + public async Task AddToGroup(Groups group, string user = "") + { + string connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user; + await Groups.AddToGroupAsync(connectionId, group.ToString()); + logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}"); + } + /// + /// USE ONLY FOR DEBUG + /// + /// + /// DESTRUCTION + public async Task ExecCommandOnAll(string command,object[] act) + { + await Clients.All.SendCoreAsync(command,act); + } + + public async Task RemoveFromGroup(Groups group, string user = "") + { + + string connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user; + await Groups.RemoveFromGroupAsync(connectionId, group.ToString()); + logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}"); + } + + public async Task Startstream(string clientId) + { + var client = Clients.Client(clientId); + + await client.SendAsync("Start"); + } + + + public async Task StopStream(string clientId) + { + var client = Clients.Client(clientId); + + await client.SendAsync("Stop"); + } + public async Task UploadByteStream(IAsyncEnumerable stream) { await foreach (var frame in stream) @@ -25,5 +71,6 @@ namespace Squirrowse.Service.Hubs await Task.Delay(100); //leave some delay for debug purpose } } + } } diff --git a/Squirrowse.Service/IUserProvider.cs b/Squirrowse.Service/IUserProvider.cs new file mode 100644 index 0000000..6c98471 --- /dev/null +++ b/Squirrowse.Service/IUserProvider.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Squirrowse.Service +{ + public interface IUserProvider + { + string GetUserId(); + } +} diff --git a/Squirrowse.Service/Squirrowse.Service.csproj b/Squirrowse.Service/Squirrowse.Service.csproj index 5d22ada..c705037 100644 --- a/Squirrowse.Service/Squirrowse.Service.csproj +++ b/Squirrowse.Service/Squirrowse.Service.csproj @@ -18,4 +18,8 @@ + + + + diff --git a/Squirrowse.Service/UserProvider.cs b/Squirrowse.Service/UserProvider.cs new file mode 100644 index 0000000..9b4279b --- /dev/null +++ b/Squirrowse.Service/UserProvider.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Squirrowse.Service +{ + public class UserProvider : IUserProvider + { + public string GetUserId() + { + throw new NotImplementedException(); + } + } +}