Files
devops-lab4/src/CommunicationControl/DevOpsProject/Controllers/ClientController.cs
T

87 lines
3.2 KiB
C#
Raw Normal View History

2025-02-16 20:15:22 +00:00
using Asp.Versioning;
using DevOpsProject.CommunicationControl.API.DTO.Client.Request;
2025-02-13 13:52:02 +02:00
using DevOpsProject.CommunicationControl.Logic.Services.Interfaces;
using DevOpsProject.Shared.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace DevOpsProject.CommunicationControl.API.Controllers
{
2025-02-16 20:15:22 +00:00
[ApiVersion("1.0")]
2025-02-13 13:52:02 +02:00
[ApiController]
2025-02-16 20:15:22 +00:00
[Route("api/v{version:apiVersion}/client")]
2025-02-13 13:52:02 +02:00
public class ClientController : Controller
{
private readonly ICommunicationControlService _communicationControlService;
private readonly IOptionsMonitor<OperationalAreaConfig> _operationalAreaConfig;
2025-02-16 20:15:22 +00:00
private readonly ILogger<ClientController> _logger;
2025-02-13 13:52:02 +02:00
2025-02-16 20:15:22 +00:00
public ClientController(ICommunicationControlService communicationControlService, IOptionsMonitor<OperationalAreaConfig> operationalAreaConfig, ILogger<ClientController> logger)
2025-02-13 13:52:02 +02:00
{
_communicationControlService = communicationControlService;
_operationalAreaConfig = operationalAreaConfig;
2025-02-16 20:15:22 +00:00
_logger = logger;
2025-02-13 13:52:02 +02:00
}
[HttpGet("area")]
2025-02-16 20:15:22 +00:00
public IActionResult GetOperationalArea()
2025-02-13 13:52:02 +02:00
{
return Ok(_operationalAreaConfig.CurrentValue);
}
[HttpGet("hive/{hiveId}")]
public async Task<IActionResult> GetHive(string hiveId)
{
var hiveExists = await _communicationControlService.IsHiveConnected(hiveId);
if (!hiveExists)
{
_logger.LogWarning("Failed to get Hive for HiveID: {hiveId}", hiveId);
return NotFound($"Hive with HiveID: {hiveId} is not found");
}
2025-02-13 13:52:02 +02:00
var hiveModel = await _communicationControlService.GetHiveModel(hiveId);
2025-02-13 13:52:02 +02:00
return Ok(hiveModel);
}
[HttpGet("hive")]
public async Task<IActionResult> GetHives()
{
var hives = await _communicationControlService.GetAllHives();
return Ok(hives);
}
[HttpDelete("hive/{hiveId}")]
public async Task<IActionResult> DisconnectHive(string hiveId)
{
var disconnetResult = await _communicationControlService.DisconnectHive(hiveId);
return Ok(disconnetResult);
}
[HttpPatch("hive")]
2025-02-16 20:15:22 +00:00
public IActionResult SendBulkHiveMovingSignal(MoveHivesRequest request)
2025-02-13 13:52:02 +02:00
{
if (request?.Hives == null || !request.Hives.Any())
return BadRequest("No hive IDs provided.");
_logger.LogInformation("Hive moving request accepted by enpdoint. Request: {@request}", request);
2025-02-13 13:52:02 +02:00
foreach (var id in request.Hives)
{
2025-02-16 20:15:22 +00:00
_ = Task.Run(async () =>
{
try
{
await _communicationControlService.SendHiveControlSignal(id, request.Destination);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to send control signal for HiveID: {id} \n Request: {@request}", id, request);
2025-02-16 20:15:22 +00:00
}
});
2025-02-13 13:52:02 +02:00
}
2025-02-16 20:15:22 +00:00
2025-02-13 13:52:02 +02:00
return Accepted("Hives are being moved asynchronously.");
}
}
}