2024-03-12 14:05:27 -04:00
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-03-12 14:05:27 -04:00
|
|
|
using TwitchChatTTS.OBS.Socket.Data;
|
2024-07-16 00:48:55 -04:00
|
|
|
using TwitchChatTTS.OBS.Socket.Manager;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
|
|
|
namespace TwitchChatTTS.OBS.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class EventMessageHandler : IWebSocketHandler
|
|
|
|
{
|
2024-07-16 00:48:55 -04:00
|
|
|
private readonly OBSManager _manager;
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
public int OperationCode { get; } = 5;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-07-16 00:48:55 -04:00
|
|
|
public EventMessageHandler(OBSManager manager, ILogger logger)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-16 00:48:55 -04:00
|
|
|
_manager = manager;
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger = logger;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data data)
|
2024-03-12 14:05:27 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
if (data is not EventMessage message || message == null)
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
switch (message.EventType)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
case "StreamStateChanged":
|
|
|
|
if (sender is not OBSSocketClient client)
|
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
string? raw_state = message.EventData["outputState"].ToString();
|
2024-03-12 14:05:27 -04:00
|
|
|
string? state = raw_state?.Substring(21).ToLower();
|
2024-07-16 00:48:55 -04:00
|
|
|
_manager.Streaming = message.EventData["outputActive"].ToString().ToLower() == "true";
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Warning("Stream " + (state != null && state.EndsWith("ing") ? "is " : "has ") + state + ".");
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-07-16 00:48:55 -04:00
|
|
|
if (_manager.Streaming == false && state != null && !state.EndsWith("ing"))
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-07-16 00:48:55 -04:00
|
|
|
// Stream ended
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2024-07-16 00:48:55 -04:00
|
|
|
_logger.Debug(message.EventType + " EVENT: " + string.Join(" | ", message.EventData?.Select(x => x.Key + "=" + x.Value?.ToString()) ?? Array.Empty<string>()));
|
2024-03-12 14:05:27 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|