//
// Created by huli on 2020/7/20.
//

#include "dispatch_manager.h"
#include "../tool/proto_tool.h"
#include <google/protobuf/text_format.h>

#include <cstdlib>

Dispatch_manager::Dispatch_manager()
{
	m_dispatch_manager_status = E_DISPATCH_MANAGER_UNKNOW;
	m_dispatch_manager_id = -1;
//	m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;

	m_dispatch_manager_thread = nullptr;

	m_device_floor = 1;
	m_count_flag = false;
	m_post_flag = false;

	m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;

	m_plc_floor = 0;
	m_car_type = Common_data::Car_type::UNKNOW_CAR_TYPE;
}

Dispatch_manager::~Dispatch_manager()
{
	dispatch_manager_uninit();
}



//调度管理 初始化
Error_manager Dispatch_manager::dispatch_manager_init(int dispatch_manager_id)
{
	m_dispatch_manager_id = dispatch_manager_id;
	return  dispatch_manager_init_from_protobuf(DISPATCH_DEVICE_PARAMETER_PATH);
}

//调度管理 初始化
Error_manager Dispatch_manager::dispatch_manager_init()
{
	return  dispatch_manager_init_from_protobuf(DISPATCH_DEVICE_PARAMETER_PATH);
}

//初始化 调度管理 模块。从文件读取
Error_manager Dispatch_manager::dispatch_manager_init_from_protobuf(std::string prototxt_path)
{
	Dispatch_proto::Dispatch_device_parameter_all t_dispatch_device_parameter_all;
	if(!  proto_tool::read_proto_param(prototxt_path,t_dispatch_device_parameter_all) )
	{
		return Error_manager(DISPATCH_MANAGER_READ_PROTOBUF_ERROR,MINOR_ERROR,
							 "Dispatch_manager read_proto_param  failed");
	}
	return dispatch_manager_init_from_protobuf(t_dispatch_device_parameter_all);
}
//初始化 调度管理 模块。从protobuf读取
Error_manager Dispatch_manager::dispatch_manager_init_from_protobuf(Dispatch_proto::Dispatch_device_parameter_all& dispatch_device_parameter_all)
{
	LOG(INFO) << " ----Dispatch_manager::dispatch_manager_init_from_protobuf----- "<< this;

	Error_manager t_error;

	m_dispatch_plc.dispatch_plc_init(m_dispatch_manager_id);
	m_dispatch_ground_lidar[0].dispatch_ground_lidar_init(m_dispatch_manager_id, 0);
	m_dispatch_ground_lidar[1].dispatch_ground_lidar_init(m_dispatch_manager_id, 1);
	m_dispatch_singlechip[0].dispatch_singlechip_init(m_dispatch_manager_id, 0, 1);
	m_dispatch_singlechip[1].dispatch_singlechip_init(m_dispatch_manager_id, 1, 1);
	m_dispatch_singlechip[2].dispatch_singlechip_init(m_dispatch_manager_id, 0, 2);
	m_dispatch_singlechip[3].dispatch_singlechip_init(m_dispatch_manager_id, 1, 2);

//	m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;

	//	线程默认开启
	m_dispatch_manager_condition.reset(false, true, false);
//	m_dispatch_manager_thread = new std::thread(&Dispatch_manager::resource_allocation, this);
//	m_dispatch_manager_thread = new std::thread(&Dispatch_manager::process_control, this);
	m_dispatch_manager_thread = new std::thread(&Dispatch_manager::process_sql, this);

	m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
	return Error_code::SUCCESS;
}

//调度管理 反初始化
Error_manager Dispatch_manager::dispatch_manager_uninit()
{
	if (m_dispatch_manager_thread)
	{
		m_dispatch_manager_condition.kill_all();
	}
	if (m_dispatch_manager_thread)
	{
		m_dispatch_manager_thread->join();
		delete m_dispatch_manager_thread;
		m_dispatch_manager_thread = NULL;
	}


	m_dispatch_manager_status = E_DISPATCH_MANAGER_UNKNOW;
	m_dispatch_manager_id = -1;

	m_dispatch_plc.dispatch_plc_uninit();
	m_dispatch_ground_lidar[0].dispatch_ground_lidar_uninit();
	m_dispatch_ground_lidar[1].dispatch_ground_lidar_uninit();
	m_dispatch_singlechip[0].dispatch_singlechip_uninit();
	m_dispatch_singlechip[1].dispatch_singlechip_uninit();
	m_dispatch_singlechip[2].dispatch_singlechip_uninit();
	m_dispatch_singlechip[3].dispatch_singlechip_uninit();


	return Error_code::SUCCESS;
}

//调度管理 设备复位
Error_manager Dispatch_manager::dispatch_manager_device_reset()
{
	//以后再写
	return Error_code::SUCCESS;
}

