// // Created by zx on 2020/7/7. // #include #include #include "dispatch_communicator.h" #include "StoreProcessTask.h" #include "process_message.pb.h" #include "command_manager.h" #include "system_communicator.h" #include "exception_solver.h" #include "uniq_key.h" StoreProcessTask::StoreProcessTask(unsigned int terminor_id) :m_publish_statu_thread(nullptr) { m_terminor_id=terminor_id; } StoreProcessTask::~StoreProcessTask() { Exception_solver::get_instance_pointer()->delete_task_cancel_condition(m_car_info.license()); //退出线程 m_publish_exit_condition.set_pass_ever(true); if(m_publish_statu_thread!= nullptr) { if(m_publish_statu_thread->joinable()) { m_publish_statu_thread->join(); } delete m_publish_statu_thread; m_publish_statu_thread=nullptr; } } Error_manager StoreProcessTask::init_task(message::Locate_information locate_info,message::Car_info car_info) { reset_msg(); m_car_info=car_info; m_locate_info=locate_info; //添加当前流程的任务取消标志位到异常处理模块 m_cancel_condition.reset(false, false, false); Error_manager code=Exception_solver::get_instance_pointer()->add_task_cancel_condition(car_info.license(),this); if(code!=SUCCESS) return code; //初始化进度状态消息基本信息 message::Base_info base_info; base_info.set_msg_type(message::eStoring_process_statu_msg); base_info.set_sender(message::eMain); base_info.set_receiver(message::eEmpty); m_process_msg.mutable_base_info()->CopyFrom(base_info); m_process_msg.set_terminal_id(m_terminor_id); m_process_msg.set_license(m_car_info.license()); m_step_statu=eAlloc_step; ///创建状态发布线程 if(m_publish_statu_thread== nullptr) { m_publish_exit_condition.reset(false, false, false); m_publish_statu_thread=new std::thread(publish_thread_func,this); } return SUCCESS; } /* * 取消任务 */ void StoreProcessTask::Cancel() { m_cancel_condition.set_pass_ever(true); tq::BaseTask::Cancel(); } Error_manager StoreProcessTask::locate_step() { Error_manager code; //检查测量模块状态 code=Locate_communicator::get_instance_pointer()->check_statu(m_terminor_id); if(code!=SUCCESS) return code; message::Base_info base_info; base_info.set_msg_type(message::eLocate_request_msg); base_info.set_sender(message::eMain); base_info.set_receiver(message::eMeasurer); base_info.set_timeout_ms(20000); //测量超时5s m_measure_request_msg.mutable_base_info()->CopyFrom(base_info); m_measure_request_msg.set_command_key(create_key()); m_measure_request_msg.set_terminal_id(m_terminor_id); code=Locate_communicator::get_instance_pointer()->locate_request(m_measure_request_msg, m_measure_response_msg,m_cancel_condition); if(code!=SUCCESS) return code; if(m_measure_response_msg.error_manager().error_code()==0) { return SUCCESS; } else return Error_manager(FAILED,MINOR_ERROR,m_measure_response_msg.error_manager().error_description().c_str()); } /* * 回退定位 */ Error_manager StoreProcessTask::back_locate_step() { m_measure_request_msg=message::Measure_request_msg(); m_measure_response_msg=message::Measure_response_msg(); return SUCCESS; } /* * 调度 */ Error_manager StoreProcessTask::dispatch_step() { Error_manager code; /* * 判断调度所需的数据是否都正常 */ //1,测量信息是否存在 if(m_measure_response_msg.has_base_info()== false ||m_measure_response_msg.has_locate_information()==false ||m_parcspace_alloc_response_msg.has_base_info()== false ||m_parcspace_alloc_response_msg.has_allocated_space_info()==false) { return Error_manager(ERROR,MAJOR_ERROR,"调度所需的前置数据(测量,车位)不存在"); } //2,判断调度节点状态 code=Dispatch_communicator::get_instance_pointer()->check_entrance_statu(m_terminor_id); if(code!=SUCCESS) return code; message::Base_info base_info; base_info.set_msg_type(message::eDispatch_request_msg); base_info.set_sender(message::eMain); base_info.set_receiver(message::eDispatch); base_info.set_timeout_ms(1000*15); //超时15s m_dispatch_request_msg.mutable_base_info()->CopyFrom(base_info); std::string key=create_key(); m_dispatch_request_msg.set_command_key(key); m_dispatch_request_msg.set_terminal_id(m_terminor_id); m_dispatch_request_msg.set_dispatch_motion_direction(message::E_STORE_CAR); m_dispatch_request_msg.set_parkspace_id(m_parcspace_alloc_response_msg.allocated_space_info().parkspace_id()); code=Dispatch_communicator::get_instance_pointer()->dispatch_request(m_dispatch_request_msg, m_dispatch_response_msg,m_cancel_condition); if(code!=SUCCESS) return code; if(m_dispatch_response_msg.error_manager().error_code()==0) { return SUCCESS; } else return Error_manager(FAILED,MINOR_ERROR,"dispatch response error_code error"); } /* * 回退调度 */ Error_manager StoreProcessTask::back_dispatch_step() { //reset 调度请求数据 m_dispatch_request_msg=message::Dispatch_request_msg(); m_dispatch_response_msg=message::Dispatch_response_msg(); return SUCCESS; } /* * 分配车位 */ Error_manager StoreProcessTask::alloc_space() { /* * 检查是否有测量数据 */ if(m_locate_info.has_locate_height()==false||m_locate_info.has_locate_width()==false) { return Error_manager(FAILED,MINOR_ERROR,"停车请求缺少车辆高度和宽度信息"); } /* * 检查车位管理模块是否正常 */ Error_manager code=Parkspace_communicator::get_instance_pointer()->check_statu(); if(code!=SUCCESS) { return code; } //发送分配请求 message::Base_info base_info; base_info.set_msg_type(message::eParkspace_allocation_request_msg); base_info.set_sender(message::eMain); base_info.set_receiver(message::eParkspace); base_info.set_timeout_ms(1000); //超时1s m_alloc_request_msg.mutable_base_info()->CopyFrom(base_info); m_alloc_request_msg.mutable_car_info()->CopyFrom(m_car_info); m_alloc_request_msg.set_command_key(create_key()); m_alloc_request_msg.set_terminal_id(m_terminor_id); code=Parkspace_communicator::get_instance_pointer()->alloc_request(m_alloc_request_msg, m_parcspace_alloc_response_msg,m_cancel_condition); if(code!=SUCCESS) { return code; } if(m_parcspace_alloc_response_msg.error_manager().error_code()==0) { message::Car_info alloc_car_info=m_parcspace_alloc_response_msg.allocated_space_info().car_info(); if(alloc_car_info.license()!=m_car_info.license()) { return Error_manager(ERROR,MINOR_ERROR,"分配车位反馈的车辆信息不匹配"); } //置步骤状态为测量状态. m_step_statu=eMeasure_step; return SUCCESS; } else { return Error_manager(FAILED,MINOR_ERROR,"分配车位反馈结果错误"); } } /* * 车位占用确认 */ Error_manager StoreProcessTask::confirm_space_step() { /* * 检查车位管理模块是否正常 */ Error_manager code=Parkspace_communicator::get_instance_pointer()->check_statu(); if(code!=SUCCESS) return code; message::Parkspace_confirm_alloc_request_msg request; message::Base_info base_info; base_info.set_msg_type(message::eParkspace_confirm_alloc_request_msg); base_info.set_sender(message::eMain); base_info.set_receiver(message::eParkspace); base_info.set_timeout_ms(1000); //测量超时1s request.mutable_base_info()->CopyFrom(base_info); message::Parkspace_info space_info=m_parcspace_alloc_response_msg.allocated_space_info(); request.mutable_confirm_space_info()->CopyFrom(space_info); request.set_command_key(create_key()); message::Parkspace_confirm_alloc_response_msg confirm_response; code=Parkspace_communicator::get_instance_pointer()->confirm_request(request,confirm_response,m_cancel_condition); if(code!=SUCCESS) return code; if(m_parcspace_alloc_response_msg.allocated_space_info().parkspace_id()!= confirm_response.confirm_alloc_space_info().parkspace_id()) { return Error_manager(ERROR,MINOR_ERROR,"占用车位与分配车位不一致"); } if(confirm_response.error_manager().error_code()==0) { /*LOG(INFO)<<"停车流程正常,确认占用车位成功,停车终端:"<check_statu(); if(code!=SUCCESS) return code; message::Parkspace_release_request_msg request; message::Base_info base_info; base_info.set_msg_type(message::eParkspace_release_request_msg); base_info.set_sender(message::eMain); base_info.set_receiver(message::eParkspace); base_info.set_timeout_ms(1000); //测量超时1s request.mutable_base_info()->CopyFrom(base_info); message::Parkspace_info space_info=m_parcspace_alloc_response_msg.allocated_space_info(); request.mutable_release_space_info()->CopyFrom(space_info); request.set_command_key(create_key()); message::Parkspace_release_response_msg release_response; code=Parkspace_communicator::get_instance_pointer()->release_request(request,release_response,m_cancel_condition); if(code!=SUCCESS) return code; if(release_response.error_manager().error_code()==0) { /*LOG(WARNING)<<"停车流程异常,释放车位成功,停车终端:"<post_process_statu(m_process_msg); } break; } //回退confirm ------------------------------------华丽的分割线------------------------------------------ if (m_step_statu == eStoring_step::eBackConfirm_step) { m_step_statu=eStoring_step::eBackDispatch_step; } if(m_step_statu==eStoring_step::eBackDispatch_step) { code=back_dispatch_step(); if(code!=SUCCESS) { //提升错误等级为四级 break; } else { m_step_statu=eStoring_step::eBackMeasure_step; } } if(m_step_statu==eStoring_step::eBackMeasure_step) { code=back_locate_step(); if(code!=SUCCESS) { //提升错误等级为四级 break; } else { m_step_statu=eStoring_step::eBackAlloc_step; } } if(m_step_statu==eStoring_step::eBackAlloc_step) { code=back_alloc_space_step(); if(code!=SUCCESS) { //提升错误等级为四级 break; } else { m_step_statu=eStoring_step::eBackComplete; break; } } } /* * 跳出循环后,判断状态,是否正常结束, 循环跳出状态只有可能是 eBackComplete(异常结束),eComplete(正常结束),任务取消状态 */ if(m_cancel_condition.wait_for_millisecond(1)==true) { LOG(ERROR) << "停车任务被强制取消,车牌号:" << m_car_info.license() << ", 终端号:" << m_terminor_id; return ; } if(m_step_statu==eStoring_step::eBackComplete) { //异常结束 usleep(1000*10000); LOG(ERROR)<<"异常停车,回退结束"<<"车牌号:"<publish_step_status(); } } void StoreProcessTask::publish_step_status() { //未收到退出信号 while(false==m_publish_exit_condition.wait_for_ex(std::chrono::milliseconds(50))) { /* * 通过communicator 发布状态 */ if(System_communicator::get_instance_pointer()) { if(updata_step_statu_msg()==SUCCESS) { System_communicator::get_instance_pointer()->post_process_statu(m_process_msg); } } } } /* * 根据当前流程状态,生成状态消息 */ Error_manager StoreProcessTask::updata_step_statu_msg() { switch (m_step_statu) { case eStoring_step::eAlloc_step: { message::Alloc_space_step_statu alloc_step_statu; alloc_step_statu.set_step_statu(message::eWorking); m_process_msg.mutable_alloc_space_step()->CopyFrom(alloc_step_statu); break; } case eStoring_step::eMeasure_step: { message::Measure_step_statu measure_step_statu; measure_step_statu.set_step_statu(message::eWorking); measure_step_statu.mutable_locate_info()->CopyFrom(m_locate_info); m_process_msg.mutable_measure_step()->CopyFrom(measure_step_statu); if(m_process_msg.has_alloc_space_step()) { m_process_msg.mutable_alloc_space_step()->set_step_statu(message::eComplete); } break; } case eStoring_step::eCompare_step:{ message::Compare_step_statu compare_step; compare_step.mutable_locate_info_wj()->CopyFrom(m_locate_info); compare_step.mutable_locate_info_dj()->CopyFrom(m_measure_response_msg.locate_information()); compare_step.mutable_locate_info_result()->CopyFrom(m_compare_location_data); compare_step.set_step_statu(message::eWorking); m_process_msg.mutable_compare_step()->CopyFrom(compare_step); if(m_process_msg.has_measure_step()) m_process_msg.mutable_measure_step()->set_step_statu(message::eComplete); break; } case eStoring_step::eDispatch_step: { message::Dispatch_store_step_statu dispatch_step_statu; dispatch_step_statu.set_step_statu(message::eWorking); dispatch_step_statu.mutable_locate_info()->CopyFrom(m_locate_info); dispatch_step_statu.mutable_space_info()->CopyFrom(m_parcspace_alloc_response_msg.allocated_space_info()); m_process_msg.mutable_dispatch_step()->CopyFrom(dispatch_step_statu); if(m_process_msg.has_compare_step()) m_process_msg.mutable_compare_step()->set_step_statu(message::eComplete); break; } case eStoring_step::eConfirm_step: { message::Confirm_space_step_statu confirm_step_statu; confirm_step_statu.set_step_statu(message::eWorking); confirm_step_statu.mutable_space_info()->CopyFrom(m_parcspace_alloc_response_msg.allocated_space_info()); m_process_msg.mutable_confirm_space_step()->CopyFrom(confirm_step_statu); if(m_process_msg.has_dispatch_step()) m_process_msg.mutable_dispatch_step()->set_step_statu(message::eComplete); break; } case eStoring_step::eComplete: { m_process_msg.set_completed(true); break; } case eStoring_step::eBackConfirm_step: { message::Back_confirm_space_step_statu back_confirm_step_statu; back_confirm_step_statu.set_step_statu(message::eWaiting); if (m_process_msg.has_confirm_space_step()) m_process_msg.mutable_confirm_space_step()->set_step_statu(message::eError); break; } case eStoring_step::eBackDispatch_step: { message::Back_dispatch_store_step_statu back_dispatch_step_statu; back_dispatch_step_statu.set_step_statu(message::eWorking); back_dispatch_step_statu.mutable_space_info()->CopyFrom(m_parcspace_alloc_response_msg.allocated_space_info()); back_dispatch_step_statu.mutable_locate_info()->CopyFrom(m_compare_location_data); m_process_msg.mutable_back_dispatch_step()->CopyFrom(back_dispatch_step_statu); if(m_process_msg.has_back_confirm_step()) m_process_msg.mutable_back_confirm_step()->set_step_statu(message::eComplete); else m_process_msg.mutable_dispatch_step()->set_step_statu(message::eError); break; } case eStoring_step::eBack_compare_step:{ message::Back_compare_step_statu back_compare_step_statu; back_compare_step_statu.set_step_statu(message::eWorking); back_compare_step_statu.mutable_locate_info_wj()->CopyFrom(m_locate_info); back_compare_step_statu.mutable_locate_info_dj()->CopyFrom(m_measure_response_msg.locate_information()); back_compare_step_statu.mutable_locate_info_result()->CopyFrom(m_compare_location_data); m_process_msg.mutable_compare_step()->CopyFrom(back_compare_step_statu); if(m_process_msg.has_back_dispatch_step()) m_process_msg.mutable_back_dispatch_step()->set_step_statu(message::eComplete); else m_process_msg.mutable_compare_step()->set_step_statu(message::eError); break; } case eStoring_step::eBackMeasure_step: { message::Back_measure_step_statu back_measure_step_statu; back_measure_step_statu.set_step_statu(message::eWorking); m_process_msg.mutable_back_measure_step()->CopyFrom(back_measure_step_statu); if(m_process_msg.has_back_compare_step()) m_process_msg.mutable_back_compare_step()->set_step_statu(message::eComplete); else m_process_msg.mutable_measure_step()->set_step_statu(message::eError); break; } case eStoring_step::eBackAlloc_step: { message::Back_alloc_space_step_statu back_alloc_step_statu; back_alloc_step_statu.set_step_statu(message::eWorking); back_alloc_step_statu.mutable_space_info()->CopyFrom(m_parcspace_alloc_response_msg.allocated_space_info()); m_process_msg.mutable_back_alloc_space_step()->CopyFrom(back_alloc_step_statu); if(m_process_msg.has_back_measure_step()) m_process_msg.mutable_back_measure_step()->set_step_statu(message::eComplete); else m_process_msg.mutable_alloc_space_step()->set_step_statu(message::eError); break; } case eStoring_step::eBackComplete: { m_process_msg.set_back_completed(true); break; } default: break; } return SUCCESS; } /* * 初始化 接收到的消息 */ void StoreProcessTask::reset_msg() { m_alloc_request_msg = message::Parkspace_allocation_request_msg(); m_measure_request_msg = message::Measure_request_msg(); m_dispatch_request_msg = message::Dispatch_request_msg(); m_confirm_request_msg = message::Parkspace_confirm_alloc_request_msg(); m_locate_info = message::Locate_information(); m_measure_response_msg = message::Measure_response_msg(); //测量模块的测量数据 m_parcspace_alloc_response_msg = message::Parkspace_allocation_response_msg(); //分配的车位数据 m_dispatch_response_msg = message::Dispatch_response_msg(); //调度模块的反馈数据 }