diff --git a/backend/api/EventHandlers/MissionEventHandler.cs b/backend/api/EventHandlers/MissionEventHandler.cs index 7b6cfcea4..65cd5acaf 100644 --- a/backend/api/EventHandlers/MissionEventHandler.cs +++ b/backend/api/EventHandlers/MissionEventHandler.cs @@ -34,8 +34,6 @@ IServiceScopeFactory scopeFactory private IMissionScheduling MissionSchedulingService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService(); - private IMqttEventHandler MqttEventHandlerService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService(); - private IList MissionRunQueue(string robotId) { return MissionService @@ -57,7 +55,7 @@ private IList MissionRunQueue(string robotId) public override void Subscribe() { MissionRunService.MissionRunCreated += OnMissionRunCreated; - MqttEventHandler.RobotAvailable += OnRobotAvailable; + MissionScheduling.RobotAvailable += OnRobotAvailable; EmergencyActionService.EmergencyButtonPressedForRobot += OnEmergencyButtonPressedForRobot; EmergencyActionService.EmergencyButtonDepressedForRobot += OnEmergencyButtonDepressedForRobot; } @@ -65,7 +63,7 @@ public override void Subscribe() public override void Unsubscribe() { MissionRunService.MissionRunCreated -= OnMissionRunCreated; - MqttEventHandler.RobotAvailable -= OnRobotAvailable; + MissionScheduling.RobotAvailable -= OnRobotAvailable; EmergencyActionService.EmergencyButtonPressedForRobot -= OnEmergencyButtonPressedForRobot; EmergencyActionService.EmergencyButtonDepressedForRobot -= OnEmergencyButtonDepressedForRobot; } @@ -116,10 +114,10 @@ private async void OnRobotAvailable(object? sender, RobotAvailableEventArgs e) var missionRun = (MissionRun?)null; - if (robot.MissionQueueFrozen == true) + if (robot.MissionQueueFrozen) { missionRun = MissionRunQueue(robot.Id).FirstOrDefault(missionRun => missionRun.Robot.Id == robot.Id && - missionRun.MissionRunPriority == MissionRunPriority.Emergency); + missionRun.MissionRunPriority == MissionRunPriority.Emergency); } else { @@ -186,8 +184,7 @@ private async void OnEmergencyButtonPressedForRobot(object? sender, EmergencyBut await MissionSchedulingService.UnfreezeMissionRunQueueForRobot(e.RobotId); } - MqttEventHandlerService.TriggerRobotAvailable(new RobotAvailableEventArgs(robot.Id)); - + MissionSchedulingService.TriggerRobotAvailable(new RobotAvailableEventArgs(robot.Id)); } private async void OnEmergencyButtonDepressedForRobot(object? sender, EmergencyButtonPressedForRobotEventArgs e) @@ -213,7 +210,7 @@ private async void OnEmergencyButtonDepressedForRobot(object? sender, EmergencyB _logger.LogInformation("Robot {RobotName} was unfrozen but the mission to return to safe zone will be completed before further missions are started", robot.Id); } - MqttEventHandlerService.TriggerRobotAvailable(new RobotAvailableEventArgs(robot.Id)); + MissionSchedulingService.TriggerRobotAvailable(new RobotAvailableEventArgs(robot.Id)); } } } diff --git a/backend/api/EventHandlers/MissionScheduling.cs b/backend/api/EventHandlers/MissionScheduling.cs index cd4edc90c..36df7846d 100644 --- a/backend/api/EventHandlers/MissionScheduling.cs +++ b/backend/api/EventHandlers/MissionScheduling.cs @@ -2,6 +2,7 @@ using Api.Controllers.Models; using Api.Database.Models; using Api.Services; +using Api.Services.Events; using Api.Utilities; namespace Api.EventHandlers { @@ -22,6 +23,8 @@ public interface IMissionScheduling public Task ScheduleMissionToReturnToSafePosition(string robotId, string areaId); public Task UnfreezeMissionRunQueueForRobot(string robotId); + + public void TriggerRobotAvailable(RobotAvailableEventArgs e); } public class MissionScheduling : IMissionScheduling @@ -181,6 +184,7 @@ public async Task StopCurrentMissionRun(string robotId) } } + try { await _isarService.StopMission(robot); @@ -253,6 +257,11 @@ public async Task ScheduleMissionToReturnToSafePosition(string robotId, string a await _missionRunService.Create(missionRun); } + public void TriggerRobotAvailable(RobotAvailableEventArgs e) + { + OnRobotAvailable(e); + } + public async Task TheSystemIsAvailableToRunAMission(Robot robot, MissionRun missionRun) { bool ongoingMission = await OngoingMission(robot.Id); @@ -285,5 +294,7 @@ public static bool MissionRunQueueIsEmpty(IList missionRunQueue) { return !missionRunQueue.Any(); } + protected virtual void OnRobotAvailable(RobotAvailableEventArgs e) { RobotAvailable?.Invoke(this, e); } + public static event EventHandler? RobotAvailable; } } diff --git a/backend/api/EventHandlers/MqttEventHandler.cs b/backend/api/EventHandlers/MqttEventHandler.cs index 510ed16c3..d3fe8fe33 100644 --- a/backend/api/EventHandlers/MqttEventHandler.cs +++ b/backend/api/EventHandlers/MqttEventHandler.cs @@ -16,12 +16,7 @@ namespace Api.EventHandlers /// /// A background service which listens to events and performs callback functions. /// - public interface IMqttEventHandler - { - public void TriggerRobotAvailable(RobotAvailableEventArgs e); - } - - public class MqttEventHandler : EventHandlerBase, IMqttEventHandler + public class MqttEventHandler : EventHandlerBase { private readonly ILogger _logger; private readonly IServiceScopeFactory _scopeFactory; @@ -35,10 +30,6 @@ public MqttEventHandler(ILogger logger, IServiceScopeFactory s Subscribe(); } - public void TriggerRobotAvailable(RobotAvailableEventArgs e) - { - OnRobotAvailable(e); - } private IServiceProvider GetServiceProvider() { return _scopeFactory.CreateScope().ServiceProvider; } @@ -68,18 +59,15 @@ public override void Unsubscribe() protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await stoppingToken; } - protected virtual void OnRobotAvailable(RobotAvailableEventArgs e) - { - RobotAvailable?.Invoke(this, e); - } - - public static event EventHandler? RobotAvailable; private async void OnIsarRobotStatus(object? sender, MqttReceivedArgs mqttArgs) { var provider = GetServiceProvider(); var robotService = provider.GetRequiredService(); + var missionScheduling = provider.GetRequiredService(); + var isarRobotStatus = (IsarRobotStatusMessage)mqttArgs.Message; + var robot = await robotService.ReadByIsarId(isarRobotStatus.IsarId); if (robot == null) @@ -94,7 +82,7 @@ private async void OnIsarRobotStatus(object? sender, MqttReceivedArgs mqttArgs) robot = await robotService.Update(robot); _logger.LogInformation("Updated status for robot {Name} to {Status}", robot.Name, robot.Status); - if (robot.Status == RobotStatus.Available) { OnRobotAvailable(new RobotAvailableEventArgs(robot.Id)); } + if (robot.Status == RobotStatus.Available) { missionScheduling.TriggerRobotAvailable(new RobotAvailableEventArgs(robot.Id)); } } private async void OnIsarRobotInfo(object? sender, MqttReceivedArgs mqttArgs) diff --git a/backend/api/Program.cs b/backend/api/Program.cs index 13a14522b..2f12715c0 100644 --- a/backend/api/Program.cs +++ b/backend/api/Program.cs @@ -15,7 +15,6 @@ using Microsoft.AspNetCore.Rewrite; using Microsoft.Identity.Web; using Microsoft.OpenApi.Models; - var builder = WebApplication.CreateBuilder(args); Console.WriteLine($"\nENVIRONMENT IS SET TO '{builder.Environment.EnvironmentName}'\n"); @@ -90,7 +89,6 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddScoped(); builder.Services.AddTransient(); diff --git a/backend/api/Services/MissionSchedulingService.cs b/backend/api/Services/MissionSchedulingService.cs index fbbe9f6d2..badc3dff2 100644 --- a/backend/api/Services/MissionSchedulingService.cs +++ b/backend/api/Services/MissionSchedulingService.cs @@ -101,6 +101,5 @@ private static float CalculateDistance(Pose pose1, Pose pose2) var pos2 = pose2.Position; return (float)Math.Sqrt(Math.Pow(pos1.X - pos2.X, 2) + Math.Pow(pos1.Y - pos2.Y, 2) + Math.Pow(pos1.Z - pos2.Z, 2)); } - } }