//新的对外接口, 负责接受Rabbitmq_message,然后内部处理
Error_manager Dispatch_manager::execute_rabbitmq_message_new(Rabbitmq_message* p_msg)
{
	std::unique_lock<std::mutex> t_lock(m_lock);

	//通过key来判断消息类型
	if ( p_msg->m_routing_key == "measure_1_statu_port" ||
			  p_msg->m_routing_key == "measure_3_statu_port" ||
			  p_msg->m_routing_key == "measure_5_statu_port" )
	{
		m_measure_statu_msg_1 = *p_msg;
		if(google::protobuf::TextFormat::ParseFromString(m_measure_statu_msg_1.m_message_buf, &m_measure_info_1))
		{
			m_dispatch_ground_lidar[0].execute_for_ground_status_msg_new(m_measure_info_1);
		}
	}
	else if ( p_msg->m_routing_key == "measure_2_statu_port" ||
			  p_msg->m_routing_key == "measure_4_statu_port" ||
			  p_msg->m_routing_key == "measure_6_statu_port" )
	{
//		if ( p_msg->m_routing_key == "measure_2_statu_port" )
//		{
//			std::cout << " huli test :::: " << " nnnnnnnnnnnnnnnnnnnnnhhhhhhhhhhhhhhhhhhhhhhhhhh = " << p_msg->m_message_buf  << std::endl;
//		}

		m_measure_statu_msg_2 = *p_msg;
		if(google::protobuf::TextFormat::ParseFromString(m_measure_statu_msg_2.m_message_buf, &m_measure_info_2))
		{
			m_dispatch_ground_lidar[1].execute_for_ground_status_msg_new(m_measure_info_2);
		}
	}
	else if ( p_msg->m_routing_key == "in_mcpu_1_statu_port" ||
			  p_msg->m_routing_key == "in_mcpu_3_statu_port" ||
			  p_msg->m_routing_key == "in_mcpu_5_statu_port" )
	{
//		std::cout << " huli test :::: " << " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = " << p_msg->m_message_buf << std::endl;


//		if ( p_msg->m_routing_key == "in_mcpu_3_statu_port" )
//		{
//		    std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbffffffffffffffffff in_mcpu_3_statu_port= " << p_msg->m_message_buf  << std::endl;
//
//		}

		m_in_mcpu_statu_msg_1 = *p_msg;
		if(google::protobuf::TextFormat::ParseFromString(m_in_mcpu_statu_msg_1.m_message_buf, &m_in_mcpu_statu_1))
		{
			m_dispatch_singlechip[0].execute_for_singlechip_data_msg_new(m_in_mcpu_statu_1, true);
		}
	}
	else if ( p_msg->m_routing_key == "in_mcpu_2_statu_port" ||
			  p_msg->m_routing_key == "in_mcpu_4_statu_port" ||
			  p_msg->m_routing_key == "in_mcpu_6_statu_port" )
	{
//		std::cout << " huli test :::: " << " cccccccccccccccccccccccccccccccccccccccccccccccccc = " << p_msg->m_message_buf << std::endl;

//		if ( p_msg->m_routing_key == "in_mcpu_4_statu_port" )
//		{
//		    std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbffffffffffffffffff in_mcpu_4_statu_port= " << p_msg->m_message_buf  << std::endl;
//		}

		m_in_mcpu_statu_msg_2 = *p_msg;
		if(google::protobuf::TextFormat::ParseFromString(m_in_mcpu_statu_msg_2.m_message_buf, &m_in_mcpu_statu_2))
		{
			m_dispatch_singlechip[1].execute_for_singlechip_data_msg_new(m_in_mcpu_statu_2, true);
		}
	}
	else if ( p_msg->m_routing_key == "out_mcpu_1_statu_port" ||
			  p_msg->m_routing_key == "out_mcpu_3_statu_port" ||
			  p_msg->m_routing_key == "out_mcpu_5_statu_port" )
	{
//		if ( p_msg->m_routing_key == "out_mcpu_1_statu_port" )
//		{
//			std::cout << " huli test :::: " << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv out_mcpu_1_statu_port= " << p_msg->m_message_buf << std::endl;
//		}
//		std::cout << " huli test :::: " << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv = " << p_msg->m_message_buf << std::endl;

		m_out_mcpu_statu_msg_1 = *p_msg;
		if(google::protobuf::TextFormat::ParseFromString(m_out_mcpu_statu_msg_1.m_message_buf, &m_out_mcpu_statu_1))
		{
			m_dispatch_singlechip[2].execute_for_singlechip_data_msg_new(m_out_mcpu_statu_1, true);
		}
	}
	else if ( p_msg->m_routing_key == "out_mcpu_2_statu_port" ||
			  p_msg->m_routing_key == "out_mcpu_4_statu_port" ||
			  p_msg->m_routing_key == "out_mcpu_6_statu_port" )
	{
//		if ( p_msg->m_routing_key == "out_mcpu_2_statu_port" )
//		{
//			std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb out_mcpu_2_statu_port= " << p_msg->m_message_buf << std::endl;
//		}
//		std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = " << p_msg->m_message_buf << std::endl;

		m_out_mcpu_statu_msg_2 = *p_msg;
		if(google::protobuf::TextFormat::ParseFromString(m_out_mcpu_statu_msg_2.m_message_buf, &m_out_mcpu_statu_2))
		{
			m_dispatch_singlechip[3].execute_for_singlechip_data_msg_new(m_out_mcpu_statu_2, true);
		}
	}
	else
	{
	    LOG(INFO) << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx error Rabbitmq_message =  "<< p_msg->m_message_buf << " --- " << this;
//		park_table t_park_table;
//		if(google::protobuf::TextFormat::ParseFromString(p_msg->m_message_buf, &t_park_table))
//		{
//			std::string t_str = t_park_table.DebugString();
//			std::cout << " huli test :::: " << " t_str = " << t_str << std::endl;
//
//			std::string t_str1 = t_park_table.statu().statu_description();
//			std::cout << " huli test :::: " << " t_str1 = " << t_str1 << std::endl;
//
//			std::string t_str2 = t_park_table.car_number();
//			std::cout << " huli test :::: " << " t_str2 = " << t_str2 << std::endl;
//
//		}
	}


	return Error_code::SUCCESS;
}

//给dispatch_plc底层提供接口, ack答复服务器,
Error_manager Dispatch_manager::ack_rabbitmq_message_new()
{
	if ( m_count_flag )
	{
		m_count_flag = false;
		System_communication::get_instance_references().ack_msg(&m_count_command_signal_msg);

	}
	if ( m_post_flag )
	{
		m_post_flag = false;
		System_communication::get_instance_references().ack_msg(&m_post_command_msg);
	}
	m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;

	return Error_code::SUCCESS;
}

//网络通信的对外接口. 对单片机通信
Error_manager Dispatch_manager::execute_network_message_new(Network_message* p_msg)
{
	std::unique_lock<std::mutex> t_lock(m_lock);

	//根据自定义的socket_id, 分发给4个单片机模块.
	if ( 0 <= p_msg->m_socket_id && p_msg->m_socket_id < 4 )
	{
		return m_dispatch_singlechip[p_msg->m_socket_id].execute_for_singlechip_data_msg_new(p_msg->m_message_buf);
	}

	return Error_code::SUCCESS;
}

