Skip to content

Commit

Permalink
Refactor updates into separate services
Browse files Browse the repository at this point in the history
  • Loading branch information
aeshub committed Jan 8, 2024
1 parent e172318 commit 509e416
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 40 deletions.
43 changes: 4 additions & 39 deletions backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,54 +330,19 @@ private async void OnStepUpdate(object? sender, MqttReceivedArgs mqttArgs)
private async void OnBatteryUpdate(object? sender, MqttReceivedArgs mqttArgs)
{
var provider = GetServiceProvider();
var robotService = provider.GetRequiredService<IRobotService>();
var timeseriesService = provider.GetRequiredService<ITimeseriesService>();
var batteryTimeseriesService = provider.GetRequiredService<IBatteryTimeseriesService>();

var batteryStatus = (IsarBatteryMessage)mqttArgs.Message;

var robot = await robotService.ReadByIsarId(batteryStatus.IsarId);
if (robot == null)
{
_logger.LogWarning(
"Could not find corresponding robot for battery update on robot '{RobotName}' with ISAR id '{IsarId}'", batteryStatus.RobotName, batteryStatus.IsarId);
}
else
{
await robotService.UpdateRobotBatteryLevel(robot.Id, batteryStatus.BatteryLevel);

try { await timeseriesService.AddBatteryEntry(robot.CurrentMissionId!, batteryStatus.BatteryLevel, robot.Id); }
catch (NpgsqlException e)
{
_logger.LogError(e, "An error occurred while connecting to the timeseries database");
return;
}

_logger.LogDebug("Updated battery on robot '{RobotName}' with ISAR id '{IsarId}'", robot.Name, robot.IsarId);
}
await batteryTimeseriesService.AddBatteryEntry(batteryStatus.BatteryLevel, batteryStatus.IsarId);
}

private async void OnPressureUpdate(object? sender, MqttReceivedArgs mqttArgs)
{
var provider = GetServiceProvider();
var robotService = provider.GetRequiredService<IRobotService>();
var timeseriesService = provider.GetRequiredService<ITimeseriesService>();
var pressureTimeseriesService = provider.GetRequiredService<IPressureTimeseriesService>();

var pressureStatus = (IsarPressureMessage)mqttArgs.Message;

var robot = await robotService.ReadByIsarId(pressureStatus.IsarId);
if (robot == null)
{
_logger.LogWarning(
"Could not find corresponding robot for pressure update on robot '{RobotName}' with ISAR id '{IsarId}'", pressureStatus.RobotName, pressureStatus.IsarId);
}
else
{
if (pressureStatus.PressureLevel == robot.PressureLevel) return;

await robotService.UpdateRobotPressureLevel(robot.Id, pressureStatus.PressureLevel);
await timeseriesService.AddPressureEntry(robot.CurrentMissionId!, pressureStatus.PressureLevel, robot.Id);
_logger.LogDebug("Updated pressure on '{RobotName}' with ISAR id '{IsarId}'", robot.Name, robot.IsarId);
}
await pressureTimeseriesService.AddPressureEntry(pressureStatus.PressureLevel, pressureStatus.IsarId);
}

private async void OnPoseUpdate(object? sender, MqttReceivedArgs mqttArgs)
Expand Down
5 changes: 4 additions & 1 deletion backend/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@
builder.Services.AddScoped<IMissionSchedulingService, MissionSchedulingService>();
builder.Services.AddScoped<ICustomMissionSchedulingService, CustomMissionSchedulingService>();
builder.Services.AddScoped<ITaskDurationService, TaskDurationService>();
builder.Services.AddScoped<IPoseTimeseriesService, PoseTimeseriesService>();
builder.Services.AddScoped<ILastMissionRunService, LastMissionRunService>();

builder.Services.AddScoped<IBatteryTimeseriesService, BatteryTimeseriesService>();
builder.Services.AddScoped<IPressureTimeseriesService, PressureTimeseriesService>();
builder.Services.AddScoped<IPoseTimeseriesService, PoseTimeseriesService>();

bool useInMemoryDatabase = builder.Configuration
.GetSection("Database")
.GetValue<bool>("UseInMemoryDatabase");
Expand Down
34 changes: 34 additions & 0 deletions backend/api/Services/ActionServices/BatteryTimeseriesService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Npgsql;

namespace Api.Services.ActionServices
{
public interface IBatteryTimeseriesService
{
public Task AddBatteryEntry(float batteryLevel, string isarId);
}

public class BatteryTimeseriesService(ILogger<BatteryTimeseriesService> logger, IRobotService robotService, ITimeseriesService timeseriesService) : IBatteryTimeseriesService
{
private const double Tolerance = 1E-05D;

public async Task AddBatteryEntry(float batteryLevel, string isarId)
{
var robot = await robotService.ReadByIsarId(isarId);
if (robot == null)
{
logger.LogWarning("Could not find corresponding robot for battery update on robot with ISAR id'{IsarId}'", isarId);
return;
}

if (Math.Abs(batteryLevel - robot.BatteryLevel) > Tolerance) await robotService.UpdateRobotBatteryLevel(robot.Id, batteryLevel);

try { await timeseriesService.AddBatteryEntry(robot.CurrentMissionId!, batteryLevel, robot.Id); }
catch (NpgsqlException e)
{
logger.LogError(e, "An error occurred while connecting to the timeseries database");
return;
}
logger.LogDebug("Updated battery on robot '{RobotName}' with ISAR id '{IsarId}'", robot.Name, robot.IsarId);
}
}
}
36 changes: 36 additions & 0 deletions backend/api/Services/ActionServices/PressureTimeseriesService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Npgsql;

namespace Api.Services.ActionServices
{
public interface IPressureTimeseriesService
{
public Task AddPressureEntry(float pressureLevel, string isarId);
}

public class PressureTimeseriesService(ILogger<PressureTimeseriesService> logger, IRobotService robotService, ITimeseriesService timeseriesService) : IPressureTimeseriesService
{
private const double Tolerance = 1E-05D;

public async Task AddPressureEntry(float pressureLevel, string isarId)
{
var robot = await robotService.ReadByIsarId(isarId);
if (robot == null)
{
logger.LogWarning("Could not find corresponding robot for pressure update on robot with ISAR id'{IsarId}'", isarId);
return;
}

if (robot.PressureLevel is null) return;

if (Math.Abs(pressureLevel - (float)robot.PressureLevel) > Tolerance) await robotService.UpdateRobotPressureLevel(robot.Id, pressureLevel);

try { await timeseriesService.AddPressureEntry(robot.CurrentMissionId!, pressureLevel, robot.Id); }
catch (NpgsqlException e)
{
logger.LogError(e, "An error occurred while connecting to the timeseries database");
return;
}
logger.LogDebug("Updated pressure on robot '{RobotName}' with ISAR id '{IsarId}'", robot.Name, robot.IsarId);
}
}
}

0 comments on commit 509e416

Please sign in to comment.