Proof Of Concept initial

Adds initial files for a proof-of-concept project, including Dockerfile, .gitignore, project files, and MQTT-related services.

This commit sets up the basic structure and configuration for exploring and validating the core concepts of the project.
This commit is contained in:
2025-08-06 15:40:21 +02:00
parent bbc704fe6b
commit 043f504cdd
11 changed files with 1019 additions and 41 deletions

View File

@@ -1,20 +1,61 @@
using Microsoft.Extensions.Options;
using MQTTnet;
namespace ProofOfConcept.Services;
public class MQTTClient
public class MQTTClient : IHostedService
{
private ILogger<MQTTClient> logger;
private MQTTClientConfiguration configuration;
private MQTTServerConfiguration serverConfiguration;
public MQTTClient(ILogger<MQTTClient> logger, IOptionsMonitor<MQTTClientConfiguration> options)
private readonly IMqttClient client;
public MQTTClient(ILogger<MQTTClient> logger, IOptions<MQTTClientConfiguration> options, IOptions<MQTTServerConfiguration> serverOptions, IMessageProcessor messageProcessor)
{
this.logger = logger;
this.configuration = options.CurrentValue;
options.OnChange(newValue =>
this.configuration = options.Value;
this.serverConfiguration = serverOptions.Value;
client = new MqttClientFactory().CreateMqttClient();
this.client.ApplicationMessageReceivedAsync += (e) =>
{
this.configuration = newValue;
logger.LogInformation("Configuration of {ClassName} changed", nameof(MQTTClient));
});
logger.LogInformation("Message received: {Message}", e.ApplicationMessage.Payload);
messageProcessor.ProcessMessage(e.ApplicationMessage.Payload.ToString());
return Task.CompletedTask;
};
}
public async Task StartAsync(CancellationToken cancellationToken)
{
this.logger.LogTrace("Stating...");
MqttClientOptions options = new MqttClientOptionsBuilder()
.WithTcpServer("localhost", this.serverConfiguration.Port)
.WithClientId(this.serverConfiguration.LocalClient.ClientID)
.WithCredentials(this.serverConfiguration.LocalClient.Username, this.serverConfiguration.LocalClient.Password)
.Build();
await this.client.ConnectAsync(options, cancellationToken);
this.logger.LogTrace("Connected");
await this.client.SubscribeAsync("telemetry", cancellationToken: cancellationToken);
this.logger.LogTrace("Subscribed");
this.logger.LogInformation("Started");
}
public async Task StopAsync(CancellationToken cancellationToken)
{
logger.LogTrace("Stopping...");
await this.client.UnsubscribeAsync("telemetry", cancellationToken: cancellationToken);
this.logger.LogTrace("Unsubscribed");
await this.client.DisconnectAsync(cancellationToken: cancellationToken);
this.logger.LogTrace("Disconnected");
logger.LogInformation("Stopped");
}
}