From f13133829a3ec6fdb37013455641668295e3b05d Mon Sep 17 00:00:00 2001 From: kunkliricsi Date: Wed, 11 Nov 2020 20:35:26 +0100 Subject: [PATCH] Extended DeviceHub with update notifiers --- .../src/components/heatmap/Heatmap.jsx | 34 ++++++++++++++++--- Birdmap.API/Controllers/DevicesController.cs | 11 +++++- Birdmap.API/Services/Hubs/DevicesHub.cs | 16 +++++++-- .../Services/{ => Hubs}/IDevicesHubClient.cs | 2 ++ 4 files changed, 54 insertions(+), 9 deletions(-) rename Birdmap.API/Services/{ => Hubs}/IDevicesHubClient.cs (70%) diff --git a/Birdmap.API/ClientApp/src/components/heatmap/Heatmap.jsx b/Birdmap.API/ClientApp/src/components/heatmap/Heatmap.jsx index d13b43e..bbcd39d 100644 --- a/Birdmap.API/ClientApp/src/components/heatmap/Heatmap.jsx +++ b/Birdmap.API/ClientApp/src/components/heatmap/Heatmap.jsx @@ -5,7 +5,9 @@ import DevicesService, { DeviceService } from '../devices/DeviceService' import { HubConnectionBuilder } from '@microsoft/signalr'; const hub_url = '/hubs/devices'; -const method_name = 'NotifyDeviceAsync'; +const probability_method_name = 'NotifyDeviceAsync'; +const update_method_name = 'NotifyDeviceUpdatedAsync'; +const update_all_method_name = 'NotifyAllUpdatedAsync'; export default class MapContainer extends Component { constructor(props) { @@ -22,13 +24,20 @@ export default class MapContainer extends Component { } } - componentDidMount() { - const service = new DeviceService(); + handleAllDevicesUpdated(service = null) { + if (service === null) { + service = new DevicesService() + } service.getall().then(result => { this.setState({ devices: result }) }).catch(ex => { console.log(ex) }); + } + + componentDidMount() { + const service = new DeviceService(); + this.handleAllDevicesUpdated(service); const newConnection = new HubConnectionBuilder() .withUrl(hub_url) @@ -41,7 +50,7 @@ export default class MapContainer extends Component { .then(result => { console.log('Hub Connected!'); - newConnection.on(method_name, (id, date, prob) => { + newConnection.on(probability_method_name, (id, date, prob) => { this.state.points.push({ id, date, prob }); //console.log(method_name + " recieved: [id: " + id + ", date: " + date + ", prob: " + prob + "]"); if (prob > 0.5) { @@ -57,13 +66,28 @@ export default class MapContainer extends Component { } } }); + + newConnection.on(update_all_method_name, () => { + this.handleAllDevicesUpdated(service); + }); + + newConnection.on(update_method_name, (id) => { + service.getdevice(id).then(result => { + var index = this.state.devices.findIndex((d => d.id == id)) + this.state.devices[index] = result; + }).catch(ex => { + console.log("Device update error", ex); + }); + }); }) .catch(e => console.log('Hub Connection failed: ', e)); } componentWillUnmount() { if (this.state.connection) { - this.state.connection.off(method_name); + this.state.connection.off(probability_method_name); + this.state.connection.off(update_all_method_name); + this.state.connection.off(update_method_name); } } diff --git a/Birdmap.API/Controllers/DevicesController.cs b/Birdmap.API/Controllers/DevicesController.cs index ab06a40..54da564 100644 --- a/Birdmap.API/Controllers/DevicesController.cs +++ b/Birdmap.API/Controllers/DevicesController.cs @@ -7,6 +7,9 @@ using System.Collections.Generic; using Microsoft.AspNetCore.Mvc.ModelBinding; using System.Linq; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.SignalR; +using Birdmap.API.Services.Hubs; +using Birdmap.API.Services; namespace Birdmap.API.Controllers { @@ -16,11 +19,13 @@ namespace Birdmap.API.Controllers public class DevicesController : ControllerBase { private readonly IDeviceService _service; + private readonly IHubContext _hubContext; private readonly ILogger _logger; - public DevicesController(IDeviceService service, ILogger logger) + public DevicesController(IDeviceService service, IHubContext hubContext, ILogger logger) { _service = service; + _hubContext = hubContext; _logger = logger; } @@ -43,6 +48,7 @@ namespace Birdmap.API.Controllers _logger.LogInformation("Turning off all devices and sensors..."); await _service.OfflineallAsync(); + await _hubContext.Clients.All.NotifyAllUpdatedAsync(); return Ok(); } @@ -56,6 +62,7 @@ namespace Birdmap.API.Controllers _logger.LogInformation("Turning on all devices and sensors..."); await _service.OnlineallAsync(); + await _hubContext.Clients.All.NotifyAllUpdatedAsync(); return Ok(); } @@ -81,6 +88,7 @@ namespace Birdmap.API.Controllers _logger.LogInformation($"Turning off device [{deviceID}]..."); await _service.OfflinedeviceAsync(deviceID); + await _hubContext.Clients.All.NotifyDeviceUpdatedAsync(deviceID); return Ok(); } @@ -95,6 +103,7 @@ namespace Birdmap.API.Controllers _logger.LogInformation($"Turning on device [{deviceID}]..."); await _service.OnlinedeviceAsync(deviceID); + await _hubContext.Clients.All.NotifyDeviceUpdatedAsync(deviceID); return Ok(); } diff --git a/Birdmap.API/Services/Hubs/DevicesHub.cs b/Birdmap.API/Services/Hubs/DevicesHub.cs index 4e8d5ae..887b548 100644 --- a/Birdmap.API/Services/Hubs/DevicesHub.cs +++ b/Birdmap.API/Services/Hubs/DevicesHub.cs @@ -16,21 +16,31 @@ namespace Birdmap.API.Services.Hubs public override Task OnConnectedAsync() { - _logger.LogInformation("Client connected."); + _logger.LogInformation("Hub Client connected."); return base.OnConnectedAsync(); } public override Task OnDisconnectedAsync(Exception exception) { - _logger.LogInformation("Client disconnected."); + _logger.LogInformation("Hub Client disconnected."); return base.OnDisconnectedAsync(exception); } - public Task SendNotification(Guid deviceId, DateTime date, double probability) + public Task SendProbabilityAsync(Guid deviceId, DateTime date, double probability) { return Clients.All.NotifyDeviceAsync(deviceId, date, probability); } + + public Task SendDeviceUpdateAsync(Guid deviceId) + { + return Clients.All.NotifyDeviceUpdatedAsync(deviceId); + } + + public Task SendAllUpdatedAsync() + { + return Clients.All.NotifyAllUpdatedAsync(); + } } } diff --git a/Birdmap.API/Services/IDevicesHubClient.cs b/Birdmap.API/Services/Hubs/IDevicesHubClient.cs similarity index 70% rename from Birdmap.API/Services/IDevicesHubClient.cs rename to Birdmap.API/Services/Hubs/IDevicesHubClient.cs index df59217..af2650c 100644 --- a/Birdmap.API/Services/IDevicesHubClient.cs +++ b/Birdmap.API/Services/Hubs/IDevicesHubClient.cs @@ -6,5 +6,7 @@ namespace Birdmap.API.Services public interface IDevicesHubClient { Task NotifyDeviceAsync(Guid deviceId, DateTime date, double probability); + Task NotifyDeviceUpdatedAsync(Guid deviceId); + Task NotifyAllUpdatedAsync(); } }