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

#include "dispatch_singlechip.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 ;
	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_dispatch_singlechip_status = DISPATCH_SINGLECHIP_READY;

	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;
}

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


//判断出口是否空闲
bool Dispatch_singlechip::is_outlet_ready()
{
	std::unique_lock<std::mutex> t_lock(m_lock);
	//   outside_safety 0无效, 1无车, 2有车
	if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY &&
//		 m_singlechip_data_msg.insideexistenceflag() == 0)
m_out_mcpu_statu.outside_safety() == 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}


//执行外界任务的执行函数
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));

			std::this_thread::yield();

//			std::cout << " huli test :::: " << "uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu m_singlechip_data_msg_updata_flag = " << m_singlechip_data_msg_updata_flag << std::endl;

			if (m_singlechip_data_msg_updata_flag)
			{

//				std::cout << " huli test :::: " << "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj m_singlechip_data_msg_updata_flag = " << m_singlechip_data_msg_updata_flag << std::endl;
//				std::cout << " huli test :::: " << " m_singlechip_data_msg = " << m_singlechip_data_msg.DebugString() << std::endl;



				std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
				std::unique_lock<std::mutex> t_lock2(m_lock);

				//将nnxx的protobuf 转化为 snap7的DB块,  把店面雷达数据转发给plc
//				int t_temp = (m_singlechip_data_msg.terminalid() % 2) + (m_singlechip_data_msg.dispatchdirection()-1)*2;
				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];

				if (p_request->m_request_communication_mode != 2)
				{
					p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
//					int heartbeat = p_response->m_response_heartbeat;
//					std::cout << " huli test :::: " << " mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmp_response->m_response_heartbeat = " << heartbeat << std::endl;
					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;
					}
				}

				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;
				}
			}
		}

	}
}

