Codecleanup

This commit is contained in:
danielgrabowski 2019-11-04 09:29:37 +01:00
parent 0f05560c45
commit e91a5143dd
23 changed files with 64 additions and 106 deletions

View File

@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Squirrowse.Client.Service; using Squirrowse.Client.Service;
@ -15,12 +11,14 @@ namespace Squirrowse.Client
CreateHostBuilder(args).Build().Run(); CreateHostBuilder(args).Build().Run();
} }
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args)
Host.CreateDefaultBuilder(args) {
return Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) => .ConfigureServices((hostContext, services) =>
{ {
services.AddHostedService<Worker>(); services.AddHostedService<Worker>();
services.AddTransient<IConnectionManager, ConnectionManager>();//keep as transient for now services.AddTransient<IConnectionManager, ConnectionManager>(); //keep as transient for now
}); });
}
} }
} }

View File

@ -7,4 +7,4 @@
} }
} }
} }
} }

View File

@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using OpenCvSharp; using OpenCvSharp;
@ -8,15 +6,14 @@ namespace Squirrowse.Client.Service
{ {
public class CameraService : ICameraService public class CameraService : ICameraService
{ {
public VideoCapture GetCamera(int height=480,int widght=640,double fps=15f,bool disposable = false) public VideoCapture GetCamera(int height = 480, int widght = 640, double fps = 15f, bool disposable = false)
{ {
var cam = new VideoCapture(CaptureDevice.Any); var cam = new VideoCapture(CaptureDevice.Any);
cam.Fps = fps; cam.Fps = fps;
cam.FrameHeight = height; cam.FrameHeight = height;
cam.FrameWidth = widght; cam.FrameWidth = widght;
cam.IsEnabledDispose = disposable; cam.IsEnabledDispose = disposable;
return cam; return cam;
} }
@ -25,4 +22,4 @@ namespace Squirrowse.Client.Service
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
} }

View File

@ -14,9 +14,11 @@ namespace Squirrowse.Client.Service
.AddMessagePackProtocol() .AddMessagePackProtocol()
.WithAutomaticReconnect() .WithAutomaticReconnect()
.Build(); .Build();
} }
public HubConnection Connect() => _connection; public HubConnection Connect()
{
return _connection;
}
} }
} }

View File

@ -1,14 +1,11 @@
using OpenCvSharp; using System.Threading.Tasks;
using System; using OpenCvSharp;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Squirrowse.Client.Service namespace Squirrowse.Client.Service
{ {
public interface ICameraService public interface ICameraService
{ {
public VideoCapture GetCamera(int height = 480, int widght = 640, double fps = 15f, bool disposable = false); VideoCapture GetCamera(int height = 480, int widght = 640, double fps = 15f, bool disposable = false);
public Task<Mat> GetFrame(VideoCapture video); Task<Mat> GetFrame(VideoCapture video);
} }
} }

View File

@ -5,6 +5,5 @@ namespace Squirrowse.Client.Service
public interface IConnectionManager public interface IConnectionManager
{ {
HubConnection Connect(); HubConnection Connect();
} }
} }

View File

@ -1,14 +1,11 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Squirrowse.Client.Service namespace Squirrowse.Client.Service
{ {
public interface IStreamService public interface IStreamService
{ {
Task SendStreamAsync(IAsyncEnumerable<byte[]> asb); Task SendStreamAsync(IAsyncEnumerable<byte[]> asb);
Task SayHello(); Task SayHello();
} }
} }

View File

@ -1,10 +1,8 @@
using Microsoft.AspNetCore.SignalR.Client; using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Squirrowse.Core.Models; using Squirrowse.Core.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Squirrowse.Client.Service namespace Squirrowse.Client.Service
{ {
@ -13,6 +11,7 @@ namespace Squirrowse.Client.Service
private readonly IConnectionManager connectionManager; private readonly IConnectionManager connectionManager;
private readonly ILogger<StreamService> logger; private readonly ILogger<StreamService> logger;
private readonly HubConnection session; private readonly HubConnection session;
public StreamService(ILogger<StreamService> logger, IConnectionManager connectionManager) public StreamService(ILogger<StreamService> logger, IConnectionManager connectionManager)
{ {
this.connectionManager = connectionManager; this.connectionManager = connectionManager;
@ -23,14 +22,7 @@ namespace Squirrowse.Client.Service
public async Task SayHello() public async Task SayHello()
{ {
try await session.SendAsync("AddToGroup", Groups.debug);
{
await session.SendAsync("AddToGroup",Groups.debug);
}
finally
{
}
} }
public async Task SendStreamAsync(IAsyncEnumerable<byte[]> asb) public async Task SendStreamAsync(IAsyncEnumerable<byte[]> asb)
@ -38,8 +30,7 @@ namespace Squirrowse.Client.Service
try try
{ {
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream"); logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
await session.SendAsync("UploadByteStream", asb, default); await session.SendAsync("UploadByteStream", asb);
} }
finally finally
{ {
@ -47,4 +38,4 @@ namespace Squirrowse.Client.Service
} }
} }
} }
} }

View File

@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -25,9 +23,9 @@ namespace Squirrowse.Client
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
{ {
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await Task.Delay(1000, stoppingToken); await Task.Delay(1000, stoppingToken);
} }
} }
} }
} }

View File

@ -6,4 +6,4 @@
"Microsoft": "Information" "Microsoft": "Information"
} }
} }
} }

View File

@ -6,4 +6,4 @@
"Microsoft.Hosting.Lifetime": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }
} }
} }

View File

@ -1,5 +1,4 @@
using System; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Squirrowse.Core namespace Squirrowse.Core
@ -13,4 +12,4 @@ namespace Squirrowse.Core
return services; return services;
} }
} }
} }

View File

