Extended DeviceHub with update notifiers

This commit is contained in:
kunkliricsi 2020-11-11 20:35:26 +01:00
parent 4c1258dc33
commit f13133829a
4 changed files with 54 additions and 9 deletions

View File

@ -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);
}
}

View File

@ -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<DevicesHub, IDevicesHubClient> _hubContext;
private readonly ILogger<ServicesController> _logger;
public DevicesController(IDeviceService service, ILogger<ServicesController> logger)
public DevicesController(IDeviceService service, IHubContext<DevicesHub, IDevicesHubClient> hubContext, ILogger<ServicesController> 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();
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}