mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-13 17:22:54 -05:00
Add SignalR method to request status
This commit is contained in:
@@ -28,7 +28,10 @@ public class MessageHandler : IHostedService
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_configuration["Hub:DeviceStatus"]))
|
||||
{
|
||||
_hubConnection = new HubConnectionBuilder().WithUrl(_configuration["Hub:DeviceStatus"]).Build();
|
||||
_hubConnection.On("RequestLatestStatus", async () => await RequestLatestStatus());
|
||||
}
|
||||
|
||||
var mqttFactory = new MqttFactory();
|
||||
|
||||
@@ -40,6 +43,12 @@ public class MessageHandler : IHostedService
|
||||
await _mqttServer.StartAsync();
|
||||
}
|
||||
|
||||
private async Task RequestLatestStatus()
|
||||
{
|
||||
foreach (var device in _deviceRepository.Values)
|
||||
await SendDeviceStatus(device);
|
||||
}
|
||||
|
||||
private async Task OnInterceptingPublishAsync(InterceptingPublishEventArgs arg)
|
||||
{
|
||||
var topic = arg.ApplicationMessage.Topic;
|
||||
@@ -91,6 +100,11 @@ public class MessageHandler : IHostedService
|
||||
|
||||
await _laundryMonitor.HandleDeviceMessage(newDevice);
|
||||
|
||||
await SendDeviceStatus(newDevice);
|
||||
}
|
||||
|
||||
private async Task SendDeviceStatus(Device device)
|
||||
{
|
||||
if (_hubConnection == null)
|
||||
return;
|
||||
|
||||
@@ -99,7 +113,7 @@ public class MessageHandler : IHostedService
|
||||
if (_hubConnection.State == HubConnectionState.Disconnected)
|
||||
await _hubConnection.StartAsync();
|
||||
|
||||
var json = JsonSerializer.Serialize(newDevice);
|
||||
var json = JsonSerializer.Serialize(device);
|
||||
|
||||
await _hubConnection.InvokeAsync("SendLatestStatus", json);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,12 @@ namespace ChrisKaczor.HomeMonitor.Hub.Service.Hubs
|
||||
[UsedImplicitly]
|
||||
public class DeviceStatusHub : Microsoft.AspNetCore.SignalR.Hub
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public async Task RequestLatestStatus()
|
||||
{
|
||||
await Clients.Others.SendAsync("RequestLatestStatus");
|
||||
}
|
||||
|
||||
[UsedImplicitly]
|
||||
public async Task SendLatestStatus(string message)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user