/*
 * @Author: yct 18202736439@163.com
 * @Date: 2022-09-30 18:11:59
 * @LastEditors: yct 18202736439@163.com
 * @LastEditTime: 2022-09-30 18:41:52
 * @FilePath: /puai_wj_2021/system/system_communication mq.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
//
// Created by huli on 2020/6/28.
//

#include "system_communication_mq.h"
#include "../system/system_executor.h"

System_communication_mq::System_communication_mq()
{
    //1~6
    for (int j = 1; j < 7; ++j)
    {
        dispatch_region_info t_dispatch_region_info;
        t_dispatch_region_info.set_terminal_id(j);
        t_dispatch_region_info.set_turnplate_angle_min(0);
        t_dispatch_region_info.set_turnplate_angle_max(0);
        dispatch_region_info_map[j] = t_dispatch_region_info;
    }
}

System_communication_mq::~System_communication_mq()
{

}


//检查消息是否有效, 主要检查消息类型和接受者, 判断这条消息是不是给我的.
Error_manager System_communication_mq::check_msg(Rabbitmq_message*  p_msg)
{
	return SUCCESS;
}

//检查执行者的状态, 判断能否处理这条消息, 需要子类重载
Error_manager System_communication_mq::check_executer(Rabbitmq_message*  p_msg)
{
	return SUCCESS;
}

//处理消息, 需要子类重载
Error_manager System_communication_mq::execute_msg(Rabbitmq_message* p_msg)
{
//    return SUCCESS;
    return execute_dispatch_status_msg(p_msg);
}

//定时封装发送消息, 一般为心跳和状态信息, 需要子类重载
Error_manager System_communication_mq::auto_encapsulate_status()
{
//    return SUCCESS;
	return System_executor::get_instance_references().encapsulate_send_mq_status();
}

Error_manager System_communication_mq::execute_dispatch_status_msg(Rabbitmq_message* p_msg)
{
    std::unique_lock<std::mutex> t_lock(m_lock);

    if ( p_msg->m_routing_key == "dispatch_1_statu_port" ||
         p_msg->m_routing_key == "dispatch_2_statu_port" ||
         p_msg->m_routing_key == "dispatch_3_statu_port" )
    {
        dispatch_node_statu t_dispatch_node_statu;
        if(google::protobuf::TextFormat::ParseFromString(p_msg->m_message_buf, &t_dispatch_node_statu))
        {
//            std::cout<< "hulixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << t_dispatch_node_statu.DebugString() << std::endl;
//            LOG(INFO) << "hulixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << t_dispatch_node_statu.DebugString() << this;

            for (int j = 0; j < t_dispatch_node_statu.dispatch_region_info_vector_size() ; ++j)
            {
                int t_terminal = t_dispatch_node_statu.dispatch_region_info_vector(j).terminal_id();
                dispatch_region_info_map[t_terminal] = t_dispatch_node_statu.dispatch_region_info_vector(j);

//                LOG(INFO) << "hulixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx t_terminal = " << t_terminal << "  ";
//                LOG(INFO) << "hulixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx " << dispatch_region_info_map[t_terminal].terminal_id() << "  ";
//                LOG(INFO) << "hulixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx " << dispatch_region_info_map[t_terminal].turnplate_angle_min()<< "  ";
//                LOG(INFO) << "hulixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx " << dispatch_region_info_map[t_terminal].turnplate_angle_max()<< "  ";

            }
        }
    }





    return SUCCESS;
}







