//
// Created by huli on 2021/11/18.
//

#include "dispatch_singlechip.h"
#include "../system/system_communication.h"
#include "../dispatch/dispatch_manager.h"

Dispatch_singlechip::Dispatch_singlechip()
{
	m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_UNKNOW;
	m_plc_id = 0;
	m_singlechip_id = 0;
	m_singlechip_data_msg_updata_flag = false;
	m_validity = false;

}

Dispatch_singlechip::~Dispatch_singlechip()
{
	dispatch_singlechip_uninit();
}


//调度单片机 初始化,  单片机id, 0或者1, 单片机方向, 1是入口, 2是出口
Error_manager Dispatch_singlechip::dispatch_singlechip_init(int plc_id, int singlechip_id, int singlechip_direction)
{
	m_plc_id =	plc_id;
	m_singlechip_id = singlechip_id;
	m_singlechip_direction = singlechip_direction;
	mp_execute_thread = nullptr ;
	mp_updata_thread = nullptr ;
	m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
	m_singlechip_data_msg_updata_flag = false;

	//	线程默认开启
	m_execute_condition.reset(false, true, false);
	mp_execute_thread = new std::thread(&Dispatch_singlechip::execute_thread_fun, this);

	//	线程默认开启
	m_updata_condition.reset(false, true, false);
	mp_updata_thread = new std::thread(&Dispatch_singlechip::updata_thread_fun, this);

	m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_READY;

	m_close_outsidedoor_flag = false;

	return Error_code::SUCCESS;
}
//调度单片机 反初始化
Error_manager Dispatch_singlechip::dispatch_singlechip_uninit()
{
	if (mp_execute_thread)
	{
		m_execute_condition.kill_all();
	}
	if (mp_execute_thread)
	{
		mp_execute_thread->join();
		delete mp_execute_thread;
		mp_execute_thread = NULL;
	}

	m_dispatch_singlechip_status = Dispatch_singlechip::DISPATCH_SINGLECHIP_UNKNOW;

	return Error_code::SUCCESS;
}
//调度单片机 执行状态消息
//Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg(message::Singlechip_data &singlechip_data_msg, bool validity)
//{
//
////	std::unique_lock<std::mutex> t_lock2(m_lock);
////	m_singlechip_data_msg = singlechip_data_msg;
////	m_validity = validity;
////	m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
////	m_singlechip_data_msg_updata_flag = true;
//
//	return Error_code::SUCCESS;
//}
//调度单片机 执行状态消息
Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg_new(in_mcpu_statu &t_in_mcpu_statu, bool validity)
{
	std::unique_lock<std::mutex> t_lock2(m_lock);

	m_in_mcpu_statu = t_in_mcpu_statu;
	m_validity = validity;
	m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
	m_singlechip_data_msg_updata_flag = true;

	return Error_code::SUCCESS;

}
//调度单片机 执行状态消息
Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg_new(out_mcpu_statu &t_out_mcpu_statu, bool validity)
{
	std::unique_lock<std::mutex> t_lock2(m_lock);

	m_out_mcpu_statu = t_out_mcpu_statu;
	m_validity = validity;
	m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
	m_singlechip_data_msg_updata_flag = true;

	return Error_code::SUCCESS;
}

//单片机消息, 数据处理, 去头,去尾 //删除头部 @501, 删除尾部 $,
Error_manager Dispatch_singlechip::data_processing(std::string & put_in, std::string & put_out)
{
	int t_head_index = put_in.find("@");
	int t_tail_index = put_in.find('$');

	if ( t_head_index >=0 && t_tail_index > t_head_index )
	{
		//删除头部 @501, 删除尾部 $, 保留 {}
		put_out = put_in.substr(4, t_tail_index-t_head_index-4);
	}
	else
	{
		return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
							 " fun error ");
	}

	return Error_code::SUCCESS;
}

//调度单片机 执行状态消息, 单片机通过tcp发过来的原始字符串
Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg_new(std::string singlechip_data_string)
{
	Error_manager t_error;
	std::string t_singlechip_data_string_processing;	//处理后的临时字符串
	//单片机消息, 数据处理, 去头,去尾 //删除头部 @501, 删除尾部 $,
	t_error = data_processing(singlechip_data_string, t_singlechip_data_string_processing);
	if ( t_error == Error_code::SUCCESS )
	{
		//json转protobuf
		message::Singlechip_data t_singlechip_data_message;	//单片机的数据, proto格式
		auto result = google::protobuf::util::JsonStringToMessage(t_singlechip_data_string_processing,&t_singlechip_data_message);
		if ( result.error_code() == 0 )
		{
			m_singlechip_data_msg = t_singlechip_data_message;
			m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
			m_singlechip_data_msg_updata_flag = true;
			m_validity = true;
		}
		else
		{
			return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
								 " fun error ");
		}
	}
	else
	{
		return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
							 " fun error ");
	}

	return Error_code::SUCCESS;
}

