using Birdmap.BLL.Interfaces; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; using System.Threading.Tasks; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc.ModelBinding; using System.Linq; using Microsoft.AspNetCore.Authorization; namespace Birdmap.API.Controllers { [Authorize(Roles = "Admin")] [ApiController] [Route("api/[controller]")] public class DevicesController : ControllerBase { private readonly IDeviceService _service; private readonly ILogger _logger; public DevicesController(IDeviceService service, ILogger logger) { _service = service; _logger = logger; } /// Get all device info /// Array of devices [Authorize(Roles = "User,Admin")] [HttpGet] public async Task>> Getall() { _logger.LogInformation("Getting all devices..."); return (await _service.GetallAsync()).ToList(); } /// Shut down all devices /// Message sent [HttpPost, Route("offline")] public async Task Offlineall() { _logger.LogInformation("Turning off all devices and sensors..."); await _service.OfflineallAsync(); return Ok(); } /// Bring all devices online /// Message sent [HttpPost, Route("online")] public async Task Onlineall() { _logger.LogInformation("Turning on all devices and sensors..."); await _service.OnlineallAsync(); return Ok(); } /// Get all device info /// ID of device to query /// Information about a particular device [Authorize(Roles = "User,Admin")] [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 [HttpPost, Route("{deviceID}/offline")] public async Task Offlinedevice([BindRequired] Guid deviceID) { _logger.LogInformation($"Turning off device [{deviceID}]..."); await _service.OfflinedeviceAsync(deviceID); return Ok(); } /// Bring device online /// ID of device to bring online /// Message sent [HttpPost, Route("{deviceID}/online")] public async Task Onlinedevice([BindRequired] Guid deviceID) { _logger.LogInformation($"Turning on device [{deviceID}]..."); await _service.OnlinedeviceAsync(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 [Authorize(Roles = "User,Admin")] [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 [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); return Ok(); } /// Bring sensor online /// ID of device to query /// ID of sensor to query /// Message sent [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); return Ok(); } } } #pragma warning restore 1591 #pragma warning restore 1573 #pragma warning restore 472 #pragma warning restore 114 #pragma warning restore 108