2019-11-06 19:17:06 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-11-12 16:47:20 +01:00
|
|
|
|
using System.Globalization;
|
2019-11-07 14:26:46 +01:00
|
|
|
|
using System.Linq;
|
2019-10-31 12:53:57 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2019-11-12 16:47:20 +01:00
|
|
|
|
using OpenCvSharp;
|
2019-11-02 20:36:31 +01:00
|
|
|
|
using Squirrowse.Core.Models;
|
2019-11-12 16:47:20 +01:00
|
|
|
|
using Squirrowse.Core.Services;
|
2019-10-31 12:53:57 +01:00
|
|
|
|
|
|
|
|
|
namespace Squirrowse.Service.Hubs
|
|
|
|
|
{
|
2019-11-02 20:36:31 +01:00
|
|
|
|
public class StreamHub : Hub, IStreamHub //fujka
|
2019-10-31 12:53:57 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<StreamHub> logger;
|
2019-11-05 17:00:36 +01:00
|
|
|
|
private readonly IStreamManager manager;
|
2019-11-08 16:36:20 +01:00
|
|
|
|
|
2019-11-06 13:47:00 +01:00
|
|
|
|
|
2019-11-05 17:00:36 +01:00
|
|
|
|
public StreamHub(ILogger<StreamHub> logger, IStreamManager manager)
|
2019-10-31 12:53:57 +01:00
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
2019-11-05 17:00:36 +01:00
|
|
|
|
this.manager = manager;
|
2019-10-31 12:53:57 +01:00
|
|
|
|
}
|
2019-11-02 20:36:31 +01:00
|
|
|
|
|
2019-11-06 17:02:06 +01:00
|
|
|
|
public async Task AddUser(string UserName, ConnectionType type)
|
2019-11-02 20:36:31 +01:00
|
|
|
|
{
|
2019-11-07 11:52:27 +01:00
|
|
|
|
await manager.AddUser(Context.ConnectionId, UserName, type);
|
2019-11-07 14:26:46 +01:00
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case ConnectionType.Client:
|
|
|
|
|
await AddToGroup(Core.Models.Groups.normal);
|
|
|
|
|
break;
|
|
|
|
|
case ConnectionType.Server:
|
|
|
|
|
await AddToGroup(Core.Models.Groups.superUser);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 19:17:06 +01:00
|
|
|
|
logger.LogInformation($"{nameof(AddUser)}: {UserName} of {type}");
|
2019-11-02 20:36:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 19:17:06 +01:00
|
|
|
|
|
2019-11-02 20:36:31 +01:00
|
|
|
|
public async Task AddToGroup(Groups group, string user = "")
|
|
|
|
|
{
|
2019-11-04 09:29:37 +01:00
|
|
|
|
var connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user;
|
2019-11-02 20:36:31 +01:00
|
|
|
|
await Groups.AddToGroupAsync(connectionId, group.ToString());
|
|
|
|
|
logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}");
|
|
|
|
|
}
|
2019-11-04 09:29:37 +01:00
|
|
|
|
|
2019-11-02 20:36:31 +01:00
|
|
|
|
/// <summary>
|
2019-11-04 09:29:37 +01:00
|
|
|
|
/// USE ONLY FOR DEBUG
|
2019-11-02 20:36:31 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="act"></param>
|
|
|
|
|
/// <returns>DESTRUCTION</returns>
|
2019-11-04 09:29:37 +01:00
|
|
|
|
public async Task ExecCommandOnAll(string command, object[] act)
|
2019-11-02 20:36:31 +01:00
|
|
|
|
{
|
2019-11-04 09:29:37 +01:00
|
|
|
|
await Clients.All.SendCoreAsync(command, act);
|
2019-11-02 20:36:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task RemoveFromGroup(Groups group, string user = "")
|
|
|
|
|
{
|
2019-11-04 09:29:37 +01:00
|
|
|
|
var connectionId = string.IsNullOrWhiteSpace(user) ? Context.ConnectionId : user;
|
2019-11-02 20:36:31 +01:00
|
|
|
|
await Groups.RemoveFromGroupAsync(connectionId, group.ToString());
|
|
|
|
|
logger.LogInformation($"{nameof(AddToGroup)}: {connectionId} joined to {group}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Startstream(string clientId)
|
|
|
|
|
{
|
2019-11-12 16:47:20 +01:00
|
|
|
|
streamOn = true;
|
2019-11-07 14:26:46 +01:00
|
|
|
|
//var client = Clients.Client(clientId);
|
|
|
|
|
await Clients.Groups(Core.Models.Groups.normal.ToString()).SendAsync("Start");
|
|
|
|
|
// await client.SendAsync("Start");
|
2019-11-02 20:36:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task StopStream(string clientId)
|
|
|
|
|
{
|
2019-11-08 16:36:20 +01:00
|
|
|
|
streamOn = false;
|
|
|
|
|
//var client = Clients.Client(clientId);
|
|
|
|
|
// await Clients.Groups(Core.Models.Groups.normal.ToString()).SendAsync("Stop");
|
|
|
|
|
//await client.SendAsync("Stop");
|
2019-11-02 20:36:31 +01:00
|
|
|
|
}
|
2019-11-06 13:47:00 +01:00
|
|
|
|
|
2019-11-12 16:47:20 +01:00
|
|
|
|
public static bool streamOn { get; set; }
|
|
|
|
|
|
2019-10-31 12:53:57 +01:00
|
|
|
|
public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream)
|
|
|
|
|
{
|
2019-11-12 16:47:20 +01:00
|
|
|
|
manager.SaveData(stream);
|
2019-11-07 11:52:27 +01:00
|
|
|
|
await foreach (var frame in stream)
|
2019-11-06 17:02:06 +01:00
|
|
|
|
{
|
2019-11-12 16:47:20 +01:00
|
|
|
|
var frameStamp = frame.ConvertByteToMat();
|
|
|
|
|
string timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff",
|
|
|
|
|
CultureInfo.InvariantCulture);
|
|
|
|
|
Cv2.PutText(frameStamp, timestamp, new Point(1,480/2), HersheyFonts.HersheySimplex,0.5f,Scalar.Red);
|
|
|
|
|
logger.LogInformation($"{this.GetHashCode()}: send frame");
|
|
|
|
|
//logger.LogInformation($"Got frame size: {frame.Length} ");
|
|
|
|
|
//if (!streamOn) continue;
|
|
|
|
|
// logger.LogInformation($"Send frame of size: {frame.Length} to {Core.Models.Groups.superUser.ToString()}");
|
|
|
|
|
await Clients.Groups(Core.Models.Groups.superUser.ToString()).SendAsync("RecData", frameStamp.ConvertToJpgByte());
|
|
|
|
|
logger.LogInformation($"{this.GetHashCode()}: send data");
|
2019-11-08 16:36:20 +01:00
|
|
|
|
//await Task.Delay(100); //leave some delay for debug purpose
|
2019-11-12 16:47:20 +01:00
|
|
|
|
await Task.Delay(1000/30);
|
2019-10-31 12:53:57 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-06 19:17:06 +01:00
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<User>> GetListOfTypeUser(ConnectionType t)
|
|
|
|
|
{
|
|
|
|
|
if (t == ConnectionType.Client) return manager.getClientSideUsers();
|
|
|
|
|
if (t == ConnectionType.Server) return manager.getServerSideUsers();
|
2019-11-07 16:47:51 +01:00
|
|
|
|
return manager.getAllUsers();
|
2019-11-06 22:34:02 +01:00
|
|
|
|
}
|
2019-11-07 11:52:27 +01:00
|
|
|
|
|
2019-11-06 19:17:06 +01:00
|
|
|
|
public async Task<IEnumerable<User>> GetAllUsers()
|
|
|
|
|
{
|
|
|
|
|
return manager.getAllUsers();
|
|
|
|
|
}
|
2019-11-07 11:52:27 +01:00
|
|
|
|
|
|
|
|
|
public async IAsyncEnumerable<User> GetListOfTypeUserAsync(ConnectionType t)
|
|
|
|
|
{
|
|
|
|
|
var client = await GetListOfTypeUser(t);
|
|
|
|
|
foreach (var va in client) yield return va;
|
|
|
|
|
}
|
2019-10-31 12:53:57 +01:00
|
|
|
|
}
|
2019-11-04 09:29:37 +01:00
|
|
|
|
}
|