diff --git a/backend/api.test/Database/DatabaseUtilities.cs b/backend/api.test/Database/DatabaseUtilities.cs index 6e08aaaf2..dbd89c6e4 100644 --- a/backend/api.test/Database/DatabaseUtilities.cs +++ b/backend/api.test/Database/DatabaseUtilities.cs @@ -164,10 +164,7 @@ public async Task 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); } } diff --git a/backend/api.test/Services/RobotService.cs b/backend/api.test/Services/RobotService.cs index cde9ac9fb..7c0489d25 100644 --- a/backend/api.test/Services/RobotService.cs +++ b/backend/api.test/Services/RobotService.cs @@ -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); diff --git a/backend/api/Database/Models/Robot.cs b/backend/api/Database/Models/Robot.cs index c7020dc50..0db2063e4 100644 --- a/backend/api/Database/Models/Robot.cs +++ b/backend/api/Database/Models/Robot.cs @@ -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) { var videoStreams = new List(); foreach (var videoStreamQuery in createQuery.VideoStreams) @@ -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)] diff --git a/backend/api/Services/MissionRunService.cs b/backend/api/Services/MissionRunService.cs index aa063332b..2835016a9 100644 --- a/backend/api/Services/MissionRunService.cs +++ b/backend/api/Services/MissionRunService.cs @@ -167,8 +167,8 @@ public async Task> ReadMissionRunQueue(string robotId, bool re public async Task 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> ReadMissionRuns(string robotId, MissionRunType? missionRunType, IList? filterStatuses = null, bool readOnly = true) @@ -216,59 +216,38 @@ public async Task 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 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 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 Update(MissionRun missionRun) diff --git a/backend/api/Services/RobotService.cs b/backend/api/Services/RobotService.cs index b63918694..b6db02cd3 100644 --- a/backend/api/Services/RobotService.cs +++ b/backend/api/Services/RobotService.cs @@ -82,10 +82,7 @@ public async Task 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; diff --git a/backend/api/Services/SourceService.cs b/backend/api/Services/SourceService.cs index ddfc42588..cd46fa329 100644 --- a/backend/api/Services/SourceService.cs +++ b/backend/api/Services/SourceService.cs @@ -20,8 +20,6 @@ public interface ISourceService public abstract Task CreateSourceIfDoesNotExist(List tasks, bool readOnly = true); - public abstract Task Update(Source source); - public abstract Task Delete(string id); public void DetachTracking(Source source); @@ -125,13 +123,6 @@ public async Task CreateSourceIfDoesNotExist(List tasks, bo return newSource; } - public async Task Update(Source source) - { - var entry = context.Update(source); - await context.SaveChangesAsync(); - return entry.Entity; - } - public async Task Delete(string id) { var source = await GetSources()