2025-02-16 20:15:22 +00:00
|
|
|
|
using Asp.Versioning;
|
2025-02-13 13:52:02 +02:00
|
|
|
|
using DevOpsProject.CommunicationControl.Logic.Services.Interfaces;
|
|
|
|
|
using DevOpsProject.Shared.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
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}/hive")]
|
2025-02-13 13:52:02 +02:00
|
|
|
|
public class HiveController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly ICommunicationControlService _communicationControlService;
|
2025-02-20 00:05:33 +02:00
|
|
|
|
private readonly ILogger<HiveController> _logger;
|
2025-02-13 13:52:02 +02:00
|
|
|
|
|
2025-02-20 00:05:33 +02:00
|
|
|
|
public HiveController(ICommunicationControlService communicationControlService, ILogger<HiveController> logger)
|
2025-02-13 13:52:02 +02:00
|
|
|
|
{
|
|
|
|
|
_communicationControlService = communicationControlService;
|
2025-02-20 00:05:33 +02:00
|
|
|
|
_logger = logger;
|
2025-02-13 13:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("connect")]
|
|
|
|
|
public async Task<IActionResult> Connect(HiveConnectRequest request)
|
|
|
|
|
{
|
|
|
|
|
var hiveModel = new HiveModel
|
|
|
|
|
{
|
|
|
|
|
HiveID = request.HiveID,
|
|
|
|
|
HiveIP = request.HiveIP,
|
|
|
|
|
HivePort = request.HivePort,
|
2025-02-19 23:02:15 +02:00
|
|
|
|
HiveSchema = request.HiveSchema
|
2025-02-13 13:52:02 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var hiveOperationalArea = await _communicationControlService.ConnectHive(hiveModel);
|
|
|
|
|
var connectResponse = new HiveConnectResponse
|
|
|
|
|
{
|
|
|
|
|
ConnectResult = true,
|
|
|
|
|
OperationalArea = hiveOperationalArea,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Ok(connectResponse);
|
2025-02-20 00:05:33 +02:00
|
|
|
|
|
2025-02-13 13:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("telemetry")]
|
|
|
|
|
public async Task<IActionResult> Telemetry(HiveTelemetryRequest request)
|
|
|
|
|
{
|
|
|
|
|
var hiveTelemetryModel = new HiveTelemetryModel
|
|
|
|
|
{
|
|
|
|
|
HiveID = request.HiveID,
|
|
|
|
|
Location = request.Location,
|
|
|
|
|
Speed = request.Speed,
|
|
|
|
|
Height = request.Height,
|
|
|
|
|
State = request.State,
|
|
|
|
|
Timestamp = DateTime.Now
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-20 00:05:33 +02:00
|
|
|
|
bool isHiveConnected = await _communicationControlService.IsHiveConnected(request.HiveID);
|
|
|
|
|
if (isHiveConnected)
|
2025-02-13 13:52:02 +02:00
|
|
|
|
{
|
2025-02-20 00:05:33 +02:00
|
|
|
|
var telemetryUpdateTimestamp = await _communicationControlService.AddTelemetry(hiveTelemetryModel);
|
|
|
|
|
var telemetryResponse = new HiveTelemetryResponse
|
|
|
|
|
{
|
|
|
|
|
Timestamp = telemetryUpdateTimestamp
|
|
|
|
|
};
|
2025-02-13 13:52:02 +02:00
|
|
|
|
|
2025-02-20 00:05:33 +02:00
|
|
|
|
return Ok(telemetryResponse);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Failed to write telemetry. Hive with HiveID: {hiveId} is not connected. Request: {@request}", request.HiveID, request);
|
|
|
|
|
return NotFound($"Failed to write telemetry. Hive with HiveID: {request.HiveID} is not connected");
|
|
|
|
|
}
|
2025-02-13 13:52:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|