From b5d406d52547bb82c23cee50db060cbf858b8f83 Mon Sep 17 00:00:00 2001 From: oysand Date: Fri, 3 May 2024 15:18:31 +0200 Subject: [PATCH] Refactor robot status --- .../EventHandlers/TestMissionEventHandler.cs | 22 ++++-------- backend/api/EventHandlers/MqttEventHandler.cs | 24 ++++++------- .../api/MQTT/MessageModels/IsarRobotStatus.cs | 35 ------------------- backend/api/MQTT/MessageModels/IsarStatus.cs | 20 +++++++++++ backend/api/MQTT/MqttService.cs | 8 ++--- backend/api/MQTT/MqttTopics.cs | 2 +- backend/api/appsettings.Development.json | 2 +- backend/api/appsettings.Local.json | 2 +- backend/api/appsettings.Production.json | 2 +- backend/api/appsettings.Staging.json | 2 +- backend/api/appsettings.Test.json | 2 +- 11 files changed, 46 insertions(+), 75 deletions(-) delete mode 100644 backend/api/MQTT/MessageModels/IsarRobotStatus.cs create mode 100644 backend/api/MQTT/MessageModels/IsarStatus.cs diff --git a/backend/api.test/EventHandlers/TestMissionEventHandler.cs b/backend/api.test/EventHandlers/TestMissionEventHandler.cs index 4d496ee76..7ea592bd6 100644 --- a/backend/api.test/EventHandlers/TestMissionEventHandler.cs +++ b/backend/api.test/EventHandlers/TestMissionEventHandler.cs @@ -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 @@ -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); diff --git a/backend/api/EventHandlers/MqttEventHandler.cs b/backend/api/EventHandlers/MqttEventHandler.cs index bfbcad14f..4cadd8d84 100644 --- a/backend/api/EventHandlers/MqttEventHandler.cs +++ b/backend/api/EventHandlers/MqttEventHandler.cs @@ -36,7 +36,7 @@ public MqttEventHandler(ILogger logger, IServiceScopeFactory s public override void Subscribe() { - MqttService.MqttIsarRobotStatusReceived += OnIsarRobotStatus; + MqttService.MqttIsarStatusReceived += OnIsarStatus; MqttService.MqttIsarRobotInfoReceived += OnIsarRobotInfo; MqttService.MqttIsarMissionReceived += OnIsarMissionUpdate; MqttService.MqttIsarTaskReceived += OnIsarTaskUpdate; @@ -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; @@ -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(); var missionSchedulingService = provider.GetRequiredService(); - 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) diff --git a/backend/api/MQTT/MessageModels/IsarRobotStatus.cs b/backend/api/MQTT/MessageModels/IsarRobotStatus.cs deleted file mode 100644 index bd0741444..000000000 --- a/backend/api/MQTT/MessageModels/IsarRobotStatus.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Text.Json.Serialization; -using Api.Database.Models; -namespace Api.Mqtt.MessageModels -{ -#nullable disable - public class IsarRobotStatusMessage : MqttMessage - { - [JsonPropertyName("robot_name")] - public string RobotName { get; set; } - - [JsonPropertyName("isar_id")] - public string IsarId { get; set; } - - [JsonPropertyName("robot_status")] - public RobotStatus RobotStatus { get; set; } - - [JsonPropertyName("previous_robot_status")] - public RobotStatus? PreviousRobotStatus { get; set; } - - [JsonPropertyName("current_isar_state")] - public string CurrentState { get; set; } - - [JsonPropertyName("current_mission_id")] - public string CurrentMissionId { get; set; } - - [JsonPropertyName("current_task_id")] - public string CurrentTaskId { get; set; } - - [JsonPropertyName("current_step_id")] - public string CurrentStepId { get; set; } - - [JsonPropertyName("timestamp")] - public DateTime Timestamp { get; set; } - } -} diff --git a/backend/api/MQTT/MessageModels/IsarStatus.cs b/backend/api/MQTT/MessageModels/IsarStatus.cs new file mode 100644 index 000000000..2f3cc014f --- /dev/null +++ b/backend/api/MQTT/MessageModels/IsarStatus.cs @@ -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; } + } +} diff --git a/backend/api/MQTT/MqttService.cs b/backend/api/MQTT/MqttService.cs index 0eba8a497..480430f7e 100644 --- a/backend/api/MQTT/MqttService.cs +++ b/backend/api/MQTT/MqttService.cs @@ -81,7 +81,7 @@ public MqttService(ILogger logger, IConfiguration config) var topics = mqttConfig.GetSection("Topics").Get>() ?? []; SubscribeToTopics(topics); } - public static event EventHandler? MqttIsarRobotStatusReceived; + public static event EventHandler? MqttIsarStatusReceived; public static event EventHandler? MqttIsarRobotInfoReceived; public static event EventHandler? MqttIsarRobotHeartbeatReceived; public static event EventHandler? MqttIsarMissionReceived; @@ -122,8 +122,8 @@ private Task OnMessageReceived(MqttApplicationMessageReceivedEventArgs messageRe switch (messageType) { - case Type type when type == typeof(IsarRobotStatusMessage): - OnIsarTopicReceived(content); + case Type type when type == typeof(IsarStatusMessage): + OnIsarTopicReceived(content); break; case Type type when type == typeof(IsarRobotInfoMessage): OnIsarTopicReceived(content); @@ -291,7 +291,7 @@ private void OnIsarTopicReceived(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, diff --git a/backend/api/MQTT/MqttTopics.cs b/backend/api/MQTT/MqttTopics.cs index 91fd14bc4..3d6fcbf42 100644 --- a/backend/api/MQTT/MqttTopics.cs +++ b/backend/api/MQTT/MqttTopics.cs @@ -14,7 +14,7 @@ public static class MqttTopics new() { { - "isar/+/robot_status", typeof(IsarRobotStatusMessage) + "isar/+/status", typeof(IsarStatusMessage) }, { "isar/+/robot_info", typeof(IsarRobotInfoMessage) diff --git a/backend/api/appsettings.Development.json b/backend/api/appsettings.Development.json index afb89a5fb..7ddb6a1b9 100644 --- a/backend/api/appsettings.Development.json +++ b/backend/api/appsettings.Development.json @@ -23,7 +23,7 @@ "Port": 1883, "Username": "flotilla", "Topics": [ - "isar/+/robot_status", + "isar/+/status", "isar/+/robot_info", "isar/+/robot_heartbeat", "isar/+/mission", diff --git a/backend/api/appsettings.Local.json b/backend/api/appsettings.Local.json index b75059188..c4aed545d 100644 --- a/backend/api/appsettings.Local.json +++ b/backend/api/appsettings.Local.json @@ -23,7 +23,7 @@ "Port": 1883, "Username": "flotilla", "Topics": [ - "isar/+/robot_status", + "isar/+/status", "isar/+/robot_info", "isar/+/robot_heartbeat", "isar/+/mission", diff --git a/backend/api/appsettings.Production.json b/backend/api/appsettings.Production.json index 99d395fd9..229b262fd 100644 --- a/backend/api/appsettings.Production.json +++ b/backend/api/appsettings.Production.json @@ -23,7 +23,7 @@ "Port": 1883, "Username": "flotilla", "Topics": [ - "isar/+/robot_status", + "isar/+/status", "isar/+/robot_info", "isar/+/robot_heartbeat", "isar/+/mission", diff --git a/backend/api/appsettings.Staging.json b/backend/api/appsettings.Staging.json index a844551e0..e3a0df9d5 100644 --- a/backend/api/appsettings.Staging.json +++ b/backend/api/appsettings.Staging.json @@ -27,7 +27,7 @@ "Port": 1883, "Username": "flotilla", "Topics": [ - "isar/+/robot_status", + "isar/+/status", "isar/+/robot_info", "isar/+/robot_heartbeat", "isar/+/mission", diff --git a/backend/api/appsettings.Test.json b/backend/api/appsettings.Test.json index 0ef276501..0e11ec10b 100644 --- a/backend/api/appsettings.Test.json +++ b/backend/api/appsettings.Test.json @@ -25,7 +25,7 @@ "Port": 1883, "Username": "flotilla", "Topics": [ - "isar/+/robot_status", + "isar/+/status", "isar/+/robot_info", "isar/+/robot_heartbeat", "isar/+/mission",