/*
本程序用于使用cpp编写的ros2服务端节点
*/
#include "rclcpp/rclcpp.hpp"
#include "cpp_prococol/srv/position.hpp"

using Positon = cpp_prococol::srv::Position;

class cppServerService:public rclcpp::Node
{
public:
    cppServerService():Node("cpp_server_service")
    {
        // 创建服务器对象
        service_ = this->create_service<Positon>("position",std::bind(&cppServerService::handle_service,this,std::placeholders::_1,std::placeholders::_2));
        // 打印服务端启动成功的日志信息
        RCLCPP_INFO(this->get_logger(),"cpp server service is ready.");
    }

    // 处理服务请求的回调函数
    /*
    Params:
    request: 请求数据，包含客户端发送的请求内容
    response: 响应数据，服务器将通过这个参数发送响应内容给客户端
    */
    void handle_service(const std::shared_ptr<Positon::Request> request,std::shared_ptr<Positon::Response> response)
    {
        // 打印接收到的请求数据
        RCLCPP_INFO(this->get_logger(),"Receiverd request: target_x =%f, target_y = %f",request->target_x,request->target_y);   

        //处理请求数据,生成结果
        if(request->target_x == 0.0 && request->target_y == 0.0)
        {
            response->result = Positon::Response::FALL;
        }
        else
        {
            response->result = Positon::Response::SUCCESS;
        }
    }
private:
    // 声明服务端对象
    rclcpp::Service<Positon>::SharedPtr service_;
};



int main(int argc, char **argv)
{
    // 初始化ROS2客户端库
    rclcpp::init(argc,argv);
    
    auto node = std::make_shared<cppServerService>();

    rclcpp::spin(node);

    rclcpp::shutdown();
}