mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
257 lines
10 KiB
C#
257 lines
10 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/home")]
|
|
[ApiController]
|
|
public class HomeController : ControllerBase
|
|
{
|
|
private HomeDatabaseService _HomeDatabaseService;
|
|
private AlarmDatabaseService _AlarmDatabaseService;
|
|
private RoomDatabaseService _RoomDatabaseService;
|
|
private DeviceDatabaseService _DeviceDatabaseService;
|
|
private ProviderDatabaseService _ProviderDatabaseService;
|
|
private UserDatabaseService _UserDatabaseService;
|
|
private GroupDatabaseService _GroupDatabaseService;
|
|
private AutomationDatabaseService _AutomationDatabaseService;
|
|
private readonly IMqttClientService _mqttClientService;
|
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
|
|
|
public HomeController(HomeDatabaseService homeDatabaseService, RoomDatabaseService roomDatabaseService, DeviceDatabaseService deviceDatabaseService, ProviderDatabaseService providerDatabaseService, UserDatabaseService userDatabaseService, AutomationDatabaseService automationDatabaseService, GroupDatabaseService groupDatabaseService, AlarmDatabaseService alarmDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
|
{
|
|
this._HomeDatabaseService = homeDatabaseService;
|
|
this._RoomDatabaseService = roomDatabaseService;
|
|
this._DeviceDatabaseService = deviceDatabaseService;
|
|
this._ProviderDatabaseService = providerDatabaseService;
|
|
this._UserDatabaseService = userDatabaseService;
|
|
this._AutomationDatabaseService = automationDatabaseService;
|
|
this._GroupDatabaseService = groupDatabaseService;
|
|
this._AlarmDatabaseService = alarmDatabaseService;
|
|
this._mqttClientService = provider.MqttClientService;
|
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all home for specified user
|
|
/// </summary>
|
|
/// <param name="userId">User Id</param>
|
|
[ProducesResponseType(typeof(List<HomeDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{userId}")]
|
|
public ObjectResult GetAll(string userId)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (userId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
UserInfo user = _UserDatabaseService.GetById(userId);
|
|
if (user == null)
|
|
throw new KeyNotFoundException("User does not exist");
|
|
|
|
List<HomeDTO> HomesResult = new List<HomeDTO>();
|
|
foreach (var homeId in user.HomeIds)
|
|
{
|
|
Home home = _HomeDatabaseService.GetById(homeId);
|
|
HomesResult.Add(home.ToDTO());
|
|
}
|
|
|
|
return new OkObjectResult(HomesResult);
|
|
}
|
|
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>
|
|
/// Get detail info of a specified home
|
|
/// </summary>
|
|
/// <param name="homeId">home id</param>
|
|
[ProducesResponseType(typeof(HomeDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("detail/{homeId}")]
|
|
public ObjectResult GetDetail(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<UserInfo> users = _UserDatabaseService.GetByHomeId(home.Id);
|
|
List<Device> devices = _DeviceDatabaseService.GetByHomeId(home.Id);
|
|
List<Automation> automations = _AutomationDatabaseService.GetByHomeId(home.Id);
|
|
List<Provider> providers = _ProviderDatabaseService.GetByHomeId(home.Id);
|
|
List<Group> groups = _GroupDatabaseService.GetByHomeId(home.Id);
|
|
|
|
return new OkObjectResult(home.ToDetailDTO(users, devices, automations, providers, groups));
|
|
}
|
|
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 a home
|
|
/// </summary>
|
|
/// <param name="createOrUpdateHomeDTO">Home to create</param>
|
|
[ProducesResponseType(typeof(HomeDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost]
|
|
public ObjectResult Create([FromBody] CreateOrUpdateHomeDTO createOrUpdateHomeDTO)
|
|
{
|
|
try
|
|
{
|
|
if (createOrUpdateHomeDTO == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (createOrUpdateHomeDTO.UsersIds.Count <= 0 && _UserDatabaseService.IsExistMultiple(createOrUpdateHomeDTO.UsersIds))
|
|
throw new ArgumentNullException("To create an home, you need at least one user");
|
|
|
|
Home homeCreated = HomeService.CreateOrUpdate(this._HomeDatabaseService, this._AlarmDatabaseService, createOrUpdateHomeDTO, true);
|
|
|
|
if (homeCreated.IsAlarm)
|
|
// Create default alarm modes
|
|
AlarmService.CreateDefaultAlarmModes(_HomeDatabaseService, _AlarmDatabaseService, homeCreated); // Create and by default select desarmed mode
|
|
|
|
foreach (var userId in createOrUpdateHomeDTO.UsersIds)
|
|
{
|
|
UserInfo user = _UserDatabaseService.GetById(userId);
|
|
if (user.HomeIds == null)
|
|
user.HomeIds = new List<string>();
|
|
|
|
if (!user.HomeIds.Contains(homeCreated.Id))
|
|
user.HomeIds.Add(homeCreated.Id);
|
|
_UserDatabaseService.Update(user);
|
|
}
|
|
|
|
return new OkObjectResult(homeCreated.ToDTO());
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a home
|
|
/// </summary>
|
|
/// <param name="createOrUpdateHomeDTO">Home to update</param>
|
|
[ProducesResponseType(typeof(HomeDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut]
|
|
public ObjectResult Update([FromBody] CreateOrUpdateHomeDTO createOrUpdateHomeDTO)
|
|
{
|
|
try
|
|
{
|
|
if (!_HomeDatabaseService.IsExist(createOrUpdateHomeDTO.Id))
|
|
throw new KeyNotFoundException("Home does not exist");
|
|
|
|
if (createOrUpdateHomeDTO.UsersIds.Count <= 0)
|
|
throw new ArgumentNullException("To create an home, you need at least one user");
|
|
|
|
Home homeUpdated = HomeService.CreateOrUpdate(this._HomeDatabaseService, this._AlarmDatabaseService, createOrUpdateHomeDTO, false);
|
|
|
|
return new OkObjectResult(homeUpdated.ToDTO());
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete a home
|
|
/// </summary>
|
|
/// <param name="homeId">Id of home to delete</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("{homeId}")]
|
|
public ObjectResult Delete(string homeId)
|
|
{
|
|
try
|
|
{
|
|
if (homeId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_HomeDatabaseService.IsExist(homeId))
|
|
throw new KeyNotFoundException("Home does not exist");
|
|
|
|
Home home = _HomeDatabaseService.GetById(homeId);
|
|
// Check if something needs to be done.. (delete all devices related to home, all automations, all groups, all alarm modes, etc)
|
|
// Delete home
|
|
_HomeDatabaseService.Remove(homeId);
|
|
|
|
return new OkObjectResult("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 };
|
|
}
|
|
}
|
|
}
|
|
}
|