mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
385 lines
16 KiB
C#
385 lines
16 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.Services;
|
|
using MyCore.Services.Devices;
|
|
using MyCore.Services.MyControlPanel;
|
|
|
|
namespace MyCore.Service.Controllers
|
|
{
|
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
|
[Route("api/room")]
|
|
[ApiController]
|
|
public class RoomController : ControllerBase
|
|
{
|
|
private HomeDatabaseService _HomeDatabaseService;
|
|
private RoomDatabaseService _RoomDatabaseService;
|
|
private DeviceDatabaseService _DeviceDatabaseService;
|
|
private RoomService _RoomService;
|
|
private readonly IMqttClientService _mqttClientService;
|
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
|
|
|
public RoomController(HomeDatabaseService homeDatabaseService, RoomDatabaseService roomDatabaseService, DeviceDatabaseService deviceDatabaseService, RoomService roomService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
|
{
|
|
this._HomeDatabaseService = homeDatabaseService;
|
|
this._RoomDatabaseService = roomDatabaseService;
|
|
this._DeviceDatabaseService = deviceDatabaseService;
|
|
this._RoomService = roomService;
|
|
this._mqttClientService = provider.MqttClientService;
|
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all rooms for the specified home
|
|
/// </summary>
|
|
/// <param name="homeId">Home Id</param>
|
|
[ProducesResponseType(typeof(List<RoomSummaryDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{homeId}")]
|
|
public ObjectResult GetAll(string homeId)
|
|
{
|
|
try
|
|
{
|
|
List<Room> Rooms = _RoomDatabaseService.GetAll(homeId);
|
|
|
|
List<RoomSummaryDTO> roomsSummaryDTO = Rooms.Select(d => d.ToSummaryDTO()).ToList();
|
|
|
|
return new OkObjectResult(roomsSummaryDTO);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all rooms main details for the specified home
|
|
/// </summary>
|
|
/// <param name="homeId">Home Id</param>
|
|
[ProducesResponseType(typeof(List<RoomMainDetailDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{homeId}/details")]
|
|
public ObjectResult GetAllWithMainDetails(string homeId)
|
|
{
|
|
try
|
|
{
|
|
List<Room> rooms = _RoomDatabaseService.GetAll(homeId);
|
|
List<RoomMainDetailDTO> roomMainDetailDTOs = new List<RoomMainDetailDTO>();
|
|
foreach (var room in rooms)
|
|
{
|
|
RoomMainDetailDTO roomMainDetailDTO = room.ToMainDetailsDTO(_DeviceDatabaseService.GetByRoom(room.Id).Select(d => d.ToDTO()).ToList());
|
|
roomMainDetailDTO.Temperature = (string) RoomService.GetValueFromJson(roomMainDetailDTO.EnvironmentalDevices, DeviceType.Environment, "temperature", false); // Todo take the one selected by user as default if more than one
|
|
roomMainDetailDTO.IsTemperature = roomMainDetailDTO.Temperature != null;
|
|
roomMainDetailDTO.Humidity = (string) RoomService.GetValueFromJson(roomMainDetailDTO.EnvironmentalDevices, DeviceType.Environment, "humidity", false); // Todo take the one selected by user as default if more than one
|
|
roomMainDetailDTO.IsHumidity = roomMainDetailDTO.Humidity != null;
|
|
roomMainDetailDTO.Motion = (bool?) RoomService.GetValueFromJson(roomMainDetailDTO.SecurityDevices, DeviceType.Motion, "motion", true);
|
|
roomMainDetailDTO.IsMotion = roomMainDetailDTO.SecurityDevices.Any(sd => sd.Type == DeviceType.Motion) ? roomMainDetailDTO.Motion != null : false;
|
|
roomMainDetailDTO.Door = (bool?) RoomService.GetValueFromJson(roomMainDetailDTO.SecurityDevices, DeviceType.Door, "contact", true); // Todo handle more than just by string
|
|
roomMainDetailDTO.IsDoor = roomMainDetailDTO.SecurityDevices.Any(sd => sd.Type == DeviceType.Door) ? roomMainDetailDTO.Door != null : false;
|
|
roomMainDetailDTOs.Add(roomMainDetailDTO);
|
|
}
|
|
|
|
return new OkObjectResult(roomMainDetailDTOs);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get detail info of a specified room
|
|
/// </summary>
|
|
/// <param name="roomId">room id</param>
|
|
[ProducesResponseType(typeof(RoomDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("detail/{roomId}")]
|
|
public ObjectResult GetDetail(string roomId)
|
|
{
|
|
try
|
|
{
|
|
if (roomId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
Room room = _RoomDatabaseService.GetById(roomId);
|
|
if (room == null)
|
|
throw new KeyNotFoundException("Room does not exist");
|
|
List<Device> devices = _DeviceDatabaseService.GetByRoom(roomId);
|
|
|
|
return new OkObjectResult(room.ToDTO(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 a room
|
|
/// </summary>
|
|
/// <param name="roomCreateOrUpdateDetail">Room to create</param>
|
|
[ProducesResponseType(typeof(RoomDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost]
|
|
public ObjectResult Create([FromBody] RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetail) // TODO handle groupdIds
|
|
{
|
|
try
|
|
{
|
|
if (roomCreateOrUpdateDetail == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
RoomDetailDTO roomCreated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, roomCreateOrUpdateDetail.HomeId, roomCreateOrUpdateDetail, true);
|
|
|
|
return new OkObjectResult(roomCreated);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a room
|
|
/// </summary>
|
|
/// <param name="roomCreateOrUpdateDetail">room to update</param>
|
|
[ProducesResponseType(typeof(RoomCreateOrUpdateDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut]
|
|
public ObjectResult Update([FromBody] RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetail)
|
|
{
|
|
try
|
|
{
|
|
if (!_RoomDatabaseService.IsExist(roomCreateOrUpdateDetail.Id))
|
|
throw new KeyNotFoundException("Room does not exist");
|
|
|
|
RoomDetailDTO roomUpdated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, roomCreateOrUpdateDetail.HomeId, roomCreateOrUpdateDetail, false);
|
|
|
|
return new OkObjectResult(roomUpdated);
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add devices in the specified room
|
|
/// </summary>
|
|
/// <param name="roomId">Room Id</param>
|
|
/// <param name="deviceIds">Device Ids</param>
|
|
[ProducesResponseType(typeof(RoomDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut("{roomId}")]
|
|
public ObjectResult AddDeviceToRoom(string roomId, [FromBody] List<string> deviceIds) // TODO handle groupdIds
|
|
{
|
|
try
|
|
{
|
|
if (roomId == null || deviceIds.Count == 0)
|
|
{
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
}
|
|
|
|
if (!_RoomDatabaseService.IsExist(roomId))
|
|
throw new KeyNotFoundException("Room does not exist");
|
|
|
|
Room room = _RoomDatabaseService.GetById(roomId);
|
|
List<string> deviceIdsToTake = new List<string>();
|
|
|
|
foreach (var deviceId in deviceIds) {
|
|
if (!_DeviceDatabaseService.IsExist(deviceId))
|
|
break;
|
|
deviceIdsToTake.Add(deviceId);
|
|
}
|
|
|
|
RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetailDTO = room.ToCreateOrUpdateDetailDTO();
|
|
roomCreateOrUpdateDetailDTO.DeviceIds.AddRange(deviceIdsToTake);
|
|
|
|
RoomDetailDTO roomUpdated = RoomService.CreateOrUpdate(this._RoomDatabaseService, this._DeviceDatabaseService, room.HomeId, roomCreateOrUpdateDetailDTO, false);
|
|
|
|
return new OkObjectResult(roomUpdated);
|
|
}
|
|
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 device from a room
|
|
/// </summary>
|
|
/// <param name="deviceId">Id of device to delete from the room</param>
|
|
/// <param name="roomId">Id of room </param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("{roomId}/device/{deviceId}")]
|
|
public ObjectResult Delete(string deviceId, string roomId)
|
|
{
|
|
try
|
|
{
|
|
if (roomId == null && deviceId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_RoomDatabaseService.IsExist(roomId))
|
|
throw new KeyNotFoundException("Room does not exist");
|
|
|
|
// Update room
|
|
Room room = _RoomDatabaseService.GetById(roomId);
|
|
room.DevicesIds = room.DevicesIds.Where(d => d != deviceId).ToList();
|
|
room.UpdatedDate = DateTime.Now;
|
|
_RoomDatabaseService.Update(room);
|
|
|
|
// Update device
|
|
Device device = _DeviceDatabaseService.GetById(deviceId);
|
|
|
|
if (device == null)
|
|
throw new KeyNotFoundException("Device does not exist");
|
|
|
|
device.RoomId = null;
|
|
device.UpdatedDate = DateTime.Now;
|
|
_DeviceDatabaseService.Update(device);
|
|
|
|
|
|
return new OkObjectResult("Device has been removed from specified room") { 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 a room
|
|
/// </summary>
|
|
/// <param name="roomId">Id of room to delete</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("{roomId}")]
|
|
public ObjectResult Delete(string roomId)
|
|
{
|
|
try
|
|
{
|
|
if (roomId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_RoomDatabaseService.IsExist(roomId))
|
|
throw new KeyNotFoundException("Room does not exist");
|
|
|
|
Room room = _RoomDatabaseService.GetById(roomId);
|
|
// Delete location from all devices
|
|
List<Device> devices = _DeviceDatabaseService.GetByRoom(roomId);
|
|
foreach (var device in devices)
|
|
{
|
|
device.RoomId = null;
|
|
device.UpdatedDate = DateTime.Now;
|
|
_DeviceDatabaseService.Update(device);
|
|
}
|
|
// Delete room
|
|
_RoomDatabaseService.Remove(roomId);
|
|
|
|
return new OkObjectResult("Room 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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete all rooms 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");
|
|
|
|
_RoomDatabaseService.RemoveForHome(homeId);
|
|
|
|
return new OkObjectResult("All rooms 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 };
|
|
}
|
|
}
|
|
}
|
|
}
|