Skip to content

Commit

Permalink
Make emergency endpoints async
Browse files Browse the repository at this point in the history
  • Loading branch information
aeshub committed Nov 14, 2023
1 parent 6fa5c61 commit 99b0018
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
15 changes: 5 additions & 10 deletions backend/api/Controllers/EmergencyActionController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Globalization;
using Api.Controllers.Models;
using Api.Controllers.Models;
using Api.Services;
using Api.Services.Events;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -36,13 +35,11 @@ public EmergencyActionController(IRobotService robotService, IEmergencyActionSer
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public ActionResult<string> AbortCurrentMissionAndSendAllRobotsToSafeZone(
public async Task<ActionResult<string>> AbortCurrentMissionAndSendAllRobotsToSafeZone(
[FromRoute] string installationCode)
{

var robots = _robotService.ReadAll().Result.ToList().FindAll(a =>
a.CurrentInstallation.ToLower(CultureInfo.CurrentCulture).Equals(installationCode.ToLower(CultureInfo.CurrentCulture), StringComparison.Ordinal) &&
a.CurrentArea != null);
var robots = await _robotService.ReadLocalizedRobotsForInstallation(installationCode);

foreach (var robot in robots)
{
Expand All @@ -67,12 +64,10 @@ public ActionResult<string> AbortCurrentMissionAndSendAllRobotsToSafeZone(
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public ActionResult<string> ClearEmergencyStateForAllRobots(
public async Task<ActionResult<string>> ClearEmergencyStateForAllRobots(
[FromRoute] string installationCode)
{
var robots = _robotService.ReadAll().Result.ToList().FindAll(a =>
a.CurrentInstallation.ToLower(CultureInfo.CurrentCulture).Equals(installationCode.ToLower(CultureInfo.CurrentCulture), StringComparison.Ordinal) &&
a.CurrentArea != null);
var robots = await _robotService.ReadLocalizedRobotsForInstallation(installationCode);

foreach (var robot in robots)
{
Expand Down
21 changes: 16 additions & 5 deletions backend/api/Services/RobotService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Api.Controllers.Models;
using Api.Database.Context;
using Api.Database.Models;
Expand All @@ -14,6 +15,7 @@ public interface IRobotService
public Task<IEnumerable<string>> ReadAllActivePlants();
public Task<Robot?> ReadById(string id);
public Task<Robot?> ReadByIsarId(string isarId);
public Task<IList<Robot>> ReadLocalizedRobotsForInstallation(string installationCode);
public Task<Robot> Update(Robot robot);
public Task<Robot> UpdateRobotStatus(string robotId, RobotStatus status);
public Task<Robot> UpdateRobotBatteryLevel(string robotId, float batteryLevel);
Expand Down Expand Up @@ -48,6 +50,11 @@ public RobotService(FlotillaDbContext context, ILogger<RobotService> logger, IRo
_signalRService = signalRService;
}

public void Dispose()
{
_robotSemaphore.Dispose();
}

public async Task<Robot> Create(Robot newRobot)
{
if (newRobot.CurrentArea is not null) { _context.Entry(newRobot.CurrentArea).State = EntityState.Unchanged; }
Expand Down Expand Up @@ -127,6 +134,15 @@ public async Task<Robot> Update(Robot robot)
return robot;
}

public async Task<IList<Robot>> ReadLocalizedRobotsForInstallation(string installationCode)
{
return await GetRobotsWithSubModels()
.Where(robot =>
robot.CurrentInstallation.Equals(installationCode)
&& robot.CurrentArea != null)
.ToListAsync();
}

private async Task<Robot> UpdateRobotProperty(string robotId, string propertyName, object? value)
{
_robotSemaphore.WaitOne();
Expand Down Expand Up @@ -162,10 +178,5 @@ private IQueryable<Robot> GetEnabledRobotsWithSubModels()
{
return GetRobotsWithSubModels().Where(r => r.Enabled && r.Status != RobotStatus.Deprecated);
}

public void Dispose()
{
_robotSemaphore.Dispose();
}
}
}

0 comments on commit 99b0018

Please sign in to comment.