Skip to content

Commit

Permalink
have mqtt subscriber as hosted service
Browse files Browse the repository at this point in the history
  • Loading branch information
juuwel committed May 27, 2024
1 parent 253bb4a commit d05a3e1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Core.Services.External;
namespace Core.Services.External.BackgroundRemoval;

public interface IImageBackgroundRemoverService
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Core.Services.External.BackgroundRemoval;

namespace Core.Services.External;

public class MockImageBackgroundRemoverService : IImageBackgroundRemoverService
Expand Down
25 changes: 18 additions & 7 deletions Core/Services/MqttSubscriberService.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using System.Text;
using System.Text.Json;
using Core.Options;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using MQTTnet;
using MQTTnet.Client;
using Shared.Dtos;

namespace Core.Services;

public class MqttSubscriberService
public class MqttSubscriberService : IHostedService
{
private readonly IOptions<MqttOptions> _options;
private readonly ConditionsLogsService _conditionsLogService;
private IMqttClient? _mqttClient;

public MqttSubscriberService(IOptions<MqttOptions> options, ConditionsLogsService conditionsLogService)
{
Expand All @@ -22,17 +24,17 @@ public MqttSubscriberService(IOptions<MqttOptions> options, ConditionsLogsServic
throw new Exception("MQTT username not set in appsettings.json");
}

public async Task SubscribeAsync()
public async Task StartAsync(CancellationToken cancellationToken)
{
var mqttFactory = new MqttFactory();

using var mqttClient = mqttFactory.CreateMqttClient();
_mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = new MqttClientOptionsBuilder()
.WithTcpServer(_options.Value.Server, _options.Value.Port)
.WithCredentials(_options.Value.Username)
.Build();

mqttClient.ApplicationMessageReceivedAsync += async e =>
_mqttClient.ApplicationMessageReceivedAsync += async e =>
{
var payload = Encoding.UTF8.GetString(e.ApplicationMessage.PayloadSegment);
var conditions = JsonSerializer.Deserialize<CreateConditionsLogDto>(payload, options:
Expand All @@ -43,15 +45,24 @@ public async Task SubscribeAsync()
await _conditionsLogService.CreateConditionsLogAsync(conditions);
};

await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
await _mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
.WithTopicFilter(
f => { f.WithTopic(_options.Value.SubscribeTopic); })
.Build();

await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
await _mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);

Console.ReadLine();
Console.WriteLine($"Subscribed to {_options.Value.SubscribeTopic}");
}

public async Task StopAsync(CancellationToken cancellationToken)
{
if (_mqttClient != null)
{
await _mqttClient.DisconnectAsync(cancellationToken: cancellationToken);
_mqttClient.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using api.Extensions;
using Core.Services.External;
using Core.Services.External.BackgroundRemoval;
using Fleck;
using lib;
using Shared.Exceptions;
Expand Down
2 changes: 1 addition & 1 deletion api/Extensions/AddServicesAndRepositoriesExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void AddServicesAndRepositories(this IServiceCollection services)
services.AddSingleton<CollectionsService>();
services.AddSingleton<ConditionsLogsService>();
services.AddSingleton<RequirementService>();
services.AddSingleton<MqttSubscriberService>();
services.AddHostedService<MqttSubscriberService>();
services.AddSingleton<MqttPublisherService>();

// External services
Expand Down
6 changes: 0 additions & 6 deletions api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,6 @@ public static async Task<WebApplication> StartApi(string[] args)
};
});

/*
// Connect and subscribe to MQTT
var mqttSubscriberService = app.Services.GetRequiredService<MqttSubscriberService>();
_ = mqttSubscriberService.SubscribeAsync();
*/

return app;
}
}

0 comments on commit d05a3e1

Please sign in to comment.