using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Threading.Tasks; using Manager.Interfaces.DTO; using Manager.Interfaces.Models; using Manager.Services; using ManagerService.Service.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using NSwag.Annotations; namespace ManagerService.Controllers { [Authorize] // TODO Add ROLES (Roles = "Admin") [ApiController, Route("api/[controller]")] [OpenApiTag("Device", Description = "Device management")] public class DeviceController : ControllerBase { private DeviceDatabaseService _deviceService; private ConfigurationDatabaseService _configurationService; private readonly ILogger _logger; public DeviceController(ILogger logger, DeviceDatabaseService deviceService, ConfigurationDatabaseService configurationService) { _logger = logger; _deviceService = deviceService; _configurationService = configurationService; } /// /// Get a list of all devices /// [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet] public ObjectResult Get() { try { List devices = _deviceService.GetAll(); return new OkObjectResult(devices.Select(d => d.ToDTO())); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get a specific device /// /// id device [AllowAnonymous] [ProducesResponseType(typeof(DeviceDetailDTO), 200)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{id}/detail")] public ObjectResult GetDetail(string id) { try { Device device = _deviceService.GetById(id); if (device == null) throw new KeyNotFoundException("This device was not found"); return new OkObjectResult(device.ToDetailDTO()); } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Create a new device /// /// New device info [AllowAnonymous] [ProducesResponseType(typeof(DeviceDetailDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 409)] [ProducesResponseType(typeof(string), 500)] [HttpPost] public ObjectResult Create([FromBody] DeviceDetailDTO newDevice) { try { if (newDevice == null) throw new ArgumentNullException("Device param is null"); var configuration = _configurationService.GetById(newDevice.ConfigurationId); if (configuration == null) throw new KeyNotFoundException("Configuration does not exist"); Device device = new Device(); if (_deviceService.IsExistIdentifier(newDevice.Identifier)) { // Update info device = _deviceService.GetByIdentifier(newDevice.Identifier); device.DateUpdate = DateTime.Now; } else { // Creation device.Identifier = newDevice.Identifier; device.DateCreation = DateTime.Now; } device.Name = newDevice.Name; device.Configuration = configuration.Label; device.ConfigurationId = newDevice.ConfigurationId; device.IpAddressETH = newDevice.IpAddressETH; device.IpAddressWLAN = newDevice.IpAddressWLAN; device.Connected = newDevice.Connected; device.ConnectionLevel = newDevice.ConnectionLevel; device.LastConnectionLevel = newDevice.LastConnectionLevel; device.BatteryLevel = newDevice.BatteryLevel; device.LastBatteryLevel = newDevice.LastBatteryLevel; Device deviceCreated = _deviceService.IsExistIdentifier(newDevice.Identifier) ? _deviceService.Update(device.Id, device) : _deviceService.Create(device); return new OkObjectResult(deviceCreated.ToDTO()); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (InvalidOperationException ex) { return new ConflictObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Update a device /// /// Device to update [ProducesResponseType(typeof(DeviceDetailDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpPut] public ObjectResult Update([FromBody] DeviceDetailDTO updatedDevice) { try { if (updatedDevice == null) throw new ArgumentNullException("Device param is null"); Device device = _deviceService.GetById(updatedDevice.Id); if (device == null) throw new KeyNotFoundException("Device does not exist"); // Todo add some verification ? device.Name = updatedDevice.Name; device.Identifier = updatedDevice.Identifier; device.IpAddressWLAN = updatedDevice.IpAddressWLAN; device.IpAddressETH = updatedDevice.IpAddressETH; device.Connected = updatedDevice.Connected; device.ConnectionLevel = updatedDevice.ConnectionLevel; device.LastConnectionLevel = updatedDevice.LastConnectionLevel; device.BatteryLevel = updatedDevice.BatteryLevel; device.LastBatteryLevel = updatedDevice.LastBatteryLevel; Device deviceModified = _deviceService.Update(updatedDevice.Id, device); return new OkObjectResult(deviceModified.ToDTO()); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Update device main info /// /// Device to update [ProducesResponseType(typeof(DeviceDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpPut("mainInfos")] public ObjectResult UpdateMainInfos([FromBody] DeviceDTO deviceIn) { try { if (deviceIn == null) throw new ArgumentNullException("Device param is null"); Device device = _deviceService.GetById(deviceIn.Id); if (device == null) throw new KeyNotFoundException("Device does not exist"); var configuration = _configurationService.GetById(deviceIn.ConfigurationId); if (configuration == null) throw new KeyNotFoundException("Configuration does not exist"); // Todo add some verification ? device.Name = deviceIn.Name; device.Connected = deviceIn.Connected; device.Configuration = configuration.Label; device.ConfigurationId = deviceIn.ConfigurationId; Device deviceModified = _deviceService.Update(device.Id, device); return new OkObjectResult(deviceModified.ToDTO()); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Delete a device /// /// Id of device to delete [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("{id}")] public ObjectResult Delete(string id) { try { if (id == null) throw new ArgumentNullException("Device param is null"); if (!_deviceService.IsExist(id)) throw new KeyNotFoundException("Device does not exist"); _deviceService.Remove(id); return new ObjectResult("The device has been deleted") { StatusCode = 202 }; } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } } }