using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Security.Authentication; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using MongoDB.Bson; using Mqtt.Client.AspNetCore.Services; using MyCore.Interfaces.DTO; using MyCore.Interfaces.Models; using MyCore.Service.Services; using MyCore.Services; using MyCore.Services.Devices; using MyCore.Services.MyControlPanel; namespace MyCore.Service.Controllers { [Authorize] // TODO Add ROLES (Roles = "Admin") [Route("api/alarm")] [ApiController] public class AlarmController : ControllerBase { private UserDatabaseService _UserDatabaseService; private RoomDatabaseService _RoomDatabaseService; private DeviceDatabaseService _DeviceDatabaseService; private AlarmDatabaseService _AlarmDatabaseService; private HomeDatabaseService _HomeDatabaseService; private readonly IMqttClientService _mqttClientService; //private readonly IMqttOnlineClientService _mqttOnlineClientService; public AlarmController(UserDatabaseService userDatabaseService, RoomDatabaseService roomDatabaseService, DeviceDatabaseService deviceDatabaseService, AlarmDatabaseService alarmDatabaseService, HomeDatabaseService homeDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider) { this._UserDatabaseService = userDatabaseService; this._RoomDatabaseService = roomDatabaseService; this._DeviceDatabaseService = deviceDatabaseService; this._AlarmDatabaseService = alarmDatabaseService; this._HomeDatabaseService = homeDatabaseService; this._mqttClientService = provider.MqttClientService; //this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService; } /// /// Get all alarm modes for the specified home /// /// Home Id [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{homeId}")] public ObjectResult GetAll(string homeId) { try { List alarmModes = _AlarmDatabaseService.GetAll(homeId); List alarmModesDTO = alarmModes.Select(a => a.ToDTO()).ToList(); return new OkObjectResult(alarmModesDTO); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get detail info of a specified alarm mode /// /// alarm id [ProducesResponseType(typeof(AlarmModeDetailDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("detail/{alarmModeId}")] public ObjectResult GetDetail(string alarmModeId) { try { if (alarmModeId == null) throw new ArgumentNullException("Incorrect parameters"); AlarmMode alarmMode = _AlarmDatabaseService.GetById(alarmModeId); if (alarmMode == null) throw new KeyNotFoundException("Alarm does not exist"); List devices = new List(); foreach (var deviceId in alarmMode.DevicesIds) { Device device = _DeviceDatabaseService.GetById(deviceId); if (device != null) { devices.Add(device); } } return new OkObjectResult(alarmMode.ToDetailDTO(devices.Select(d => d.ToDTO()).ToList())); } 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 }; } } /// /// Create an alarm mode /// /// Alarm mode to create [ProducesResponseType(typeof(AlarmModeDetailDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 500)] [HttpPost] public ObjectResult Create([FromBody] AlarmModeCreateOrUpdateDetailDTO alarmModeCreateOrUpdateDetailDTO) { try { if (alarmModeCreateOrUpdateDetailDTO == null) throw new ArgumentNullException("Incorrect parameters"); AlarmModeDetailDTO alarmModeCreated = AlarmService.CreateOrUpdate(this._AlarmDatabaseService, this._DeviceDatabaseService, alarmModeCreateOrUpdateDetailDTO.HomeId, alarmModeCreateOrUpdateDetailDTO, true); return new OkObjectResult(alarmModeCreated); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Create default alarm modes /// /// Home Id [ProducesResponseType(typeof(bool), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 409)] [ProducesResponseType(typeof(string), 500)] [HttpPost("defaults/{homeId}")] public ObjectResult CreateDefaultAlarms (string homeId) { try { if (homeId == null) throw new ArgumentNullException("Incorrect parameters"); Home home = _HomeDatabaseService.GetById(homeId); if (home == null) throw new KeyNotFoundException("Home does not exist"); List alarmModes = _AlarmDatabaseService.GetAll(homeId); if (alarmModes.Where(a => a.IsDefault).Count() == 5) throw new SystemException("Default modes already existing"); return new ObjectResult(AlarmService.CreateDefaultAlarmModes(_HomeDatabaseService, _AlarmDatabaseService, home)) { StatusCode = 202 }; } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (SystemException ex) { return new ObjectResult(ex.Message) { StatusCode = 409 }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Activate specified alarm mode /// /// Alarm mode to activate [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 409)] [ProducesResponseType(typeof(string), 500)] [HttpPost("activate/{alarmModeId}")] public ObjectResult Activate(string alarmModeId) { try { if (alarmModeId == null) throw new ArgumentNullException("Incorrect parameters"); AlarmMode alarmMode = _AlarmDatabaseService.GetById(alarmModeId); if (alarmMode == null) throw new KeyNotFoundException("Alarm mode does not exist"); if (alarmMode.Activated) throw new AccessViolationException("This alarm mode is already activated"); bool successfull = AlarmService.Activate(_AlarmDatabaseService, alarmMode, _HomeDatabaseService); return new OkObjectResult($"Alarm mode {alarmMode.Name} has been successfully activated") { StatusCode = 202 }; } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (AccessViolationException ex) { return new ObjectResult(ex.Message) { StatusCode = 409 }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Update an alarm mode /// /// alarm mode to update [ProducesResponseType(typeof(AlarmModeDetailDTO), 200)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpPut] public ObjectResult Update([FromBody] AlarmModeCreateOrUpdateDetailDTO alarmModeCreateOrUpdateDetailDTO) { try { if (!_AlarmDatabaseService.IsExist(alarmModeCreateOrUpdateDetailDTO.Id)) throw new KeyNotFoundException("This alarm mode does not exist"); AlarmModeDetailDTO alarmModeUpdated = AlarmService.CreateOrUpdate(this._AlarmDatabaseService, this._DeviceDatabaseService, alarmModeCreateOrUpdateDetailDTO.HomeId, alarmModeCreateOrUpdateDetailDTO, false); return new OkObjectResult(alarmModeUpdated); } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Delete an alarm mode /// /// Id of alarm mode to delete [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 405)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("{alarmModeId}")] public ObjectResult Delete(string alarmModeId) { try { if (alarmModeId == null) throw new ArgumentNullException("Incorrect parameters"); if (!_AlarmDatabaseService.IsExist(alarmModeId)) throw new KeyNotFoundException("This alarm mode does not exist"); AlarmMode alarmMode = _AlarmDatabaseService.GetById(alarmModeId); if (alarmMode.IsDefault) throw new NotSupportedException("This alarm mode cannot be deleted"); // Delete alarm _AlarmDatabaseService.Remove(alarmModeId); return new OkObjectResult("Alarm mode has been successfully deleted") { StatusCode = 202 }; } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (NotSupportedException ex) { return new ObjectResult(ex.Message) { StatusCode = 405 }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Delete all alarm mode for a specified home /// /// Home Id [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("home/{homeId}")] public ObjectResult DeleteAllForHome(string homeId) { try { if (homeId == null) throw new ArgumentNullException("Incorrect parameters"); if (!_HomeDatabaseService.IsExist(homeId)) throw new KeyNotFoundException("Home does not exist"); _AlarmDatabaseService.RemoveForHome(homeId); return new OkObjectResult("All alarm mode associated to specified home has been removed") { 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 }; } } } }