using Birdmap.BLL.Interfaces; using Birdmap.BLL.Services.CommunicationServices.Hubs; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Birdmap.API.Controllers { [Authorize(Roles = "User, Admin")] [ApiController] [Route("api/[controller]")] public class DevicesController : ControllerBase { private readonly IDeviceService _service; private readonly IHubContext _hubContext; private readonly ILogger _logger; public DevicesController(IDeviceService service, IHubContext hubContext, ILogger logger) { _service = service; _hubContext = hubContext; _logger = logger; } /// Get all device info /// Array of devices [HttpGet] public async Task>> Getall() { _logger.LogInformation("Getting all devices..."); return (await _service.GetallAsync()).ToList(); } /// Shut down all devices /// Message sent [Authorize(Roles = "Admin")] [HttpPost, Route("offline")] public async Task Offlineall() { _logger.LogInformation("Turning off all devices and sensors..."); await _service.OfflineallAsync(); await _hubContext.Clients.All.NotifyAllUpdatedAsync(); return Ok(); } /// Bring all devices online /// Message sent [Authorize(Roles = "Admin")] [HttpPost, Route("online")] public async Task Onlineall() { _logger.LogInformation("Turning on all devices and sensors..."); await _service.OnlineallAsync(); await _hubContext.Clients.All.NotifyAllUpdatedAsync(); return Ok(); } /// Get all device info /// ID of device to query /// Information about a particular device [HttpGet, Route("{deviceID}")] public async Task> Getdevice([BindRequired] Guid deviceID) { _logger.LogInformation($"Getting device [{deviceID}]..."); return await _service.GetdeviceAsync(deviceID); } /// Shut down device /// ID of device to shut down /// Message sent [Authorize(Roles = "Admin")] [HttpPost, Route("{deviceID}/offline")] public async Task Offlinedevice([BindRequired] Guid deviceID) { _logger.LogInformation($"Turning off device [{deviceID}]..."); await _service.OfflinedeviceAsync(deviceID); await _hubContext.Clients.All.NotifyDeviceUpdatedAsync(deviceID); return Ok(); } /// Bring device online /// ID of device to bring online /// Message sent [Authorize(Roles = "Admin")] [HttpPost, Route("{deviceID}/online")] public async Task Onlinedevice([BindRequired] Guid deviceID) { _logger.LogInformation($"Turning on device [{deviceID}]..."); await _service.OnlinedeviceAsync(deviceID); await _hubContext.Clients.All.NotifyDeviceUpdatedAsync(deviceID); return Ok(); } /// Get info about a particular device's sensor /// ID of device to query /// ID of sensor to query /// Information about a sensor [HttpGet, Route("{deviceID}/{sensorID}")] public async Task> Getsensor([BindRequired] Guid deviceID, [BindRequired] Guid sensorID) { _logger.LogInformation($"Getting sensor [{sensorID}] of device [{deviceID}]..."); return await _service.GetsensorAsync(deviceID, sensorID); } /// Shut down sensor /// ID of device to query /// ID of sensor to query /// Message sent [Authorize(Roles = "Admin")] [HttpPost, Route("{deviceID}/{sensorID}/offline")] public async Task Offlinesensor([BindRequired] Guid deviceID, [BindRequired] Guid sensorID) { _logger.LogInformation($"Turning off sensor [{sensorID}] of device [{deviceID}]..."); await _service.OfflinesensorAsync(deviceID, sensorID); await _hubContext.Clients.All.NotifyDeviceUpdatedAsync(deviceID); return Ok(); } /// Bring sensor online /// ID of device to query /// ID of sensor to query /// Message sent [Authorize(Roles = "Admin")] [HttpPost, Route("{deviceID}/{sensorID}/online")] public async Task Onlinesensor([BindRequired] Guid deviceID, [BindRequired] Guid sensorID) { _logger.LogInformation($"Turning on sensor [{sensorID}] of device [{deviceID}]..."); await _service.OnlinesensorAsync(deviceID, sensorID); await _hubContext.Clients.All.NotifyDeviceUpdatedAsync(deviceID); return Ok(); } } } #pragma warning restore 1591 #pragma warning restore 1573 #pragma warning restore 472 #pragma warning restore 114 #pragma warning restore 108