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.Hosting;
using Squirrowse.Client.Service;
@ -15,12 +11,14 @@ namespace Squirrowse.Client
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
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.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
@ -8,15 +6,14 @@ namespace Squirrowse.Client.Service
{
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);
cam.Fps = fps;
cam.FrameHeight = height;
cam.FrameWidth = widght;
cam.IsEnabledDispose = disposable;
return cam;
}
@ -25,4 +22,4 @@ namespace Squirrowse.Client.Service
throw new NotImplementedException();
}
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,4 @@
using System.Linq;
using System.Runtime.CompilerServices;
using OpenCvSharp;
namespace Squirrowse.Core.Services
@ -8,7 +7,7 @@ namespace Squirrowse.Core.Services
{
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[] { };
}

View File

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

View File

@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using NLog;
using Squirrowse.Core.Models;
namespace Squirrowse.Service.Hubs
@ -13,7 +10,7 @@ namespace Squirrowse.Service.Hubs
public class StreamHub : Hub, IStreamHub //fujka
{
private readonly ILogger<StreamHub> logger;
public StreamHub(ILogger<StreamHub> logger)
{
this.logger = logger;
@ -26,24 +23,24 @@ namespace Squirrowse.Service.Hubs
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());
logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}");
}
/// <summary>
/// USE ONLY FOR DEBUG
/// USE ONLY FOR DEBUG
/// </summary>
/// <param name="act"></param>
/// <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 = "")
{
string connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user;
var connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user;
await Groups.RemoveFromGroupAsync(connectionId, group.ToString());
logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}");
}
@ -52,7 +49,7 @@ namespace Squirrowse.Service.Hubs
{
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
}
}
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -24,7 +24,8 @@ namespace Squirrowse.Service
services.AddControllers();
services.AddMediatR(Assembly.GetAssembly(typeof(Startup)));
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();
}

View File

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

View File

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