//对外的接口函数，负责接受并处理任务单，
//Error_manager Dispatch_manager::execute_task(Dispatch_manager::Dispatch_motion_direction dispatch_motion_direction)
//{
//	return Error_code::SUCCESS;
////	std::this_thread::sleep_for(std::chrono::seconds(rand()%3+3));
//
//	if ( dispatch_motion_direction == E_PICKUP_CAR )
//	{
//		return Error_code::SUCCESS;
//	}
//
////	return Error_code::SUCCESS;
//
////	srand(0);
//	unsigned int t_probability = rand();
//
//	if ( t_probability%100 >=20 )
//	{
//		return Error_code::SUCCESS;
//	}
//	else
//	{
//		return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
//							 " Dispatch_manager::execute_task() error ");
//	}
//}

//检查能否执行消息指令
Error_manager Dispatch_manager::check_execute_msg(Rabbitmq_message* p_msg)
{
	return Error_code::SUCCESS;
	/*
	Error_manager t_error = Dispatch_manager::get_instance_references().check_status();
	if ( t_error == Error_code::SUCCESS )
	{
		return Error_code::SUCCESS;
	}
	else if (t_error.get_error_level() == NEGLIGIBLE_ERROR)//一级故障,轻微故障,
	{
		std::cout << "Dispatch_manager   _is_busy , " << std::endl;
		//返回繁忙之后, 通信模块1秒后再次调用check
		return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
	}

	switch ( p_msg->get_message_type() )
	{
		case Communication_message::Message_type::eDispatch_request_msg:
		{
			return Error_code::SUCCESS;
			break;
		}
		case Communication_message::Message_type::eDispatch_plan_response_msg:
		{
			return Error_code::SUCCESS;
			break;
		}
		case Communication_message::Message_type::eDispatch_control_request_msg:
		{
			return Error_code::SUCCESS;
			break;
		}
		default :
		{
			//无效的消息,
			return Error_manager(Error_code::INVALID_MESSAGE, Error_level::NEGLIGIBLE_ERROR,
								 " INVALID_MESSAGE error ");
			break;
		}
	}
	 */
	return Error_code::SUCCESS;
}
//检查状态
Error_manager Dispatch_manager::check_status()
{
	if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_READY )
	{
		return Error_code::SUCCESS;
	}
	else if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_STORE || m_dispatch_manager_status == E_DISPATCH_MANAGER_PICKUP )
	{
		return Error_code::SUCCESS;
		//无论存取, 状态都是返回正常,运行接受下一个任务
//		return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
//							 " Dispatch_manager::check_status() error ");
	}
	else
	{
		return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_ERROR, Error_level::MINOR_ERROR,
							 " Dispatch_manager::check_status() error ");
	}
	return Error_code::SUCCESS;
}

//调度模块 //执行搬运请求(主控->调度管理)
//Error_manager Dispatch_manager::execute_for_dispatch_request_msg(message::Dispatch_request_msg& dispatch_request_msg)
//{
//	LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_request_msg--- "<< dispatch_request_msg.DebugString() << " "<< this;
//	LOG(INFO) << " dispatch_request_msg->m_command_key =  "<<dispatch_request_msg.command_key()<< "   "<< this;
//
//	Error_manager t_error;
//
//	std::unique_lock<std::mutex> t_lock(m_lock);
//	if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
//	{
//		m_dispatch_request_store_list.push_back(dispatch_request_msg);
//		m_store_updata_time = std::chrono::system_clock::now();
//	}
//	else
//	{
//		m_dispatch_request_pickup_list.push_back(dispatch_request_msg);
//		m_pickup_updata_time = std::chrono::system_clock::now();
//	}
//
//
//
//	//need, 目前直接转发给调度plc执行, 后续要存队列,然后增加调度逻辑.
//
////	m_dispatch_plc.execute_for_dispatch_request_msg(dispatch_request_msg);
//
//
//
//	return Error_code::SUCCESS;
//}
////调度模块 答复数据异常
//Error_manager Dispatch_manager::send_dispatch_response_msg_with_error(message::Dispatch_request_msg &dispatch_request_msg, Error_manager error)
//{
//	message::Dispatch_response_msg t_dispatch_response_msg;
//	t_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
//	t_dispatch_response_msg.mutable_base_info()->set_timeout_ms(dispatch_request_msg.base_info().timeout_ms());
//	t_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
//	t_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
//	t_dispatch_response_msg.set_command_key(dispatch_request_msg.command_key());
//
//	t_dispatch_response_msg.mutable_error_manager()->set_error_code(error.get_error_code());
//	t_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)error.get_error_level());
//	t_dispatch_response_msg.mutable_error_manager()->set_error_description(error.get_error_description());
//
////	System_communication::get_instance_references().encapsulate_msg(t_dispatch_response_msg.SerializeAsString());
//	System_communication::get_instance_references().encapsulate_task_msg(t_dispatch_response_msg.SerializeAsString());
//	return Error_code::SUCCESS;
//}
//
////调度模块 //调度总规划的答复(调度算法->调度管理)
//Error_manager Dispatch_manager::execute_for_dispatch_plan_response_msg(message::Dispatch_plan_response_msg &dispatch_plan_response_msg)
//{
//	LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_plan_response_msg--- "<< this;
//	LOG(INFO) << " dispatch_plan_response_msg->m_command_key =  "<<dispatch_plan_response_msg.command_key()<< "   "<< this;
//
//	Error_manager t_error;
//
//	return Error_code::SUCCESS;
//}
////调度模块 //调度控制的任务请求(调度算法->调度管理)
//Error_manager Dispatch_manager::execute_for_dispatch_control_request_msg(message::Dispatch_control_request_msg &dispatch_control_request_msg)
//{
//	LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_control_request_msg--- "<< this;
//	LOG(INFO) << " dispatch_control_request_msg->m_command_key =  "<<dispatch_control_request_msg.command_key()<< "   "<< this;
//
//	Error_manager t_error;
//
//	return Error_code::SUCCESS;
//}

