//
// Created by zx on 2020/8/28.
//

#include "message_communicator.h"

Message_communicator::Message_communicator()
    :m_ros_data_callback(nullptr)
{
}

Message_communicator::~Message_communicator()
{
}
/*
     * 发送消息
     */
Error_manager Message_communicator::send_msg(Communication_message* p_msg)
{
    return Communication_socket_base::encapsulate_msg(p_msg);
}

//定时封装发送消息, 一般为心跳和状态信息, 需要子类重载
Error_manager Message_communicator::encapsulate_send_data()
{
    return Error_code::SUCCESS;
}

/*
 * 检测消息是否合法
 */
Error_manager Message_communicator::check_msg(Communication_message* p_msg)
{
    if ( (p_msg->get_sender() == Communication_message::Communicator::eAGV
            ||p_msg->get_sender() == Communication_message::Communicator::eDispatch) &&
         (p_msg->get_message_type() == Communication_message::Message_type::ePointCloud_msg
         ||p_msg->get_message_type() == Communication_message::Message_type::ePose2d_msg
          ))
    {
        return Error_code::SUCCESS;
    }
    return Error_code::INVALID_MESSAGE;
}

//检查消息是否可以被处理, 需要重载
Error_manager Message_communicator::check_executer(Communication_message* p_msg)
{
    return SUCCESS;
}

/*
 * 处理接收到的消息
 */
Error_manager Message_communicator::execute_msg(Communication_message* p_msg)
{
    /*
     * 接收终端指令, 生成流程
     */
    Error_manager code;

    switch(p_msg->get_message_type())
    {
        case Communication_message::ePose2d_msg:
        case Communication_message::ePointCloud_msg:
        {
            if (m_ros_data_callback != nullptr)
            {
                m_ros_data_callback(p_msg);
            }
            break;
        }
        default:break;
    }
    return SUCCESS;
}
