2024-06-16 20:19:31 -04:00
|
|
|
using System.Text.Json;
|
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Serilog;
|
|
|
|
using Serilog.Context;
|
|
|
|
using TwitchChatTTS.OBS.Socket.Data;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.OBS.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class RequestBatchResponseHandler : IWebSocketHandler
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
public int OperationCode { get; } = 9;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
public RequestBatchResponseHandler(ILogger logger)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data data)
|
|
|
|
{
|
|
|
|
if (data is not RequestBatchResponseMessage message || message == null)
|
|
|
|
return;
|
2024-07-19 12:56:41 -04:00
|
|
|
if (sender is not OBSSocketClient obs)
|
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
var results = message.Results.ToList();
|
|
|
|
_logger.Debug($"Received request batch response of {results.Count} messages.");
|
2024-06-24 18:11:36 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
int count = results.Count;
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
if (results[i] == null)
|
|
|
|
continue;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
try
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-19 12:56:41 -04:00
|
|
|
_logger.Debug($"Request response from OBS request batch #{i + 1}/{count}: {results[i]}");
|
|
|
|
var response = JsonSerializer.Deserialize<RequestResponseMessage>(results[i].ToString()!, new JsonSerializerOptions()
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-19 12:56:41 -04:00
|
|
|
PropertyNameCaseInsensitive = false,
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
|
|
});
|
|
|
|
if (response == null)
|
|
|
|
continue;
|
2024-06-24 18:11:36 -04:00
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
await obs.ExecuteRequest(response);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Error(ex, "Failed to process an item in a request batch message.");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
}
|
2024-07-19 12:56:41 -04:00
|
|
|
|
|
|
|
_logger.Debug($"Finished processing all request in this batch.");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|