//定时发送 调度管理的状态
Error_manager Dispatch_manager::encapsulate_send_dispatch_manager_status()
{
//	Error_manager t_error;
//
//	int t_dispatch_manager_id = get_dispatch_manager_id();
//
//	std::string t_msg1;
//	//创建一条 调度管理总管理的状态  旧版
//	message::Dispatch_terminal_status_msg t_dispatch_terminal_status_msg;
//	t_dispatch_terminal_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_status_msg);
//	t_dispatch_terminal_status_msg.mutable_base_info()->set_timeout_ms(5000);
//	t_dispatch_terminal_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
//	t_dispatch_terminal_status_msg.mutable_base_info()->set_receiver(message::Communicator::eEmpty);
//
//	t_dispatch_terminal_status_msg.mutable_id_struct()->set_unit_id(t_dispatch_manager_id);
//	t_dispatch_terminal_status_msg.set_terminal_status(message::Terminal_status(m_dispatch_manager_status));
//	t_dispatch_terminal_status_msg.set_passageway_direction(message::Passageway_direction::E_BILATERAL);
//
//
//	//E_DISPATCH_POCESS_OVER
//	{
//		std::unique_lock<std::mutex> t_lock(m_lock);
//		for (auto iter = m_dispatch_response_store_map.begin(); iter != m_dispatch_response_store_map.end(); ++iter)
//		{
//			message::Dispatch_process_information * tp_dispatch_process_information =
//			t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
//			tp_dispatch_process_information->set_car_numberplate(iter->second.parkspace_info_ex(0).car_info().car_numberplate());
//			tp_dispatch_process_information->set_command_key(iter->second.command_key());
//			tp_dispatch_process_information->set_dispatch_motion_direction(iter->second.dispatch_motion_direction());
//			tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->second.id_struct());
//			tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->second.locate_information());
//			tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->second.parkspace_info_ex());
//			tp_dispatch_process_information->set_car_type(iter->second.car_type());
//
//			tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_OVER);
//		}
//		for (auto iter = m_dispatch_response_pickup_map.begin(); iter != m_dispatch_response_pickup_map.end(); ++iter)
//		{
//			message::Dispatch_process_information * tp_dispatch_process_information =
//			t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
//			tp_dispatch_process_information->set_car_numberplate(iter->second.parkspace_info_ex(0).car_info().car_numberplate());
//			tp_dispatch_process_information->set_command_key(iter->second.command_key());
//			tp_dispatch_process_information->set_dispatch_motion_direction(iter->second.dispatch_motion_direction());
//			tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->second.id_struct());
//			tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->second.locate_information());
//			tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->second.parkspace_info_ex());
//			tp_dispatch_process_information->set_car_type(iter->second.car_type());
//
//			tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_OVER);
//		}
//	}
//
//
//	//E_DISPATCH_POCESS_WORKING
//	{
//		if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST ||
//		m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_WORKING||
//		m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_BUSY)
//		{
//			message::Dispatch_request_msg t_dispatch_request_msg = m_dispatch_plc.get_dispatch_request_msg();
//			if ( t_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
//			{
//				message::Dispatch_process_information * tp_dispatch_process_information =
//				t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
//				tp_dispatch_process_information->set_car_numberplate(t_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate());
//				tp_dispatch_process_information->set_command_key(t_dispatch_request_msg.command_key());
//				tp_dispatch_process_information->set_dispatch_motion_direction(t_dispatch_request_msg.dispatch_motion_direction());
//				tp_dispatch_process_information->mutable_id_struct()->CopyFrom(t_dispatch_request_msg.id_struct());
//				tp_dispatch_process_information->mutable_locate_information()->CopyFrom(t_dispatch_request_msg.locate_information());
//				tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(t_dispatch_request_msg.parkspace_info_ex());
//				tp_dispatch_process_information->set_car_type(t_dispatch_request_msg.car_type());
//
//				tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WORKING);
//				tp_dispatch_process_information->set_working_total_time(m_dispatch_plc.get_response_working_total_time());
//				tp_dispatch_process_information->set_working_remaining_time(m_dispatch_plc.get_response_working_remaining_time());
//			}
//			else
//			{
//				message::Dispatch_process_information * tp_dispatch_process_information =
//				t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
//				tp_dispatch_process_information->set_car_numberplate(t_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate());
//				tp_dispatch_process_information->set_command_key(t_dispatch_request_msg.command_key());
//				tp_dispatch_process_information->set_dispatch_motion_direction(t_dispatch_request_msg.dispatch_motion_direction());
//				tp_dispatch_process_information->mutable_id_struct()->CopyFrom(t_dispatch_request_msg.id_struct());
//				tp_dispatch_process_information->mutable_locate_information()->CopyFrom(t_dispatch_request_msg.locate_information());
//				tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(t_dispatch_request_msg.parkspace_info_ex());
//				tp_dispatch_process_information->set_car_type(t_dispatch_request_msg.car_type());
//
//				tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WORKING);
//				tp_dispatch_process_information->set_working_total_time(m_dispatch_plc.get_response_working_total_time());
//				tp_dispatch_process_information->set_working_remaining_time(m_dispatch_plc.get_response_working_remaining_time());
//			}
//		}
//	}
//
//	//E_DISPATCH_POCESS_WAIT
//	{
//		std::unique_lock<std::mutex> t_lock(m_lock);
//		int t_store_wait_number = 1;
//		for (auto iter = m_dispatch_request_store_list.begin(); iter != m_dispatch_request_store_list.end(); ++iter)
//		{
//			message::Dispatch_process_information * tp_dispatch_process_information =
//			t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
//			tp_dispatch_process_information->set_car_numberplate(iter->parkspace_info_ex(0).car_info().car_numberplate());
//			tp_dispatch_process_information->set_command_key(iter->command_key());
//			tp_dispatch_process_information->set_dispatch_motion_direction(iter->dispatch_motion_direction());
//			tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->id_struct());
//			tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->locate_information());
//			tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->parkspace_info_ex());
//			tp_dispatch_process_information->set_car_type(iter->car_type());
//
//			tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WAIT);
//			tp_dispatch_process_information->set_wait_number(t_store_wait_number);
//			t_store_wait_number++;
//		}
//		int t_pickup_wait_number = 1;
//		for (auto iter = m_dispatch_request_pickup_list.begin(); iter != m_dispatch_request_pickup_list.end(); ++iter)
//		{
//			message::Dispatch_process_information * tp_dispatch_process_information =
//			t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
//			tp_dispatch_process_information->set_car_numberplate(iter->parkspace_info_ex(0).car_info().car_numberplate());
//			tp_dispatch_process_information->set_command_key(iter->command_key());
//			tp_dispatch_process_information->set_dispatch_motion_direction(iter->dispatch_motion_direction());
//			tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->id_struct());
//			tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->locate_information());
//			tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->parkspace_info_ex());
//			tp_dispatch_process_information->set_car_type(iter->car_type());
//
//			tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WAIT);
//			tp_dispatch_process_information->set_wait_number(t_pickup_wait_number);
//			t_pickup_wait_number++;
//		}
//	}
//
//	std::string t_store_updata_time = Time_tool::get_instance_references().get_time_string_millisecond(m_store_updata_time);
//	std::string t_pickup_updata_time = Time_tool::get_instance_references().get_time_string_millisecond(m_pickup_updata_time);
//
//	t_dispatch_terminal_status_msg.set_dispatch_store_process_information_updata_time(t_store_updata_time);
//	t_dispatch_terminal_status_msg.set_dispatch_pickup_process_information_updata_time(t_pickup_updata_time);
//
//
//	t_msg1 = t_dispatch_terminal_status_msg.SerializeAsString();
//	System_communication::get_instance_references().encapsulate_status_msg(t_msg1);
//

	return Error_code::SUCCESS;
}

