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

Refactor robot status #1584

Merged
merged 1 commit into from
May 24, 2024
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
22 changes: 6 additions & 16 deletions backend/api.test/EventHandlers/TestMissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,16 @@ public async void NewMissionIsStartedWhenRobotBecomesAvailable()
Thread.Sleep(100);

var mqttEventArgs = new MqttReceivedArgs(
new IsarRobotStatusMessage
new IsarStatusMessage
{
RobotName = robot.Name,
IsarId = robot.IsarId,
RobotStatus = RobotStatus.Available,
PreviousRobotStatus = RobotStatus.Busy,
CurrentState = "idle",
CurrentMissionId = "",
CurrentTaskId = "",
CurrentStepId = "",
Status = RobotStatus.Available,
Timestamp = DateTime.UtcNow
});

// Act
_mqttService.RaiseEvent(nameof(MqttService.MqttIsarRobotStatusReceived), mqttEventArgs);
_mqttService.RaiseEvent(nameof(MqttService.MqttIsarStatusReceived), mqttEventArgs);
Thread.Sleep(500);

// Assert
Expand All @@ -239,21 +234,16 @@ public async void ReturnToHomeMissionIsStartedIfQueueIsEmptyWhenRobotBecomesAvai
var robot = await _databaseUtilities.NewRobot(RobotStatus.Busy, installation, area);

var mqttEventArgs = new MqttReceivedArgs(
new IsarRobotStatusMessage
new IsarStatusMessage
{
RobotName = robot.Name,
IsarId = robot.IsarId,
RobotStatus = RobotStatus.Available,
PreviousRobotStatus = RobotStatus.Busy,
CurrentState = "idle",
CurrentMissionId = "",
CurrentTaskId = "",
CurrentStepId = "",
Status = RobotStatus.Available,
Timestamp = DateTime.UtcNow
});

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

