// // Created by huli on 2021/3/22. // #include "dispatch_process.h" #include "../system/system_communication.h" #include "../dispatch/dispatch_manager.h" Dispatch_process::Dispatch_process() { m_dispatch_process_status = DISPATCH_PROCESS_STATUS_UNKNOW; m_dispatch_process_type = DISPATCH_PROCESS_TYPE_UNKNOW; m_dispatch_source = 0; m_dispatch_destination = 0; } Dispatch_process::~Dispatch_process() { Dispatch_process_uninit(); } //初始化, 就把主控发送的请求传入即可. Error_manager Dispatch_process::Dispatch_process_init(message::Dispatch_request_msg dispatch_request_msg) { 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(); if ( dispatch_request_msg.dispatch_motion_direction() == message::E_STORE_CAR ) { m_dispatch_process_type = DISPATCH_PROCESS_STORE; m_dispatch_source = dispatch_request_msg.terminal_id() + PASSAGEWAY_ID_BASE ; m_dispatch_destination = dispatch_request_msg.parkspace_info().parkspace_id() + PARKSPACE_ID_BASE; Common_data::copy_data(m_parkspace_information, dispatch_request_msg.parkspace_info()); Common_data::copy_data(m_car_measure_information, dispatch_request_msg.locate_information()); } else if( dispatch_request_msg.dispatch_motion_direction() == message::E_PICKUP_CAR ) { m_dispatch_process_type = DISPATCH_PROCESS_PICKUP; m_dispatch_source = dispatch_request_msg.parkspace_info().parkspace_id() + PARKSPACE_ID_BASE; m_dispatch_destination = dispatch_request_msg.terminal_id() + PASSAGEWAY_ID_BASE ; Common_data::copy_data(m_parkspace_information, dispatch_request_msg.parkspace_info()); } else { m_dispatch_process_type = DISPATCH_PROCESS_TYPE_UNKNOW; return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR, " Dispatch_process::Dispatch_process_init ERROR "); } m_dispatch_process_status = DISPATCH_PROCESS_CREATED; return Error_code::SUCCESS; } //反初始化 Error_manager Dispatch_process::Dispatch_process_uninit() { return Error_code::SUCCESS; } void Dispatch_process::Main() { Error_manager t_error; //主流程, 循环执行 while ( std::chrono::system_clock::now() - m_start_time < std::chrono::milliseconds(m_timeout_ms) ) { std::this_thread::sleep_for(std::chrono::microseconds(1)); switch ( m_dispatch_process_status ) { case DISPATCH_PROCESS_CREATED: { //检查调度请求 m_result = check_dispatch_request_msg(); if ( m_result !=Error_code::SUCCESS) { m_dispatch_process_status = DISPATCH_PROCESS_FAULT; break; } //发送调度总计划 m_result = send_dispatch_plan_request_msg(); if ( m_result !=Error_code::SUCCESS) { m_dispatch_process_status = DISPATCH_PROCESS_FAULT; break; } //流程正常, 就进入等待状态, 等待调度控制发送动作指令 m_dispatch_process_status = DISPATCH_PROCESS_READY; break; } case DISPATCH_PROCESS_READY: { //等待控制指令 m_result = wait_dispatch_control_request_msg(); if ( m_result !=Error_code::SUCCESS) { //不成功, 就表示没有新的指令, 那么什么都不做, 原地待命 } else { //流程正常, 就进入工作状态, m_dispatch_process_status = DISPATCH_PROCESS_WORKING; break; } //等待调度总计划答复 m_result = wait_dispatch_plan_response_msg(); if ( m_result !=Error_code::SUCCESS) { //不成功, 就表示没有总计划答复, 那么什么都不做, 原地待命 } else { //流程正常, 就进入完成状态, m_dispatch_process_status = DISPATCH_PROCESS_OVER; break; } break; } case DISPATCH_PROCESS_WORKING: { break; } case DISPATCH_PROCESS_OVER: { //发送调度答复, 发给主控的 m_result = send_dispatch_response_msg(); if ( m_result !=Error_code::SUCCESS) { m_dispatch_process_status = DISPATCH_PROCESS_FAULT; break; } //流程正常, 就进入等待状态, 等待调度控制发送动作指令 m_dispatch_process_status = DISPATCH_PROCESS_RELEASE; break; } case DISPATCH_PROCESS_RELEASE: { //通知调度管理, 释放资源, m_result = release_resource(); if ( m_result !=Error_code::SUCCESS) { m_dispatch_process_status = DISPATCH_PROCESS_FAULT; break; } //在这里, 整个流程彻底结束, 之后线程池会自动回收 这个流程对象的资源 return; break; } case DISPATCH_PROCESS_FAULT: { break; } default: { break; } } } //任务超时 return; } //检查调度请求 Error_manager Dispatch_process::check_dispatch_request_msg() { std::unique_lock t_lock(m_lock); return Error_code::SUCCESS; } //发送调度总计划 Error_manager Dispatch_process::send_dispatch_plan_request_msg() { std::unique_lock t_lock(m_lock); m_dispatch_plan_request_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_plan_request_msg); m_dispatch_plan_request_msg.mutable_base_info()->set_timeout_ms(m_timeout_ms); m_dispatch_plan_request_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_mamager); m_dispatch_plan_request_msg.mutable_base_info()->set_receiver(message::Communicator::eDispatch_control); m_dispatch_plan_request_msg.set_command_key(m_command_key); m_dispatch_plan_request_msg.set_dispatch_task_type((message::Dispatch_task_type)m_dispatch_process_type); m_dispatch_plan_request_msg.set_dispatch_source(m_dispatch_source); m_dispatch_plan_request_msg.set_dispatch_destination(m_dispatch_destination); //这里不写错误码 std::string t_msg = m_dispatch_plan_request_msg.SerializeAsString(); System_communication::get_instance_references().encapsulate_msg(t_msg); return Error_code::SUCCESS; } //等待控制指令 Error_manager Dispatch_process::wait_dispatch_control_request_msg() { std::unique_lock t_lock(m_lock); //key不相等 就表示 收到了新的控制指令 if ( m_dispatch_control_request_msg.command_key() == m_dispatch_control_response_msg.command_key() ) { return Error_code::NODATA; } else { return Error_code::SUCCESS; } } //执行调度控制指令, 并根据完成情况给答复 Error_manager Dispatch_process::excute_dispatch_control() { std::unique_lock t_lock(m_lock); return Error_code::SUCCESS; } //等待调度总计划答复 Error_manager Dispatch_process::wait_dispatch_plan_response_msg() { std::unique_lock t_lock(m_lock); //key 相等 就表示 收到了总计划答复 if ( m_dispatch_plan_request_msg.command_key() == m_dispatch_plan_response_msg.command_key() ) { return Error_code::SUCCESS; } else { return Error_code::NODATA; } } //发送调度答复, 发给主控的 Error_manager Dispatch_process::send_dispatch_response_msg() { std::unique_lock 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_timeout_ms); m_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_mamager); m_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain); m_dispatch_response_msg.set_command_key(m_command_key); m_dispatch_response_msg.mutable_error_manager()->CopyFrom(m_dispatch_plan_response_msg.error_manager()); std::string t_msg = m_dispatch_response_msg.SerializeAsString(); System_communication::get_instance_references().encapsulate_msg(t_msg); return Error_code::SUCCESS; } //通知调度管理, 释放资源, Error_manager Dispatch_process::release_resource() { return Dispatch_manager::get_instance_references().release_dispatch_process(m_command_key); }