//定时发送 调度管理的状态
Error_manager Dispatch_manager::encapsulate_send_dispatch_manager_status_new()
{
	std::unique_lock<std::mutex> t_lock(m_lock);

	dispatch_node_statu t_dispatch_node_statu_msg;
	if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_READY )
	{
		t_dispatch_node_statu_msg.set_statu(eIdle);
//		t_dispatch_node_statu_msg.set_statu(eBusy);
	}
	else if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_UNKNOW ||
	m_dispatch_manager_status == E_DISPATCH_MANAGER_DISCONNECT ||
	m_dispatch_manager_status == E_DISPATCH_MANAGER_FAULT	)
	{
		t_dispatch_node_statu_msg.set_statu(eFault);
	}
	else
	{
		t_dispatch_node_statu_msg.set_statu(eBusy);
	}

	t_dispatch_node_statu_msg.set_idle_stop_floor(m_device_floor);

	if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE ||
		 m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REALLOCATE)
	{
		t_dispatch_node_statu_msg.mutable_running_pack_info()->CopyFrom(m_park_table_msg);
	}
	else if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP ||
			  m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REVOCATION )
	{
		t_dispatch_node_statu_msg.mutable_running_pick_info()->CopyFrom(m_pick_table_msg);
	}
	else
	{

	}

	t_dispatch_node_statu_msg.set_unit_id(m_dispatch_manager_id+1);
	t_dispatch_node_statu_msg.set_plc_heartbeat(m_dispatch_plc.m_plc_heartbeat);
	t_dispatch_node_statu_msg.set_plc_status_info(m_dispatch_plc.m_plc_status_info);

	dispatch_region_info * p_dispatch_region_info_1 = t_dispatch_node_statu_msg.mutable_dispatch_region_info_vector()->Add();
	p_dispatch_region_info_1->set_terminal_id(m_dispatch_manager_id*2+1);
	p_dispatch_region_info_1->set_turnplate_angle_min(m_dispatch_plc.m_turnplate_angle_min1);
	p_dispatch_region_info_1->set_turnplate_angle_max(m_dispatch_plc.m_turnplate_angle_max1);
	dispatch_region_info * p_dispatch_region_info_2 = t_dispatch_node_statu_msg.mutable_dispatch_region_info_vector()->Add();
	p_dispatch_region_info_2->set_terminal_id(m_dispatch_manager_id*2+2);
	p_dispatch_region_info_2->set_turnplate_angle_min(m_dispatch_plc.m_turnplate_angle_min2);
	p_dispatch_region_info_2->set_turnplate_angle_max(m_dispatch_plc.m_turnplate_angle_max2);

	std::string t_msg = t_dispatch_node_statu_msg.DebugString();
//	std::cout << " huli test t_dispatch_node_statu_msg:::: " << " t_msg.size() = " << t_msg.size() << std::endl;
//	std::cout << " huli test t_dispatch_node_statu_msg:::: " << " t_msg = " << t_msg << std::endl;
	System_communication::get_instance_references().encapsulate_status_msg(t_msg);

	return Error_code::SUCCESS;
}

//在流程的map 里面释放指定的流程
Error_manager Dispatch_manager::release_dispatch_process(std::string command_key)
{
	std::unique_lock<std::mutex> t_lock(m_lock);

	return Error_code::SUCCESS;
}

//调度模块 ///地面雷达的状态消息(地面雷达->null)
//Error_manager Dispatch_manager::execute_for_ground_status_msg(message::Ground_status_msg ground_status_msg)
//{
//	int t_inlet_id = ground_status_msg.mutable_id_struct()->terminal_id() %2;
//	return	m_dispatch_ground_lidar[t_inlet_id].execute_for_ground_status_msg(ground_status_msg);
//
//	return Error_code::SUCCESS;
//}

