8#include <ament_index_cpp/get_package_share_directory.hpp>
11#include <rclcpp/rclcpp.hpp>
12#include <pluginlib/class_loader.hpp>
22#include <fabric_msgs/srv/set_fabric_plan.hpp>
23#include <fabric_msgs/srv/cancel_fabric_plan.hpp>
24#include <fabric_msgs/srv/complete_fabric.hpp>
25#include <fabric_msgs/srv/get_plan_status.hpp>
26#include <fabric_msgs/msg/fabric_status.hpp>
48 Fabric(
const rclcpp::NodeOptions& options = rclcpp::NodeOptions())
49 : Node(
"Fabric", options)
56 if (shared_from_this())
61 catch (
const std::bad_weak_ptr&)
76 const std::string default_plan_file_path = ament_index_cpp::get_package_share_directory(
"fabric_server") +
"/plans/default.xml";
77 this->declare_parameter(
"plan_file_path", default_plan_file_path);
83 plan_server_ = this->create_service<SetFabricPlan>(
"/fabric/set_plan",
99 this->declare_parameter(
"compatibility_validation_plugin",
"fabric::CompatibilityValidation");
100 std::string compatibility_validation_plugin_name = this->get_parameter(
"compatibility_validation_plugin").as_string();
102 RCLCPP_INFO(this->get_logger(),
"[server] Loading compatibility validation plugin: %s", compatibility_validation_plugin_name.c_str());
107 RCLCPP_INFO(this->get_logger(),
"[server] Initialized compatibility validation plugin: %s", compatibility_validation_plugin_name.c_str());
113 this->declare_parameter(
"parsing_plugin",
"fabric::XMLParser");
114 std::string parsing_plugin_name = this->get_parameter(
"parsing_plugin").as_string();
116 RCLCPP_INFO(this->get_logger(),
"[server] Loading parsing plugin: %s", parsing_plugin_name.c_str());
121 RCLCPP_INFO(this->get_logger(),
"[server] Initialized parsing plugin: %s", parsing_plugin_name.c_str());
135 RCLCPP_ERROR(this->get_logger(),
"[server] Failed to load default plan from file: %s",
plan_file_path_.c_str());
143 RCLCPP_INFO(this->get_logger(),
"[server] Fabric node initialized");
164 RCLCPP_INFO(this->get_logger(),
"[server] A new Fabric plan processing starting");
172 RCLCPP_INFO(this->get_logger(),
"[server] Skipping cancelled plan with id: %s",
current_plan_.
plan_id.c_str());
179 RCLCPP_INFO(this->get_logger(),
"[server] Parsing the fabric plan.");
186 RCLCPP_ERROR(this->get_logger(),
"[server] Fabric plan parsing failed with error: %s", e.
what());
190 RCLCPP_INFO(this->get_logger(),
"[server] Fabric plan parsing completed successfully.");
195 RCLCPP_INFO(this->get_logger(),
"[server] Getting capabilities required for the plan.");
203 RCLCPP_ERROR(this->get_logger(),
"[server] Capability information retrieval failed with error: %s", e.
what());
208 RCLCPP_INFO(this->get_logger(),
"[server] Capability information retrieval completed successfully.");
213 RCLCPP_INFO(this->get_logger(),
"[server] Validating the fabric plan for compatibility.");
219 RCLCPP_ERROR(this->get_logger(),
"[server] Compatibility validation failed with error: %s", e.
what());
227 RCLCPP_INFO(this->get_logger(),
"[server] Requesting bond from capabilities2 server.");
234 RCLCPP_ERROR(this->get_logger(),
"[server] Capability bond request failed with error: %s", e.
what());
240 RCLCPP_INFO(this->get_logger(),
"[server] Establishing bond with id : %s",
current_plan_.
bond_id.c_str());
250 RCLCPP_INFO(this->get_logger(),
"[server] Stopping and removing old bond with id : %s", old_bond_id.c_str());
253 RCLCPP_INFO(this->get_logger(),
"[server] Bond established with id : %s",
current_plan_.
bond_id.c_str());
258 RCLCPP_INFO(this->get_logger(),
"[server] Requesting use of capabilities for the plan.");
265 RCLCPP_ERROR(this->get_logger(),
"[server] Capability usage failed with error: %s", e.
what());
275 RCLCPP_INFO(this->get_logger(),
"[server] Connecting capabilities as per the plan.");
282 RCLCPP_ERROR(this->get_logger(),
"[server] Capability connection failed with error: %s", e.
what());
289 RCLCPP_INFO(this->get_logger(),
"[server] Capabilities connected successfully.");
294 RCLCPP_INFO(this->get_logger(),
"[server] Triggering the first capability in the plan.");
301 RCLCPP_ERROR(this->get_logger(),
"[server] Capability trigger failed with error: %s", e.
what());
304 RCLCPP_INFO(this->get_logger(),
"[server] First capability triggered successfully.");
314 RCLCPP_INFO(this->get_logger(),
"[server] Fabric plan cancelled. Releasing capabilities.");
320 RCLCPP_INFO(this->get_logger(),
"[server] Fabric processing completed. Waiting for next plan.");
327 void setPlanCallback(
const std::shared_ptr<SetFabricPlan::Request> request, std::shared_ptr<SetFabricPlan::Response> response)
329 RCLCPP_INFO(this->get_logger(),
"[server] Received the request with a plan");
332 new_plan.
plan = request->plan;
338 RCLCPP_ERROR(this->get_logger(),
"[server] Plan received via service request not compatible with the loaded parser.");
339 response->plan_id =
"";
340 response->error =
"[server] Plan is not compatible with the loaded parser.";
343 RCLCPP_INFO(this->get_logger(),
"[server] Plan accepted from service request message");
346 response->plan_id = new_plan.
plan_id;
352 void cancelPlanCallback(
const std::shared_ptr<CancelFabricPlan::Request> request, std::shared_ptr<CancelFabricPlan::Response> response)
354 RCLCPP_INFO(this->get_logger(),
"[server] Plan cancelling requested");
355 std::string plan_id = request->plan_id;
356 std::string bond_id_to_cancel;
372 if (plan.plan_id == plan_id)
375 bond_id_to_cancel = plan.bond_id;
383 if (!bond_id_to_cancel.empty())
385 RCLCPP_INFO(this->get_logger(),
"[server] Cancelling plan with id: %s", plan_id.c_str());
389 if (bond_id == bond_id_to_cancel)
392 RCLCPP_INFO(this->get_logger(),
"[server] Bond with id : %s stopped", bond_id.c_str());
398 response->success = found;
404 void setCompleteCallback(
const std::shared_ptr<CompleteFabric::Request> request, std::shared_ptr<CompleteFabric::Response> response)
407 RCLCPP_INFO(this->get_logger(),
"[server] Plan completion received for plan id: %s", request->plan_id.c_str());
411 RCLCPP_WARN(this->get_logger(),
"[server] Received plan id does not match the current plan id");
415 RCLCPP_INFO(this->get_logger(),
"[server] Current plan id matches the received plan id. Proceeding to complete the plan.");
424 void getPlanStatusCallback(
const std::shared_ptr<GetPlanStatus::Request> request, std::shared_ptr<GetPlanStatus::Response> response)
426 response->header.stamp = this->now();
442 if (plan.plan_id == request->plan_id)
447 response->rejected_list = plan.rejected_list;
454 response->status.code = fabric_msgs::msg::FabricStatus::UNKNOWN;
455 response->rejected_list.clear();
467 status_msg.code = fabric_msgs::msg::FabricStatus::UNKNOWN;
470 status_msg.code = fabric_msgs::msg::FabricStatus::QUEUED;
473 status_msg.code = fabric_msgs::msg::FabricStatus::PARSING;
476 status_msg.code = fabric_msgs::msg::FabricStatus::PARSE_FAILED;
479 status_msg.code = fabric_msgs::msg::FabricStatus::VALIDATING;
482 status_msg.code = fabric_msgs::msg::FabricStatus::VALIDATION_FAILED;
485 status_msg.code = fabric_msgs::msg::FabricStatus::BONDING;
488 status_msg.code = fabric_msgs::msg::FabricStatus::BOND_FAILED;
491 status_msg.code = fabric_msgs::msg::FabricStatus::CAPABILITY_STARTING;
494 status_msg.code = fabric_msgs::msg::FabricStatus::CAPABILITY_START_FAILED;
497 status_msg.code = fabric_msgs::msg::FabricStatus::CAPABILITY_CONNECTING;
500 status_msg.code = fabric_msgs::msg::FabricStatus::CAPABILITY_CONNECT_FAILED;
503 status_msg.code = fabric_msgs::msg::FabricStatus::RUNNING;
506 status_msg.code = fabric_msgs::msg::FabricStatus::COMPLETED;
509 status_msg.code = fabric_msgs::msg::FabricStatus::CANCELLED;
512 status_msg.code = fabric_msgs::msg::FabricStatus::UNKNOWN;
523 uuid_generate_random(uuid);
525 uuid_unparse(uuid, uuid_str);
526 return std::string(uuid_str);
Class representing the main server of the fabric.
Definition server.hpp:36
std::shared_ptr< fabric::ParserBase > parsing_plugin_
Definition server.hpp:563
std::vector< CapabilityInfo > capability_list_
Definition server.hpp:556
std::shared_ptr< fabric::ValidationBase > compatibility_validation_plugin_
Definition server.hpp:566
fabric_msgs::srv::CompleteFabric CompleteFabric
Definition server.hpp:40
void getPlanStatusCallback(const std::shared_ptr< GetPlanStatus::Request > request, std::shared_ptr< GetPlanStatus::Response > response)
Callback function to get the status of a fabric plan.
Definition server.hpp:424
pluginlib::ClassLoader< fabric::ValidationBase > validation_loader_
Definition server.hpp:559
std::string bond_id_
Definition server.hpp:569
bool plan_completed_
Definition server.hpp:548
Fabric(const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
Construct a new Fabric object.
Definition server.hpp:48
std::map< std::string, std::shared_ptr< BondClient > > bond_client_cache_
Definition server.hpp:572
void process()
The main process thread of the fabric.
Definition server.hpp:157
void cancelPlanCallback(const std::shared_ptr< CancelFabricPlan::Request > request, std::shared_ptr< CancelFabricPlan::Response > response)
Callback function to cancel a fabric plan.
Definition server.hpp:352
static std::string generate_uuid()
Generate a UUID string for plan tracking.
Definition server.hpp:520
fabric::Plan current_plan_
Definition server.hpp:545
void reset()
Reset internal data structures for processing a new plan.
Definition server.hpp:533
fabric_msgs::srv::CancelFabricPlan CancelFabricPlan
Definition server.hpp:39
std::condition_variable plan_cv_
Definition server.hpp:550
std::shared_ptr< CapabilityClient > capability_client_
Definition server.hpp:553
void setPlanCallback(const std::shared_ptr< SetFabricPlan::Request > request, std::shared_ptr< SetFabricPlan::Response > response)
Callback function to set a new fabric plan.
Definition server.hpp:327
fabric_msgs::srv::GetPlanStatus GetPlanStatus
Definition server.hpp:41
rclcpp::Service< CancelFabricPlan >::SharedPtr cancel_server_
Definition server.hpp:581
rclcpp::Service< SetFabricPlan >::SharedPtr plan_server_
Definition server.hpp:578
std::deque< fabric::Plan > plan_queue_
Definition server.hpp:542
rclcpp::Service< CompleteFabric >::SharedPtr completion_server_
Definition server.hpp:584
void setCompleteCallback(const std::shared_ptr< CompleteFabric::Request > request, std::shared_ptr< CompleteFabric::Response > response)
Callback function to set the completion of a fabric plan.
Definition server.hpp:404
fabric_msgs::srv::SetFabricPlan SetFabricPlan
Definition server.hpp:38
fabric_msgs::msg::FabricStatus status_msg(PlanStatus status) const
Convert internal plan status to fabric_msgs/PlanStatus message.
Definition server.hpp:461
rclcpp::Service< GetPlanStatus >::SharedPtr get_plan_status_server_
Definition server.hpp:587
void initialize()
Initialize the Fabric node, action server and event client.
Definition server.hpp:71
std::string plan_file_path_
Definition server.hpp:575
std::thread process_thread
Definition server.hpp:590
std::mutex plan_mutex_
Definition server.hpp:549
pluginlib::ClassLoader< fabric::ParserBase > parsing_loader_
Definition server.hpp:560
Definition parser_base.hpp:9
PlanStatus
Definition structs.hpp:51
@ CAPABILITY_CONNECT_FAILED
@ CAPABILITY_START_FAILED
Definition structs.hpp:70
std::vector< std::string > rejected_list
Definition structs.hpp:75
PlanStatus status
Definition structs.hpp:76
std::string plan
Definition structs.hpp:72
std::string plan_id
Definition structs.hpp:71
std::string bond_id
Definition structs.hpp:73
Base class for driver exceptions.
Definition exception.hpp:14
virtual const char * what() const noexcept override
Definition exception.hpp:21