Add SignalR for environment service

This commit is contained in:
2024-01-13 22:30:06 -05:00
parent aab20263ad
commit bdc5548ec2
4 changed files with 39 additions and 3 deletions

View File

@@ -2,6 +2,8 @@
using ChrisKaczor.HomeMonitor.Environment.Service.Data;
using MQTTnet;
using MQTTnet.Client;
using Microsoft.AspNetCore.SignalR.Client;
using System.Threading;
namespace ChrisKaczor.HomeMonitor.Environment.Service;
@@ -10,6 +12,7 @@ public class MessageHandler : IHostedService
private readonly IConfiguration _configuration;
private readonly Database _database;
private readonly IMqttClient _mqttClient;
private HubConnection _hubConnection;
private readonly MqttFactory _mqttFactory;
private readonly string _topic;
@@ -30,20 +33,26 @@ public class MessageHandler : IHostedService
_mqttClient = _mqttFactory.CreateMqttClient();
_mqttClient.ApplicationMessageReceivedAsync += OnApplicationMessageReceivedAsync;
_hubConnection = new HubConnectionBuilder().WithUrl(configuration["Environment:Hub:Url"] ?? string.Empty).Build();
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await _hubConnection.StartAsync(cancellationToken);
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer(_configuration["Mqtt:Server"]).Build();
await _mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
await _mqttClient.ConnectAsync(mqttClientOptions, cancellationToken);
var mqttSubscribeOptions = _mqttFactory.CreateSubscribeOptionsBuilder().WithTopicFilter($"{_topic}/#").Build();
await _mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
await _mqttClient.SubscribeAsync(mqttSubscribeOptions, cancellationToken);
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _mqttClient.DisconnectAsync(new MqttClientDisconnectOptionsBuilder().Build(), CancellationToken.None);
await _mqttClient.DisconnectAsync(new MqttClientDisconnectOptionsBuilder().Build(), cancellationToken);
await _hubConnection.StopAsync(cancellationToken);
}
private async Task OnApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg)
@@ -59,6 +68,25 @@ public class MessageHandler : IHostedService
return;
await _database.StoreMessageAsync(message);
await SendLatestReading(message);
}
private async Task SendLatestReading(Message message)
{
try
{
if (_hubConnection.State == HubConnectionState.Disconnected)
await _hubConnection.StartAsync();
var json = JsonSerializer.Serialize(message);
await _hubConnection.InvokeAsync("SendLatestReading", json);
}
catch (Exception exception)
{
WriteLog($"Hub exception: {exception}");
}
}
private static void WriteLog(string message)

View File

@@ -23,6 +23,7 @@
<PackageReference Include="Dapper" Version="2.1.28" />
<PackageReference Include="dbup-sqlserver" Version="5.0.37" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.1" />
<PackageReference Include="MQTTnet.AspNetCore" Version="4.3.3.952" />
</ItemGroup>

View File

@@ -13,8 +13,12 @@
"Host": "localhost",
"User": "sa",
"Password": "newpassword",
"Port": 1433,
"Name": "Environment",
"TrustServerCertificate": true
},
"Hub": {
"Url": "http://localhost:8080/environment"
}
}
}

View File

@@ -18,6 +18,9 @@
"Name": "Environment",
"TrustServerCertificate": true,
"Port": 1435
},
"Hub": {
"Url": "http://hub-server/environment"
}
}
}