Dispatch_singlechip::Dispatch_singlechip_status Dispatch_singlechip::get_dispatch_singlechip_status()
{
	return m_dispatch_singlechip_status;
}


//判断出口是否空闲, 没有车就是空闲, 返回true
bool Dispatch_singlechip::is_outlet_ready()
{
	std::unique_lock<std::mutex> t_lock(m_lock);

	if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY &&
		 //		 m_singlechip_data_msg.insideexistenceflag() == 0)
		 m_out_mcpu_statu.outside_safety() == 1)//   outside_safety 0无效, 1无车, 2有车
	{
		return true;
	}
	else
	{
		return false;
	}
}

//判断入口是否空闲, 没有车就是空闲, 返回true
bool Dispatch_singlechip::is_inlet_ready()
{
	std::unique_lock<std::mutex> t_lock(m_lock);

	if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY &&
		 //		 m_singlechip_data_msg.insideexistenceflag() == 0)
		 m_in_mcpu_statu.is_occupy() == 1)//    0无效, 1无车, 2有车
	{
		return true;
	}
	else
	{
		return false;
	}
}

//获取外门状态, 0表示未知, 1表示外门开到位, 2表示外门关到位, 3表示外门运行中 4表示外门故障
message::Outside_door_status Dispatch_singlechip::get_outside_door_status()
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY )
	{
		return m_singlechip_data_msg.outsidedoorstatus();
	}
	else
	{
		return message::Outside_door_status::OUTSIDE_DOOR_STATUS_UNKNOWN;
	}
}
//获取内部是否有车, 0表示无车, 1表示有车. -1表示未知
int Dispatch_singlechip::get_inside_existence_flag()
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY )
	{
		return m_singlechip_data_msg.insideexistenceflag();
	}
	else
	{
		return -1;
	}
}

//任务执行线程, 负责门控和数据库操作
void Dispatch_singlechip::execute_thread_fun()
{
	LOG(INFO) << " Dispatch_singlechip::execute_thread_fun() start " << this;
	Error_manager t_error;

	while (m_execute_condition.is_alive())
	{
		m_execute_condition.wait();
		if (m_execute_condition.is_alive())
		{
			std::this_thread::sleep_for(std::chrono::milliseconds(1));
//			std::this_thread::sleep_for(std::chrono::seconds(1));
			//门控线程, 每1秒循环一次即可.
			static auto s_last_time = std::chrono::system_clock::now();
			if ( std::chrono::system_clock::now() - s_last_time >= std::chrono::seconds(1) )
			{
			    if ( m_singlechip_direction == 1 )
			    {
					inlet_outsidedoor_control();
			    }
			    else if ( m_singlechip_direction == 2 )
				{
					outlet_outsidedoor_control();
				}

				s_last_time = std::chrono::system_clock::now();
			}

			std::this_thread::yield();

			//...
		}

	}
}

