mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 17:51:20 +00:00
184 lines
6.4 KiB
C#
184 lines
6.4 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 UserDatabaseService _UserDatabaseService;
|
|
private AutomationDatabaseService _AutomationDatabaseService;
|
|
private readonly IMqttClientService _mqttClientService;
|
|
private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
|
|
|
public AutomationController(UserDatabaseService userDatabaseService, AutomationDatabaseService automationDatabaseService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider)
|
|
{
|
|
this._UserDatabaseService = userDatabaseService;
|
|
this._AutomationDatabaseService = automationDatabaseService;
|
|
this._mqttClientService = provider.MqttClientService;
|
|
this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all automations for the specified user
|
|
/// </summary>
|
|
/// <param name="userId">Id of user</param>
|
|
[ProducesResponseType(typeof(List<RoomSummaryDTO>), 200)]
|
|
[HttpGet("{userId}")]
|
|
public ObjectResult GetAll(string userId)
|
|
{
|
|
try
|
|
{
|
|
List<Automation> Automations = _AutomationDatabaseService.GetAll(userId);
|
|
|
|
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)]
|
|
[HttpGet("detail/{roomId}")]
|
|
public ObjectResult GetDetail(string automationId)
|
|
{
|
|
try
|
|
{
|
|
Automation automation = _AutomationDatabaseService.GetById(automationId);
|
|
|
|
return new OkObjectResult(automation.ToDetailDTO());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
|
|
//// <summary>
|
|
/// Create an automation
|
|
/// </summary>
|
|
/// <param name="automationCreateOrUpdateDetail">Automation to create</param>
|
|
[ProducesResponseType(typeof(AutomationDTO), 200)]
|
|
[HttpPost]
|
|
public ObjectResult Create([FromBody] AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetail)
|
|
{
|
|
try
|
|
{
|
|
if (automationCreateOrUpdateDetail == null)
|
|
throw new KeyNotFoundException("Automation is null");
|
|
|
|
AutomationDTO automationCreated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationCreateOrUpdateDetail.UserId, automationCreateOrUpdateDetail, true);
|
|
|
|
return new OkObjectResult(automationCreated);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Update an automation
|
|
/// </summary>
|
|
/// <param name="automationCreateOrUpdateDetail">automation to update</param>
|
|
[ProducesResponseType(typeof(AutomationCreateOrUpdateDetailDTO), 200)]
|
|
[HttpPut]
|
|
public ObjectResult Update([FromBody] AutomationCreateOrUpdateDetailDTO automationCreateOrUpdateDetail)
|
|
{
|
|
try
|
|
{
|
|
if (!_AutomationDatabaseService.IsExist(automationCreateOrUpdateDetail.Id))
|
|
throw new KeyNotFoundException("Automation does not exist");
|
|
|
|
AutomationDTO automationUpdated = AutomationService.CreateOrUpdate(this._AutomationDatabaseService, automationCreateOrUpdateDetail.UserId, automationCreateOrUpdateDetail, false);
|
|
|
|
return new OkObjectResult(automationUpdated);
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete an automation
|
|
/// </summary>
|
|
/// <param name="automationId">Id of automation to delete</param>
|
|
[HttpDelete("{automationId}")]
|
|
public ObjectResult Delete(string automationId)
|
|
{
|
|
try
|
|
{
|
|
if (automationId != null)
|
|
{
|
|
if (_AutomationDatabaseService.IsExist(automationId))
|
|
{
|
|
_AutomationDatabaseService.Remove(automationId);
|
|
}
|
|
}
|
|
|
|
return new OkObjectResult(201);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete all automation for a specified
|
|
/// </summary>
|
|
/// <param name="userId">Id of user</param>
|
|
[HttpDelete("user/{userId}")]
|
|
public ObjectResult DeleteAllForUser(string userId)
|
|
{
|
|
try
|
|
{
|
|
if (userId != null)
|
|
{
|
|
if (_UserDatabaseService.IsExist(userId))
|
|
{
|
|
_AutomationDatabaseService.RemoveForUser(userId);
|
|
}
|
|
}
|
|
|
|
return new OkObjectResult(201);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
}
|
|
}
|