HiveMind PoC + P1, P3 fixes
This commit is contained in:
parent
1744425d5f
commit
9f6c1f5135
|
@ -1,5 +1,6 @@
|
|||
src/MapClient/node_modules
|
||||
src/CommunicationControl/.idea
|
||||
src/CommunicationControl/.vs
|
||||
bin
|
||||
obj
|
||||
Logs
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Nullable>disable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using DevOpsProject.CommunicationControl.Logic.Services.Interfaces;
|
||||
using DevOpsProject.Shared.Clients;
|
||||
using DevOpsProject.Shared.Configuration;
|
||||
using DevOpsProject.Shared.Enums;
|
||||
using DevOpsProject.Shared.Exceptions;
|
||||
using DevOpsProject.Shared.Messages;
|
||||
using DevOpsProject.Shared.Models;
|
||||
|
@ -15,11 +16,12 @@ namespace DevOpsProject.CommunicationControl.Logic.Services
|
|||
private readonly IRedisKeyValueService _redisService;
|
||||
private readonly RedisKeys _redisKeys;
|
||||
private readonly IPublishService _messageBus;
|
||||
private readonly HiveHttpClient _hiveHttpClient;
|
||||
private readonly CommunicationControlHttpClient _hiveHttpClient;
|
||||
private readonly ILogger<CommunicationControlService> _logger;
|
||||
private readonly IOptionsMonitor<ComControlCommunicationConfiguration> _communicationControlConfiguration;
|
||||
|
||||
public CommunicationControlService(ISpatialService spatialService, IRedisKeyValueService redisService, IOptionsSnapshot<RedisKeys> redisKeysSnapshot,
|
||||
IPublishService messageBus, HiveHttpClient hiveHttpClient, ILogger<CommunicationControlService> logger)
|
||||
IPublishService messageBus, CommunicationControlHttpClient hiveHttpClient, ILogger<CommunicationControlService> logger, IOptionsMonitor<ComControlCommunicationConfiguration> communicationControlConfiguration)
|
||||
{
|
||||
_spatialService = spatialService;
|
||||
_redisService = redisService;
|
||||
|
@ -27,6 +29,7 @@ namespace DevOpsProject.CommunicationControl.Logic.Services
|
|||
_messageBus = messageBus;
|
||||
_hiveHttpClient = hiveHttpClient;
|
||||
_logger = logger;
|
||||
_communicationControlConfiguration = communicationControlConfiguration;
|
||||
}
|
||||
|
||||
public async Task<bool> DisconnectHive(string hiveId)
|
||||
|
@ -65,7 +68,7 @@ namespace DevOpsProject.CommunicationControl.Logic.Services
|
|||
bool result = await _redisService.SetAsync(GetHiveKey(model.HiveID), model);
|
||||
if (result)
|
||||
{
|
||||
var operationalArea = await _spatialService.GetHiveOperationalArea(model);
|
||||
var operationalArea = _spatialService.GetHiveOperationalArea(model);
|
||||
await _messageBus.Publish(new HiveConnectedMessage
|
||||
{
|
||||
HiveID = model.HiveID,
|
||||
|
@ -119,7 +122,7 @@ namespace DevOpsProject.CommunicationControl.Logic.Services
|
|||
|
||||
}
|
||||
|
||||
public async Task<string?> SendHiveControlSignal(string hiveId, Location destination)
|
||||
public async Task<string> SendHiveControlSignal(string hiveId, Location destination)
|
||||
{
|
||||
var hive = await GetHiveModel(hiveId);
|
||||
if (hive == null)
|
||||
|
@ -131,8 +134,15 @@ namespace DevOpsProject.CommunicationControl.Logic.Services
|
|||
|
||||
try
|
||||
{
|
||||
// TODO: Schema can be moved to appsettings
|
||||
var result = await _hiveHttpClient.SendHiveControlCommandAsync("http", hive.HiveIP, hive.HivePort, destination);
|
||||
var command = new MoveHiveMindCommand
|
||||
{
|
||||
CommandType = State.Move,
|
||||
Location = destination,
|
||||
Timestamp = DateTime.Now
|
||||
};
|
||||
|
||||
var result = await _hiveHttpClient.SendHiveControlCommandAsync(_communicationControlConfiguration.CurrentValue.RequestScheme,
|
||||
hive.HiveIP, hive.HivePort, _communicationControlConfiguration.CurrentValue.HiveMindPath, command);
|
||||
isSuccessfullySent = true;
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -9,6 +9,6 @@ namespace DevOpsProject.CommunicationControl.Logic.Services.Interfaces
|
|||
Task<List<HiveModel>> GetAllHives();
|
||||
Task<HiveOperationalArea> ConnectHive(HiveModel model);
|
||||
Task<DateTime> AddTelemetry(HiveTelemetryModel model);
|
||||
Task<string?> SendHiveControlSignal(string hiveId, Location destination);
|
||||
Task<string> SendHiveControlSignal(string hiveId, Location destination);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,6 @@ namespace DevOpsProject.CommunicationControl.Logic.Services.Interfaces
|
|||
{
|
||||
public interface ISpatialService
|
||||
{
|
||||
Task<HiveOperationalArea> GetHiveOperationalArea(HiveModel hiveModel);
|
||||
HiveOperationalArea GetHiveOperationalArea(HiveModel hiveModel);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,10 +11,10 @@ namespace DevOpsProject.CommunicationControl.Logic.Services
|
|||
private readonly IConnectionMultiplexer _connectionMultiplexer;
|
||||
private readonly RedisOptions _redisOptions;
|
||||
|
||||
public RedisPublishService(IConnectionMultiplexer connectionMultiplexer, IOptions<RedisOptions> redisOptions)
|
||||
public RedisPublishService(IConnectionMultiplexer connectionMultiplexer, IOptionsMonitor<RedisOptions> redisOptions)
|
||||
{
|
||||
_connectionMultiplexer = connectionMultiplexer;
|
||||
_redisOptions = redisOptions.Value;
|
||||
_redisOptions = redisOptions.CurrentValue;
|
||||
}
|
||||
|
||||
public async Task Publish<T>(T message)
|
||||
|
@ -22,7 +22,14 @@ namespace DevOpsProject.CommunicationControl.Logic.Services
|
|||
var pubsub = _connectionMultiplexer.GetSubscriber();
|
||||
var messageJson = JsonSerializer.Serialize(message);
|
||||
|
||||
if (_redisOptions.PublishChannel != null)
|
||||
{
|
||||
await pubsub.PublishAsync(_redisOptions.PublishChannel, messageJson);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Error while attempting to publish message to Message Bus, publish channel: {_redisOptions.PublishChannel}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace DevOpsProject.CommunicationControl.Logic.Services
|
|||
_operationalAreaConfig = operationalAreaConfig;
|
||||
}
|
||||
|
||||
public async Task<HiveOperationalArea> GetHiveOperationalArea(HiveModel hiveModel)
|
||||
public HiveOperationalArea GetHiveOperationalArea(HiveModel hiveModel)
|
||||
{
|
||||
var operationalArea = new HiveOperationalArea
|
||||
{
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Nullable>disable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
using Asp.Versioning;
|
||||
using DevOpsProject.HiveMind.Logic.Services.Interfaces;
|
||||
using DevOpsProject.Shared.Configuration;
|
||||
using DevOpsProject.Shared.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DevOpsProject.HiveMind.API.Controllers
|
||||
{
|
||||
[ApiVersion("1.0")]
|
||||
[ApiController]
|
||||
[Route("api/v{version:apiVersion}")]
|
||||
public class HiveMindController : Controller
|
||||
{
|
||||
private readonly IHiveMindService _hiveMindService;
|
||||
private readonly IHiveMindMovingService _hiveMindMovingService;
|
||||
|
||||
public HiveMindController(IHiveMindService hiveMindService, IHiveMindMovingService hiveMindMovingService)
|
||||
{
|
||||
_hiveMindService = hiveMindService;
|
||||
_hiveMindMovingService = hiveMindMovingService;
|
||||
}
|
||||
|
||||
[HttpGet("ping")]
|
||||
public IActionResult Ping(IOptionsSnapshot<HiveCommunicationConfig> config)
|
||||
{
|
||||
return Ok(new
|
||||
{
|
||||
Timestamp = DateTime.Now,
|
||||
ID = config.Value.HiveID
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost("connect")]
|
||||
public async Task<IActionResult> TriggerConnectHive()
|
||||
{
|
||||
await _hiveMindService.ConnectHive();
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("command")]
|
||||
public IActionResult MoveHideMind(MoveHiveMindCommand command)
|
||||
{
|
||||
_hiveMindMovingService.MoveToLocation(command.Location);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using DevOpsProject.HiveMind.Logic.Services;
|
||||
using DevOpsProject.HiveMind.Logic.Services.Interfaces;
|
||||
|
||||
namespace DevOpsProject.HiveMind.API.DI
|
||||
{
|
||||
public static class LogicConfiguration
|
||||
{
|
||||
public static IServiceCollection AddHiveMindLogic(this IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddTransient<IHiveMindService, HiveMindService>();
|
||||
serviceCollection.AddTransient<IHiveMindMovingService, HiveMindMovingService>();
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>disable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
|
||||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="9.0.2" />
|
||||
<PackageReference Include="Serilog" Version="4.2.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DevOpsProject.HiveMind.Logic\DevOpsProject.HiveMind.Logic.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ActiveDebugProfile>https</ActiveDebugProfile>
|
||||
<Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID>
|
||||
<Controller_SelectedScaffolderCategoryPath>root/Common/MVC/Controller</Controller_SelectedScaffolderCategoryPath>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -0,0 +1,6 @@
|
|||
@DevOpsProject.HiveMind.API_HostAddress = http://localhost:5149
|
||||
|
||||
GET {{DevOpsProject.HiveMind.API_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
|
@ -0,0 +1,35 @@
|
|||
using Microsoft.AspNetCore.Diagnostics;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DevOpsProject.HiveMind.API.Middleware
|
||||
{
|
||||
public class ExceptionHandlingMiddleware : IExceptionHandler
|
||||
{
|
||||
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
|
||||
private readonly IHostEnvironment _hostEnvironment;
|
||||
|
||||
public ExceptionHandlingMiddleware(ILogger<ExceptionHandlingMiddleware> logger, IHostEnvironment hostEnvironment)
|
||||
{
|
||||
_hostEnvironment = hostEnvironment;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogError(exception, "Unhandled exception occured: {Message}", exception.Message);
|
||||
|
||||
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||
httpContext.Response.ContentType = "application/json";
|
||||
|
||||
var errorResponse = new
|
||||
{
|
||||
Message = "Unexpected error occured",
|
||||
Detail = _hostEnvironment.IsDevelopment() ? exception.ToString() : null
|
||||
};
|
||||
|
||||
var jsonResponse = JsonSerializer.Serialize(errorResponse, new JsonSerializerOptions { WriteIndented = true });
|
||||
await httpContext.Response.WriteAsync(jsonResponse, cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
using Asp.Versioning;
|
||||
using DevOpsProject.HiveMind.API.DI;
|
||||
using DevOpsProject.HiveMind.API.Middleware;
|
||||
using DevOpsProject.Shared.Clients;
|
||||
using DevOpsProject.Shared.Configuration;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Polly;
|
||||
using Polly.Extensions.Http;
|
||||
using Serilog;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Host.UseSerilog((context, services, loggerConfig) =>
|
||||
loggerConfig.ReadFrom.Configuration(context.Configuration)
|
||||
.ReadFrom.Services(services)
|
||||
.Enrich.FromLogContext());
|
||||
|
||||
builder.Services.AddApiVersioning(options =>
|
||||
{
|
||||
options.DefaultApiVersion = new ApiVersion(1, 0);
|
||||
options.AssumeDefaultVersionWhenUnspecified = true;
|
||||
options.ReportApiVersions = true;
|
||||
options.ApiVersionReader = ApiVersionReader.Combine(
|
||||
new UrlSegmentApiVersionReader(),
|
||||
new HeaderApiVersionReader("X-Api-Version")
|
||||
);
|
||||
}).AddApiExplorer(options =>
|
||||
{
|
||||
options.GroupNameFormat = "'v'VVV";
|
||||
options.SubstituteApiVersionInUrl = true;
|
||||
});
|
||||
|
||||
// TODO: double check following approach
|
||||
builder.Services.AddControllers().AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.PropertyNamingPolicy = null;
|
||||
});
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "HiveMind - V1", Version = "v1.0" });
|
||||
});
|
||||
builder.Services.AddHiveMindLogic();
|
||||
|
||||
builder.Services.Configure<HiveCommunicationConfig>(builder.Configuration.GetSection("CommunicationConfiguration"));
|
||||
|
||||
var communicationControlRetryPolicy = HttpPolicyExtensions
|
||||
.HandleTransientHttpError()
|
||||
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
|
||||
builder.Services.AddHttpClient<HiveMindHttpClient>()
|
||||
.AddPolicyHandler(communicationControlRetryPolicy);
|
||||
|
||||
string corsPolicyName = "HiveMindCorsPolicy";
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(name: corsPolicyName,
|
||||
policy =>
|
||||
{
|
||||
policy.AllowAnyOrigin() //SECURITY WARNING ! Never allow all origins
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader();
|
||||
});
|
||||
});
|
||||
|
||||
builder.Services.AddExceptionHandler<ExceptionHandlingMiddleware>();
|
||||
builder.Services.AddProblemDetails();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseExceptionHandler();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseCors(corsPolicyName);
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:39179",
|
||||
"sslPort": 44372
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5149",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7167;http://localhost:5149",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Information",
|
||||
"System": "Information"
|
||||
}
|
||||
},
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "Console",
|
||||
"Args": {
|
||||
"restrictedToMinimumLevel": "Information"
|
||||
}
|
||||
},
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/log-.txt",
|
||||
"rollingInterval": "Day",
|
||||
"rollOnFileSizeLimit": true,
|
||||
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
|
||||
"restrictedToMinimumLevel": "Warning"
|
||||
}
|
||||
}
|
||||
],
|
||||
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
|
||||
"Properties": {
|
||||
"Application": "DevOpsProject.HiveMind",
|
||||
"Environment": "Development"
|
||||
}
|
||||
},
|
||||
"CommunicationConfiguration": {
|
||||
"RequestSchema": "http",
|
||||
"CommunicationControlIP": "localhost",
|
||||
"CommunicationControlPort": 8080,
|
||||
"CommunicationControlPath": "api/v1/hive",
|
||||
"HiveIP": "localhost",
|
||||
"HivePort": 5149,
|
||||
"HiveID": "1",
|
||||
"InitialLocation": {
|
||||
"Latitude": 48.719547,
|
||||
"Longitude": 38.092680
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DevOpsProject.Shared\DevOpsProject.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,101 @@
|
|||
using DevOpsProject.HiveMind.Logic.Services.Interfaces;
|
||||
using DevOpsProject.HiveMind.Logic.State;
|
||||
using DevOpsProject.Shared.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DevOpsProject.HiveMind.Logic.Services
|
||||
{
|
||||
public class HiveMindMovingService : IHiveMindMovingService
|
||||
{
|
||||
private readonly ILogger<HiveMindMovingService> _logger;
|
||||
private Timer _movementTimer;
|
||||
public HiveMindMovingService(ILogger<HiveMindMovingService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void MoveToLocation(Location destination)
|
||||
{
|
||||
lock (typeof(HiveInMemoryState))
|
||||
{
|
||||
if (HiveInMemoryState.OperationalArea == null || HiveInMemoryState.CurrentLocation == null)
|
||||
{
|
||||
_logger.LogWarning("Cannot start moving: OperationalArea or CurrentLocation is not set.");
|
||||
return;
|
||||
}
|
||||
|
||||
// If already moving - stop movement
|
||||
if (HiveInMemoryState.IsMoving)
|
||||
{
|
||||
StopMovement();
|
||||
}
|
||||
|
||||
HiveInMemoryState.Destination = destination;
|
||||
HiveInMemoryState.IsMoving = true;
|
||||
|
||||
_logger.LogInformation($"Received move command: Moving towards {destination}");
|
||||
|
||||
// Start the movement timer if not already running
|
||||
if (_movementTimer == null)
|
||||
{
|
||||
// TODO: Recalculating position each N seconds
|
||||
_movementTimer = new Timer(UpdateMovement, null, TimeSpan.Zero, TimeSpan.FromSeconds(3));
|
||||
_logger.LogInformation("Movement timer started.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateMovement(object state)
|
||||
{
|
||||
lock (typeof(HiveInMemoryState))
|
||||
{
|
||||
var currentLocation = HiveInMemoryState.CurrentLocation;
|
||||
var destination = HiveInMemoryState.Destination;
|
||||
|
||||
if (currentLocation == null || destination == null)
|
||||
{
|
||||
StopMovement();
|
||||
return;
|
||||
}
|
||||
|
||||
if (AreLocationsEqual(currentLocation.Value, destination.Value))
|
||||
{
|
||||
StopMovement();
|
||||
return;
|
||||
}
|
||||
|
||||
Location newLocation = CalculateNextPosition(currentLocation.Value, destination.Value, 0.1f);
|
||||
HiveInMemoryState.CurrentLocation = newLocation;
|
||||
|
||||
_logger.LogInformation($"Moved closer: {newLocation}");
|
||||
}
|
||||
}
|
||||
|
||||
private void StopMovement()
|
||||
{
|
||||
_movementTimer?.Dispose();
|
||||
_movementTimer = null;
|
||||
HiveInMemoryState.IsMoving = false;
|
||||
HiveInMemoryState.Destination = null;
|
||||
_logger.LogInformation("Movement stopped: Reached destination.");
|
||||
}
|
||||
|
||||
private static bool AreLocationsEqual(Location loc1, Location loc2)
|
||||
{
|
||||
const float tolerance = 0.000001f;
|
||||
return Math.Abs(loc1.Latitude - loc2.Latitude) < tolerance &&
|
||||
Math.Abs(loc1.Longitude - loc2.Longitude) < tolerance;
|
||||
}
|
||||
|
||||
private static Location CalculateNextPosition(Location current, Location destination, float stepSize)
|
||||
{
|
||||
float newLat = current.Latitude + (destination.Latitude - current.Latitude) * stepSize;
|
||||
float newLon = current.Longitude + (destination.Longitude - current.Longitude) * stepSize;
|
||||
return new Location
|
||||
{
|
||||
Latitude = newLat,
|
||||
Longitude = newLon
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
using DevOpsProject.HiveMind.Logic.Services.Interfaces;
|
||||
using DevOpsProject.Shared.Clients;
|
||||
using DevOpsProject.Shared.Models;
|
||||
using DevOpsProject.Shared.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Text.Json;
|
||||
using DevOpsProject.HiveMind.Logic.State;
|
||||
|
||||
namespace DevOpsProject.HiveMind.Logic.Services
|
||||
{
|
||||
public class HiveMindService : IHiveMindService
|
||||
{
|
||||
private readonly HiveMindHttpClient _httpClient;
|
||||
private readonly ILogger<HiveMindService> _logger;
|
||||
private readonly HiveCommunicationConfig _communicationConfigurationOptions;
|
||||
private Timer _telemetryTimer;
|
||||
|
||||
public HiveMindService(HiveMindHttpClient httpClient, ILogger<HiveMindService> logger, IOptionsSnapshot<HiveCommunicationConfig> communicationConfigurationOptions)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
_communicationConfigurationOptions = communicationConfigurationOptions.Value;
|
||||
}
|
||||
|
||||
public async Task ConnectHive()
|
||||
{
|
||||
var request = new HiveConnectRequest
|
||||
{
|
||||
HiveIP = _communicationConfigurationOptions.HiveIP,
|
||||
HivePort = _communicationConfigurationOptions.HivePort,
|
||||
HiveID = _communicationConfigurationOptions.HiveID
|
||||
};
|
||||
|
||||
var connectResult = await _httpClient.SendCommunicationControlConnectAsync(_communicationConfigurationOptions.RequestSchema,
|
||||
_communicationConfigurationOptions.CommunicationControlIP, _communicationConfigurationOptions.CommunicationControlPort,
|
||||
_communicationConfigurationOptions.CommunicationControlPath, request);
|
||||
|
||||
_logger.LogInformation($"Connect result for HiveID: {request.HiveID}: {connectResult}");
|
||||
|
||||
if (connectResult != null)
|
||||
{
|
||||
var hiveConnectResponse = JsonSerializer.Deserialize<HiveConnectResponse>(connectResult);
|
||||
|
||||
if (hiveConnectResponse != null && hiveConnectResponse.ConnectResult)
|
||||
{
|
||||
HiveInMemoryState.OperationalArea = hiveConnectResponse.OperationalArea;
|
||||
HiveInMemoryState.CurrentLocation = _communicationConfigurationOptions.InitialLocation;
|
||||
|
||||
// HERE - we are starting to send telemetry
|
||||
StartTelemetry();
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation($"Connecting hive failed for ID: {request.HiveID}");
|
||||
throw new Exception($"Failed to connect HiveID: {request.HiveID}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError($"Unable to connect Hive with ID: {request.HiveID}, Port: {request.HivePort}, IP: {request.HiveIP} to Communication Control. \n" +
|
||||
$"Requested IP: {_communicationConfigurationOptions.CommunicationControlIP}, Port: {_communicationConfigurationOptions.HivePort}");
|
||||
throw new Exception($"Failed to connect hive for HiveID: {request.HiveID}");
|
||||
}
|
||||
}
|
||||
|
||||
public void StopAllTelemetry()
|
||||
{
|
||||
StopTelemetry();
|
||||
}
|
||||
|
||||
#region private methods
|
||||
private void StartTelemetry()
|
||||
{
|
||||
if (HiveInMemoryState.IsTelemetryRunning) return;
|
||||
// TODO: Sending telemetry each N seconds
|
||||
_telemetryTimer = new Timer(SendTelemetry, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
|
||||
|
||||
_logger.LogInformation("Telemetry timer started.");
|
||||
}
|
||||
|
||||
private void StopTelemetry()
|
||||
{
|
||||
_telemetryTimer?.Dispose();
|
||||
HiveInMemoryState.IsTelemetryRunning = false;
|
||||
|
||||
_logger.LogInformation("Telemetry timer stopped.");
|
||||
}
|
||||
|
||||
private async void SendTelemetry(object state)
|
||||
{
|
||||
var currentLocation = HiveInMemoryState.CurrentLocation;
|
||||
|
||||
try
|
||||
{
|
||||
var request = new HiveTelemetryRequest
|
||||
{
|
||||
HiveID = _communicationConfigurationOptions.HiveID,
|
||||
Location = HiveInMemoryState.CurrentLocation ?? default,
|
||||
// TODO: MOCKED FOR NOW
|
||||
Height = 5,
|
||||
Speed = 15,
|
||||
State = Shared.Enums.State.Move
|
||||
};
|
||||
|
||||
var connectResult = await _httpClient.SendCommunicationControlTelemetryAsync(_communicationConfigurationOptions.RequestSchema,
|
||||
_communicationConfigurationOptions.CommunicationControlIP, _communicationConfigurationOptions.CommunicationControlPort,
|
||||
_communicationConfigurationOptions.CommunicationControlPath, request);
|
||||
|
||||
_logger.LogInformation($"Telemetry sent for HiveID: {request.HiveID}: {connectResult}");
|
||||
|
||||
if (connectResult != null)
|
||||
{
|
||||
// TODO: Store timestamp
|
||||
var hiveConnectResponse = JsonSerializer.Deserialize<HiveTelemetryResponse>(connectResult);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError($"Unable to send Hive telemetry for HiveID: {request.HiveID}.");
|
||||
throw new Exception($"Failed to send telemetry for HiveID: {request.HiveID}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Error sending telemetry: {Message}", ex.Message);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
using DevOpsProject.Shared.Models;
|
||||
|
||||
namespace DevOpsProject.HiveMind.Logic.Services.Interfaces
|
||||
{
|
||||
public interface IHiveMindMovingService
|
||||
{
|
||||
void MoveToLocation(Location destination);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
using DevOpsProject.Shared.Models;
|
||||
|
||||
namespace DevOpsProject.HiveMind.Logic.Services.Interfaces
|
||||
{
|
||||
public interface IHiveMindService
|
||||
{
|
||||
Task ConnectHive();
|
||||
void StopAllTelemetry();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
using DevOpsProject.Shared.Models;
|
||||
|
||||
namespace DevOpsProject.HiveMind.Logic.State
|
||||
{
|
||||
public static class HiveInMemoryState
|
||||
{
|
||||
private static readonly object _operationalAreaLock = new();
|
||||
private static readonly object _telemetryLock = new();
|
||||
private static readonly object _movementLock = new();
|
||||
|
||||
private static HiveOperationalArea _operationalArea;
|
||||
|
||||
private static bool _isTelemetryRunning;
|
||||
private static bool _isMoving;
|
||||
|
||||
private static Location? _currentLocation;
|
||||
private static Location? _destination;
|
||||
|
||||
|
||||
public static HiveOperationalArea OperationalArea
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_operationalAreaLock)
|
||||
{
|
||||
return _operationalArea;
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
lock (_operationalAreaLock)
|
||||
{
|
||||
_operationalArea = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsTelemetryRunning
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_telemetryLock)
|
||||
{
|
||||
return _isTelemetryRunning;
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
lock (_telemetryLock)
|
||||
{
|
||||
_isTelemetryRunning = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsMoving
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_movementLock)
|
||||
{
|
||||
return _isMoving;
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
lock (_movementLock)
|
||||
{
|
||||
_isMoving = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Location? CurrentLocation
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_movementLock) { return _currentLocation; }
|
||||
}
|
||||
set
|
||||
{
|
||||
lock (_movementLock) { _currentLocation = value; }
|
||||
}
|
||||
}
|
||||
|
||||
public static Location? Destination
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_movementLock) { return _destination; }
|
||||
}
|
||||
set
|
||||
{
|
||||
lock (_movementLock) { _destination = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,28 +1,29 @@
|
|||
using System.Text;
|
||||
using DevOpsProject.Shared.Models;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DevOpsProject.Shared.Clients
|
||||
{
|
||||
public class HiveHttpClient
|
||||
public class CommunicationControlHttpClient
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public HiveHttpClient(HttpClient httpClient)
|
||||
public CommunicationControlHttpClient(HttpClient httpClient)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task<string?> SendHiveControlCommandAsync(string scheme, string ip, int port, object payload)
|
||||
public async Task<string> SendHiveControlCommandAsync(string scheme, string ip, int port, string path, MoveHiveMindCommand command)
|
||||
{
|
||||
var uriBuilder = new UriBuilder
|
||||
{
|
||||
Scheme = scheme,
|
||||
Host = ip,
|
||||
Port = port,
|
||||
Path = "api/control"
|
||||
Path = $"{path}/command"
|
||||
};
|
||||
|
||||
var jsonContent = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
|
||||
var jsonContent = new StringContent(JsonSerializer.Serialize(command), Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await _httpClient.PostAsync(uriBuilder.Uri, jsonContent);
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
using DevOpsProject.Shared.Models;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DevOpsProject.Shared.Clients
|
||||
{
|
||||
public class HiveMindHttpClient
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public HiveMindHttpClient(HttpClient httpClient)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task<string> SendCommunicationControlConnectAsync(string requestSchema, string ip, int port, string path, HiveConnectRequest payload)
|
||||
{
|
||||
var uriBuilder = new UriBuilder
|
||||
{
|
||||
Scheme = requestSchema,
|
||||
Host = ip,
|
||||
Port = port,
|
||||
Path = $"{path}/connect"
|
||||
};
|
||||
|
||||
var jsonContent = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await _httpClient.PostAsync(uriBuilder.Uri, jsonContent);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<string> SendCommunicationControlTelemetryAsync(string requestSchema, string ip, int port, string path, HiveTelemetryRequest payload)
|
||||
{
|
||||
var uriBuilder = new UriBuilder
|
||||
{
|
||||
Scheme = requestSchema,
|
||||
Host = ip,
|
||||
Port = port,
|
||||
Path = $"{path}/telemetry"
|
||||
};
|
||||
|
||||
var jsonContent = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await _httpClient.PostAsync(uriBuilder.Uri, jsonContent);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace DevOpsProject.Shared.Configuration
|
||||
{
|
||||
public class ComControlCommunicationConfiguration
|
||||
{
|
||||
public string RequestScheme { get; set; }
|
||||
public string HiveMindPath { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using DevOpsProject.Shared.Models;
|
||||
|
||||
namespace DevOpsProject.Shared.Configuration
|
||||
{
|
||||
public class HiveCommunicationConfig
|
||||
{
|
||||
public string RequestSchema { get; set; }
|
||||
public string CommunicationControlIP { get; set; }
|
||||
public int CommunicationControlPort { get; set; }
|
||||
public string CommunicationControlPath { get; set; }
|
||||
public string HiveIP { get; set; }
|
||||
public int HivePort { get; set; }
|
||||
public string HiveID { get; set; }
|
||||
public Location InitialLocation { get; set; }
|
||||
}
|
||||
}
|
|
@ -3,7 +3,7 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Nullable>disable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,4 +1,10 @@
|
|||
namespace DevOpsProject.CommunicationControl.API.DTO.Hive.Request
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DevOpsProject.Shared.Models
|
||||
{
|
||||
public class HiveConnectRequest
|
||||
{
|
|
@ -0,0 +1,10 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace DevOpsProject.Shared.Models
|
||||
{
|
||||
public class HiveConnectResponse
|
||||
{
|
||||
public bool ConnectResult { get; set; }
|
||||
public HiveOperationalArea OperationalArea { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
using DevOpsProject.Shared.Enums;
|
||||
using DevOpsProject.Shared.Models;
|
||||
|
||||
namespace DevOpsProject.CommunicationControl.API.DTO.Hive.Request
|
||||
namespace DevOpsProject.Shared.Models
|
||||
{
|
||||
public class HiveTelemetryRequest
|
||||
{
|
|
@ -1,4 +1,6 @@
|
|||
namespace DevOpsProject.CommunicationControl.API.DTO.Hive.Response
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace DevOpsProject.Shared.Models
|
||||
{
|
||||
public class HiveTelemetryResponse
|
||||
{
|
|
@ -0,0 +1,12 @@
|
|||
using DevOpsProject.Shared.Enums;
|
||||
|
||||
namespace DevOpsProject.Shared.Models
|
||||
{
|
||||
public class MoveHiveMindCommand
|
||||
{
|
||||
public State CommandType { get; set; }
|
||||
// TODO: CLARIFY CommandPayload
|
||||
public Location Location { get; set; }
|
||||
public DateTime Timestamp { get; set; }
|
||||
}
|
||||
}
|
|
@ -11,6 +11,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevOpsProject.Shared", "Dev
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevOpsProject.Example.MessageListener", "DevOpsProject.Example.MessageListener\DevOpsProject.Example.MessageListener.csproj", "{CBB302CE-D22A-4DA0-8811-E4F8FDFC1C4B}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevOpsProject.HiveMind.API", "DevOpsProject.HiveMind.API\DevOpsProject.HiveMind.API.csproj", "{65F5C602-86C3-4653-B3B4-326FC2D01B8D}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevOpsProject.HiveMind.Logic", "DevOpsProject.HiveMind.Logic\DevOpsProject.HiveMind.Logic.csproj", "{83D3A1EA-64FE-407C-AB3A-A35CC2C9F8EE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -33,6 +37,14 @@ Global
|
|||
{CBB302CE-D22A-4DA0-8811-E4F8FDFC1C4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CBB302CE-D22A-4DA0-8811-E4F8FDFC1C4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CBB302CE-D22A-4DA0-8811-E4F8FDFC1C4B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{65F5C602-86C3-4653-B3B4-326FC2D01B8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{65F5C602-86C3-4653-B3B4-326FC2D01B8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{65F5C602-86C3-4653-B3B4-326FC2D01B8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{65F5C602-86C3-4653-B3B4-326FC2D01B8D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{83D3A1EA-64FE-407C-AB3A-A35CC2C9F8EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{83D3A1EA-64FE-407C-AB3A-A35CC2C9F8EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{83D3A1EA-64FE-407C-AB3A-A35CC2C9F8EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{83D3A1EA-64FE-407C-AB3A-A35CC2C9F8EE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
[
|
||||
{
|
||||
"Name": "New Profile",
|
||||
"Projects": [
|
||||
{
|
||||
"Path": "DevOpsProject\\DevOpsProject.CommunicationControl.API.csproj",
|
||||
"Action": "Start"
|
||||
},
|
||||
{
|
||||
"Path": "DevOpsProject.Example.MessageListener\\DevOpsProject.Example.MessageListener.csproj",
|
||||
"Action": "Start"
|
||||
},
|
||||
{
|
||||
"Path": "DevOpsProject.HiveMind.API\\DevOpsProject.HiveMind.API.csproj",
|
||||
"Action": "Start"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
|
@ -1,4 +1,5 @@
|
|||
using DevOpsProject.CommunicationControl.API.DTO.Client.Request;
|
||||
using Asp.Versioning;
|
||||
using DevOpsProject.CommunicationControl.API.DTO.Client.Request;
|
||||
using DevOpsProject.CommunicationControl.Logic.Services.Interfaces;
|
||||
using DevOpsProject.Shared.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
@ -6,22 +7,24 @@ using Microsoft.Extensions.Options;
|
|||
|
||||
namespace DevOpsProject.CommunicationControl.API.Controllers
|
||||
{
|
||||
|
||||
[ApiVersion("1.0")]
|
||||
[ApiController]
|
||||
[Route("api/client")]
|
||||
[Route("api/v{version:apiVersion}/client")]
|
||||
public class ClientController : Controller
|
||||
{
|
||||
private readonly ICommunicationControlService _communicationControlService;
|
||||
private readonly IOptionsMonitor<OperationalAreaConfig> _operationalAreaConfig;
|
||||
private readonly ILogger<ClientController> _logger;
|
||||
|
||||
public ClientController(ICommunicationControlService communicationControlService, IOptionsMonitor<OperationalAreaConfig> operationalAreaConfig)
|
||||
public ClientController(ICommunicationControlService communicationControlService, IOptionsMonitor<OperationalAreaConfig> operationalAreaConfig, ILogger<ClientController> logger)
|
||||
{
|
||||
_communicationControlService = communicationControlService;
|
||||
_operationalAreaConfig = operationalAreaConfig;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet("area")]
|
||||
public async Task<IActionResult> GetOperationalArea()
|
||||
public IActionResult GetOperationalArea()
|
||||
{
|
||||
return Ok(_operationalAreaConfig.CurrentValue);
|
||||
}
|
||||
|
@ -51,15 +54,26 @@ namespace DevOpsProject.CommunicationControl.API.Controllers
|
|||
}
|
||||
|
||||
[HttpPatch("hive")]
|
||||
public async Task<IActionResult> SendBulkHiveMovingSignal(MoveHivesRequest request)
|
||||
public IActionResult SendBulkHiveMovingSignal(MoveHivesRequest request)
|
||||
{
|
||||
if (request?.Hives == null || !request.Hives.Any())
|
||||
return BadRequest("No hive IDs provided.");
|
||||
|
||||
foreach (var id in request.Hives)
|
||||
{
|
||||
Task.Run(async () => await _communicationControlService.SendHiveControlSignal(id, request.Destination));
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await _communicationControlService.SendHiveControlSignal(id, request.Destination);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"Failed to send control signal for HiveID: {id}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return Accepted("Hives are being moved asynchronously.");
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
using DevOpsProject.CommunicationControl.API.DTO.Hive.Request;
|
||||
using DevOpsProject.CommunicationControl.API.DTO.Hive.Response;
|
||||
using Asp.Versioning;
|
||||
using DevOpsProject.CommunicationControl.Logic.Services.Interfaces;
|
||||
using DevOpsProject.Shared.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DevOpsProject.CommunicationControl.API.Controllers
|
||||
{
|
||||
[ApiVersion("1.0")]
|
||||
[ApiController]
|
||||
[Route("api/hive")]
|
||||
[Route("api/v{version:apiVersion}/hive")]
|
||||
public class HiveController : Controller
|
||||
{
|
||||
private readonly ICommunicationControlService _communicationControlService;
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
using DevOpsProject.Shared.Models;
|
||||
|
||||
namespace DevOpsProject.CommunicationControl.API.DTO.Hive.Response
|
||||
{
|
||||
public class HiveConnectResponse
|
||||
{
|
||||
public bool ConnectResult { get; set; }
|
||||
public HiveOperationalArea OperationalArea { get;set; }
|
||||
}
|
||||
}
|
|
@ -2,11 +2,20 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<Nullable>disable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="DTO\Client\Response\**" />
|
||||
<Content Remove="DTO\Client\Response\**" />
|
||||
<EmbeddedResource Remove="DTO\Client\Response\**" />
|
||||
<None Remove="DTO\Client\Response\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
|
||||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="9.0.1" />
|
||||
<PackageReference Include="Serilog" Version="4.2.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||
|
@ -22,8 +31,4 @@
|
|||
<ProjectReference Include="..\DevOpsProject.Shared\DevOpsProject.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="DTO\Client\Response\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
using Asp.Versioning;
|
||||
using DevOpsProject.CommunicationControl.API.DI;
|
||||
using DevOpsProject.CommunicationControl.API.Middleware;
|
||||
using DevOpsProject.Shared.Clients;
|
||||
using DevOpsProject.Shared.Configuration;
|
||||
using DevOpsProject.Shared.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Polly;
|
||||
using Polly.Extensions.Http;
|
||||
using Serilog;
|
||||
|
@ -18,10 +21,31 @@ internal class Program
|
|||
.ReadFrom.Services(services)
|
||||
.Enrich.FromLogContext());
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddApiVersioning(options =>
|
||||
{
|
||||
options.DefaultApiVersion = new ApiVersion(1, 0);
|
||||
options.AssumeDefaultVersionWhenUnspecified = true;
|
||||
options.ReportApiVersions = true;
|
||||
options.ApiVersionReader = ApiVersionReader.Combine(
|
||||
new UrlSegmentApiVersionReader(),
|
||||
new HeaderApiVersionReader("X-Api-Version")
|
||||
);
|
||||
}).AddApiExplorer(options =>
|
||||
{
|
||||
options.GroupNameFormat = "'v'VVV";
|
||||
options.SubstituteApiVersionInUrl = true;
|
||||
});
|
||||
|
||||
// TODO: consider this approach
|
||||
builder.Services.AddControllers().AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.PropertyNamingPolicy = null;
|
||||
}); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "CommunicationControl - V1", Version = "v1.0" });
|
||||
});
|
||||
|
||||
// TODO: LATER - ADD OpenTelemtry
|
||||
|
||||
|
@ -29,13 +53,12 @@ internal class Program
|
|||
builder.Services.AddCommunicationControlLogic();
|
||||
|
||||
builder.Services.Configure<OperationalAreaConfig>(builder.Configuration.GetSection("OperationalArea"));
|
||||
builder.Services.AddSingleton<IOptionsMonitor<OperationalAreaConfig>, OptionsMonitor<OperationalAreaConfig>>();
|
||||
|
||||
builder.Services.Configure<ComControlCommunicationConfiguration>(builder.Configuration.GetSection("CommunicationConfiguration"));
|
||||
|
||||
var hiveRetryPolicy = HttpPolicyExtensions
|
||||
.HandleTransientHttpError()
|
||||
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
|
||||
builder.Services.AddHttpClient<HiveHttpClient>()
|
||||
builder.Services.AddHttpClient<CommunicationControlHttpClient>()
|
||||
.AddPolicyHandler(hiveRetryPolicy);
|
||||
|
||||
|
||||
|
|
|
@ -10,7 +10,10 @@
|
|||
},
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "Console"
|
||||
"Name": "Console",
|
||||
"Args": {
|
||||
"restrictedToMinimumLevel": "Information"
|
||||
}
|
||||
},
|
||||
{
|
||||
"Name": "File",
|
||||
|
@ -18,7 +21,8 @@
|
|||
"path": "Logs/log-.txt",
|
||||
"rollingInterval": "Day",
|
||||
"rollOnFileSizeLimit": true,
|
||||
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
|
||||
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
|
||||
"restrictedToMinimumLevel": "Warning"
|
||||
}
|
||||
}
|
||||
],
|
||||
|
@ -45,6 +49,10 @@
|
|||
"PingInterval_MS": 15000
|
||||
|
||||
},
|
||||
"CommunicationConfiguration": {
|
||||
"RequestScheme": "http",
|
||||
"HiveMindPath": "api/v1"
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Urls": "http://0.0.0.0:8080"
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
VITE_API_BASE_URL=http://localhost:8080/api/v1/client
|
|
@ -1,6 +1,6 @@
|
|||
import axios from "axios";
|
||||
|
||||
const API_BASE_URL = "http://localhost:8080/api/client";
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL;
|
||||
|
||||
// Fetch the center coordinates for the initial map load
|
||||
export const fetchCenterCoordinates = async () => {
|
||||
|
@ -19,9 +19,9 @@ export const fetchHives = async () => {
|
|||
const response = await axios.get(`${API_BASE_URL}/hive`);
|
||||
|
||||
return response.data.map(hive => ({
|
||||
id: hive.hiveID,
|
||||
lat: hive.telemetry?.location?.latitude ?? null,
|
||||
lon: hive.telemetry?.location?.longitude ?? null,
|
||||
id: hive.HiveID,
|
||||
lat: hive.Telemetry?.Location?.Latitude ?? null,
|
||||
lon: hive.Telemetry?.Location?.Longitude ?? null,
|
||||
})).filter(hive => hive.lat !== null && hive.lon !== null); // Remove invalid locations
|
||||
|
||||
} catch (error) {
|
||||
|
|
|
@ -12,6 +12,8 @@ import { Style, Icon, Text, Fill, Stroke } from "ol/style";
|
|||
import Popup from "./Popup";
|
||||
import { fetchCenterCoordinates, fetchHives, moveHives } from "../api/mapService";
|
||||
|
||||
// TEST STAGING
|
||||
|
||||
// TODO: Hardcoded marker icon path
|
||||
const MARKER_ICON_URL = "/256x256.png";
|
||||
|
||||
|
@ -32,7 +34,7 @@ const MapView = () => {
|
|||
try {
|
||||
const center = await fetchCenterCoordinates();
|
||||
if (center) {
|
||||
initMap(center.latitude, center.longitude);
|
||||
initMap(center.Latitude, center.Longitude);
|
||||
await fetchAndDrawHives();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue