hermes-client/Veadotube/Handlers/FetchStatesHandler.cs

34 lines
1.1 KiB
C#

using System.Text.Json;
using Serilog;
namespace TwitchChatTTS.Veadotube.Handlers
{
public class FetchStatesHandler : IVeadotubeMessageHandler
{
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public string Name => "list";
public FetchStatesHandler(JsonSerializerOptions options, ILogger logger)
{
_options = options;
_logger = logger;
}
public Task Handle(VeadoSocketClient client, VeadoPayloadMessage message)
{
_logger.Information("Triggered Veadotube handler.");
var payload = JsonSerializer.Deserialize<VeadoNodeStateListMessage>(message.Payload.ToString()!, _options);
if (payload == null)
{
_logger.Warning("Invalid message received from Veadotube for listing states.");
return Task.CompletedTask;
}
var states = payload.States.ToDictionary(s => s.Name, s => s.Id);
client.UpdateState(states);
return Task.CompletedTask;
}
}
}