#include "rabbitmq_communication.h"

RabbitmqCommunicationTof3D::RabbitmqCommunicationTof3D() = default;

RabbitmqCommunicationTof3D::~RabbitmqCommunicationTof3D() = default;


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

//检查执行者的状态, 判断能否处理这条消息, 需要子类重载
Error_manager RabbitmqCommunicationTof3D::check_executer(Rabbitmq_message *p_msg) {
    return {SUCCESS, NORMAL, "Don't check any message."};
}

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

//处理消息, 需要子类重载
Error_manager RabbitmqCommunicationTof3D::execute_time_consume_msg(Rabbitmq_message *p_msg) {
    if (p_msg->m_routing_key.find("dispatch_") != std::string::npos &&
        p_msg->m_routing_key.find("_statu_port") != std::string::npos) {
        dispatch_node_statu t_dispatch_node_statu;
        if(google::protobuf::TextFormat::ParseFromString(p_msg->m_message_buf, &t_dispatch_node_statu))
        {
            // TODO:开关门控制算法流程
//            usleep(5 * 1000 * 1000);
//            DetectManager::iter()->Stop();
//            DeviceTof3D::iter()->Stop();
//
//            usleep(5 * 1000 * 1000);
//            DeviceTof3D::iter()->Continue();
//            DetectManager::iter()->Continue();
        }
    }

    //终端点击存车时, 发送存车表单给检查节点, 这里顺便发送给感测节点.
    if (p_msg->m_routing_key == "user_park_command_request_port") {
        park_table t_park_table_msg; //存车表单
        if(google::protobuf::TextFormat::ParseFromString(p_msg->m_message_buf, &t_park_table_msg))
        {
            // TODO:从算法取数据保存
        }
    }

    return {};
}

//定时封装发送消息, 一般为心跳和状态信息, 需要子类重载
Error_manager RabbitmqCommunicationTof3D::auto_encapsulate_status() {
    static int heart = 0;
    if (++heart > 999) {
        heart = 0;
    }

    measure_buffer rabbit_measure_info;
    DetectManager::DetectResult detect_measure_info;

    // 设备状态
    if (DeviceTof3D::iter()->getDeviceStatus() != SUCCESS) {
        rabbit_measure_info.mutable_measure_info_to_plc_forward()->set_ground_status(MeasureStatu::Lidar_Disconnect);
    }
    MeasureStatu measure_status = DetectManager::iter()->getMeasureResult(detect_measure_info);
    if (measure_status != MeasureStatu::Measure_OK) {
        rabbit_measure_info.mutable_measure_info_to_plc_forward()->set_ground_status(measure_status);
    }

    // 超界判断
    JetStream::MeasureInfo info;
    m_boundary.transMeasureInfo(detect_measure_info, info);
    m_boundary.limit(info);

    // 保存grpc数据中间块
    TransitData::MeasureInfo grpc_measure_info;
    m_boundary.transMeasureInfo(info, grpc_measure_info);
    grpc_measure_info.error.append("heart: " + std::to_string(heart));
    grpc_measure_info.error.append(detect_measure_info.info());
    TransitData::get_instance_pointer()->setMeasureInfo(grpc_measure_info);

    // 发送rabbitmq消息
    m_boundary.transMeasureInfo(info, rabbit_measure_info);
    encapsulate_status_msg(rabbit_measure_info.DebugString(), 0);

    return {};
}

Error_manager RabbitmqCommunicationTof3D::rabbitmq_init_from_protobuf(std::string prototxt_path) {
    m_boundary.init(ETC_PATH PROJECT_NAME "/limit.prototxt") ?
    printf("boundary init success.\n") :
    printf("boundary init failed form %s.\n", ETC_PATH PROJECT_NAME "/limit.prototxt");
    return Rabbitmq_base::rabbitmq_init_from_protobuf(prototxt_path);
}








