Skip to content

Commit

Permalink
feat: add more diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Sallin Marc, I212 committed Aug 26, 2023
1 parent 8621b63 commit 6670089
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Backend2023/Hubs/AudioHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@
using Backend2023.Modules;
using Backend2023.Persistence;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Concurrent;

namespace Backend2023.Hubs;

public class AudioHub : Hub
{
private readonly ILogger<AudioHub> _logger;

private readonly SpeechServiceProvider _speechServiceProvider;

private readonly IChatBot _chatBot;

private readonly IConversations _conversations;

private readonly IEmotionDetectionClient _emotionDetectionClient;

// Dictionary to hold audio data for each client
private static readonly ConcurrentDictionary<string, MemoryStream> AudioData = new();

public AudioHub(SpeechServiceProvider speechServiceProvider, IChatBot chatBot, IConversations conversations)
public AudioHub(ILogger<AudioHub> logger, SpeechServiceProvider speechServiceProvider, IChatBot chatBot, IConversations conversations, IEmotionDetectionClient emotionDetectionClient)
{
_logger = logger;
_speechServiceProvider = speechServiceProvider;
_chatBot = chatBot;
_conversations = conversations;
_emotionDetectionClient = emotionDetectionClient;
}

public async Task Handshake(string text)
Expand Down Expand Up @@ -71,14 +78,15 @@ public async Task CloseAudioStream()

await _conversations.AddResponseMessage(connectionId, textResponse);
await _speechServiceProvider.TextToWavFile(new TextToSpeedRequest(textResponse, waveResponseFile));
audio?.DisposeAsync();
var byteContent = await File.ReadAllBytesAsync(waveResponseFile);

await Clients.Caller
.SendAsync("audioResponse", new AudioResponse(userMessage, textResponse, Convert.ToBase64String(byteContent)));
}
finally
{
audio?.Dispose();

if (File.Exists(waveUserFile))
{
File.Delete(waveUserFile);
Expand All @@ -90,4 +98,16 @@ await Clients.Caller
}
}
}

public override Task OnConnectedAsync()
{
_logger.LogInformation("Connceted clientId: {connectionId}", Context.ConnectionId);
return base.OnConnectedAsync();
}

public override Task OnDisconnectedAsync(Exception? exception)
{
_logger.LogError(exception, "Disconnected clientId: {connectionId}", Context.ConnectionId);
return base.OnDisconnectedAsync(exception);
}
}

0 comments on commit 6670089

Please sign in to comment.