Files
WMS/HHWMS.Web/Services/MqttRuntimeStateRefresher.cs
T

31 lines
1.1 KiB
C#

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<bool> RefreshFromSubscriptionsAsync(string changedBy = null, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
using var scope = _scopeFactory.CreateScope();
var unitwork = scope.ServiceProvider.GetRequiredService<IUnitWork>();
var enabled = unitwork.GetCount<MqttSubscription>(x => x.IsEnabled) > 0;
_runtimeState.Update(enabled, changedBy);
return Task.FromResult(enabled);
}
}
}