mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
266 lines
10 KiB
C#
266 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.Services;
|
|
using MyCore.Services.Devices;
|
|
using MyCore.Services.MyControlPanel;
|
|
|
|
namespace MyCore.Service.Controllers
|
|
{
|
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
|
[Route("api/event")]
|
|
[ApiController]
|
|
public class EventController : ControllerBase
|
|
{
|
|
private HomeDatabaseService _HomeDatabaseService;
|
|
private RoomDatabaseService _RoomDatabaseService;
|
|
private EventDatabaseService _EventDatabaseService;
|
|
private DeviceDatabaseService _DeviceDatabaseService;
|
|
private readonly IMqttClientService _mqttClientService;
|
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
|
|
|
public EventController(HomeDatabaseService homeDatabaseService, RoomDatabaseService roomDatabaseService, EventDatabaseService eventDatabaseService, DeviceDatabaseService deviceDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
|
{
|
|
this._HomeDatabaseService = homeDatabaseService;
|
|
this._RoomDatabaseService = roomDatabaseService;
|
|
this._EventDatabaseService = eventDatabaseService;
|
|
this._DeviceDatabaseService = deviceDatabaseService;
|
|
this._mqttClientService = provider.MqttClientService;
|
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get events for the specified home
|
|
/// </summary>
|
|
/// <param name="homeId">Home Id</param>
|
|
/// <param name="eventHomeFilter">Filter params</param>
|
|
[ProducesResponseType(typeof(List<EventDetailDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{homeId}")]
|
|
public ObjectResult Get(string homeId, [FromQuery] EventHomeFilter eventHomeFilter) // TODO Add filter by date etc
|
|
{
|
|
try
|
|
{
|
|
List<Event> events = _EventDatabaseService.GetAll(homeId); // To check if best
|
|
if (eventHomeFilter.EventType != null)
|
|
{
|
|
events = events.Where(e => e.Type == eventHomeFilter.EventType).OrderBy(e => e.Date).ToList();
|
|
}
|
|
|
|
if (eventHomeFilter.DeviceType != null)
|
|
{
|
|
events = events.Where(e => e.DeviceState.DeviceType == eventHomeFilter.DeviceType).OrderBy(e => e.Date).ToList();
|
|
}
|
|
|
|
if (eventHomeFilter.DeviceId != null)
|
|
{
|
|
events = events.Where(e => e.DeviceState.DeviceId == eventHomeFilter.DeviceId).OrderBy(e => e.Date).ToList();
|
|
}
|
|
|
|
if (eventHomeFilter.RoomId != null)
|
|
{
|
|
events = events.Where(e => e.RoomId == eventHomeFilter.RoomId).OrderBy(e => e.Date).ToList();
|
|
}
|
|
|
|
if (eventHomeFilter.DateStart != null && eventHomeFilter.DateEnd != null) {
|
|
events = events.Where(e => e.Date >= eventHomeFilter.DateStart && e.Date < eventHomeFilter.DateEnd).OrderBy(e => e.Date).ToList();
|
|
}
|
|
|
|
var totalCount = events.Count();
|
|
events = events.Skip(eventHomeFilter.StartIndex).Take(eventHomeFilter.Count).ToList();
|
|
|
|
List<EventDetailDTO> eventDTOs = events.Select(d => d.ToDetailDTO()).OrderBy(e => e.Date).ToList();
|
|
|
|
var lr = new ListResponse<EventDetailDTO, EventHomeFilter>(eventDTOs, eventHomeFilter);
|
|
lr.TotalCount = totalCount;
|
|
lr.ActualCount = eventDTOs.Count();
|
|
|
|
return new OkObjectResult(lr);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get detail info of a specified event
|
|
/// </summary>
|
|
/// <param name="eventId">event id</param>
|
|
[ProducesResponseType(typeof(EventDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("detail/{eventId}")]
|
|
public ObjectResult GetDetail(string eventId)
|
|
{
|
|
try
|
|
{
|
|
if (eventId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
Event evt = _EventDatabaseService.GetById(eventId);
|
|
if (evt == null)
|
|
throw new KeyNotFoundException("Event does not exist");
|
|
|
|
return new OkObjectResult(evt.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 event
|
|
/// </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)
|
|
{
|
|
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 an event
|
|
/// </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>
|
|
/// Delete an event
|
|
/// </summary>
|
|
/// <param name="eventId">Id of event to delete</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("{eventId}")]
|
|
public ObjectResult Delete(string eventId)
|
|
{
|
|
try
|
|
{
|
|
if (eventId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_EventDatabaseService.IsExist(eventId))
|
|
throw new KeyNotFoundException("Event does not exist");
|
|
|
|
_EventDatabaseService.Remove(eventId);
|
|
|
|
return new OkObjectResult("Event 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 events 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");
|
|
|
|
_EventDatabaseService.RemoveForHome(homeId);
|
|
|
|
return new OkObjectResult("All events 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 };
|
|
}
|
|
}
|
|
}
|
|
}
|