2024-07-12 13:36:09 -04:00
|
|
|
using System.Text.Json;
|
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":
|
2024-07-16 00:48:55 -04:00
|
|
|
_logger.Debug($"Fetched stream's live status [live: {_manager.Streaming}][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);
|
|
|
|
|
2024-07-12 13:36:09 -04:00
|
|
|
requestData.ResponseValues = message.ResponseData;
|
2024-07-06 23:42:33 -04:00
|
|
|
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}]");
|
2024-07-12 13:36:09 -04:00
|
|
|
requestData.ResponseValues = message.ResponseData;
|
2024-07-06 23:42:33 -04:00
|
|
|
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}]");
|
2024-07-12 13:36:09 -04:00
|
|
|
requestData.ResponseValues = message.ResponseData;
|
2024-07-06 23:42:33 -04:00
|
|
|
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-12 13:36:09 -04:00
|
|
|
case "GetGroupList":
|
|
|
|
{
|
|
|
|
if (message.ResponseData == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"OBS Response is null [obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!message.ResponseData.TryGetValue("groups", out object? value) || value == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to fetch the scene item visibility [obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var groups = JsonSerializer.Deserialize<IEnumerable<string>>(value.ToString());
|
|
|
|
_logger.Debug($"Fetched OBS groups [obs request id: {message.RequestId}]");
|
|
|
|
requestData.ResponseValues = new Dictionary<string, object>()
|
|
|
|
{
|
|
|
|
{ "groups", groups }
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "GetGroupSceneItemList":
|
|
|
|
{
|
|
|
|
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 (message.ResponseData == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"OBS Response is null [scene: {sceneName}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!message.ResponseData.TryGetValue("sceneItems", out object? value) || value == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to fetch the scene item visibility [scene: {sceneName}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_logger.Debug($"Fetched OBS scene items in group [scene: {sceneName}][obs request id: {message.RequestId}]");
|
|
|
|
var sceneItems = JsonSerializer.Deserialize<IEnumerable<OBSSceneItem>>(value.ToString()!, new JsonSerializerOptions()
|
|
|
|
{
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
|
|
});
|
|
|
|
if (sceneItems == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to deserialize the data received [scene: {sceneName}][obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var sceneItem in sceneItems)
|
|
|
|
_manager.AddSourceId(sceneItem.SourceName, sceneItem.SceneItemId);
|
|
|
|
|
|
|
|
requestData.ResponseValues = new Dictionary<string, object>()
|
|
|
|
{
|
|
|
|
{ "groups", sceneItems }
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
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)
|
|
|
|
{
|
2024-07-12 13:36:09 -04:00
|
|
|
_logger.Warning($"Failed to find the amount of time to sleep for [obs request id: {message.RequestId}]");
|
2024-07-06 23:42:33 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_logger.Debug($"Received response from OBS for sleeping [sleep: {sleepMillis}][obs request id: {message.RequestId}]");
|
|
|
|
break;
|
|
|
|
}
|
2024-07-16 00:48:55 -04:00
|
|
|
case "GetStreamStatus":
|
|
|
|
{
|
|
|
|
if (message.ResponseData == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"OBS Response is null [obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!message.ResponseData.TryGetValue("outputActive", out object? outputActive) || outputActive == null)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Failed to fetch the scene item visibility [obs request id: {message.RequestId}]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_manager.Streaming = outputActive?.ToString()!.ToLower() == "true";
|
|
|
|
requestData.ResponseValues = message.ResponseData;
|
|
|
|
_logger.Information($"OBS is currently {(_manager.Streaming ? "" : "not ")}streaming.");
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|