mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
427 lines
16 KiB
C#
427 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.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/group")]
|
|
[ApiController]
|
|
public class GroupController : ControllerBase
|
|
{
|
|
private HomeDatabaseService _HomeDatabaseService;
|
|
private GroupDatabaseService _GroupDatabaseService;
|
|
private DeviceDatabaseService _DeviceDatabaseService;
|
|
private ProviderDatabaseService _ProviderDatabaseService;
|
|
private readonly IMqttClientService _mqttClientService;
|
|
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
|
|
|
public GroupController(HomeDatabaseService homeDatabaseService, GroupDatabaseService groupDatabaseService, DeviceDatabaseService deviceDatabaseService, ProviderDatabaseService providerDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
|
|
{
|
|
this._HomeDatabaseService = homeDatabaseService;
|
|
this._GroupDatabaseService = groupDatabaseService;
|
|
this._DeviceDatabaseService = deviceDatabaseService;
|
|
this._ProviderDatabaseService = providerDatabaseService;
|
|
this._mqttClientService = provider.MqttClientService;
|
|
//this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all groups for the specified home
|
|
/// </summary>
|
|
/// <param name="homeId">Home Id</param>
|
|
[ProducesResponseType(typeof(List<GroupSummaryDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{homeId}")]
|
|
public ObjectResult GetAll(string homeId)
|
|
{
|
|
try
|
|
{
|
|
if (homeId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
List<Group> Groups = _GroupDatabaseService.GetAll(homeId);
|
|
|
|
List<GroupSummaryDTO> groupsSummaryDTO = Groups.Select(d => d.ToSummaryDTO()).ToList();
|
|
|
|
return new OkObjectResult(groupsSummaryDTO);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get detail info of a specified group
|
|
/// </summary>
|
|
/// <param name="groupId">groupid</param>
|
|
[ProducesResponseType(typeof(GroupDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("detail/{groupId}")]
|
|
public ObjectResult GetDetail(string groupId)
|
|
{
|
|
try
|
|
{
|
|
if (groupId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
Group group = _GroupDatabaseService.GetById(groupId);
|
|
if (group == null)
|
|
throw new KeyNotFoundException("Group not found");
|
|
|
|
List<Device> devices = _DeviceDatabaseService.GetByGroup(groupId);
|
|
|
|
return new OkObjectResult(group.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>
|
|
/// Get list of group from a type
|
|
/// </summary>
|
|
/// <param name="homeId">home Id</param>
|
|
/// <param name="type">group type</param>
|
|
[ProducesResponseType(typeof(List<GroupSummaryDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{homeId}/type/{type}")]
|
|
public ObjectResult GetGroupsByType(string homeId, string type) // Change string type to enum ? Lights etc ?
|
|
{
|
|
try
|
|
{
|
|
if (homeId == null || type == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
List<Group> groups = _GroupDatabaseService.GetByType(homeId, type);
|
|
|
|
return new OkObjectResult(groups.Select(d => d.ToSummaryDTO()));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all zigbee2Mqtt groups
|
|
/// </summary>
|
|
/// <param name="homeId">Home Id</param>
|
|
[ProducesResponseType(typeof(List<GroupDetailDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("zigbee2Mqtt/{homeId}")]
|
|
public ObjectResult GetGroupsFromZigbee2Mqtt(string homeId)
|
|
{
|
|
try
|
|
{
|
|
if (homeId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!HomeService.IsExist(_HomeDatabaseService, homeId))
|
|
throw new KeyNotFoundException("Home not found");
|
|
|
|
// GET ALL LOCAL GROUPS
|
|
var groups = MqttClientService.groups; // Be carefull, we only got the exact result after each connection
|
|
|
|
return new OkObjectResult(groups);
|
|
}
|
|
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>
|
|
/// Create a group
|
|
/// </summary>
|
|
/// <param name="groupCreateOrUpdateDetail">Group to create</param>
|
|
[ProducesResponseType(typeof(GroupDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost]
|
|
public ObjectResult Create([FromBody] GroupCreateOrUpdateDetailDTO groupCreateOrUpdateDetail)
|
|
{
|
|
try
|
|
{
|
|
if (groupCreateOrUpdateDetail == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
GroupDetailDTO groupCreated = GroupService.CreateOrUpdate(this._GroupDatabaseService, this._DeviceDatabaseService, groupCreateOrUpdateDetail.HomeId, groupCreateOrUpdateDetail, true);
|
|
|
|
return new OkObjectResult(groupCreated);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create groups from provider
|
|
/// </summary>
|
|
/// <param name="homeId">Home Id</param>
|
|
[ProducesResponseType(typeof(List<GroupDetailDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost("{homeId}/fromZigbee")]
|
|
public ObjectResult CreateDevicesFromZigbee2Mqtt(string homeId)
|
|
{
|
|
try
|
|
{
|
|
if (homeId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!HomeService.IsExist(_HomeDatabaseService, homeId))
|
|
throw new KeyNotFoundException("Home not found");
|
|
|
|
Provider provider = _ProviderDatabaseService.GetByType(ProviderType.zigbee2mqtt);
|
|
if (provider == null)
|
|
throw new KeyNotFoundException("Zigbee2mqtt provider not found");
|
|
|
|
// GET ALL LOCAL GROUPS
|
|
var groups = MqttClientService.groups; // Be carefull, we only got the exact result after each connection
|
|
|
|
List<GroupDetailDTO> groupsDetail= GroupService.CreateFromZigbeeAsync(_GroupDatabaseService, _DeviceDatabaseService, homeId, groups);
|
|
|
|
return new OkObjectResult(groupsDetail);
|
|
}
|
|
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>
|
|
/// Update a group
|
|
/// </summary>
|
|
/// <param name="groupCreateOrUpdateDetail">group to update</param>
|
|
[ProducesResponseType(typeof(GroupCreateOrUpdateDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut]
|
|
public ObjectResult Update([FromBody] GroupCreateOrUpdateDetailDTO groupCreateOrUpdateDetail)
|
|
{
|
|
try
|
|
{
|
|
if (groupCreateOrUpdateDetail == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_GroupDatabaseService.IsExist(groupCreateOrUpdateDetail.Id))
|
|
throw new KeyNotFoundException("Group does not exist");
|
|
|
|
GroupDetailDTO groupUpdated = GroupService.CreateOrUpdate(this._GroupDatabaseService, this._DeviceDatabaseService, groupCreateOrUpdateDetail.HomeId, groupCreateOrUpdateDetail, false);
|
|
|
|
return new OkObjectResult(groupUpdated);
|
|
}
|
|
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 group
|
|
/// </summary>
|
|
/// <param name="deviceId">Id of device to delete from the group</param>
|
|
/// <param name="groupId">Id of group </param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("{groupId}/device/{deviceId}")]
|
|
public ObjectResult Delete(string deviceId, string groupId)
|
|
{
|
|
try
|
|
{
|
|
if (groupId == null || deviceId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_GroupDatabaseService.IsExist(groupId))
|
|
throw new KeyNotFoundException("Group does not exist");
|
|
|
|
// Update group
|
|
Group group = _GroupDatabaseService.GetById(groupId);
|
|
group.DevicesIds = group.DevicesIds.Where(d => d != deviceId).ToList();
|
|
group.UpdatedDate = DateTime.Now;
|
|
_GroupDatabaseService.Update(group);
|
|
|
|
// Update device
|
|
Device device = _DeviceDatabaseService.GetById(deviceId);
|
|
if (device == null)
|
|
throw new KeyNotFoundException("Device does not exist");
|
|
|
|
device.GroupIds = device.GroupIds.Where(g => g != groupId).ToList();
|
|
device.UpdatedDate = DateTime.Now;
|
|
_DeviceDatabaseService.Update(device);
|
|
|
|
return new ObjectResult("Device has been successfully removed from specified group") { 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 group
|
|
/// </summary>
|
|
/// <param name="groupId">Id of group to delete</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("{groupId}")]
|
|
public ObjectResult Delete(string groupId)
|
|
{
|
|
try
|
|
{
|
|
if (groupId == null)
|
|
throw new ArgumentNullException("Incorrect parameters");
|
|
|
|
if (!_GroupDatabaseService.IsExist(groupId))
|
|
throw new KeyNotFoundException("Group does not exist");
|
|
|
|
Group group = _GroupDatabaseService.GetById(groupId);
|
|
// Delete group from all devices
|
|
List<Device> devices = _DeviceDatabaseService.GetByGroup(groupId);
|
|
foreach (var device in devices)
|
|
{
|
|
device.GroupIds = device.GroupIds.Where(g => g != groupId).ToList();
|
|
device.UpdatedDate = DateTime.Now;
|
|
_DeviceDatabaseService.Update(device);
|
|
}
|
|
// Delete group
|
|
_GroupDatabaseService.Remove(groupId);
|
|
|
|
return new ObjectResult("The group 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 group 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");
|
|
|
|
_GroupDatabaseService.RemoveForHome(homeId);
|
|
|
|
return new ObjectResult("All group 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 };
|
|
}
|
|
}
|
|
}
|
|
}
|