using System.Threading; using System.Threading.Tasks; using HHWMS.Web.Models; using Microsoft.Extensions.DependencyInjection; namespace HHWMS.Web.Services { public class MqttRuntimeStateRefresher { private readonly IServiceScopeFactory _scopeFactory; private readonly MqttRuntimeState _runtimeState; public MqttRuntimeStateRefresher(IServiceScopeFactory scopeFactory, MqttRuntimeState runtimeState) { _scopeFactory = scopeFactory; _runtimeState = runtimeState; } public Task RefreshFromSubscriptionsAsync(string changedBy = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); using var scope = _scopeFactory.CreateScope(); var unitwork = scope.ServiceProvider.GetRequiredService(); var enabled = unitwork.GetCount(x => x.IsEnabled) > 0; _runtimeState.Update(enabled, changedBy); return Task.FromResult(enabled); } } }