Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
MattJeanes committed Jan 4, 2025
1 parent 066e32a commit 2e533a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
18 changes: 12 additions & 6 deletions HomeAutomation.Web/Controllers/EnergyController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using HomeAutomation.Web.Data;
using Microsoft.AspNetCore.Mvc;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Protocol;
using System.Net;
using System.Text.Json.Nodes;
Expand All @@ -11,13 +10,13 @@ namespace HomeAutomation.Web.Controllers;
[Route("api/[controller]")]
public class EnergyController : BaseController
{
private readonly MqttFactory _mqttFactory;
private readonly MqttClientFactory _mqttClientFactory;
private readonly MqttClientOptions _mqttClientOptions;
private readonly ILogger<EnergyController> _logger;

public EnergyController(MqttFactory mqttFactory, MqttClientOptions mqttClientOptions, ILogger<EnergyController> logger)
public EnergyController(MqttClientFactory mqttClientFactory, MqttClientOptions mqttClientOptions, ILogger<EnergyController> logger)
{
_mqttFactory = mqttFactory;
_mqttClientFactory = mqttClientFactory;
_mqttClientOptions = mqttClientOptions;
_logger = logger;
}
Expand All @@ -34,17 +33,24 @@ public async Task<IActionResult> Invoke(string slug, [FromBody] JsonObject reque
});
}

using var mqttClient = _mqttFactory.CreateMqttClient();
using var mqttClient = _mqttClientFactory.CreateMqttClient();
await mqttClient.ConnectAsync(_mqttClientOptions);
await mqttClient.SubscribeAsync("energy/solar/result");

var correlationId = Guid.NewGuid().ToString();
request["correlationId"] = correlationId;

JsonObject response = null;
mqttClient.ApplicationMessageReceivedAsync += (message) =>
{
JsonObject tempResponse;
try
{
tempResponse = JsonNode.Parse(message.ApplicationMessage.PayloadSegment).AsObject();
tempResponse = JsonNode.Parse(message.ApplicationMessage.ConvertPayloadToString()).AsObject();
if (tempResponse.TryGetPropertyValue("correlationId", out var correlationIdNode) && correlationIdNode.GetValue<string>() == correlationId)
{
response = tempResponse;
}
}
catch (Exception ex)
{
Expand Down
3 changes: 1 addition & 2 deletions HomeAutomation.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using HomeAutomation.Web.Middleware;
using Microsoft.AspNetCore.StaticFiles;
using MQTTnet;
using MQTTnet.Client;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks();
Expand All @@ -15,7 +14,7 @@

builder.Services.AddControllers();
builder.Services.AddHttpClient();
builder.Services.AddSingleton<MqttFactory>();
builder.Services.AddSingleton<MqttClientFactory>();
builder.Services.AddSingleton(mqttClientOptions);
builder.Services.Configure<PackageOptions>(builder.Configuration.GetSection("Package"));
builder.Services.AddMemoryCache();
Expand Down

0 comments on commit 2e533a4

Please sign in to comment.