mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
348 lines
12 KiB
C#
348 lines
12 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.Controllers
|
|
{
|
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
|
[Route("api/device")]
|
|
[ApiController]
|
|
public class DeviceController : ControllerBase
|
|
{
|
|
private DeviceDatabaseService _DeviceDatabaseService;
|
|
private ProviderDatabaseService _ProviderDatabaseService;
|
|
private LocationDatabaseService _LocationDatabaseService;
|
|
private UserDatabaseService _UserDatabaseService;
|
|
private readonly IMqttClientService _mqttClientService;
|
|
private readonly IMqttOnlineClientService _mqttOnlineClientService;
|
|
|
|
public DeviceController(DeviceDatabaseService DeviceDatabaseService, ProviderDatabaseService ProviderDatabaseService, LocationDatabaseService LocationDatabaseService, UserDatabaseService UserDatabaseService, MqttClientServiceProvider provider, MqttClientOnlineServiceProvider onlineProvider)
|
|
{
|
|
this._DeviceDatabaseService = DeviceDatabaseService;
|
|
this._ProviderDatabaseService = ProviderDatabaseService;
|
|
this._LocationDatabaseService = LocationDatabaseService;
|
|
this._UserDatabaseService = UserDatabaseService;
|
|
this._mqttClientService = provider.MqttClientService;
|
|
this._mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
|
|
}
|
|
|
|
// GET: Devices
|
|
/// <summary>
|
|
/// Get all devices summary
|
|
/// </summary>
|
|
/// <param name="userId">Id of user</param>
|
|
[ProducesResponseType(typeof(List<DeviceSummaryDTO>), 200)]
|
|
[HttpGet("{userId}")]
|
|
public ObjectResult GetAll(string userId)
|
|
{
|
|
try
|
|
{
|
|
List<Device> Devices = _DeviceDatabaseService.GetAll(userId);
|
|
|
|
List<DeviceSummaryDTO> devicesSummaryDTO = Devices.Select(d => d.ToSummaryDTO()).ToList();
|
|
|
|
foreach (var device in devicesSummaryDTO)
|
|
{
|
|
device.ProviderName = _ProviderDatabaseService.GetById(userId, device.ProviderId).Name;
|
|
}
|
|
|
|
return new OkObjectResult(devicesSummaryDTO);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a specific device info
|
|
/// </summary>
|
|
/// <param name="deviceId">id of device</param>
|
|
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
|
|
[HttpGet("detail/{deviceId}")]
|
|
public ObjectResult GetDetail(string deviceId)
|
|
{
|
|
try
|
|
{
|
|
Device device = _DeviceDatabaseService.GetById(deviceId);
|
|
|
|
return new OkObjectResult(device.ToDTO());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a device
|
|
/// </summary>
|
|
/// <param name="deviceDetailDTO">Device to create</param>
|
|
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
|
|
[HttpPost]
|
|
public ObjectResult Create([FromBody] DeviceDetailDTO deviceDetailDTO)
|
|
{
|
|
try
|
|
{
|
|
if (deviceDetailDTO == null)
|
|
throw new KeyNotFoundException("Device is null");
|
|
|
|
DeviceDetailDTO deviceCreated = DeviceService.CreateOrUpdate(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, deviceDetailDTO.UserId, deviceDetailDTO, true);
|
|
|
|
return new OkObjectResult(deviceCreated);
|
|
|
|
}
|
|
catch (DuplicateWaitObjectException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create devices from provider
|
|
/// </summary>
|
|
/// <param name="userId">User Id</param>
|
|
/// <param name="providerId">Id of Provider</param>
|
|
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
|
[HttpPost("fromProvider/{userId}")]
|
|
public async Task<ObjectResult> CreateDevicesFromProvider(string userId, string providerId)
|
|
{
|
|
try
|
|
{
|
|
if (userId == null)
|
|
throw new InvalidOperationException("User not found");
|
|
|
|
if (!UserService.IsExist(_UserDatabaseService, userId))
|
|
throw new KeyNotFoundException("User not found");
|
|
|
|
Provider provider = ProviderService.GetProviderById(this._ProviderDatabaseService, userId, providerId);
|
|
if (provider == null)
|
|
throw new KeyNotFoundException("Provider id is null");
|
|
|
|
List<DeviceDetailDTO> devicesCreated = await DeviceService.CreateFromProvider(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, userId, provider);
|
|
|
|
return new OkObjectResult(devicesCreated);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
|
|
}
|
|
catch (AuthenticationException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 401 };
|
|
}
|
|
catch (HttpRequestException ex) // Not ok if not http request..
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 421 };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all zigbee2Mqtt devices
|
|
/// </summary>
|
|
/// <param name="userId">User Id</param>
|
|
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
|
[HttpGet("zigbee2Mqtt/{userId}")]
|
|
public async Task<ObjectResult> GetDevicesFromZigbee2Mqtt(string userId)
|
|
{
|
|
try
|
|
{
|
|
if (userId == null)
|
|
throw new InvalidOperationException("User not found");
|
|
|
|
if (!UserService.IsExist(_UserDatabaseService, userId))
|
|
throw new KeyNotFoundException("User not found");
|
|
|
|
// GET ALL LOCAL DEVICES
|
|
var devices = MqttClientService.devices;
|
|
|
|
// Test mqtt
|
|
await MqttClientService.PublishMessage("test", "zdz").ContinueWith(res => {
|
|
|
|
if (res.Status == TaskStatus.RanToCompletion)
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("Publish error");
|
|
}
|
|
});
|
|
|
|
// Test online mqtt
|
|
await MqttClientOnlineService.PublishMessage("test", "zdz").ContinueWith(res => {
|
|
|
|
if (res.Status == TaskStatus.RanToCompletion)
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("Publish error");
|
|
}
|
|
});
|
|
|
|
return new OkObjectResult(devices);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create devices from provider
|
|
/// </summary>
|
|
/// <param name="userId">User Id</param>
|
|
[ProducesResponseType(typeof(List<DeviceDetailDTO>), 200)]
|
|
[HttpPost("fromZigbee2Mqtt/{userId}")]
|
|
public async Task<ObjectResult> CreateDevicesFromZigbee2Mqtt(string userId)
|
|
{
|
|
try
|
|
{
|
|
if (userId == null)
|
|
throw new InvalidOperationException("User not found");
|
|
|
|
if (!UserService.IsExist(_UserDatabaseService, userId))
|
|
throw new KeyNotFoundException("User not found");
|
|
|
|
// Test mqtt
|
|
await MqttClientService.PublishMessage("test", "zdz").ContinueWith(res => {
|
|
|
|
if (res.Status == TaskStatus.RanToCompletion)
|
|
{
|
|
|
|
}
|
|
else {
|
|
throw new Exception("Publish error");
|
|
}
|
|
});
|
|
|
|
var test = MqttClientService.devices;
|
|
// Peut etre juste mettre un ok pour ajout zigbee vu qu'on fait le get device lors de la connexion.
|
|
List<DeviceDetailDTO> devicesCreated = await DeviceService.CreateFromZigbee(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, userId);
|
|
|
|
return new OkObjectResult(test);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 400 };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a device
|
|
/// </summary>
|
|
/// <param name="deviceDetailDTO">Device to update</param>
|
|
[ProducesResponseType(typeof(DeviceDetailDTO), 200)]
|
|
[HttpPut("{deviceId}")]
|
|
public ObjectResult Update([FromBody] DeviceDetailDTO deviceDetailDTO)
|
|
{
|
|
try
|
|
{
|
|
if (!_DeviceDatabaseService.IsExist(deviceDetailDTO.Id))
|
|
throw new KeyNotFoundException("Device does not exist");
|
|
|
|
DeviceDetailDTO deviceUpdated = DeviceService.CreateOrUpdate(this._DeviceDatabaseService, this._ProviderDatabaseService, this._LocationDatabaseService, deviceDetailDTO.UserId, deviceDetailDTO, false);
|
|
|
|
return new OkObjectResult(deviceUpdated);
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { StatusCode = 404 };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete a device
|
|
/// </summary>
|
|
/// <param name="deviceId">Id of device to delete</param>
|
|
[HttpDelete("{deviceId}")]
|
|
public ObjectResult Delete(string deviceId)
|
|
{
|
|
try
|
|
{
|
|
if (deviceId != null)
|
|
{
|
|
if (_DeviceDatabaseService.IsExist(deviceId))
|
|
{
|
|
_DeviceDatabaseService.Remove(deviceId);
|
|
}
|
|
}
|
|
|
|
return new OkObjectResult(201);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete all device 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))
|
|
{
|
|
_DeviceDatabaseService.RemoveForUser(userId);
|
|
}
|
|
}
|
|
|
|
return new OkObjectResult(201);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
}
|
|
}
|