2019-11-06 19:17:06 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-10-31 12:53:57 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2019-11-05 10:30:31 +01:00
|
|
|
|
using OpenCvSharp;
|
2019-11-02 20:36:31 +01:00
|
|
|
|
using Squirrowse.Core.Models;
|
2019-11-05 10:30:31 +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-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-06 17:02:06 +01:00
|
|
|
|
await manager.AddUser(Context.ConnectionId, UserName,type);
|
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)
|
|
|
|
|
{
|
|
|
|
|
var client = Clients.Client(clientId);
|
|
|
|
|
|
2019-11-04 09:29:37 +01:00
|
|
|
|
await client.SendAsync("Start");
|
2019-11-02 20:36:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task StopStream(string clientId)
|
|
|
|
|
{
|
|
|
|
|
var client = Clients.Client(clientId);
|
|
|
|
|
|
|
|
|
|
await client.SendAsync("Stop");
|
|
|
|
|
}
|
2019-11-06 13:47:00 +01:00
|
|
|
|
|
2019-10-31 12:53:57 +01:00
|
|
|
|
public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream)
|
|
|
|
|
{
|
2019-11-06 17:02:06 +01:00
|
|
|
|
foreach (var user in manager.getServerSideUsers())
|
2019-10-31 12:53:57 +01:00
|
|
|
|
{
|
2019-11-06 19:17:06 +01:00
|
|
|
|
await Clients.Client(user.ConnectionId).SendAsync("RecData", stream);
|
2019-11-06 17:02:06 +01:00
|
|
|
|
}
|
|
|
|
|
await foreach (var frame in stream)
|
|
|
|
|
{
|
|
|
|
|
logger.LogInformation($"Got frame size: {frame.Length} ");
|
|
|
|
|
await Task.Delay(100); //leave some delay for debug purpose
|
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();
|
|
|
|
|
throw new Exception("not found") ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<User>> GetAllUsers()
|
|
|
|
|
{
|
|
|
|
|
return manager.getAllUsers();
|
|
|
|
|
}
|
2019-10-31 12:53:57 +01:00
|
|
|
|
}
|
2019-11-04 09:29:37 +01:00
|
|
|
|
}
|