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;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.OBS.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class EventMessageHandler : IWebSocketHandler
|
|
|
|
{
|
2024-06-16 20:19:31 -04:00
|
|
|
private ILogger _logger { get; }
|
|
|
|
private IServiceProvider _serviceProvider { get; }
|
2024-03-12 14:05:27 -04:00
|
|
|
public int OperationCode { get; set; } = 5;
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public EventMessageHandler(ILogger logger, IServiceProvider serviceProvider)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_serviceProvider = serviceProvider;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
|
|
|
{
|
|
|
|
if (message is not EventMessage obj || obj == null)
|
|
|
|
return;
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
switch (obj.EventType)
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
case "StreamStateChanged":
|
|
|
|
case "RecordStateChanged":
|
|
|
|
if (sender is not OBSSocketClient client)
|
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
string? raw_state = obj.EventData["outputState"].ToString();
|
2024-03-12 14:05:27 -04:00
|
|
|
string? state = raw_state?.Substring(21).ToLower();
|
2024-03-15 08:27:35 -04:00
|
|
|
client.Live = obj.EventData["outputActive"].ToString() == "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-06-16 20:19:31 -04:00
|
|
|
if (client.Live == false && state != null && !state.EndsWith("ing"))
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
OnStreamEnd();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Debug(obj.EventType + " EVENT: " + string.Join(" | ", obj.EventData?.Select(x => x.Key + "=" + x.Value?.ToString()) ?? new string[0]));
|
2024-03-12 14:05:27 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
private void OnStreamEnd()
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|