2024-03-12 14:05:27 -04:00
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using TwitchChatTTS.OBS.Socket.Data;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.OBS.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class EventMessageHandler : IWebSocketHandler
|
|
|
|
{
|
|
|
|
private ILogger Logger { get; }
|
|
|
|
private IServiceProvider ServiceProvider { get; }
|
|
|
|
public int OperationCode { get; set; } = 5;
|
|
|
|
|
|
|
|
public EventMessageHandler(ILogger<EventMessageHandler> logger, IServiceProvider serviceProvider) {
|
|
|
|
Logger = logger;
|
|
|
|
ServiceProvider = serviceProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
|
|
|
{
|
|
|
|
if (message is not EventMessage obj || obj == null)
|
|
|
|
return;
|
|
|
|
|
2024-03-15 08:27:35 -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-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-03-12 14:05:27 -04:00
|
|
|
Logger.LogWarning("Stream " + (state != null && state.EndsWith("ing") ? "is " : "has ") + state + ".");
|
|
|
|
|
|
|
|
if (client.Live == false && state != null && !state.EndsWith("ing")) {
|
|
|
|
OnStreamEnd();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2024-03-15 08:27:35 -04:00
|
|
|
Logger.LogDebug(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnStreamEnd() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|