using System.Text.Json; using CommonSocketLibrary.Abstract; using CommonSocketLibrary.Common; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using TwitchChatTTS.Seven.Socket.Data; namespace TwitchChatTTS.Seven.Socket.Handlers { public class DispatchHandler : IWebSocketHandler { private ILogger Logger { get; } private IServiceProvider ServiceProvider { get; } public int OperationCode { get; set; } = 0; public DispatchHandler(ILogger logger, IServiceProvider serviceProvider) { Logger = logger; ServiceProvider = serviceProvider; } public async Task Execute(SocketClient sender, Data message) { if (message is not DispatchMessage obj || obj == null) return; Do(obj?.Body?.Pulled, cf => cf.OldValue); Do(obj?.Body?.Pushed, cf => cf.Value); } private void Do(IEnumerable? fields, Func getter) { if (fields is null) return; //ServiceProvider.GetRequiredService() foreach (var val in fields) { if (getter(val) == null) continue; var o = JsonSerializer.Deserialize(val.OldValue.ToString(), new JsonSerializerOptions() { PropertyNameCaseInsensitive = false, PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }); } } } }