//入口外门控制函数
Error_manager Dispatch_singlechip::inlet_outsidedoor_control()
{
//	return;
	Error_manager t_error;

	if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY )
	{
		int t_terminal_id = m_plc_id*2 + m_singlechip_id;
		int t_singlechip_index = (m_singlechip_direction-1)*2 + m_singlechip_id;

		std::string t_primary_key_now = "";
		t_error = Dispatch_manager::get_instance_references().m_dispatch_command.get_primary_key_for_store(t_terminal_id+1, t_primary_key_now);
			if ( t_error != Error_code::SUCCESS )
			{
				return t_error;
			}
			else if(t_primary_key_now != "")
			{
				int t_plc_outsidedoor_control = Dispatch_communication::get_instance_references().m_singlechip_request_from_plc_to_manager[t_singlechip_index].m_request_outsidedoor_control;

				if ( ( t_primary_key_now != m_primary_key_last && get_inside_existence_flag() == 1)
				 || t_plc_outsidedoor_control == 0  )
			    {
					m_close_outsidedoor_flag = true;
					m_primary_key_last = t_primary_key_now;
			    }

			    if ( m_close_outsidedoor_flag == true )
			    {
					//有车就开门, 检查门的状态, 并发送关门指令
					if ( get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_OPEN ||
						 get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_RUN )
					{

						//发送关门指令
						close_outside_door();
					}
					else if (get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_CLOSE)
					{
						m_close_outsidedoor_flag = false;
					}
			    }
			}
			else
			{

				return Error_code::SUCCESS; ;
			}


//		//检查出口取车是否完成, 出口有取车完成指令就返回成功, 否则报错.
//		if ( Dispatch_manager::get_instance_references().m_dispatch_command.check_park_is_start(t_terminal_id+1) == Error_code::SUCCESS )
//		{
//			int t_outsidedoor_control = Dispatch_communication::get_instance_references().m_singlechip_request_from_plc_to_manager[t_singlechip_index].m_request_outsidedoor_control;
//
//			//检测入口是否有车, //有车就开门, 检查门的状态, 并发送关门指令
//			if ( get_inside_existence_flag() == 1 || t_outsidedoor_control == 0  )
//			{
//				//有车就开门, 检查门的状态, 并发送关门指令
//				if ( get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_OPEN ||
//					 get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_RUN )
//				{
//
//					//发送关门指令
//					close_outside_door();
//				}
//				//else 等待汽车离开
//			}
//			//else  什么也不做
//		}
//		//else 什么也不做
	}
	return Error_code::SUCCESS;
}
//出口外门控制函数
Error_manager Dispatch_singlechip::outlet_outsidedoor_control()
{
//	return;
	if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY )
	{
		int t_terminal_id = m_plc_id*2 + m_singlechip_id;
		int t_singlechip_index = (m_singlechip_direction-1)*2 + m_singlechip_id;
		//检查出口取车是否完成, 出口有取车完成指令就返回成功, 否则报错.
	    if ( Dispatch_manager::get_instance_references().m_dispatch_command.check_pickup_is_finish(m_plc_id, t_terminal_id+1) == Error_code::SUCCESS )
	    {
			//检测出口是否有车, 如果没有车, 直接清除数据库的指令
			if ( get_inside_existence_flag() == 0 )
			{
			    //delete
				Dispatch_manager::get_instance_references().m_dispatch_command.delete_command_queue_for_export_id(m_plc_id, t_terminal_id+1);
			}
			else if ( get_inside_existence_flag() == 1 )
			{
				//有车就开门, 检查门的状态, 并发送开门指令
				if ( get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_CLOSE ||
					 get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_RUN )
				{
					//发送开门指令
					open_outside_door();
			    }
				//else 等待汽车离开
			}
			//else  什么也不做
	    }
	    //else 什么也不做
	}
	return Error_code::SUCCESS;;
}

//发送开门指令
Error_manager Dispatch_singlechip::open_outside_door()
{
	int t_terminal_id = m_plc_id*2 + m_singlechip_id;//terminal_id 0~5
	char buf[1024] = {0};
	//ProcessControl 为3表示半自动关门, 为4表示半自动开门
	sprintf(buf, "{\"TerminalID\": %d, \"DispatchDirection\": %d, \"OutPutDo\": {\"Do0\": 0, \"Do1\": 0, \"Do2\": 0,\"Do3\": 0,\"Do4\": 0, \"Do5\": 0, \"Do6\": 0, \"Do7\": 0}, \"ProcessControl\": 4}",
			t_terminal_id, m_singlechip_direction);

	//4个单片机分别对应tcp通道口的0~3
	int t_tcp_id = (m_singlechip_direction-1)*2 + m_singlechip_id;
	Dispatch_network::get_instance_references().encapsulate_msg(std::string(buf), t_tcp_id);

	return Error_code::SUCCESS;
}
//发送关门指令
Error_manager Dispatch_singlechip::close_outside_door()
{
	int t_terminal_id = m_plc_id*2 + m_singlechip_id; //terminal_id 0~5
	char buf[1024] = {0};
	//ProcessControl 为3表示半自动关门, 为4表示半自动开门
	sprintf(buf, "{\"TerminalID\": %d, \"DispatchDirection\": %d, \"OutPutDo\": {\"Do0\": 0, \"Do1\": 0, \"Do2\": 0,\"Do3\": 0,\"Do4\": 0, \"Do5\": 0, \"Do6\": 0, \"Do7\": 0}, \"ProcessControl\": 3}",
			t_terminal_id, m_singlechip_direction);

	//4个单片机分别对应tcp通道口的0~3
	int t_tcp_id = (m_singlechip_direction-1)*2 + m_singlechip_id;
	Dispatch_network::get_instance_references().encapsulate_msg(std::string(buf), t_tcp_id);

	return Error_code::SUCCESS;
}

