Skip to content

Commit

Permalink
Only schedule return home if supported by robot
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Jul 24, 2024
1 parent 3b2af29 commit 5ab0e57
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
3 changes: 2 additions & 1 deletion backend/api.test/Database/DatabaseUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public async Task<Robot> NewRobot(RobotStatus status, Installation installation,
VideoStreams = new List<CreateVideoStreamQuery>(),
Host = "localhost",
Port = 3000,
Status = status
Status = status,
RobotCapabilities = [RobotCapabilitiesEnum.drive_to_pose, RobotCapabilitiesEnum.take_image, RobotCapabilitiesEnum.return_to_home, RobotCapabilitiesEnum.localize]
};

var robotModel = await _robotModelService.ReadByRobotType(createRobotQuery.RobotType);
Expand Down
37 changes: 37 additions & 0 deletions backend/api.test/EventHandlers/TestMissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,43 @@ public async Task ReturnToHomeMissionIsStartedIfQueueIsEmptyWhenRobotBecomesAvai
Assert.True(ongoingMission.Any());
}

[Fact]
public async Task ReturnToHomeMissionIsNotStartedIfReturnToHomeIsNotSupported()
{
// Arrange
var installation = await _databaseUtilities.NewInstallation();
var plant = await _databaseUtilities.NewPlant(installation.InstallationCode);
var deck = await _databaseUtilities.NewDeck(installation.InstallationCode, plant.PlantCode);
var area = await _databaseUtilities.NewArea(installation.InstallationCode, plant.PlantCode, deck.Name);
var robot = await _databaseUtilities.NewRobot(RobotStatus.Busy, installation, area);
robot.RobotCapabilities!.Remove(RobotCapabilitiesEnum.return_to_home);

var mqttEventArgs = new MqttReceivedArgs(
new IsarStatusMessage
{
RobotName = robot.Name,
IsarId = robot.IsarId,
Status = RobotStatus.Available,
Timestamp = DateTime.UtcNow
});

// Act
_mqttService.RaiseEvent(nameof(MqttService.MqttIsarStatusReceived), mqttEventArgs);

// Assert
Thread.Sleep(1000);
var ongoingMission = await _missionRunService.ReadAll(
new MissionRunQueryStringParameters
{
Statuses = [
MissionStatus.Ongoing
],
OrderBy = "DesiredStartTime",
PageSize = 100
});
Assert.False(ongoingMission.Any());
}

[Fact]
public async Task MissionRunIsStartedForOtherAvailableRobotIfOneRobotHasAnOngoingMissionRun()
{
Expand Down
39 changes: 26 additions & 13 deletions backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,37 @@ public async Task StartNextMissionRunIfSystemIsAvailable(string robotId)
return;
}

try { missionRun = await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(robot.Id); }
catch (ReturnToHomeMissionFailedToScheduleException)
if (robot.RobotCapabilities == null || !robot.RobotCapabilities.Contains(RobotCapabilitiesEnum.return_to_home))
{
signalRService.ReportGeneralFailToSignalR(robot, $"Failed to schedule return to home for robot {robot.Name}", "");
logger.LogError("Failed to schedule a return to home mission for robot {RobotId}", robot.Id);
await robotService.UpdateCurrentArea(robot.Id, null);
return;
}

if (missionRun == null) { return; } // The robot is already home

var postReturnToHomeMissionCreatedRobot = await robotService.ReadById(missionRun.Robot.Id);
if (postReturnToHomeMissionCreatedRobot == null)
else
{
logger.LogInformation("Could not find robot {Name}", missionRun.Robot.Name);
return;
try { missionRun = await returnToHomeService.ScheduleReturnToHomeMissionRunIfNotAlreadyScheduledOrRobotIsHome(robot.Id); }
catch (ReturnToHomeMissionFailedToScheduleException)
{
signalRService.ReportGeneralFailToSignalR(robot, $"Failed to schedule return to home for robot {robot.Name}", "");
logger.LogError("Failed to schedule a return to home mission for robot {RobotId}", robot.Id);
await robotService.UpdateCurrentArea(robot.Id, null);
}

if (missionRun == null) { return; } // The robot is already home

var postReturnToHomeMissionCreatedRobot = await robotService.ReadById(missionRun.Robot.Id);
if (postReturnToHomeMissionCreatedRobot == null)
{
logger.LogInformation("Could not find robot {Name}", missionRun.Robot.Name);
return;
}

logger.LogInformation(
"Post return to home mission created: Robot {robotName} has status {robotStatus} and current area {areaName}",
postReturnToHomeMissionCreatedRobot.Name,
postReturnToHomeMissionCreatedRobot.Status,
postReturnToHomeMissionCreatedRobot.CurrentArea?.Name
);
}

logger.LogInformation("Post return to home mission created: Robot {robotName} has status {robotStatus} and current area {areaName}", postReturnToHomeMissionCreatedRobot.Name, postReturnToHomeMissionCreatedRobot.Status, postReturnToHomeMissionCreatedRobot.CurrentArea?.Name);
}

if (!await TheSystemIsAvailableToRunAMission(robot.Id, missionRun.Id))
Expand Down

0 comments on commit 5ab0e57

Please sign in to comment.