-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor updates into separate services
- Loading branch information
Showing
4 changed files
with
78 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
backend/api/Services/ActionServices/BatteryTimeseriesService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
backend/api/Services/ActionServices/PressureTimeseriesService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |