Skip to content

Commit

Permalink
Avoid code duplication in sendScript
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-siffert-ocado committed Nov 7, 2024
1 parent bb9d17a commit 7639529
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/ur/ur_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,21 +555,27 @@ bool UrDriver::sendScript(const std::string& program)
const uint8_t* data = reinterpret_cast<const uint8_t*>(program_with_newline.c_str());
size_t written;

if (secondary_stream_->write(data, len, written))
const auto send_script_contents = [this, program_with_newline, data, len,
&written](const std::string&& description) -> bool {
if (secondary_stream_->write(data, len, written))
{
URCL_LOG_DEBUG("Sent program to robot:\n%s", program_with_newline.c_str());
return true;
}
const std::string error_message = "Could not send program to robot: " + description;
URCL_LOG_ERROR(error_message.c_str());
return false;
};

if (send_script_contents("initial attempt"))
{
URCL_LOG_DEBUG("Sent program to robot:\n%s", program_with_newline.c_str());
return true;
}
URCL_LOG_ERROR("Could not send program to robot");

URCL_LOG_INFO("Reconnecting secondary stream to retry sending program...");
secondary_stream_->close();
if (secondary_stream_->connect() && secondary_stream_->write(data, len, written))
if (reconnectSecondaryStream())
{
URCL_LOG_DEBUG("Sent program to robot:\n%s", program_with_newline.c_str());
return true;
return send_script_contents("after reconnecting secondary stream");
}
URCL_LOG_ERROR("Retry sending program failed!");

return false;
}
Expand All @@ -596,7 +602,7 @@ bool UrDriver::reconnectSecondaryStream()
URCL_LOG_DEBUG("Secondary stream connected");
return true;
}
URCL_LOG_ERROR("Failed to reconnect secodary stream!");
URCL_LOG_ERROR("Failed to reconnect secondary stream!");
return false;
}

Expand Down

0 comments on commit 7639529

Please sign in to comment.