Skip to content

Implement POST /components/{component_id}/services/{service_name} endpoint to enable calling ROS 2 services on components #30

@mfaferek93

Description

@mfaferek93

Summary

Implement POST /components/{component_id}/services/{service_name} endpoint to enable calling ROS 2 services on components via the REST API. This endpoint allows external systems to invoke service operations (calibration, configuration, etc.) through the gateway, enabling remote service management capabilities.

The endpoint should:

  • Accept JSON payloads with service type and request data
  • Validate service type format
  • Convert JSON to YAML for ROS 2 CLI
  • Call services using ros2 service call
  • Parse and return service response
  • Handle synchronous service execution

Proposed solution (optional)

  1. Create ServiceManager (include/ros2_medkit_gateway/service_manager.hpp + src/service_manager.cpp):
  • Method: ServiceResponse call_service(const std::string& service_path, const std::string& srv_type, const nlohmann::json& request)
  • Use existing ros2_cli_wrapper to execute ros2 service call
  • Use existing output_parser to parse YAML response from CLI
  • Return structured service response with success/failure status
  1. Integrate with GatewayNode (src/gateway_node.cpp):
  • Similar pattern to DataAccessManager integration
  1. Add REST Handler (src/rest_server.cpp):
  • Implement POST /components/{component_id}/services/{service_name} handler
  • Extract component_id and service_name from URL path
  • Parse JSON request body, validate required fields: type, request
  • Validate service type format using existing validators
  • Construct full service path from component namespace
  • Call service_manager->call_service()
  • Return service response
  1. Request/Response Format:
  Request body (SetBool service):
  {
    "type": "std_srvs/srv/SetBool",
    "request": {"data": true}
  }

  Success response (200) - SetBool:
  {
    "success": true,
    "message": "Lights turned ON"
  }

  Request body (Trigger service):
  {
    "type": "std_srvs/srv/Trigger",
    "request": {}
  }

  Success response (200) - Trigger:
  {
    "success": true,
    "message": "Calibration completed successfully"
  }

  Error response (400):
  {
    "error": "Missing required field",
    "field": "type"
  }

  Error response (404):
  {
    "error": "Component not found",
    "component_id": "nonexistent"
  }

Additional context (optional)

Example Use Cases

Engine Calibration:

  # Trigger engine calibration
  curl -X POST http://localhost:8080/components/calibration/services/calibrate \
    -H "Content-Type: application/json" \
    -d '{"type":"std_srvs/srv/Trigger","request":{}}'

  # Response:
  # {"success": true, "message": "Calibration completed successfully"}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions