mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
242 lines
8.8 KiB
C#
242 lines
8.8 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/automation")]
|
|
[ApiController]
|
|
public class AutomationController : ControllerBase
|
|
{
|
|
private HomeDatabaseService _HomeDatabaseService;
|
|
private AutomationDatabaseService _AutomationDatabaseService;
|
|
private readonly IMqttClientService _mqttClientService;
|
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
|
|
|
public AutomationController(HomeDatabaseService homeDatabaseService, AutomationDatabaseService automationDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
|
{
|
|
this._HomeDatabaseService = homeDatabaseService;
|
|
this._AutomationDatabaseService = automationDatabaseService;
|
|
this._mqttClientService = provider.MqttClientService;
|
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all automations for the specified home
|
|
/// </summary>
|
|
/// <param name="homeId">Home Id</param>
|
|
[ProducesResponseType(typeof(List<AutomationDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{homeId}")]
|
|
public ObjectResult GetAll(string homeId)
|
|
{
|
|
try
|
|
{
|
|
List<Automation> Automations = _AutomationDatabaseService.GetAll(homeId);
|
|
|
|
List<AutomationDTO> automationDTO = Automations.Select(d => d.ToDTO()).ToList();
|
|
|
|
return new OkObjectResult(automationDTO);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get detail info of a specified automation
|
|
/// </summary>
|
|
/// <param name="automationId">automation id</param>
|
|
[ProducesResponseType(typeof(AutomationDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("detail/{automationId}")]
|
|
public ObjectResult GetDetail(string automationId)
|
|
{
|
|
try
|
|
{
|
|
if (automationId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
Automation automation = _AutomationDatabaseService.GetById(automationId);
|
|
|
|
if (automation == null)
|
|
throw new KeyNotFoundException("Automation not found");
|
|
|
|
return new OkObjectResult(automation.ToDetailDTO());
|
|
}
|
|
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 automation
|
|
/// </summary>
|
|
/// <param name="automationDetailDTO">Automation to create</param>
|
|
[ProducesResponseType(typeof(AutomationDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost]
|
|
public ObjectResult Create([FromBody] AutomationDetailDTO automationDetailDTO)
|
|
{
|
|
try
|
|
{
|
|
if (automationDetailDTO == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
AutomationDTO automationCreated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationDetailDTO.HomeId, automationDetailDTO, true);
|
|
|
|
return new OkObjectResult(automationCreated);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Update an automation
|
|
/// </summary>
|
|
/// <param name="automationDetailDTO">automation to update</param>
|
|
[ProducesResponseType(typeof(AutomationDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut]
|
|
public ObjectResult Update([FromBody] AutomationDetailDTO automationDetailDTO)
|
|
{
|
|
try
|
|
{
|
|
if (automationDetailDTO == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_AutomationDatabaseService.IsExist(automationDetailDTO.Id))
|
|
throw new KeyNotFoundException("Automation does not exist");
|
|
|
|
AutomationDTO automationUpdated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationDetailDTO.HomeId, automationDetailDTO, false);
|
|
|
|
return new OkObjectResult(automationUpdated);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete an automation
|
|
/// </summary>
|
|
/// <param name="automationId">Id of automation to delete</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("{automationId}")]
|
|
public ObjectResult Delete(string automationId)
|
|
{
|
|
try
|
|
{
|
|
if (automationId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_AutomationDatabaseService.IsExist(automationId))
|
|
throw new KeyNotFoundException("Automation does not exist");
|
|
|
|
_AutomationDatabaseService.Remove(automationId);
|
|
|
|
return new ObjectResult("The automation 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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete all automation 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");
|
|
|
|
_AutomationDatabaseService.RemoveForHome(homeId);
|
|
|
|
return new ObjectResult("All automation linked with specified home has been successfully 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 };
|
|
}
|
|
}
|
|
}
|
|
}
|