Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various small backend refactors #1734

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions backend/api.test/Database/DatabaseUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,7 @@ public async Task<Robot> NewRobot(RobotStatus status, Installation installation,
};

var robotModel = await _robotModelService.ReadByRobotType(createRobotQuery.RobotType, readOnly: true);
var robot = new Robot(createRobotQuery, installation, area)
{
Model = robotModel!
};
var robot = new Robot(createRobotQuery, installation, robotModel!, area);
return await _robotService.Create(robot);
}
}
Expand Down
3 changes: 1 addition & 2 deletions backend/api.test/Services/RobotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ public async Task Create()
Status = RobotStatus.Available
};

var robot = new Robot(robotQuery, installation);
var robotModel = _context.RobotModels.First();
robot.Model = robotModel;
var robot = new Robot(robotQuery, installation, robotModel);

await robotService.Create(robot);
var robotsAfter = await robotService.ReadAll(readOnly: true);
Expand Down
3 changes: 2 additions & 1 deletion backend/api/Database/Models/Robot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Robot()
Pose = new Pose();
}

public Robot(CreateRobotQuery createQuery, Installation installation, Area? area = null)
public Robot(CreateRobotQuery createQuery, Installation installation, RobotModel model, Area? area = null)
andchiind marked this conversation as resolved.
Show resolved Hide resolved
{
var videoStreams = new List<VideoStream>();
foreach (var videoStreamQuery in createQuery.VideoStreams)
Expand All @@ -47,6 +47,7 @@ public Robot(CreateRobotQuery createQuery, Installation installation, Area? area
RobotCapabilities = createQuery.RobotCapabilities;
Status = createQuery.Status;
Pose = new Pose();
Model = model;
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
Expand Down
41 changes: 10 additions & 31 deletions backend/api/Services/MissionRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public async Task<IList<MissionRun>> ReadMissionRunQueue(string robotId, bool re
public async Task<MissionRun?> ReadNextScheduledLocalizationMissionRun(string robotId, bool readOnly = true)
{
return await GetMissionRunsWithSubModels(readOnly: readOnly)
.OrderBy(missionRun => missionRun.DesiredStartTime)
.FirstOrDefaultAsync(missionRun => missionRun.Robot.Id == robotId && missionRun.Status == MissionStatus.Pending && missionRun.MissionRunType == MissionRunType.Localization);
.OrderBy(missionRun => missionRun.DesiredStartTime)
.FirstOrDefaultAsync(missionRun => missionRun.Robot.Id == robotId && missionRun.Status == MissionStatus.Pending && missionRun.MissionRunType == MissionRunType.Localization);
}

public async Task<IList<MissionRun>> ReadMissionRuns(string robotId, MissionRunType? missionRunType, IList<MissionStatus>? filterStatuses = null, bool readOnly = true)
Expand Down Expand Up @@ -216,59 +216,38 @@ public async Task<bool> OngoingOrPausedLocalizationMissionRunExists(string robot
.Where(missionRun => missionRun.Robot.Id == robotId && (missionRun.Status == MissionStatus.Ongoing || missionRun.Status == MissionStatus.Paused))
.OrderBy(missionRun => missionRun.DesiredStartTime)
.ToListAsync();
foreach (var ongoingMissionRun in ongoingMissionRuns)
{
if (ongoingMissionRun.IsLocalizationMission()) { return true; }
}
return false;
return ongoingMissionRuns.Any((m) => m.IsLocalizationMission());
}

public async Task<bool> PendingOrOngoingLocalizationMissionRunExists(string robotId)
{
var pendingMissionRuns = await ReadMissionRunQueue(robotId, readOnly: true);
foreach (var pendingMissionRun in pendingMissionRuns)
{
if (pendingMissionRun.IsLocalizationMission()) { return true; }
}
if (pendingMissionRuns.Any((m) => m.IsLocalizationMission())) return true;

var ongoingMissionRuns = await GetMissionRunsWithSubModels(readOnly: true)
.Where(missionRun => missionRun.Robot.Id == robotId && missionRun.Status == MissionStatus.Ongoing)
.OrderBy(missionRun => missionRun.DesiredStartTime)
.ToListAsync();
foreach (var ongoingMissionRun in ongoingMissionRuns)
{
if (ongoingMissionRun.IsLocalizationMission()) { return true; }
}
return false;
return ongoingMissionRuns.Any((m) => m.IsLocalizationMission());
}

public async Task<bool> PendingOrOngoingReturnToHomeMissionRunExists(string robotId)
{
var pendingMissionRuns = await ReadMissionRunQueue(robotId, readOnly: true);
foreach (var pendingMissionRun in pendingMissionRuns)
{
if (pendingMissionRun.IsReturnHomeMission()) { return true; }
}
if (pendingMissionRuns.Any((m) => m.IsReturnHomeMission())) return true;

var ongoingMissionRuns = await GetMissionRunsWithSubModels(readOnly: true)
.Where(missionRun => missionRun.Robot.Id == robotId && missionRun.Status == MissionStatus.Ongoing)
.OrderBy(missionRun => missionRun.DesiredStartTime)
.ToListAsync();
foreach (var ongoingMissionRun in ongoingMissionRuns)
{
if (ongoingMissionRun.IsReturnHomeMission()) { return true; }
}
return false;

return ongoingMissionRuns.Any((m) => m.IsReturnHomeMission());
}

public bool IncludesUnsupportedInspectionType(MissionRun missionRun)
{
if (missionRun.Robot.RobotCapabilities == null) return false;

foreach (var task in missionRun.Tasks)
foreach (var inspection in task.Inspections)
if (!inspection.IsSupportedInspectionType(missionRun.Robot.RobotCapabilities))
return true;
return false;
return missionRun.Tasks.SelectMany((t) => t.Inspections).Any((i) => !i.IsSupportedInspectionType(missionRun.Robot.RobotCapabilities));
}

public async Task<MissionRun> Update(MissionRun missionRun)
Expand Down
5 changes: 1 addition & 4 deletions backend/api/Services/RobotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ public async Task<Robot> CreateFromQuery(CreateRobotQuery robotQuery)
}
}

var newRobot = new Robot(robotQuery, installation, area)
{
Model = robotModel
};
var newRobot = new Robot(robotQuery, installation, robotModel, area);

if (newRobot.CurrentArea is not null) context.Entry(newRobot.CurrentArea).State = EntityState.Unchanged;
if (newRobot.CurrentInstallation != null) context.Entry(newRobot.CurrentInstallation).State = EntityState.Unchanged;
Expand Down
9 changes: 0 additions & 9 deletions backend/api/Services/SourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public interface ISourceService

public abstract Task<Source> CreateSourceIfDoesNotExist(List<MissionTask> tasks, bool readOnly = true);

public abstract Task<Source> Update(Source source);

public abstract Task<Source?> Delete(string id);

public void DetachTracking(Source source);
Expand Down Expand Up @@ -125,13 +123,6 @@ public async Task<Source> CreateSourceIfDoesNotExist(List<MissionTask> tasks, bo
return newSource;
}

public async Task<Source> Update(Source source)
{
var entry = context.Update(source);
await context.SaveChangesAsync();
return entry.Entity;
}

public async Task<Source?> Delete(string id)
{
var source = await GetSources()
Expand Down
Loading