123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- //
- // Created by huli on 2021/8/3.
- //
- #include "dispatch_plc.h"
- #include "../system/system_communication.h"
- Dispatch_plc::Dispatch_plc()
- {
- m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_UNKNOW;
- m_plc_id = 0;
- mp_execute_thread = NULL;
- }
- Dispatch_plc::~Dispatch_plc()
- {
- dispatch_plc_uninit();
- }
- //调度plc 初始化
- Error_manager Dispatch_plc::dispatch_plc_init(int plc_id)
- {
- m_plc_id = plc_id;
- m_status_updata_time = std::chrono::system_clock::now();
- m_last_heartbeat = 0;
- // 线程默认开启
- m_execute_condition.reset(false, true, false);
- mp_execute_thread = new std::thread(&Dispatch_plc::execute_thread_fun, this);
- m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
- return Error_code::SUCCESS;
- }
- //调度plc 反初始化
- Error_manager Dispatch_plc::dispatch_plc_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_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_UNKNOW;
- return Error_code::SUCCESS;
- }
- //调度plc 执行请求
- Error_manager Dispatch_plc::execute_for_dispatch_request_msg(message::Dispatch_request_msg& dispatch_request_msg)
- {
-
- if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- //设定超时时间, 默认比任务指令里面的时间少10秒,
- if ( dispatch_request_msg.base_info().has_timeout_ms() )
- {
- m_timeout_ms = dispatch_request_msg.base_info().timeout_ms() - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
- }
- else
- {
- m_timeout_ms = DISPATCH_PROCESS_TIMEOUT_MS - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
- }
- m_command_key = dispatch_request_msg.command_key();
- m_start_time = std::chrono::system_clock::now();
- m_dispatch_request_msg = dispatch_request_msg;
- if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
- {
- m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
- }
- else if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
- {
- m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
- }
- else
- {
- return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
- " dispatch_request_msg.dispatch_motion_direction() PARAMRTER ERROR ");
- }
- m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
- m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST;
- }
- else
- {
- return Error_manager(Error_code::DISPATCH_PLC_STATUS_ERROR, Error_level::MINOR_ERROR,
- " m_dispatch_plc_status error ");
- }
- return Error_code::SUCCESS;
- }
- Dispatch_plc::Dispatch_plc_status Dispatch_plc::get_dispatch_plc_status()
- {
- return m_dispatch_plc_status;
- }
- //执行外界任务的执行函数
- void Dispatch_plc::execute_thread_fun()
- {
- LOG(INFO) << " Dispatch_plc::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::microseconds(1));
- std::this_thread::sleep_for(std::chrono::seconds(1));
- std::this_thread::yield();
- // std::unique_lock<std::mutex> t_lock(m_lock);
- std::cout << " huli test :::: " << " m_plc_id = " << m_plc_id << std::endl;
- std::cout << " huli test :::: " << " m_dispatch_plc_status = " << m_dispatch_plc_status << std::endl;
-
- switch ((Dispatch_plc_status) m_dispatch_plc_status)
- {
- case DISPATCH_PLC_READY:
- {
- if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW )
- {
- update_dispatch_plc_communication();
- }
- else
- {
- m_dispatch_plc_status = DISPATCH_PLC_REQUEST;
- }
- break;
- }
- case DISPATCH_PLC_REQUEST://给plc发送请求
- {
- if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
- {
- //超时直接报错
- m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
- " DISPATCH_PLC_TIME_OUT fun error ");
- m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
- }
- else
- {
- //封装 给plc的调度请求
- m_result = encapsulate_dispatch_request_to_plc();
- if ( m_result == Error_code::SUCCESS )
- {
- m_dispatch_plc_status = DISPATCH_PLC_WORKING;
- }
- else
- {
- m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
- }
- }
- break;
- }
- case DISPATCH_PLC_WORKING:
- {
- if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
- {
- //超时直接报错
- m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
- " DISPATCH_PLC_TIME_OUT fun error ");
- m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
- }
- else
- {
- update_dispatch_plc_communication();
- //检查plc答复反馈
- m_result = check_response_from_plc();
- if ( m_result == Error_code::SUCCESS )
- {
- m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
- }
- else if ( m_result != Error_code::NODATA )
- {
- m_dispatch_plc_status = DISPATCH_PLC_FAULT;
- }
- //else 原地等待
- }
- break;
- }
- case DISPATCH_PLC_RESPONSE:
- {
- //发送调度答复信息给主控
- send_dispatch_response_to_main_control();
- //无论前面的流程是否正确,都要给主控答复, 反馈里面填充错误码即可.
- if ( m_result == Error_code::SUCCESS )
- {
- //流程正常结束, 恢复到待机状态.
- m_dispatch_plc_status = DISPATCH_PLC_READY;
- m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
- m_result.error_manager_clear_all();
- }
- else
- {
- //流程异常结束, 进入到故障状态.
- m_dispatch_plc_status = DISPATCH_PLC_FAULT;
- }
- break;
- }
- case DISPATCH_PLC_FAULT:
- {
- //不在接受新的任务,但是保持基本的通信正常
- update_dispatch_plc_communication();
- break;
- }
- }
- }
- }
- }
- //封装 给plc的调度请求
- Error_manager Dispatch_plc::encapsulate_dispatch_request_to_plc()
- {
- //把m_dispatch_request_msg的信息传给本地的数据缓存.
- std::unique_lock<std::mutex> t_lock(m_lock);
- m_request_key = m_dispatch_request_msg.command_key();
- if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
- {
- m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
- m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_INLET;
- }
- else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
- {
- m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
- m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_OUTLET;
- }
- else
- {
- m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_UNKNOWN;
- m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN;
- }
- m_request_passageway_id = m_dispatch_request_msg.terminal_id()+1;//0~5改为1~6
- if ( m_dispatch_request_msg.locate_information().locate_correct() )
- {
- m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
- m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
- m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.5;//80~100改为-10~+10
- m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
- m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
- m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
- m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
- m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
- m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
- }
- else
- {
- m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
- m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
- m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle()-90+0.5;//80~100改为-10~+10
- m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
- m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
- m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
- m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
- m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
- m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
- }
- if ( m_dispatch_request_msg.parkspace_info_ex_size() ==1 )
- {
- m_request_parkingspace_index_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_index_id();
- m_request_parkingspace_unit_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_unit_id();
- m_request_parkingspace_floor_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_floor_id();
- m_request_parkingspace_room_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_room_id();
- //车位朝向, 小号朝前朝南, 大号朝后朝北
- m_request_parkingspace_direction = (Common_data::Parkingspace_direction)m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_direction();
- m_request_car_license = m_dispatch_request_msg.parkspace_info_ex(0).car_info().license();
- m_request_car_type = (Common_data::Car_type)m_dispatch_request_msg.parkspace_info_ex(0).car_type();
- }
- else
- {
- return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
- " m_dispatch_request_msg.parkspace_info_ex_size() !=1 error ");
- }
- return Error_code::SUCCESS;
- }
- //更新plc通信
- Error_manager Dispatch_plc::update_dispatch_plc_communication()
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
- /*
- //请求消息, 调度->plc
- Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
- & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
- Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
- & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
- memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
- int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
- m_request_key = "123456789";
- memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
- static unsigned char index = 0;
- index++;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = index;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = 02;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = 03;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = 04;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = 05;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = 6;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = 7;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = 8;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = 11;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = 12;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = 13;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = 14;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = 15;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = 16;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = 17;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = 18;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = 19;
- memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
- int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
- m_request_car_license = "ABCDEF";
- memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = 33;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = 44;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = 55;
- */
- //请求消息, 调度->plc
- Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
- & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
- Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
- & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
- memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
- int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
- memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_status = 1;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = m_request_dispatch_motion_direction;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = m_request_passageway_id;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = m_request_passageway_direction;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = m_request_parkingspace_index_id;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = m_request_parkingspace_unit_id;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = m_request_parkingspace_floor_id;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = m_request_parkingspace_room_id;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = m_request_parkingspace_direction;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = m_request_car_center_x;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = m_request_car_center_y;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = m_request_car_angle;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = m_request_car_front_theta;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = m_request_car_length;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = m_request_car_width;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = m_request_car_height;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = m_request_car_wheel_base;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = m_request_car_wheel_width;
- memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
- int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
- memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = m_request_car_type;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = m_request_anticollision_lidar_flag;
- tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = m_request_car_wheel_base_exact_value;
- //答复消息, plc->调度
- Dispatch_communication::Dispatch_response_from_plc_to_manager * tp_dispatch_response_from_plc_to_manager =
- & Dispatch_communication::get_instance_references().m_dispatch_response_from_plc_to_manager;
- m_response_key = (char*) tp_dispatch_response_from_plc_to_manager->m_response_key;
- m_response_status = (Dispatch_plc::Response_status)tp_dispatch_response_from_plc_to_manager->m_response_status;
- m_response_dispatch_motion_direction = (Common_data::Dispatch_motion_direction)tp_dispatch_response_from_plc_to_manager->m_response_dispatch_motion_direction;
- m_response_passageway_id = tp_dispatch_response_from_plc_to_manager->m_response_passageway_id;
- m_response_passageway_direction = (Common_data::Passageway_direction)tp_dispatch_response_from_plc_to_manager->m_response_passageway_direction;
- m_response_parkingspace_index_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_index_id;
- m_response_parkingspace_unit_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_unit_id;
- m_response_parkingspace_floor_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_floor_id;
- m_response_parkingspace_room_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_room_id;
- m_response_parkingspace_direction = (Common_data::Parkingspace_direction)tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_direction;
- m_response_car_center_x = tp_dispatch_response_from_plc_to_manager->m_response_car_center_x;
- m_response_car_center_y = tp_dispatch_response_from_plc_to_manager->m_response_car_center_y;
- m_response_car_angle = tp_dispatch_response_from_plc_to_manager->m_response_car_angle;
- m_response_car_front_theta = tp_dispatch_response_from_plc_to_manager->m_response_car_front_theta;
- m_response_car_length = tp_dispatch_response_from_plc_to_manager->m_response_car_length;
- m_response_car_width = tp_dispatch_response_from_plc_to_manager->m_response_car_width;
- m_response_car_height = tp_dispatch_response_from_plc_to_manager->m_response_car_height;
- m_response_car_wheel_base = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base;
- m_response_car_wheel_width = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_width;
- m_response_car_license = (char*) tp_dispatch_response_from_plc_to_manager->m_response_car_license;
- m_response_car_type = (Common_data::Car_type) tp_dispatch_response_from_plc_to_manager->m_response_car_type;
- m_response_anticollision_lidar_flag = (Common_data::Anticollision_lidar_flag)tp_dispatch_response_from_plc_to_manager->m_response_anticollision_lidar_flag;
- m_response_car_wheel_base_exact_value = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base_exact_value;
- /*
- std::cout << " huli test :::: " << " ------------------------------------------------------ = " << std::endl;
- std::cout << " huli test :::: " << " m_response_key = " << m_response_key << std::endl;
- std::cout << " huli test :::: " << " m_response_status = " << (int)m_response_status << std::endl;
- std::cout << " huli test :::: " << " m_response_dispatch_motion_direction = " << (int)m_response_dispatch_motion_direction << std::endl;
- std::cout << " huli test :::: " << " m_response_passageway_id = " << m_response_passageway_id << std::endl;
- std::cout << " huli test :::: " << " m_response_passageway_direction = " << (int)m_response_passageway_direction << std::endl;
- std::cout << " huli test :::: " << " m_response_parkingspace_index_id = " << m_response_parkingspace_index_id << std::endl;
- std::cout << " huli test :::: " << " m_response_parkingspace_unit_id = " << m_response_parkingspace_unit_id << std::endl;
- std::cout << " huli test :::: " << " m_response_parkingspace_floor_id = " << m_response_parkingspace_floor_id << std::endl;
- std::cout << " huli test :::: " << " m_response_parkingspace_room_id = " << m_response_parkingspace_room_id << std::endl;
- std::cout << " huli test :::: " << " m_response_parkingspace_direction = " << (int)m_response_parkingspace_direction << std::endl;
- std::cout << " huli test :::: " << " m_response_car_center_x = " << m_response_car_center_x << std::endl;
- std::cout << " huli test :::: " << " m_response_car_center_y = " << m_response_car_center_y << std::endl;
- std::cout << " huli test :::: " << " m_response_car_license = " << m_response_car_license << std::endl;
- */
- //状态消息, plc->调度
- Dispatch_communication::Dispatch_plc_status_from_plc_to_manager * tp_dispatch_plc_status_from_plc_to_manager =
- & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_plc_to_manager;
- //need
- // //通过心跳帧来判断通信是否正常
- // if ( m_last_heartbeat != tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat )
- // {
- // m_last_heartbeat = tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat;
- // m_status_updata_time = std::chrono::system_clock::now();
- //
- // if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT )
- // {
- // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
- // }
- // }
- // else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(COMMUNICATION_OVER_TIME_MS))
- // {
- // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT;
- // }
- // //else 继续等待,直到消息刷新或者超时.
- return Error_code::SUCCESS;
- }
- //检查plc答复反馈
- Error_manager Dispatch_plc::check_response_from_plc()
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- #ifndef WAIT_PLC_RESPONSE
- //need, 调试代码, 延时10s, 直接返回成功
- if ( std::chrono::system_clock::now() - m_start_time > std::chrono::seconds(5) )
- {
- return Error_code::SUCCESS;
- }
- //返回没有收到数据
- else
- {
- return Error_code::NODATA;
- }
- #endif
- #ifdef WAIT_PLC_RESPONSE
- //检查唯一码
- if ( m_response_key == m_request_key )
- {
- //如果故障,则添加错误码
- if ( m_response_status != RESPONS_WORKING )
- {
- //添加错误码
- Error_manager t_error(DISPATCH_PLC_RESPONS_ERROR, MINOR_ERROR, " Dispatch_plc::check_response_from_plc() m_respons_status is error");
- return t_error;
- }
- return Error_code::SUCCESS;
- }
- //返回没有收到数据
- else
- {
- return Error_code::NODATA;
- }
- #endif
- return Error_code::SUCCESS;
- }
- //发送调度答复信息给主控
- Error_manager Dispatch_plc::send_dispatch_response_to_main_control()
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- m_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
- m_dispatch_response_msg.mutable_base_info()->set_timeout_ms(m_dispatch_request_msg.base_info().timeout_ms());
- m_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
- m_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
- m_dispatch_response_msg.set_command_key(m_dispatch_request_msg.command_key());
- m_dispatch_response_msg.set_dispatch_motion_direction(m_dispatch_request_msg.dispatch_motion_direction());
- m_dispatch_response_msg.set_terminal_id(m_dispatch_request_msg.terminal_id());
- m_dispatch_response_msg.mutable_locate_information()->CopyFrom(m_dispatch_request_msg.locate_information());
- m_dispatch_response_msg.mutable_parkspace_info_ex()->CopyFrom(m_dispatch_request_msg.parkspace_info_ex());
- m_dispatch_response_msg.set_car_type(m_dispatch_request_msg.car_type());
- if ( m_dispatch_response_msg.dispatch_motion_direction() == message::E_STORE_CAR )
- {
- for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
- {
- if ( m_result == Error_code::SUCCESS )
- {
- m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
- }
- else
- {
- m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
- }
- }
- }
- else if( m_dispatch_response_msg.dispatch_motion_direction() == message::E_PICKUP_CAR )
- {
- for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
- {
- if ( m_result == Error_code::SUCCESS )
- {
- m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
- }
- else
- {
- m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
- }
- }
- }
- m_dispatch_response_msg.mutable_error_manager()->set_error_code(m_result.get_error_code());
- m_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)m_result.get_error_level());
- m_dispatch_response_msg.mutable_error_manager()->set_error_description(m_result.get_error_description());
- std::cout << " huli test :::: " << " m_dispatch_response_msg = " << m_dispatch_response_msg.DebugString()<< std::endl;
- std::string t_msg = m_dispatch_response_msg.SerializeAsString();
- System_communication::get_instance_references().encapsulate_msg(t_msg);
- return Error_code::SUCCESS;
- }
|