using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; using OpenCvSharp; using Squirrowse.Core.Models; using Squirrowse.Core.Services; namespace Squirrowse.Service.Hubs { public class StreamHub : Hub, IStreamHub //fujka { private readonly ILogger logger; private readonly IStreamManager manager; public StreamHub(ILogger logger, IStreamManager manager) { this.logger = logger; this.manager = manager; } public async Task AddUser(string UserName, ConnectionType type) { await manager.AddUser(Context.ConnectionId, UserName,type); logger.LogInformation($"{nameof(AddUser)}: {UserName} of {type}"); } public async Task AddToGroup(Groups group, string user = "") { var 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 = "") { var 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) { foreach (var user in manager.getServerSideUsers()) { await Clients.Client(user.ConnectionId).SendAsync("RecData", stream); } await foreach (var frame in stream) { logger.LogInformation($"Got frame size: {frame.Length} "); await Task.Delay(100); //leave some delay for debug purpose } } public async Task> GetListOfTypeUser(ConnectionType t) { if (t == ConnectionType.Client) return manager.getClientSideUsers(); if (t == ConnectionType.Server) return manager.getServerSideUsers(); throw new Exception("not found") ; } #warning DEBUG public async Task GetListOfTypeUserString() { string t = ""; var tasdf=manager.getClientSideUsers(); foreach (var h in tasdf) { t +=h.AgentName; } return t; } #warning DEBUG public async Task Getasuser() => manager.getClientSideUsers().FirstOrDefault(); #warning Debug public async Task> GetListOfTypeUserE(ConnectionType t) { if (t == ConnectionType.Client) return manager.getClientSideUsers().ToList(); if (t == ConnectionType.Server) return manager.getServerSideUsers().ToList(); throw new Exception("not found"); } #warning debug public async IAsyncEnumerable GetListOfTypeUserAsync(ConnectionType t) { var client = manager.getClientSideUsers(); foreach (var va in client) { await Task.Delay(500); yield return va; } } public async Task> GetAllUsers() { return manager.getAllUsers(); } } }