Enables location and gear testing via API
All checks were successful
Build, Push and Run Container / build (push) Successful in 27s

Adds query parameters for latitude and longitude to the `/TestStartParking` endpoint, allowing to simulate different locations.

Adds logic to set the gear to "P" if the received value is null to cover edge cases.

Ensures asynchronous message processing by awaiting the result of ProcessMessage to prevent potential race conditions.
This commit is contained in:
2025-08-25 08:49:57 +02:00
parent c44f0d327d
commit 22df381755
3 changed files with 6 additions and 4 deletions

View File

@@ -419,11 +419,11 @@ app.MapGet("/Status", async ([FromServices] ILogger<Configurator> logger, [FromS
return Results.Ok(sb.ToString()); return Results.Ok(sb.ToString());
}); });
app.MapGet("/TestStartParking", async ([FromServices] ILogger<Configurator> logger, [FromServices] MessageProcessor messageProcessor) => app.MapGet("/TestStartParking", async ([FromQuery] double latitude, [FromQuery] double longitude, [FromServices] ILogger<Configurator> logger, [FromServices] MessageProcessor messageProcessor) =>
{ {
logger.LogTrace("Test Start Parking..."); logger.LogTrace("Test Start Parking...");
await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "location", "{\"latitude\":47.410323,\"longitude\":19.067579}"); await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "location", $"{{\"latitude\":{latitude},\"longitude\":{longitude}}}");
await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "gear", "P"); await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "gear", "P");
await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "locked", "true"); await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "locked", "true");
await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "driverseatoccupied", "false"); await messageProcessor.ProcessMessage("5YJ3E7EB7KF291652", "driverseatoccupied", "false");

View File

@@ -35,7 +35,7 @@ public class MQTTClient : IHostedService
string? message = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); string? message = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
logger.LogInformation("Message received: {Message}", message); logger.LogInformation("Message received: {Message}", message);
messageProcessor.ProcessMessage(vin, field.ToLowerInvariant(), message.StripQuotes()); messageProcessor.ProcessMessage(vin, field.ToLowerInvariant(), message.StripQuotes()).GetAwaiter().GetResult();
} }
else else
logger.LogWarning("Topic not passed to message processor: {Topic}", topic); logger.LogWarning("Topic not passed to message processor: {Topic}", topic);

View File

@@ -41,7 +41,9 @@ public class MessageProcessor
string[] validGears = [ "P", "R", "N", "D", "SNA" ]; string[] validGears = [ "P", "R", "N", "D", "SNA" ];
if (field == "gear" && validGears.Contains(value)) if (field == "gear" && validGears.Contains(value))
this.teslaState.Gear = value; this.teslaState.Gear = value;
else if (field == "gear" && value == "null")
this.teslaState.Gear = "P";
else if (field == "locked" && bool.TryParse(value, out bool locked)) else if (field == "locked" && bool.TryParse(value, out bool locked))
this.teslaState.Locked = locked; this.teslaState.Locked = locked;