Files
Automatic-Parking/Source/ProofOfConcept/Services/MQTTClient.cs
Szakáts Alpár Zsolt 5daf0825a0
All checks were successful
Build, Push and Run Container / build (push) Successful in 25s
Adds fleet status endpoint
Adds an endpoint to retrieve fleet status information.

This endpoint uses a Tesla API proxy to fetch the fleet status
based on provided VINs. It handles authentication using a bearer
token and sends a POST request to the /api/1/vehicles/fleet_status
endpoint.

Also introduces new data models to properly serialize/deserialize the fleet status response.
2025-08-18 21:47:06 +02:00

65 lines
2.3 KiB
C#

using Microsoft.Extensions.Options;
using MQTTnet;
namespace ProofOfConcept.Services;
public class MQTTClient : IHostedService
{
private readonly ILogger<MQTTClient> logger;
private readonly MQTTClientConfiguration configuration;
private readonly MQTTServerConfiguration serverConfiguration;
private readonly IMqttClient client;
public MQTTClient(ILogger<MQTTClient> logger, IOptions<MQTTClientConfiguration> options, IOptions<MQTTServerConfiguration> serverOptions, IMessageProcessor messageProcessor)
{
this.logger = logger;
this.configuration = options.Value;
this.serverConfiguration = serverOptions.Value;
client = new MqttClientFactory().CreateMqttClient();
this.client.ApplicationMessageReceivedAsync += (e) =>
{
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");
}
}
public class MQTTClientConfiguration
{
}