Add MQTT runtime control and device data pipeline
This commit is contained in:
1 parent
96d8309793
commit
8a0aa8f7cb
31 files changed
+30842
-20
No files matched your search
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -18,23 +19,40 @@ namespace HHWMS.Web.Services
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly MqttMessageChannel _channel;
|
||||
private readonly MqttRuntimeState _runtimeState;
|
||||
private readonly MqttRuntimeStateRefresher _runtimeStateRefresher;
|
||||
private readonly HashSet<string> _subscribedTopics = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
private IManagedMqttClient _mqttClient;
|
||||
|
||||
public MqttService(IServiceProvider serviceProvider, IConfiguration configuration, MqttMessageChannel channel)
|
||||
public MqttService(
|
||||
IServiceProvider serviceProvider,
|
||||
IConfiguration configuration,
|
||||
MqttMessageChannel channel,
|
||||
MqttRuntimeState runtimeState,
|
||||
MqttRuntimeStateRefresher runtimeStateRefresher)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_configuration = configuration;
|
||||
_channel = channel;
|
||||
_runtimeState = runtimeState;
|
||||
_runtimeStateRefresher = runtimeStateRefresher;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await _runtimeStateRefresher.RefreshFromSubscriptionsAsync("startup", stoppingToken);
|
||||
|
||||
var factory = new MqttFactory();
|
||||
_mqttClient = factory.CreateManagedMqttClient();
|
||||
|
||||
// 1. Handle received messages
|
||||
_mqttClient.ApplicationMessageReceivedAsync += async e =>
|
||||
{
|
||||
if (!_runtimeState.Enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var payload = e.ApplicationMessage.PayloadSegment.Array != null ?
|
||||
System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.PayloadSegment) : string.Empty;
|
||||
|
||||
@@ -72,15 +90,34 @@ namespace HHWMS.Web.Services
|
||||
{
|
||||
try
|
||||
{
|
||||
await _runtimeStateRefresher.RefreshFromSubscriptionsAsync("sync", stoppingToken);
|
||||
|
||||
using (var scope = _serviceProvider.CreateScope())
|
||||
{
|
||||
var unitwork = scope.ServiceProvider.GetRequiredService<IUnitWork>();
|
||||
var subscriptions = unitwork.Find<MqttSubscription>(x => x.IsEnabled).ToList();
|
||||
var subscriptions = _runtimeState.Enabled
|
||||
? unitwork.Find<MqttSubscription>(x => x.IsEnabled).ToList()
|
||||
: new List<MqttSubscription>();
|
||||
|
||||
if (subscriptions.Any())
|
||||
var enabledTopics = subscriptions.Select(x => x.Topic).Where(x => string.IsNullOrWhiteSpace(x) == false).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
|
||||
var topicsToUnsubscribe = _subscribedTopics.Except(enabledTopics, StringComparer.OrdinalIgnoreCase).ToList();
|
||||
if (topicsToUnsubscribe.Any())
|
||||
{
|
||||
await _mqttClient.UnsubscribeAsync(topicsToUnsubscribe);
|
||||
foreach (var topic in topicsToUnsubscribe)
|
||||
{
|
||||
_subscribedTopics.Remove(topic);
|
||||
}
|
||||
}
|
||||
|
||||
var subscriptionsToAdd = subscriptions
|
||||
.Where(x => string.IsNullOrWhiteSpace(x.Topic) == false && _subscribedTopics.Contains(x.Topic) == false)
|
||||
.ToList();
|
||||
|
||||
if (subscriptionsToAdd.Any())
|
||||
{
|
||||
var subOptionsBuilder = factory.CreateSubscribeOptionsBuilder();
|
||||
foreach (var sub in subscriptions)
|
||||
foreach (var sub in subscriptionsToAdd)
|
||||
{
|
||||
var qosLevel = sub.Qos switch
|
||||
{
|
||||
@@ -91,10 +128,14 @@ namespace HHWMS.Web.Services
|
||||
subOptionsBuilder.WithTopicFilter(f => f.WithTopic(sub.Topic).WithQualityOfServiceLevel(qosLevel));
|
||||
}
|
||||
await _mqttClient.SubscribeAsync(subOptionsBuilder.Build().TopicFilters);
|
||||
foreach (var sub in subscriptionsToAdd)
|
||||
{
|
||||
_subscribedTopics.Add(sub.Topic);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (Exception)
|
||||
{
|
||||
// Basic logging or handling
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user