123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- //
- // Created by zx on 2020/7/7.
- //
- #include <parkspace_excutor.h>
- #include <dispatch_message.pb.h>
- #include "PickupProcessTask.h"
- #include "process_message.pb.h"
- #include "command_manager.h"
- #include "command_accepter.h"
- #include "exception_solver.h"
- #include "uniq_key.h"
- PickupProcessTask::PickupProcessTask(unsigned int terminal_id,message::Car_info car_info)
- :Process_task(terminal_id,car_info)
- {
- }
- PickupProcessTask::~PickupProcessTask()
- {
- Exception_solver::get_instance_pointer()->delete_task_cancel_condition(m_car_info.license());
- }
- Error_manager PickupProcessTask::init_task()
- {
- reset_msg();
- //添加当前流程的任务取消标志位到异常处理模块
- m_cancel_condition.reset(false, false, false);
- Error_manager code=Exception_solver::get_instance_pointer()->add_task_cancel_condition(m_car_info.license(),this);
- if(code!=SUCCESS)
- return code;
- //设置状态消息,基本信息
- message::Base_info base_info;
- base_info.set_msg_type(message::ePicking_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_current_step_type=message::eSearch_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 PickupProcessTask::reset_msg()
- {
- m_search_request_msg=message::Parkspace_search_request_msg();
- m_search_response_msg=message::Parkspace_search_response_msg();
- m_dispatch_request_msg=message::Dispatch_request_msg();
- m_dispatch_response_msg=message::Dispatch_response_msg();
- m_command_response_msg=message::Pickup_command_response_msg();
- }
- /*
- * 查询车位
- */
- Error_manager PickupProcessTask::search_space_step()
- {
- message::Base_info base_info_response;
- base_info_response.set_msg_type(message::ePickup_command_response_msg);
- base_info_response.set_sender(message::eMain);
- base_info_response.set_receiver(message::eTerminor);
- m_command_response_msg.mutable_base_info()->CopyFrom(base_info_response);
- m_command_response_msg.set_terminal_id(m_terminor_id);
- if(Command_manager::get_instance_pointer()->is_paused()==true)
- {
- return Error_manager(PAUSE, MINOR_ERROR, "系统已急停");
- }
- message::Entrance_statu statu=Command_manager::get_instance_pointer()->export_statu(m_terminor_id);
- if(statu.has_paused()==false) {
- return Error_manager(ERROR, MINOR_ERROR, "出口已禁止使用 Disable");
- }
- if(statu.paused()==true)
- {
- return Error_manager(ERROR, MINOR_ERROR, "出口已禁止使用 Disable");
- }
- /*
- * 检验汽车信息是否正常
- */
- if(m_car_info.has_car_width()==false||m_car_info.has_car_height()== false
- ||m_car_info.has_license()==false)
- {
- return Error_manager(INVALID_MESSAGE,CRITICAL_ERROR,"查询车位请求信息错误");
- }
- /*
- * 检查车位管理模块是否正常
- */
- Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
- if(code!=SUCCESS)
- {
- return code;
- }
- message::Base_info base_info_request;
- base_info_request.set_msg_type(message::eParkspace_search_request_msg);
- base_info_request.set_sender(message::eMain);
- base_info_request.set_receiver(message::eParkspace);
- base_info_request.set_timeout_ms(2000);
- m_search_request_msg.mutable_base_info()->CopyFrom(base_info_request);
- m_search_request_msg.mutable_car_info()->CopyFrom(m_car_info);
- code = Parkspace_excutor::get_instance_pointer()->search_request(m_search_request_msg,
- m_search_response_msg,m_cancel_condition);
- return code;
- }
- /*
- * 回退查询车位
- */
- Error_manager PickupProcessTask::back_search_space_step()
- {
- return SUCCESS;
- }
- /*
- * 控制流程到下一步
- */
- Error_manager PickupProcessTask::next_step()
- {
- if(m_current_step_statu==message::eWaiting || m_current_step_statu== message::eWorking)
- return Error_manager(ERROR,MINOR_ERROR,"当前状态禁止改变步骤类型");
- switch (m_current_step_type)
- {
- case message::eSearch_step:
- m_current_step_type=(m_current_step_statu==message::eFinished)?message::eDispatch_step:message::eBackComplete;
- break;
- case message::eDispatch_step:
- m_current_step_type=(m_current_step_statu==message::eFinished)?message::eWait_step:message::eBackSearch_step;
- break;
- case message::eWait_step:
- m_current_step_type=(m_current_step_statu==message::eFinished)?message::eRelease_step:message::eBackDispatch_step;
- break;
- case message::eRelease_step:
- m_current_step_type=(m_current_step_statu==message::eFinished)?message::eComplete:message::eBackWait_step;
- break;
- case message::eComplete:
- break;
- case message::eBackWait_step:
- m_current_step_type=message::eBackDispatch_step;
- break;
- case message::eBackDispatch_step:
- m_current_step_type=message::eBackSearch_step;
- break;
- case message::eBackSearch_step:
- m_current_step_type=message::eBackComplete;
- break;
- case message::eBackComplete:
- break;
- default:
- LOG(ERROR)<<"------- 取 ------- 下一步失败,当前步骤未知: "<<message::Step_type_Name(m_current_step_type);
- break;
- }
- return SUCCESS;
- }
- void PickupProcessTask::Main()
- {
- Error_manager code;
- //进入取车流程
- while(is_canceled()==false)
- {
- //第一步查询
- if(m_current_step_type==message::eSearch_step)
- {
- updata_step_statu(message::eWorking);
- code=search_space_step();
- message::Error_manager error_msg;
- error_msg.set_error_code(code.get_error_code());
- error_msg.set_error_description(code.get_error_description());
- m_command_response_msg.mutable_code()->CopyFrom(error_msg);
- Communication_message msg;
- msg.reset(m_command_response_msg.base_info(),m_command_response_msg.SerializeAsString());
- Message_communicator::get_instance_pointer()->send_msg(&msg);
- code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
- LOG_IF(ERROR, code != SUCCESS) << "------- 取 -------查询失败,取车终端:"<<m_terminor_id
- <<", 车牌号:"<<m_car_info.license()<<code.get_error_description();
- }
- if(m_current_step_type== message::eDispatch_step)
- {
- updata_step_statu(message::eWorking);
- code=dispatch_step();
- usleep(1000*500);
- code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
- LOG_IF(ERROR, code != SUCCESS) << "------- 取 -------取车调度失败,取车终端:"<<m_terminor_id
- <<", 车位id:"<<m_search_response_msg.car_position().parkspace_id()
- <<", 车位楼层:"<<m_search_response_msg.car_position().floor()
- <<", 车位序号:"<<m_search_response_msg.car_position().index()
- <<", 车牌号:"<<m_car_info.license()
- <<", 库内车牌号:"<<m_search_response_msg.car_position().car_info().license()
- <<code.get_error_description();
- }
- if(m_current_step_type== message::eWait_step)
- {
- updata_step_statu(message::eWorking);
- code=wait_for_leave_step();
- code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
- LOG_IF(ERROR, code != SUCCESS) << "------- 取 -------等待车辆离开失败,取车终端:"<<m_terminor_id
- <<", 车牌号:"<<m_car_info.license();
- }
- if(m_current_step_type== message::eRelease_step)
- {
- updata_step_statu(message::eWorking);
- code=release_space_step();
- code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
- if(code!=SUCCESS)
- {
- //提升错误等级为三级
- LOG(ERROR)<<"------- 取 ------- 释放车位失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
- Exception_solver::get_instance_pointer()->solve_exception(Error_manager(code.get_error_code(),MAJOR_ERROR,code.get_error_description()),this);
- continue;
- }
- }
- if(m_current_step_type==message::eComplete) {
- //流程结束前,保证至少发送一次流程完成状态
- updata_step_statu(message::eFinished);
- break;
- }
- // ----------------------------------------华丽的分割线 ----------------------------------------
- if(m_current_step_type== message::eBackWait_step)
- {
- updata_step_statu(message::eWorking);
- code=back_wait_for_leave_step();
- code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
- if(code.get_error_level()>=MAJOR_ERROR)
- {
- //提升错误等级为四级
- LOG(ERROR)<<"------- 取 ------- 回退等待失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
- Exception_solver::get_instance_pointer()->solve_exception(code,this);
- continue;
- }
- }
- if(m_current_step_type== message::eBackDispatch_step)
- {
- updata_step_statu(message::eWorking);
- code=back_dispatch_step();
- code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
- if(code.get_error_level()>=MAJOR_ERROR)
- {
- //提升错误等级为四级
- LOG(ERROR)<<"------- 取 ------- 回退调度失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
- Exception_solver::get_instance_pointer()->solve_exception(code,this);
- continue;
- }
- }
- if(m_current_step_type== message::eBackSearch_step)
- {
- updata_step_statu(message::eWorking);
- code=back_search_space_step();
- code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
- if(code.get_error_level()>=MAJOR_ERROR)
- {
- //提升错误等级为四级
- LOG(ERROR)<<"------- 取 ------- 回退调度失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
- Exception_solver::get_instance_pointer()->solve_exception(code,this);
- continue;
- }
- }
- if(m_current_step_type== message::eBackComplete)
- break;
- next_step();
- }
- /*
- * 跳出循环后,判断状态,是否正常结束, 循环跳出状态只有可能是 eBackComplete(异常结束),eComplete(正常结束),任务取消状态
- */
- updata_step_statu(message::eFinished);
- publish_step_status();
- if(m_cancel_condition.wait_for_millisecond(1)==true) {
- LOG(ERROR) << "------- 取 -------取车任务被强制取消,车牌号:" << m_car_info.license()
- << ", 终端号:" << m_terminor_id;
- usleep(1000*500);
- return ;
- }
- if(m_current_step_type== message::eBackComplete)
- {
- //异常结束
- usleep(1000*500);
- LOG(ERROR)<<"------- 取 -------异常取车,回退结束"<<"车牌号:"<<m_car_info.license()
- <<",xxxxxxxxxxxxxx 终端:"<<m_terminor_id<<" xxxxxxxxxxxxxx";
- }
- if(m_current_step_type== message::eComplete)
- {
- //正常结束
- usleep(1000*500);
- LOG(INFO)<<"------- 取 -------取车结束,"<<"车牌号:"<<m_car_info.license()
- <<",-------------- 终端:"<<m_terminor_id<<" --------------";
- }
- }
- /*
- * 执行取车动作请求,并等待执行完成
- */
- Error_manager PickupProcessTask::dispatch_step()
- {
- /*
- * 检查是否曾经分配过车位
- */
- if(m_search_response_msg.has_car_position()==false)
- {
- return Error_manager(FAILED,MINOR_ERROR," 取车流程释放车位请求缺少车位信息");
- }
- //2,判断调度节点状态
- Error_manager code=Dispatch_excutor::get_instance_pointer()->check_export_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*300); //测量超时300s
- m_dispatch_request_msg.mutable_base_info()->CopyFrom(base_info);
- message::Parkspace_info space_info=m_search_response_msg.car_position();
- m_dispatch_request_msg.set_dispatch_motion_direction(message::E_PICKUP_CAR);
- m_dispatch_request_msg.set_parkspace_id(space_info.parkspace_id());
- m_dispatch_request_msg.set_terminal_id(m_terminor_id);
- code=Dispatch_excutor::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,"取车流程调度反馈错误码");
- }
- /*
- * 回退取车调度
- */
- Error_manager PickupProcessTask::back_dispatch_step()
- {
- /*
- * 关门,搬运将车从出口抓回去
- */
- /*usleep(1000*1000);
- int k=rand()%10;
- if(k==0)
- return Error_manager(ERROR,MINOR_ERROR,"手动制造错误");*/
- return SUCCESS;
- }
- /*
- * 等待车辆离开
- */
- Error_manager PickupProcessTask::wait_for_leave_step()
- {
- usleep(1000*200);
- /*int k=rand()%10;
- if(k==0)
- return Error_manager(ERROR,MINOR_ERROR,"手动制造错误");*/
- return SUCCESS;
- }
- /*
- * 回退等待车辆离开
- */
- Error_manager PickupProcessTask::back_wait_for_leave_step()
- {
- return SUCCESS;
- }
- /*
- * 清除车位表中对应的车位
- */
- Error_manager PickupProcessTask::release_space_step()
- {
- /*int k=rand()%10;
- if(k==0)
- return Error_manager(ERROR,MINOR_ERROR,"手动制造错误");*/
- /*
- * 检查是否曾经分配过车位
- */
- if(m_search_response_msg.has_car_position()==false)
- {
- return Error_manager(FAILED,MINOR_ERROR," 取车流程释放车位请求缺少车位信息");
- }
- /*
- * 检查车位管理模块是否正常
- */
- Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
- if(code!=SUCCESS)
- return code;
- 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(5000); //测量超时1s
- m_release_request_msg.mutable_base_info()->CopyFrom(base_info);
- message::Parkspace_info space_info=m_search_response_msg.car_position();
- m_release_request_msg.mutable_release_space_info()->CopyFrom(space_info);
- code=Parkspace_excutor::get_instance_pointer()->release_request(m_release_request_msg,m_release_response_msg,m_cancel_condition);
- if(code!=SUCCESS)
- return code;
- if(m_release_response_msg.error_manager().error_code()==0) {
- return SUCCESS;
- }
- else
- return Error_manager(FAILED,MINOR_ERROR,"取车流程parkspace release response error_code error");
- }
- void PickupProcessTask::publish_step_status() {
- /*
- * 通过communicator 发布状态
- */
- if (Command_accepter::get_instance_pointer()) {
- std::lock_guard<std::mutex> lock(m_process_msg_lock);
- Command_accepter::get_instance_pointer()->post_process_statu(m_process_msg);
- }
- }
- /*
- * 根据当前流程状态,生成状态消息
- */
- void PickupProcessTask::updata_step_statu(message::Step_statu statu)
- {
- m_current_step_statu=statu;
- std::lock_guard<std::mutex> lock(m_process_msg_lock);
- switch (m_current_step_type) {
- case message::eSearch_step: {
- message::Search_space_step_statu search_step_statu;
- search_step_statu.set_step_statu(statu);
- m_process_msg.mutable_search_space_step()->CopyFrom(search_step_statu);
- break;
- }
- case message::eDispatch_step: {
- message::Dispatch_pick_step_statu dispatch_step_statu;
- dispatch_step_statu.set_step_statu(statu);
- dispatch_step_statu.mutable_space_info()->CopyFrom(m_search_response_msg.car_position());
- m_process_msg.mutable_dispatch_step()->CopyFrom(dispatch_step_statu);
- break;
- }
- case message::eWait_step:
- {
- message::Waitfor_leave_step_statu waitfor_leave_step;
- waitfor_leave_step.set_step_statu(statu);
- waitfor_leave_step.mutable_car_info()->CopyFrom(m_car_info);
- m_process_msg.mutable_waitfor_leave_step()->CopyFrom(waitfor_leave_step);
- break;
- }
- case message::eRelease_step: {
- message::Release_space_step_statu release_step_statu;
- release_step_statu.set_step_statu(statu);
- release_step_statu.mutable_space_info()->CopyFrom(m_search_response_msg.car_position());
- m_process_msg.mutable_release_space_step()->CopyFrom(release_step_statu);
- break;
- }
- case message::eComplete: {
- m_process_msg.set_completed(true);
- break;
- }
- case message::eBackWait_step: {
- message::Back_waitfor_leave_step_statu back_waitfor_step_statu;
- back_waitfor_step_statu.set_step_statu(statu);
- back_waitfor_step_statu.mutable_car_info()->CopyFrom(m_car_info);
- m_process_msg.mutable_back_waitfor_leave_step()->CopyFrom(back_waitfor_step_statu);
- break;
- }
- case message::eBackDispatch_step: {
- message::Back_dispatch_pick_step_statu back_dispatch_step_statu;
- back_dispatch_step_statu.set_step_statu(statu);
- back_dispatch_step_statu.mutable_space_info()->CopyFrom(m_search_response_msg.car_position());
- m_process_msg.mutable_back_dispatch_step()->CopyFrom(back_dispatch_step_statu);
- break;
- }
- case message::eBackSearch_step: {
- message::Back_search_space_step_statu back_search_step_statu;
- back_search_step_statu.set_step_statu(statu);
- back_search_step_statu.mutable_car_info()->CopyFrom(m_car_info);
- m_process_msg.mutable_back_search_space_step_statu()->CopyFrom(back_search_step_statu);
- break;
- }
- case message::eBackComplete: {
- m_process_msg.set_back_completed(true);
- break;
- }
- default:
- break;
- }
- }
|