Skip to content

Commit

Permalink
Refactor add timer functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
aeshub committed Oct 27, 2023
1 parent 653820b commit d5dcecc
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions backend/api/EventHandlers/IsarConnectionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,36 @@ private async void OnIsarRobotHeartbeat(object? sender, MqttReceivedArgs mqttArg
if (robot == null)
{
_logger.LogInformation(
"Received message from unknown ISAR '{isarId}' ('{robotName}')",
"Received message from unknown ISAR '{IsarId}' ('{RobotName}')",
isarRobotHeartbeat.IsarId,
isarRobotHeartbeat.RobotName
);
return;
}

if (!_isarConnectionTimers.ContainsKey(robot.IsarId))
{
var timer = new Timer(_isarConnectionTimeout * 1000);
timer.Elapsed += (_, _) => OnTimeoutEvent(isarRobotHeartbeat);
timer.Start();
if (_isarConnectionTimers.TryAdd(robot.IsarId, timer))
_logger.LogInformation(
"Added new timer for ISAR '{isarId}' ('{robotName}')",
robot.IsarId,
robot.Name
);
}
if (!_isarConnectionTimers.ContainsKey(robot.IsarId)) { AddTimerForRobot(isarRobotHeartbeat, robot); }

_logger.LogDebug(
"Reset connection timer for ISAR '{isarId}' ('{robotName}')",
_logger.LogInformation(
"Reset connection timer for ISAR '{IsarId}' ('{RobotName}')",
robot.IsarId,
robot.Name
);

_isarConnectionTimers[robot.IsarId].Reset();

if (!robot.Enabled)
{
robot.Enabled = true;
await RobotService.Update(robot);
}
if (robot.Enabled) { return; }
robot.Enabled = true;
await RobotService.Update(robot);
}

private void AddTimerForRobot(IsarRobotHeartbeatMessage isarRobotHeartbeat, Robot robot)
{
var timer = new Timer(_isarConnectionTimeout * 1000);
timer.Elapsed += (_, _) => OnTimeoutEvent(isarRobotHeartbeat);
timer.Start();

if (_isarConnectionTimers.TryAdd(robot.IsarId, timer)) { _logger.LogInformation("Added new timer for ISAR '{IsarId}' ('{RobotName}')", robot.IsarId, robot.Name); }
else { _logger.LogWarning("Failed to add new timer for ISAR '{IsarId}' ('{RobotName})'", robot.IsarId, robot.Name); }
}

private async void OnTimeoutEvent(IsarRobotHeartbeatMessage robotHeartbeatMessage)
Expand All @@ -105,15 +103,15 @@ private async void OnTimeoutEvent(IsarRobotHeartbeatMessage robotHeartbeatMessag
if (robot is null)
{
_logger.LogError(
"Connection to ISAR instance '{id}' ('{robotName}') timed out but the corresponding robot could not be found in the database.",
"Connection to ISAR instance '{Id}' ('{RobotName}') timed out but the corresponding robot could not be found in the database",
robotHeartbeatMessage.IsarId,
robotHeartbeatMessage.IsarId
);
}
else
{
_logger.LogWarning(
"Connection to ISAR instance '{id}' timed out - It will be disabled and active missions aborted",
"Connection to ISAR instance '{Id}' timed out - It will be disabled and active missions aborted",
robotHeartbeatMessage.IsarId
);
robot.Enabled = false;
Expand All @@ -124,7 +122,7 @@ private async void OnTimeoutEvent(IsarRobotHeartbeatMessage robotHeartbeatMessag
if (missionRun != null)
{
_logger.LogError(
"Mission '{missionId}' ('{missionName}') failed due to ISAR timeout",
"Mission '{MissionId}' ('{MissionName}') failed due to ISAR timeout",
missionRun.Id,
missionRun.Name
);
Expand All @@ -136,11 +134,10 @@ private async void OnTimeoutEvent(IsarRobotHeartbeatMessage robotHeartbeatMessag
await RobotService.Update(robot);
}

if (_isarConnectionTimers.TryGetValue(robotHeartbeatMessage.IsarId, out var timer))
{
timer.Close();
_isarConnectionTimers.Remove(robotHeartbeatMessage.IsarId, out _);
}
if (!_isarConnectionTimers.TryGetValue(robotHeartbeatMessage.IsarId, out var timer)) { return; }
timer.Close();
_isarConnectionTimers.Remove(robotHeartbeatMessage.IsarId, out _);
_logger.LogError("Removed timer for ISAR instance {RobotName} with ID '{Id}'", robotHeartbeatMessage.RobotName, robotHeartbeatMessage.IsarId);
}
}
}

0 comments on commit d5dcecc

Please sign in to comment.