From c9b35d39ccac31f47a3cda8da5a087a21ad84fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Chirico=20Indreb=C3=B8?= Date: Thu, 12 Dec 2024 14:43:43 +0100 Subject: [PATCH] Split up function --- backend/api/EventHandlers/MqttEventHandler.cs | 64 ++++++++++--------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/backend/api/EventHandlers/MqttEventHandler.cs b/backend/api/EventHandlers/MqttEventHandler.cs index b1ca1bce..ecef8c1b 100644 --- a/backend/api/EventHandlers/MqttEventHandler.cs +++ b/backend/api/EventHandlers/MqttEventHandler.cs @@ -129,7 +129,39 @@ private async void OnIsarStatus(object? sender, MqttReceivedArgs mqttArgs) _updateRobotSemaphore.Release(); _logger.LogDebug("Semaphore released after updating robot current mission id"); } - MissionScheduling.TriggerRobotAvailable(new RobotAvailableEventArgs(robot.Id)); + await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot.Id); + } + } + + private async void CreateRobot(IsarRobotInfoMessage isarRobotInfo, Installation installation) + { + _logger.LogInformation( + "Received message from new ISAR instance '{Id}' with robot name '{Name}'. Adding new robot to database", + isarRobotInfo.IsarId, isarRobotInfo.RobotName); + + var robotQuery = new CreateRobotQuery + { + IsarId = isarRobotInfo.IsarId, + Name = isarRobotInfo.RobotName, + RobotType = isarRobotInfo.RobotType, + SerialNumber = isarRobotInfo.SerialNumber, + CurrentInstallationCode = installation.InstallationCode, + Documentation = isarRobotInfo.DocumentationQueries, + Host = isarRobotInfo.Host, + Port = isarRobotInfo.Port, + RobotCapabilities = isarRobotInfo.Capabilities, + Status = RobotStatus.Available, + }; + + try + { + var newRobot = await RobotService.CreateFromQuery(robotQuery); + _logger.LogInformation("Added robot '{RobotName}' with ISAR id '{IsarId}' to database", newRobot.Name, newRobot.IsarId); + } + catch (DbUpdateException) + { + _logger.LogError($"Failed to add robot {robotQuery.Name} with to the database"); + return; } } @@ -154,35 +186,7 @@ private async void OnIsarRobotInfo(object? sender, MqttReceivedArgs mqttArgs) if (robot == null) { - _logger.LogInformation( - "Received message from new ISAR instance '{Id}' with robot name '{Name}'. Adding new robot to database", - isarRobotInfo.IsarId, isarRobotInfo.RobotName); - - var robotQuery = new CreateRobotQuery - { - IsarId = isarRobotInfo.IsarId, - Name = isarRobotInfo.RobotName, - RobotType = isarRobotInfo.RobotType, - SerialNumber = isarRobotInfo.SerialNumber, - CurrentInstallationCode = installation.InstallationCode, - Documentation = isarRobotInfo.DocumentationQueries, - Host = isarRobotInfo.Host, - Port = isarRobotInfo.Port, - RobotCapabilities = isarRobotInfo.Capabilities, - Status = RobotStatus.Available, - }; - - try - { - var newRobot = await RobotService.CreateFromQuery(robotQuery); - _logger.LogInformation("Added robot '{RobotName}' with ISAR id '{IsarId}' to database", newRobot.Name, newRobot.IsarId); - } - catch (DbUpdateException) - { - _logger.LogError($"Failed to add robot {robotQuery.Name} with to the database"); - return; - } - + CreateRobot(isarRobotInfo, installation); return; }