//调度模块 ///单片机的状态消息
//Error_manager Dispatch_manager::execute_for_singlechip_data_msg(message::Singlechip_data singlechip_data_msg, bool validity)
//{
//	if ( singlechip_data_msg.has_terminalid() && singlechip_data_msg.has_dispatchdirection() )
//	{
//		if ( singlechip_data_msg.terminalid() >=0 &&
//			 singlechip_data_msg.terminalid() <=5 &&
//			 singlechip_data_msg.dispatchdirection() >=1 &&
//			 singlechip_data_msg.dispatchdirection() <=2 )
//		{
//			int t_temp = (singlechip_data_msg.terminalid() %2) + (singlechip_data_msg.dispatchdirection()-1)*2;
//			return	m_dispatch_singlechip[t_temp].execute_for_singlechip_data_msg(singlechip_data_msg, validity);
//		}
//	}
//
//	return Error_code::SUCCESS;
//}


//获取空闲的出口,返回0表示没有空闲出口, 返回1~6表示对应的出口
int Dispatch_manager::get_outlet_for_ready()
{
	std::cout << " huli test 111:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;

	//A单元优先分配A2, B和C正常分配

	if ( m_dispatch_manager_id == 0 )
	{
		//检查出口, 并删除取车完成       //判断2个出口是否空闲
		if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
		{
			std::cout << " huli test 444:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
			if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+2) == Error_code::SUCCESS )
			{
				std::cout << " huli test 555:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
				return m_dispatch_manager_id*2+2;
			}
		}
		else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
		{
			std::cout << " huli test 222:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
			if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+1) == Error_code::SUCCESS )
			{
				std::cout << " huli test 333:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
				return m_dispatch_manager_id*2+1;
			}
		}
		else
		{
			return 0;
		}
	}
	else
	{
		//检查出口, 并删除取车完成       //判断2个出口是否空闲
		if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
		{
			std::cout << " huli test 222:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
			if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+1) == Error_code::SUCCESS )
			{
				std::cout << " huli test 333:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
				return m_dispatch_manager_id*2+1;
			}
		}
		else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
		{
			std::cout << " huli test 444:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
			if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+2) == Error_code::SUCCESS )
			{
				std::cout << " huli test 555:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
				return m_dispatch_manager_id*2+2;
			}
		}
		else
		{
			return 0;
		}
	}

	return 0;
}

Dispatch_manager::Dispatch_manager_status Dispatch_manager::get_dispatch_manager_status()
{
	return m_dispatch_manager_status;
}

int Dispatch_manager::get_dispatch_manager_id()
{
	return m_dispatch_manager_id;
}
void Dispatch_manager::set_dispatch_manager_id(int dispatch_manager_id)
{
	m_dispatch_manager_id = dispatch_manager_id;
}