// Assert
Thread.Sleep(1000);
Expand Down
24 changes: 10 additions & 14 deletions backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public MqttEventHandler(ILogger<MqttEventHandler> logger, IServiceScopeFactory s

public override void Subscribe()
{
MqttService.MqttIsarRobotStatusReceived += OnIsarRobotStatus;
MqttService.MqttIsarStatusReceived += OnIsarStatus;
MqttService.MqttIsarRobotInfoReceived += OnIsarRobotInfo;
MqttService.MqttIsarMissionReceived += OnIsarMissionUpdate;
MqttService.MqttIsarTaskReceived += OnIsarTaskUpdate;
Expand All @@ -49,7 +49,7 @@ public override void Subscribe()

public override void Unsubscribe()
{
MqttService.MqttIsarRobotStatusReceived -= OnIsarRobotStatus;
MqttService.MqttIsarStatusReceived -= OnIsarStatus;
MqttService.MqttIsarRobotInfoReceived -= OnIsarRobotInfo;
MqttService.MqttIsarMissionReceived -= OnIsarMissionUpdate;
MqttService.MqttIsarTaskReceived -= OnIsarTaskUpdate;
Expand All @@ -63,33 +63,29 @@ public override void Unsubscribe()
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await stoppingToken; }


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

var isarRobotStatus = (IsarRobotStatusMessage)mqttArgs.Message;
var isarStatus = (IsarStatusMessage)mqttArgs.Message;

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

if (robot == null)
{
_logger.LogInformation("Received message from unknown ISAR instance {Id} with robot name {Name}", isarRobotStatus.IsarId, isarRobotStatus.RobotName);
_logger.LogInformation("Received message from unknown ISAR instance {Id} with robot name {Name}", isarStatus.IsarId, isarStatus.RobotName);
return;
}

if (robot.Status == isarRobotStatus.RobotStatus)
{
_logger.LogInformation("Robot {robotName} received a new isar robot status {isarRobotStatus}, but the robot status was already the same", robot.Name, isarRobotStatus.RobotStatus);
return;
}
if (robot.Status == isarStatus.Status) { return; }

robot.Status = isarRobotStatus.RobotStatus;
robot.Status = isarStatus.Status;
await robotService.Update(robot);
_logger.LogInformation("Updated status for robot {Name} to {Status}", robot.Name, isarRobotStatus.RobotStatus);
_logger.LogInformation("Updated status for robot {Name} to {Status}", robot.Name, isarStatus.Status);

if (isarRobotStatus.RobotStatus == RobotStatus.Available) missionSchedulingService.TriggerRobotAvailable(new RobotAvailableEventArgs(robot.Id));
if (isarStatus.Status == RobotStatus.Available) missionSchedulingService.TriggerRobotAvailable(new RobotAvailableEventArgs(robot.Id));
}

private async void OnIsarRobotInfo(object? sender, MqttReceivedArgs mqttArgs)
Expand Down
35 changes: 0 additions & 35 deletions backend/api/MQTT/MessageModels/IsarRobotStatus.cs

This file was deleted.

20 changes: 20 additions & 0 deletions backend/api/MQTT/MessageModels/IsarStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Text.Json.Serialization;
using Api.Database.Models;
namespace Api.Mqtt.MessageModels
{
#nullable disable
public class IsarStatusMessage : MqttMessage
{
[JsonPropertyName("robot_name")]
public string RobotName { get; set; }

[JsonPropertyName("isar_id")]
public string IsarId { get; set; }

[JsonPropertyName("status")]
public RobotStatus Status { get; set; }

[JsonPropertyName("timestamp")]
public DateTime Timestamp { get; set; }
}
}
8 changes: 4 additions & 4 deletions backend/api/MQTT/MqttService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public MqttService(ILogger<MqttService> logger, IConfiguration config)
var topics = mqttConfig.GetSection("Topics").Get<List<string>>() ?? [];
SubscribeToTopics(topics);
}
public static event EventHandler<MqttReceivedArgs>? MqttIsarRobotStatusReceived;
public static event EventHandler<MqttReceivedArgs>? MqttIsarStatusReceived;
public static event EventHandler<MqttReceivedArgs>? MqttIsarRobotInfoReceived;
public static event EventHandler<MqttReceivedArgs>? MqttIsarRobotHeartbeatReceived;
public static event EventHandler<MqttReceivedArgs>? MqttIsarMissionReceived;
Expand Down Expand Up @@ -122,8 +122,8 @@ private Task OnMessageReceived(MqttApplicationMessageReceivedEventArgs messageRe

switch (messageType)
{
case Type type when type == typeof(IsarRobotStatusMessage):
OnIsarTopicReceived<IsarRobotStatusMessage>(content);
case Type type when type == typeof(IsarStatusMessage):
OnIsarTopicReceived<IsarStatusMessage>(content);
break;
case Type type when type == typeof(IsarRobotInfoMessage):
OnIsarTopicReceived<IsarRobotInfoMessage>(content);
Expand Down Expand Up @@ -291,7 +291,7 @@ private void OnIsarTopicReceived<T>(string content) where T : MqttMessage
{
var raiseEvent = type switch
{
_ when type == typeof(IsarRobotStatusMessage) => MqttIsarRobotStatusReceived,
_ when type == typeof(IsarStatusMessage) => MqttIsarStatusReceived,
_ when type == typeof(IsarRobotInfoMessage) => MqttIsarRobotInfoReceived,
_ when type == typeof(IsarRobotHeartbeatMessage) => MqttIsarRobotHeartbeatReceived,
_ when type == typeof(IsarMissionMessage) => MqttIsarMissionReceived,
Expand Down
2 changes: 1 addition & 1 deletion backend/api/MQTT/MqttTopics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class MqttTopics
new()
{
{
"isar/+/robot_status", typeof(IsarRobotStatusMessage)
"isar/+/status", typeof(IsarStatusMessage)
},
{
"isar/+/robot_info", typeof(IsarRobotInfoMessage)
Expand Down
2 changes: 1 addition & 1 deletion backend/api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"Port": 1883,
"Username": "flotilla",
"Topics": [
"isar/+/robot_status",
"isar/+/status",
"isar/+/robot_info",
"isar/+/robot_heartbeat",
"isar/+/mission",
Expand Down
2 changes: 1 addition & 1 deletion backend/api/appsettings.Local.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"Port": 1883,
"Username": "flotilla",
"Topics": [
"isar/+/robot_status",
"isar/+/status",
"isar/+/robot_info",
"isar/+/robot_heartbeat",
"isar/+/mission",
Expand Down
2 changes: 1 addition & 1 deletion backend/api/appsettings.Production.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"Port": 1883,
"Username": "flotilla",
"Topics": [
"isar/+/robot_status",
"isar/+/status",
"isar/+/robot_info",
"isar/+/robot_heartbeat",
"isar/+/mission",
Expand Down
2 changes: 1 addition & 1 deletion backend/api/appsettings.Staging.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"Port": 1883,
"Username": "flotilla",
"Topics": [
"isar/+/robot_status",
"isar/+/status",
"isar/+/robot_info",
"isar/+/robot_heartbeat",
"isar/+/mission",
Expand Down
2 changes: 1 addition & 1 deletion backend/api/appsettings.Test.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"Port": 1883,
"Username": "flotilla",
"Topics": [
"isar/+/robot_status",
"isar/+/status",
"isar/+/robot_info",
"isar/+/robot_heartbeat",
"isar/+/mission",
Expand Down
Loading