//任务执行线程, 负责数据管理, 和通信转化, tcp rabbitmq snap7
void Dispatch_singlechip::updata_thread_fun()
{
	LOG(INFO) << " Dispatch_singlechip::updata_thread_fun() start " << this;
	Error_manager t_error;

	while (m_updata_condition.is_alive())
	{
		m_updata_condition.wait();
		if (m_updata_condition.is_alive())
		{
			std::this_thread::sleep_for(std::chrono::milliseconds(1));
//			std::this_thread::sleep_for(std::chrono::seconds(1));

			std::this_thread::yield();

			//数据同步更新, tcp->rabbitmq, tcp->snap7
			updata_singlechip_data();
		}

	}
}

//数据同步更新, tcp->rabbitmq, tcp->snap7
void Dispatch_singlechip::updata_singlechip_data()
{
	//如果标志位是true, 就表示最新收到单片机消息, 那么同步数据消息
	if (m_singlechip_data_msg_updata_flag)
	{
		//将protobuf 转化为 snap7的DB块,  把店面雷达数据转发给plc
		//这个{}只是为了加锁
		{
			std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
			std::unique_lock<std::mutex> t_lock2(m_lock);

			//获取snap7的共享内存指针
			int t_temp = (m_singlechip_direction-1)*2 + m_singlechip_id;
			Dispatch_communication::Singlechip_response_from_manager_to_plc *p_response = &Dispatch_communication::get_instance_references().m_singlechip_response_from_manager_to_plc[t_temp];
			Dispatch_communication::Singlechip_request_from_plc_to_manager *p_request = &Dispatch_communication::get_instance_references().m_singlechip_request_from_plc_to_manager[t_temp];

			//m_request_communication_mode是刷新模式, 0=未知, 1=自动循环模式, 2=手动刷新模式
			if (p_request->m_request_communication_mode != 2)
			{
				p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
				p_response->m_response_communication_mode = p_request->m_request_communication_mode;
				p_response->m_response_refresh_command = p_request->m_request_refresh_command;
				p_response->m_response_data_validity = m_validity;

				p_response->m_process_status = m_singlechip_data_msg.processstatus();
				p_response->m_process_control = m_singlechip_data_msg.processcontrol();
				p_response->m_over_border_status = m_singlechip_data_msg.overborderstatus();
				p_response->m_car_height_status_current = m_singlechip_data_msg.carheightstatuscurrent();
				p_response->m_car_height_status_passing = m_singlechip_data_msg.carheightstatuspassing();
				p_response->m_outside_door_status = m_singlechip_data_msg.outsidedoorstatus();
				p_response->m_outside_door_control = m_singlechip_data_msg.outsidedoorcontrol();
				p_response->m_dispatch_finish_flag = m_singlechip_data_msg.dispatchfinishflag();
				p_response->m_inside_existence_flag = m_singlechip_data_msg.insideexistenceflag();
				p_response->m_outside_existence_flag = m_singlechip_data_msg.outsideexistenceflag();
				p_response->m_reset_flag = m_singlechip_data_msg.resetflag();
				p_response->m_stop_flag = m_singlechip_data_msg.stopflag();
				p_response->m_error_code = 0;

				p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
				p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
				p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
				p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
				p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
				p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
				p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
				p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
				p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
				p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
				p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
				p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
				p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
				p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
				p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
				p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();

//			if ( m_singlechip_direction ==1 )
//			{
//				p_response->m_outside_door_status = m_in_mcpu_statu.door_statu()-1;
//				p_response->m_over_border_status = m_in_mcpu_statu.back_io()-1;
//				p_response->m_inside_existence_flag = m_in_mcpu_statu.is_occupy()-1;
//				p_response->m_car_height_status_current = m_in_mcpu_statu.heighth()-1;
//			}
//			else if(m_singlechip_direction ==2)
//			{
//				p_response->m_outside_door_status = m_out_mcpu_statu.door_statu()-1;
//				p_response->m_inside_existence_flag = m_out_mcpu_statu.outside_safety()-1;
//			}
			}
			else if (p_response->m_response_refresh_command == p_request->m_request_refresh_command)
			{
				p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
				p_response->m_response_communication_mode = p_request->m_request_communication_mode;
				p_response->m_response_refresh_command = p_request->m_request_refresh_command;
			}
			else
			{
				p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
				p_response->m_response_communication_mode = p_request->m_request_communication_mode;
				p_response->m_response_refresh_command = p_request->m_request_refresh_command;
				p_response->m_response_data_validity = m_validity;

				p_response->m_process_status = m_singlechip_data_msg.processstatus();
				p_response->m_process_control = m_singlechip_data_msg.processcontrol();
				p_response->m_over_border_status = m_singlechip_data_msg.overborderstatus();
				p_response->m_car_height_status_current = m_singlechip_data_msg.carheightstatuscurrent();
				p_response->m_car_height_status_passing = m_singlechip_data_msg.carheightstatuspassing();
				p_response->m_outside_door_status = m_singlechip_data_msg.outsidedoorstatus();
				p_response->m_outside_door_control = m_singlechip_data_msg.outsidedoorcontrol();
				p_response->m_dispatch_finish_flag = m_singlechip_data_msg.dispatchfinishflag();
				p_response->m_inside_existence_flag = m_singlechip_data_msg.insideexistenceflag();
				p_response->m_outside_existence_flag = m_singlechip_data_msg.outsideexistenceflag();
				p_response->m_reset_flag = m_singlechip_data_msg.resetflag();
				p_response->m_stop_flag = m_singlechip_data_msg.stopflag();
				p_response->m_error_code = 0;

				p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
				p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
				p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
				p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
				p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
				p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
				p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
				p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
				p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
				p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
				p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
				p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
				p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
				p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
				p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
				p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();

//			if ( m_singlechip_direction ==1 )
//			{
//				p_response->m_outside_door_status = m_in_mcpu_statu.door_statu()-1;
//				p_response->m_over_border_status = m_in_mcpu_statu.back_io()-1;
//				p_response->m_inside_existence_flag = m_in_mcpu_statu.is_occupy()-1;
//				p_response->m_car_height_status_current = m_in_mcpu_statu.heighth()-1;
//			}
//			else if(m_singlechip_direction ==2)
//			{
//				p_response->m_outside_door_status = m_out_mcpu_statu.door_statu()-1;
//				p_response->m_inside_existence_flag = m_out_mcpu_statu.outside_safety()-1;
//			}
			}
		}


		//将protobuf 转化为 rabbitmq,
		//这个{}只是为了加锁
		{
			std::unique_lock<std::mutex> t_lock2(m_lock);

			//tcp的protobuf 转 rabbitmq的protobuf, 注意了, 所有数据+1
			if ( m_singlechip_direction ==1 )
			{
				m_in_mcpu_statu.set_door_statu(m_singlechip_data_msg.outsidedoorstatus()+1);
				m_in_mcpu_statu.set_is_occupy(m_singlechip_data_msg.insideexistenceflag()+1);
				m_in_mcpu_statu.set_heighth(m_singlechip_data_msg.carheightstatuscurrent()+1);

				//di1是后光电, 1表示光电联通无障碍, 0表示光电阻挡有障碍.
				if ( m_singlechip_data_msg.inputdi().di1() == 0 )
				{
					m_in_mcpu_statu.set_back_io(1);//后超界       0无效, 1后超界, 2正常
				}
				else if ( m_singlechip_data_msg.inputdi().di1() == 1 )
				{
					m_in_mcpu_statu.set_back_io(2);//后超界       0无效, 1后超界, 2正常
				}
				else
				{
					m_in_mcpu_statu.set_back_io(0);//后超界       0无效, 1后超界, 2正常
				}

				std::string t_msg = m_in_mcpu_statu.DebugString();
				//使用的rabbitmq通道要+1, tcp0号口给调度状态了, 1~4分别对应4个单片机
				int t_rabbitmq_id = (m_singlechip_direction-1)*2 + m_singlechip_id +1; //,每个单元的4个单片机分别对应1~4
				System_communication::get_instance_references().encapsulate_status_msg(t_msg, t_rabbitmq_id);
			}
			else if(m_singlechip_direction ==2)
			{
				m_out_mcpu_statu.set_door_statu(m_singlechip_data_msg.outsidedoorstatus()+1);
				m_out_mcpu_statu.set_outside_safety(m_singlechip_data_msg.insideexistenceflag()+1);

				std::string t_msg = m_out_mcpu_statu.DebugString();
				int t_rabbitmq_id = (m_singlechip_direction-1)*2 + m_singlechip_id +1; //,每个单元的4个单片机分别对应1~4
				//使用的rabbitmq通道要+1, tcp0号口给调度状态了, 1~4分别对应4个单片机
				System_communication::get_instance_references().encapsulate_status_msg(t_msg, t_rabbitmq_id);
			}
		}

		//数据更新完成后, 将更新标志位写false,
		m_singlechip_data_msg_updata_flag = false;
	}


	//判断超时
	{
		std::unique_lock<std::mutex> t_lock3(m_lock);
		if (std::chrono::system_clock::now() - m_singlechip_data_msg_updata_time > std::chrono::seconds(5))
		{
			m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_DISCONNECT;
		}
		else
		{
			m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_READY;
		}
	}
	return;
}