//资源分配
//void Dispatch_manager::resource_allocation()
//{
//	return;
//
//	LOG(INFO) << " Dispatch_manager::resource_allocation() start " << this;
//	Error_manager t_error;
//
//
//	while (m_dispatch_manager_condition.is_alive())
//	{
//		m_dispatch_manager_condition.wait();
//		if ( m_dispatch_manager_condition.is_alive() )
//		{
//			std::this_thread::sleep_for(std::chrono::microseconds(1));
//			std::this_thread::sleep_for(std::chrono::milliseconds(100));
////			std::this_thread::sleep_for(std::chrono::seconds(1));
//			std::this_thread::yield();
//			std::unique_lock<std::mutex> t_lock(m_lock);
//
//			//排序和资源分配
//			if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
//			{
//				//补全 id_struct
//				if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP)
//				{
//					//判断2个出口是否空闲, 在资源分配时,提前分配好出口终端号
//					if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
//					{
//						auto iter = m_dispatch_request_pickup_list.begin();
//						if ( iter != m_dispatch_request_pickup_list.end() )
//						{
//							iter->mutable_id_struct()->set_terminal_id(m_dispatch_manager_id*2);
//							m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
//							m_dispatch_manager_status = E_DISPATCH_MANAGER_PICKUP;
//							m_dispatch_request_pickup_list.erase(iter);
//							m_pickup_updata_time = std::chrono::system_clock::now();
//						}
//					}
//					else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
//					{
//						auto iter = m_dispatch_request_pickup_list.begin();
//						if ( iter != m_dispatch_request_pickup_list.end() )
//						{
//							iter->mutable_id_struct()->set_terminal_id(m_dispatch_manager_id*2+1);
//							m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
//							m_dispatch_manager_status = E_DISPATCH_MANAGER_PICKUP;
//							m_dispatch_request_pickup_list.erase(iter);
//							m_pickup_updata_time = std::chrono::system_clock::now();
//						}
//					}
//					//else什么也不做,直接切换到存车就行
//
//					m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
//				}
//				else if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE )
//				{
//					auto iter = m_dispatch_request_store_list.begin();
//					if ( iter != m_dispatch_request_store_list.end() )
//					{
//						iter->mutable_id_struct()->set_unit_id(m_dispatch_manager_id );
//						m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
//						m_dispatch_manager_status = E_DISPATCH_MANAGER_STORE;
//						m_dispatch_request_store_list.erase(iter);
//						m_store_updata_time = std::chrono::system_clock::now();
//					}
//					m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
//				}
//				else
//				{
//					///////
//				}
//			}
//			else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY)
//			{
//				m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
//			}
//			else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_BUSY)
//			{
//				m_dispatch_manager_status = E_DISPATCH_MANAGER_BUSY;
//			}
//			else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT)
//			{
//				m_dispatch_manager_status = E_DISPATCH_MANAGER_DISCONNECT;
//			}
//			else
//			{
//				m_dispatch_manager_status = E_DISPATCH_MANAGER_FAULT;
//			}
//
//
//			//检查时间, 并删除存车完成
//			for (auto iter = m_dispatch_response_store_map.begin(); iter != m_dispatch_response_store_map.end();)
//			{
//			    if ( std::chrono::system_clock::now() - iter->first  >  std::chrono::seconds(10) )
//			    {
//					iter = m_dispatch_response_store_map.erase(iter);
//					m_store_updata_time = std::chrono::system_clock::now();
//			    }
//			    else
//			    {
//					++iter;
//			    }
//			}
//
//			//检查出口, 并删除取车完成       //判断2个出口是否空闲
//			if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
//			{
//				if ( m_dispatch_response_pickup_map.find(m_dispatch_manager_id*2) != m_dispatch_response_pickup_map.end() )
//				{
//					m_dispatch_response_pickup_map.erase(m_dispatch_manager_id*2);
//					m_pickup_updata_time = std::chrono::system_clock::now();
//				}
//			}
//			else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
//			{
//				if ( m_dispatch_response_pickup_map.find(m_dispatch_manager_id*2+1) != m_dispatch_response_pickup_map.end() )
//				{
//					m_dispatch_response_pickup_map.erase(m_dispatch_manager_id*2+1);
//					m_pickup_updata_time = std::chrono::system_clock::now();
//				}
//			}
//
//		}
//	}
//
//	LOG(INFO) << " Dispatch_manager::resource_allocation() end "<< this;
//
//	return;
//}
//
////新版流程控制
//void Dispatch_manager::process_control()
//{
//	LOG(INFO) << " Dispatch_manager::process_control() start " << this;
//	Error_manager t_error;
//
//	while (m_dispatch_manager_condition.is_alive())
//	{
//		m_dispatch_manager_condition.wait();
//		if (m_dispatch_manager_condition.is_alive())
//		{
//			std::this_thread::sleep_for(std::chrono::microseconds(1));
//			std::this_thread::sleep_for(std::chrono::milliseconds(1));
////			std::this_thread::sleep_for(std::chrono::seconds(1));
//			std::this_thread::yield();
//			std::unique_lock<std::mutex> t_lock(m_lock);
//
//			static int t_count = 0;
//			if (t_count % 1000 == 0 )
//			{
//				std::cout << " huli test :::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
//				std::cout << " huli test :::: " << " m_dispatch_manager_status = " << m_dispatch_manager_status << std::endl;
//			}
//			t_count++;
//
//			switch ( m_dispatch_manager_status )
//			{
//				case E_DISPATCH_MANAGER_READY:
//				{
//					//等待 count_command_signal_1_port 的消息
//					if ( m_count_flag )
//					{
//						m_dispatch_manager_status = E_DISPATCH_MANAGER_COUNT_NEW;
//					}
//					if ( m_post_flag )
//					{
//						m_dispatch_manager_status = E_DISPATCH_MANAGER_POST_NEW;
//					}
//					break;
//				}
//			    case E_DISPATCH_MANAGER_COUNT_NEW:
//			    {
//					//目前不用解析 m_count_command_signal_msg ,只要收到 count_command_signal_1_port, 直接进行下一步
//					//发送调度任务申请
//					char t_buf[256] = {0};
//					sprintf(t_buf, "%d-%d", m_dispatch_manager_id+1, m_device_floor);
//					std::string t_string(t_buf);
//					System_communication::get_instance_references().encapsulate_task_msg(t_string, 0);
//					std::cout << " huli test :::: " << " m_request_command_msg = " << t_string << std::endl;
//					m_dispatch_manager_status = E_DISPATCH_MANAGER_REQUEST_NEW;
//			        break;
//			    }
//			    case E_DISPATCH_MANAGER_REQUEST_NEW:
//			    {
//					//等待 post_command_1_port 的消息
//					if ( m_post_flag )
//					{
//						m_dispatch_manager_status = E_DISPATCH_MANAGER_POST_NEW;
//					}
//			        break;
//			    }
//				case E_DISPATCH_MANAGER_POST_NEW:
//				{
//					//解析 m_post_command_msg
//					std::string t_string = m_post_command_msg.m_message_buf;
//					//检索字符串的内容,
//					if ( t_string.find("entrance_measure_info") !=  std::string::npos )
//					{
//						//entrance_measure_info 是 park_table 特有的
//						if(! google::protobuf::TextFormat::ParseFromString(t_string, &m_park_table_msg))
//						{
//							//答复错误
//						}
//						else
//						{
////							LOG(INFO) << " m_park_table_msg =  "<< m_park_table_msg.DebugString() << " --- " << this;
//							m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
//
//							//根据雷达x坐标, 计算入口id
//							int t_terminal = 0;
//							if (m_park_table_msg.entrance_measure_info().cx()<0)
//							{
//								t_terminal = m_dispatch_manager_id*2+1;
//							}
//							else if(m_park_table_msg.entrance_measure_info().cx()>0)
//							{
//								t_terminal = m_dispatch_manager_id*2+2;
//							}
//							m_park_table_msg.set_terminal_id(t_terminal);
//							m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
//							m_device_floor = m_park_table_msg.allocated_space_info().floor();
//							m_dispatch_manager_status = E_DISPATCH_MANAGER_COMPLETED_NEW;
//						}
//					}
//					else
//					{
//						if ( get_outlet_for_ready() !=0 )
//						{
//							if(! google::protobuf::TextFormat::ParseFromString(t_string, &m_pick_table_msg))
//							{
//								//答复错误
//							}
//							else
//							{
//								m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
//								m_pick_table_msg.set_terminal_id(get_outlet_for_ready());
//								m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg, m_dispatch_process_type, m_car_type);
//								m_device_floor = 1;
//								m_dispatch_manager_status = E_DISPATCH_MANAGER_COMPLETED_NEW;
//							}
//						}
//						//else 没有空闲出口,就无限等待
//					}
//					break;
//				}
//				case E_DISPATCH_MANAGER_COMPLETED_NEW:
//				{
//					//等待dispatch_plc执行,然后ack里面切回ready
//					break;
//				}
//				default:
//				{
//					std::cout << " huli test :::: " << " Dispatch_manager::process_control() error,  m_dispatch_manager_status = "
//							  << m_dispatch_manager_status << std::endl;
//					break;
//				}
//			}
//		}
//	}
//	LOG(INFO) << " Dispatch_manager::process_control() end "<< this;
//	return;
//}


