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-06-24 18:11:36 -04:00
|
|
|
using TwitchChatTTS.OBS.Socket.Manager;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
|
|
|
namespace TwitchChatTTS.OBS.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class RequestResponseHandler : IWebSocketHandler
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly OBSManager _manager;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
public int OperationCode { get; } = 7;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
public RequestResponseHandler(OBSManager manager, ILogger logger)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_manager = manager;
|
|
|
|
_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 RequestResponseMessage message || message == null)
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Debug($"Received an OBS request response [obs request id: {message.RequestId}]");
|
2024-06-24 18:11:36 -04:00
|
|
|
|
|
|
|
var requestData = _manager.Take(message.RequestId);
|
|
|
|
if (requestData == null)
|
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Warning($"OBS Request Response not being processed: request not stored [obs request id: {message.RequestId}]");
|
2024-06-24 18:11:36 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = requestData.Message;
|
|
|
|
if (request == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (request.RequestType)
|
|
|
|
{
|
|
|
|
case "GetOutputStatus":
|
|
|
|
if (sender is not OBSSocketClient client)
|
|
|
|
return;
|
|
|
|
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Debug($"Fetched stream's live status [live: {client.Live}][obs request id: {message.RequestId}]");
|
2024-06-24 18:11:36 -04:00
|
|
|
break;
|
|
|
|
case "GetSceneItemId":
|
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
if (!request.RequestData.TryGetValue("sceneName", out object? sceneName) || sceneName == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find the scene name that was requested [obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!request.RequestData.TryGetValue("sourceName", out object? sourceName) || sourceName == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find the scene item name that was requested [scene: {sceneName}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (message.ResponseData == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"OBS Response is null [scene: {sceneName}][scene item: {sourceName}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!message.ResponseData.TryGetValue("sceneItemId", out object? sceneItemId) || sceneItemId == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to fetch the scene item id [scene: {sceneName}][scene item: {sourceName}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Debug($"Found the scene item by name [scene: {sceneName}][source: {sourceName}][id: {sceneItemId}][obs request id: {message.RequestId}].");
|
|
|
|
//_manager.AddSourceId(sceneName.ToString(), sourceName.ToString(), (long) sceneItemId);
|
|
|
|
|
|
|
|
requestData.ResponseValues = new Dictionary<string, object>
|
|
|
|
{
|
|
|
|
{ "sceneItemId", sceneItemId }
|
|
|
|
};
|
|
|
|
break;
|
2024-06-24 18:11:36 -04:00
|
|
|
}
|
2024-07-06 23:42:33 -04:00
|
|
|
case "GetSceneItemTransform":
|
2024-06-24 18:11:36 -04:00
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
if (!request.RequestData.TryGetValue("sceneName", out object? sceneName) || sceneName == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find the scene name that was requested [obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!request.RequestData.TryGetValue("sceneItemId", out object? sceneItemId) || sceneItemId == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find the scene item name that was requested [scene: {sceneName}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (message.ResponseData == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"OBS Response is null [scene: {sceneName}][scene item id: {sceneItemId}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!message.ResponseData.TryGetValue("sceneItemTransform", out object? transformData) || transformData == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to fetch the OBS transformation data [obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Debug($"Fetched OBS transformation data [scene: {sceneName}][scene item id: {sceneItemId}][transformation: {transformData}][obs request id: {message.RequestId}]");
|
|
|
|
requestData.ResponseValues = new Dictionary<string, object>
|
|
|
|
{
|
|
|
|
{ "sceneItemTransform", transformData }
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "GetSceneItemEnabled":
|
|
|
|
{
|
|
|
|
if (!request.RequestData.TryGetValue("sceneName", out object? sceneName) || sceneName == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find the scene name that was requested [obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!request.RequestData.TryGetValue("sceneItemId", out object? sceneItemId) || sceneItemId == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find the scene item name that was requested [scene: {sceneName}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (message.ResponseData == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"OBS Response is null [scene: {sceneName}][scene item id: {sceneItemId}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!message.ResponseData.TryGetValue("sceneItemEnabled", out object? sceneItemVisibility) || sceneItemVisibility == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to fetch the scene item visibility [scene: {sceneName}][scene item id: {sceneItemId}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Debug($"Fetched OBS scene item visibility [scene: {sceneName}][scene item id: {sceneItemId}][visibility: {sceneItemVisibility}][obs request id: {message.RequestId}]");
|
|
|
|
requestData.ResponseValues = new Dictionary<string, object>
|
|
|
|
{
|
|
|
|
{ "sceneItemEnabled", sceneItemVisibility }
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "SetSceneItemTransform":
|
2024-06-24 18:11:36 -04:00
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
if (!request.RequestData.TryGetValue("sceneName", out object? sceneName) || sceneName == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find the scene name that was requested [obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!request.RequestData.TryGetValue("sceneItemId", out object? sceneItemId) || sceneItemId == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find the scene item name that was requested [scene: {sceneName}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_logger.Debug($"Received response from OBS for updating scene item transformation [scene: {sceneName}][scene item id: {sceneItemId}][obs request id: {message.RequestId}]");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "SetSceneItemEnabled":
|
2024-06-24 18:11:36 -04:00
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
if (!request.RequestData.TryGetValue("sceneName", out object? sceneName) || sceneName == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find the scene name that was requested [obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!request.RequestData.TryGetValue("sceneItemId", out object? sceneItemId) || sceneItemId == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find the scene item name that was requested [scene: {sceneName}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_logger.Debug($"Received response from OBS for updating scene item visibility [scene: {sceneName}][scene item id: {sceneItemId}][obs request id: {message.RequestId}]");
|
|
|
|
break;
|
2024-06-24 18:11:36 -04:00
|
|
|
}
|
2024-07-06 23:42:33 -04:00
|
|
|
case "Sleep":
|
2024-06-24 18:11:36 -04:00
|
|
|
{
|
2024-07-06 23:42:33 -04:00
|
|
|
if (!request.RequestData.TryGetValue("sleepMillis", out object? sleepMillis) || sleepMillis == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to find the scene name that was requested [obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_logger.Debug($"Received response from OBS for sleeping [sleep: {sleepMillis}][obs request id: {message.RequestId}]");
|
|
|
|
break;
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
default:
|
2024-07-06 23:42:33 -04:00
|
|
|
_logger.Warning($"OBS Request Response not being processed [type: {request.RequestType}][{string.Join(Environment.NewLine, message.ResponseData?.Select(kvp => kvp.Key + " = " + kvp.Value?.ToString()) ?? [])}]");
|
2024-06-24 18:11:36 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-07-06 23:42:33 -04:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Error(ex, $"Failed to process the response from OBS for a request [type: {request.RequestType}]");
|
|
|
|
}
|
2024-06-24 18:11:36 -04:00
|
|
|
finally
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
if (requestData.Callback != null)
|
|
|
|
requestData.Callback(requestData.ResponseValues);
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|