Skip to content

Commit

Permalink
Remove duplicate scoped event handler
Browse files Browse the repository at this point in the history
The additional scoped service which was used to handle the
RobotAvailable event caused events to sometimes be handles by two
different objects at the same time.
  • Loading branch information
aeshub committed Nov 9, 2023
1 parent f9f5eba commit d7d425b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
15 changes: 6 additions & 9 deletions backend/api/EventHandlers/MissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ IServiceScopeFactory scopeFactory

private IMissionScheduling MissionSchedulingService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IMissionScheduling>();

private IMqttEventHandler MqttEventHandlerService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IMqttEventHandler>();

private IList<MissionRun> MissionRunQueue(string robotId)
{
return MissionService
Expand All @@ -57,15 +55,15 @@ private IList<MissionRun> MissionRunQueue(string robotId)
public override void Subscribe()
{
MissionRunService.MissionRunCreated += OnMissionRunCreated;
MqttEventHandler.RobotAvailable += OnRobotAvailable;
MissionScheduling.RobotAvailable += OnRobotAvailable;
EmergencyActionService.EmergencyButtonPressedForRobot += OnEmergencyButtonPressedForRobot;
EmergencyActionService.EmergencyButtonDepressedForRobot += OnEmergencyButtonDepressedForRobot;
}

public override void Unsubscribe()
{
MissionRunService.MissionRunCreated -= OnMissionRunCreated;
MqttEventHandler.RobotAvailable -= OnRobotAvailable;
MissionScheduling.RobotAvailable -= OnRobotAvailable;
EmergencyActionService.EmergencyButtonPressedForRobot -= OnEmergencyButtonPressedForRobot;
EmergencyActionService.EmergencyButtonDepressedForRobot -= OnEmergencyButtonDepressedForRobot;
}
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand All @@ -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));
}
}
}
11 changes: 11 additions & 0 deletions backend/api/EventHandlers/MissionScheduling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -181,6 +184,7 @@ public async Task StopCurrentMissionRun(string robotId)
}
}


try
{
await _isarService.StopMission(robot);
Expand Down Expand Up @@ -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<bool> TheSystemIsAvailableToRunAMission(Robot robot, MissionRun missionRun)
{
bool ongoingMission = await OngoingMission(robot.Id);
Expand Down Expand Up @@ -285,5 +294,7 @@ public static bool MissionRunQueueIsEmpty(IList<MissionRun> missionRunQueue)
{
return !missionRunQueue.Any();
}
protected virtual void OnRobotAvailable(RobotAvailableEventArgs e) { RobotAvailable?.Invoke(this, e); }
public static event EventHandler<RobotAvailableEventArgs>? RobotAvailable;
}
}
22 changes: 5 additions & 17 deletions backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ namespace Api.EventHandlers
/// <summary>
/// A background service which listens to events and performs callback functions.
/// </summary>
public interface IMqttEventHandler
{
public void TriggerRobotAvailable(RobotAvailableEventArgs e);
}

public class MqttEventHandler : EventHandlerBase, IMqttEventHandler
public class MqttEventHandler : EventHandlerBase
{
private readonly ILogger<MqttEventHandler> _logger;
private readonly IServiceScopeFactory _scopeFactory;
Expand All @@ -35,10 +30,6 @@ public MqttEventHandler(ILogger<MqttEventHandler> logger, IServiceScopeFactory s
Subscribe();
}

public void TriggerRobotAvailable(RobotAvailableEventArgs e)
{
OnRobotAvailable(e);
}

private IServiceProvider GetServiceProvider() { return _scopeFactory.CreateScope().ServiceProvider; }

Expand Down Expand Up @@ -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<RobotAvailableEventArgs>? RobotAvailable;

private async void OnIsarRobotStatus(object? sender, MqttReceivedArgs mqttArgs)
{
var provider = GetServiceProvider();
var robotService = provider.GetRequiredService<IRobotService>();
var missionScheduling = provider.GetRequiredService<IMissionScheduling>();

var isarRobotStatus = (IsarRobotStatusMessage)mqttArgs.Message;

var robot = await robotService.ReadByIsarId(isarRobotStatus.IsarId);

if (robot == null)
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions backend/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -90,7 +89,6 @@
builder.Services.AddScoped<RobotController>();
builder.Services.AddScoped<EmergencyActionController>();
builder.Services.AddScoped<ICustomMissionService, CustomMissionService>();
builder.Services.AddScoped<IMqttEventHandler, MqttEventHandler>();

builder.Services.AddTransient<ISignalRService, SignalRService>();

Expand Down
1 change: 0 additions & 1 deletion backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
}

0 comments on commit d7d425b

Please sign in to comment.