mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
349 lines
14 KiB
C#
349 lines
14 KiB
C#
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all alarm modes for the specified home
|
|
/// </summary>
|
|
/// <param name="homeId">Home Id</param>
|
|
[ProducesResponseType(typeof(List<AlarmModeDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{homeId}")]
|
|
public ObjectResult GetAll(string homeId)
|
|
{
|
|
try
|
|
{
|
|
List<AlarmMode> alarmModes = _AlarmDatabaseService.GetAll(homeId);
|
|
|
|
List<AlarmModeDTO> alarmModesDTO = alarmModes.Select(a => a.ToDTO()).ToList();
|
|
|
|
return new OkObjectResult(alarmModesDTO);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get detail info of a specified alarm mode
|
|
/// </summary>
|
|
/// <param name="alarmModeId">alarm id</param>
|
|
[ProducesResponseType(typeof(AlarmModeDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("detail/{alarmId}")]
|
|
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<Device> devices = new List<Device>();
|
|
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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create an alarm mode
|
|
/// </summary>
|
|
/// <param name="alarmModeCreateOrUpdateDetailDTO">Alarm mode to create</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create default alarm modes
|
|
/// </summary>
|
|
/// <param name="homeId">Home Id</param>
|
|
[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<AlarmMode> 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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Activate current alarm mode
|
|
/// </summary>
|
|
/// <param name="alarmModeId">Alarm mode to activate</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update an alarm mode
|
|
/// </summary>
|
|
/// <param name="alarmModeCreateOrUpdateDetailDTO">alarm mode to update</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete an alarm mode
|
|
/// </summary>
|
|
/// <param name="alarmModeId">Id of alarm mode to delete</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete all alarm mode for a specified home
|
|
/// </summary>
|
|
/// <param name="homeId">Home Id</param>
|
|
[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 };
|
|
}
|
|
}
|
|
}
|
|
}
|