//新版流程控制
void Dispatch_manager::process_sql()
{
	LOG(INFO) << " Dispatch_manager::process_sql() start " << this;
	Error_manager t_error;

	while (m_dispatch_manager_condition.is_alive())
	{
		m_dispatch_manager_condition.wait();
		if (m_dispatch_manager_condition.is_alive())
		{
			std::this_thread::sleep_for(std::chrono::microseconds(1));
			std::this_thread::sleep_for(std::chrono::milliseconds(1));
//			std::this_thread::sleep_for(std::chrono::seconds(1));
			std::this_thread::yield();
			std::unique_lock<std::mutex> t_lock(m_lock);


			static auto t_update_time = std::chrono::system_clock::now();
			if(std::chrono::system_clock::now() - t_update_time > std::chrono::seconds(1))
			{
				t_update_time = std::chrono::system_clock::now();
				std::cout << " huli test :::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
				std::cout << " huli test :::: " << " m_dispatch_manager_status = " << m_dispatch_manager_status << std::endl;

				std::cout << " huli test :::: " << " xxxxxxxxx  get_outlet_for_ready() = " << get_outlet_for_ready() << std::endl;

			}


			switch ( m_dispatch_manager_status )
			{
				case E_DISPATCH_MANAGER_READY:
				case E_DISPATCH_MANAGER_REQUEST:
				{
					//循环请求, 请求周期1秒
//					std::this_thread::sleep_for(std::chrono::seconds(1));
					if ( std::chrono::system_clock::now() - m_request_updata_time > std::chrono::seconds(1) )
					{
						m_request_updata_time = std::chrono::system_clock::now();

						//调度开始前, 向数据库发送请求的相关操作, 输入 穿梭机所在的楼层, 调度id 0~2, 空闲出口id, 如果不是0,就表示有空闲的出口
						t_error = m_dispatch_command.dispatch_request_to_sql(m_device_floor, m_dispatch_manager_id, get_outlet_for_ready());
						//std::cout << " huli test :::: " << " sssssssssssssssssssssssssssssssssss dispatch_request_to_sql = " << t_error << std::endl;

						if ( t_error == Error_code::SUCCESS )
						{
							//如果找到了合适的指令, 那么交给plc来执行
							if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
							{
								m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
								m_park_table_msg = m_dispatch_command.m_park_table_msg;
								m_car_type = m_dispatch_command.m_car_type;
								m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
								m_device_floor = m_park_table_msg.allocated_space_info().floor();
							}
							else if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
							{
								m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
								m_pick_table_msg = m_dispatch_command.m_pick_table_msg;
								m_car_type = m_dispatch_command.m_car_type;
								m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg, m_dispatch_process_type, m_car_type);
								m_device_floor = 1;
							}
							//切换到工作状态
							m_dispatch_manager_status = E_DISPATCH_MANAGER_WORKING;
						}
						else if(t_error != Error_code::NODATA)
						{
							LOG(INFO) << " dispatch_request_to_sql error = " << t_error << this;
						}
						//else NODATA 无限循环
					}
					//else 等待

					break;
				}
				case E_DISPATCH_MANAGER_WORKING:
				{
					//等待dispatch_plc执行, 完成后切换到 E_DISPATCH_MANAGER_RESPONSE
					if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::DISPATCH_PLC_READY ||
					m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::DISPATCH_PLC_FAULT)
					{
						if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_MIN_CAR )
						{
							//切换到 重新分配
							m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
							m_car_type = Common_data::Car_type::MIN_CAR;
						}
						else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_MID_CAR )
						{
							//切换到 重新分配
							m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
							m_car_type = Common_data::Car_type::MID_CAR;
						}
						else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_BIG_CAR )
						{
							//切换到 重新分配
							m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
							m_car_type = Common_data::Car_type::BIG_CAR;
						}
						else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_HUGE_CAR )
						{
							//切换到 重新分配
							m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
							m_car_type = Common_data::Car_type::HUGE_CAR;
						}
						else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_FAULT_CAR )
						{
							//切换到 重新分配
							m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
							m_car_type = Common_data::Car_type::FAULT_CAR;
						}
						else
						{
							//切换到答复状态
							m_dispatch_manager_status = E_DISPATCH_MANAGER_RESPONSE;
						}
					}
					//else 无限等待
					break;
				}
				case E_DISPATCH_MANAGER_REALLOCATE:
				{
					//调度 , 向数据库 重新分配车位,
					t_error = m_dispatch_command.dispatch_reallocate_to_sql(m_car_type, get_outlet_for_ready());
					if ( t_error == Error_code::SUCCESS )
					{
						//如果找到了合适的车位, 那么交给plc来执行
						if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
						{
							m_dispatch_process_type = m_dispatch_command.m_dispatch_process_type;
							m_park_table_msg = m_dispatch_command.m_park_table_msg;
							m_car_type = m_dispatch_command.m_car_type;
							m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
							m_device_floor = m_park_table_msg.allocated_space_info().floor();
						}
						//没有找到合适的车位, 那么放到出口
						else if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
						{
							m_dispatch_process_type = m_dispatch_command.m_dispatch_process_type;
							m_pick_table_msg = m_dispatch_command.m_pick_table_msg;
							m_car_type = m_dispatch_command.m_car_type;
							m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg,m_dispatch_process_type, m_car_type);
							m_device_floor = 1;
						}
						//切换到工作状态
						m_dispatch_manager_status = E_DISPATCH_MANAGER_WORKING;
					}
					else if(t_error != Error_code::NODATA)
					{
						LOG(INFO) << " dispatch_request_to_sql error = " << t_error << this;
					}
					//else NODATA 无限循环
					break;

					break;
				}
				case E_DISPATCH_MANAGER_RESPONSE:
				{
					//调度完成后, 向数据库发送答复的相关操作
					t_error = m_dispatch_command.dispatch_response_to_sql(m_dispatch_plc.get_result());
					if(t_error != Error_code::SUCCESS)
					{
						LOG(INFO) << " dispatch_response_to_sql error = " << t_error << this;
					}
					//切换到ready状态
					m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
					break;
				}
				default:
				{
					std::cout << " huli test :::: " << " Dispatch_manager::process_control() error,  m_dispatch_manager_status = "
							  << m_dispatch_manager_status << std::endl;
					break;
				}
			}
		}
	}
	LOG(INFO) << " Dispatch_manager::process_sql() end "<< this;
	return;
}






