Fix models in environment service and add more SignalR messaging

This commit is contained in:
2024-03-04 16:32:33 -05:00
parent a8e60c2e87
commit 2ef541d72f
7 changed files with 96 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
using ChrisKaczor.HomeMonitor.Environment.Service.Data;
using ChrisKaczor.HomeMonitor.Environment.Service.Models;
using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor;
using Microsoft.AspNetCore.SignalR.Client;
using MQTTnet;
@@ -41,8 +42,12 @@ public class MessageHandler : IHostedService
public async Task StartAsync(CancellationToken cancellationToken)
{
if (_hubConnection != null)
{
await _hubConnection.StartAsync(cancellationToken);
_hubConnection.On("RequestLatest", SendStoredMessage);
}
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer(_configuration["Mqtt:Server"]).Build();
await _mqttClient.ConnectAsync(mqttClientOptions, cancellationToken);
@@ -85,9 +90,36 @@ public class MessageHandler : IHostedService
if (_hubConnection.State == HubConnectionState.Disconnected)
await _hubConnection.StartAsync();
var json = JsonSerializer.Serialize(message);
var deviceReadings = new Readings(message);
await _hubConnection.InvokeAsync("SendMessage", json);
var json = JsonSerializer.Serialize(deviceReadings);
await _hubConnection.InvokeAsync("SendLatest", json);
}
catch (Exception exception)
{
WriteLog($"Hub exception: {exception}");
}
}
private async Task SendStoredMessage()
{
try
{
if (_hubConnection == null)
return;
if (_hubConnection.State == HubConnectionState.Disconnected)
await _hubConnection.StartAsync();
var recentReadings = await _database.GetRecentReadings();
foreach (var recentReading in recentReadings)
{
var json = JsonSerializer.Serialize(recentReading);
await _hubConnection.InvokeAsync("SendLatest", json);
}
}
catch (Exception exception)
{