@ -1,13 +1,9 @@
using System; namespace Squirrowse.Core.Models
using System.Collections.Generic;
using System.Text;
namespace Squirrowse.Core.Models
{ {
public enum Groups public enum Groups
{ {
debug=-1, debug = -1,
normal, normal,
superUser superUser
} }
} }

View File

@ -1,5 +1,4 @@
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using OpenCvSharp; using OpenCvSharp;
namespace Squirrowse.Core.Services namespace Squirrowse.Core.Services
@ -8,7 +7,7 @@ namespace Squirrowse.Core.Services
{ {
public static byte[] ConvertToJpgByte(this Mat mat) public static byte[] ConvertToJpgByte(this Mat mat)
{ {
Cv2.ImEncode(".jpg", mat, out var imgbuffer);//no need to dispose Cv2.ImEncode(".jpg", mat, out var imgbuffer); //no need to dispose
return imgbuffer.Any() ? imgbuffer : new byte[] { }; return imgbuffer.Any() ? imgbuffer : new byte[] { };
} }

View File

@ -1,7 +1,6 @@
using Squirrowse.Core.Models; using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Squirrowse.Core.Models;
namespace Squirrowse.Service.Hubs namespace Squirrowse.Service.Hubs
{ {
@ -11,9 +10,8 @@ namespace Squirrowse.Service.Hubs
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);
Task ExecCommandOnAll(string command,object[] act);//gni Task ExecCommandOnAll(string command, object[] act); //gni
Task AddToGroup(Groups group,string user=""); Task AddToGroup(Groups group, string user = "");
Task RemoveFromGroup(Groups group,string user=""); Task RemoveFromGroup(Groups group, string user = "");
} }
} }

View File

@ -1,11 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
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 NLog;
using Squirrowse.Core.Models; using Squirrowse.Core.Models;
namespace Squirrowse.Service.Hubs namespace Squirrowse.Service.Hubs
@ -13,7 +10,7 @@ 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;
public StreamHub(ILogger<StreamHub> logger) public StreamHub(ILogger<StreamHub> logger)
{ {
this.logger = logger; this.logger = logger;
@ -26,24 +23,24 @@ namespace Squirrowse.Service.Hubs
public async Task AddToGroup(Groups group, string user = "") public async Task AddToGroup(Groups group, string user = "")
{ {
string connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user; var connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user;
await Groups.AddToGroupAsync(connectionId, group.ToString()); await Groups.AddToGroupAsync(connectionId, group.ToString());
logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}"); logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}");
} }
/// <summary> /// <summary>
/// USE ONLY FOR DEBUG /// USE ONLY FOR DEBUG
/// </summary> /// </summary>
/// <param name="act"></param> /// <param name="act"></param>
/// <returns>DESTRUCTION</returns> /// <returns>DESTRUCTION</returns>
public async Task ExecCommandOnAll(string command,object[] act) public async Task ExecCommandOnAll(string command, object[] act)
{ {
await Clients.All.SendCoreAsync(command,act); await Clients.All.SendCoreAsync(command, act);
} }
public async Task RemoveFromGroup(Groups group, string user = "") public async Task RemoveFromGroup(Groups group, string user = "")
{ {
var connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user;
string connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user;
await Groups.RemoveFromGroupAsync(connectionId, group.ToString()); await Groups.RemoveFromGroupAsync(connectionId, group.ToString());
logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}"); logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}");
} }
@ -52,7 +49,7 @@ namespace Squirrowse.Service.Hubs
{ {
var client = Clients.Client(clientId); var client = Clients.Client(clientId);
await client.SendAsync("Start"); await client.SendAsync("Start");
} }
@ -71,6 +68,5 @@ namespace Squirrowse.Service.Hubs
await Task.Delay(100); //leave some delay for debug purpose await Task.Delay(100); //leave some delay for debug purpose
} }
} }
} }
} }

View File

@ -1,12 +1,7 @@
using System; namespace Squirrowse.Service
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Squirrowse.Service
{ {
public interface IUserProvider public interface IUserProvider
{ {
string GetUserId(); string GetUserId();
} }
} }

View File

@ -1,14 +1,12 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using NLog; using NLog;
using NLog.Extensions.Logging; using NLog.Extensions.Logging;
using NLog.Web; using NLog.Web;
namespace Squirrowse.Service namespace Squirrowse.Service
{ {
public class Program public class Program
@ -65,4 +63,4 @@ namespace Squirrowse.Service
return logger; return logger;
} }
} }
} }

View File

@ -27,4 +27,4 @@
} }
} }
} }
} }

View File

@ -22,4 +22,4 @@
<ProjectReference Include="..\Squirrowse.Core\Squirrowse.Core.csproj" /> <ProjectReference Include="..\Squirrowse.Core\Squirrowse.Core.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -24,7 +24,8 @@ namespace Squirrowse.Service
services.AddControllers(); services.AddControllers();
services.AddMediatR(Assembly.GetAssembly(typeof(Startup))); services.AddMediatR(Assembly.GetAssembly(typeof(Startup)));
services.AddSignalR() services.AddSignalR()
.AddHubOptions<StreamHub>(opt => opt.MaximumReceiveMessageSize = 102400000)//~100mb per frame instead of 32kb default .AddHubOptions<StreamHub
>(opt => opt.MaximumReceiveMessageSize = 102400000) //~100mb per frame instead of 32kb default
.AddMessagePackProtocol(); .AddMessagePackProtocol();
} }

View File

@ -1,7 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Squirrowse.Service namespace Squirrowse.Service
{ {
@ -12,4 +9,4 @@ namespace Squirrowse.Service
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
} }

View File

@ -6,4 +6,4 @@
"Microsoft": "Information" "Microsoft": "Information"
} }
} }
} }