61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
using Microsoft.Extensions.Logging;
|
|
using Squirrowse.Core.Models;
|
|
using Squirrowse.Core.Services;
|
|
|
|
namespace Squirrowse.Client.Service
|
|
{
|
|
public class ActionDispatcher : IActionDispatcher
|
|
{
|
|
private readonly ICameraService camera;
|
|
private readonly IConnectionManager connectionManager;
|
|
private readonly ILogger<ActionDispatcher> logger;
|
|
private readonly HubConnection session;
|
|
private bool streamOn = false;
|
|
public ActionDispatcher(ILogger<ActionDispatcher> logger, IConnectionManager connectionManager,
|
|
ICameraService camera)
|
|
{
|
|
this.connectionManager = connectionManager;
|
|
this.logger = logger;
|
|
this.camera = camera;
|
|
session = connectionManager.GetConnection().Result;
|
|
session.On("Start", StartStream);
|
|
//session.On("StartEnum", StartStreamEnumerable);
|
|
session.On("Stop", StopStream);
|
|
}
|
|
|
|
public Task StopStream()
|
|
{
|
|
streamOn = false;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task SayHello()
|
|
{
|
|
await session.SendAsync("AddToGroup", Groups.debug);
|
|
}
|
|
|
|
public async Task SendStreamAsync(IAsyncEnumerable<byte[]> asb)
|
|
{
|
|
if(!streamOn)return;
|
|
logger.LogInformation($"{nameof(SendStreamAsync)} Start stream");
|
|
|
|
await session.SendAsync("UploadByteStream", asb);
|
|
|
|
logger.LogInformation($"{nameof(SendStreamAsync)} End stream");
|
|
}
|
|
|
|
|
|
public async Task StartStream()
|
|
{
|
|
if (streamOn) return;
|
|
streamOn = true;
|
|
await SendStreamAsync(camera.GetFramesAsyncEnumerator());
|
|
}
|
|
|
|
|
|
}
|
|
} |