PickupProcessTask.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. //
  2. // Created by zx on 2020/7/7.
  3. //
  4. #include <parkspace_excutor.h>
  5. #include <dispatch_message.pb.h>
  6. #include "PickupProcessTask.h"
  7. #include "process_message.pb.h"
  8. #include "command_manager.h"
  9. #include "command_accepter.h"
  10. #include "exception_solver.h"
  11. #include "uniq_key.h"
  12. PickupProcessTask::PickupProcessTask(unsigned int terminal_id,message::Car_info car_info)
  13. :Process_task(terminal_id,car_info)
  14. {
  15. }
  16. PickupProcessTask::~PickupProcessTask()
  17. {
  18. Exception_solver::get_instance_pointer()->delete_task_cancel_condition(m_car_info.license());
  19. }
  20. Error_manager PickupProcessTask::init_task()
  21. {
  22. reset_msg();
  23. //添加当前流程的任务取消标志位到异常处理模块
  24. m_cancel_condition.reset(false, false, false);
  25. Error_manager code=Exception_solver::get_instance_pointer()->add_task_cancel_condition(m_car_info.license(),this);
  26. if(code!=SUCCESS)
  27. return code;
  28. //设置状态消息,基本信息
  29. message::Base_info base_info;
  30. base_info.set_msg_type(message::ePicking_process_statu_msg);
  31. base_info.set_sender(message::eMain);
  32. base_info.set_receiver(message::eEmpty);
  33. m_process_msg.mutable_base_info()->CopyFrom(base_info);
  34. m_process_msg.set_terminal_id(m_terminor_id);
  35. m_process_msg.set_license(m_car_info.license());
  36. m_current_step_type=message::eSearch_step;
  37. ///创建状态发布线程
  38. if(m_publish_statu_thread== nullptr)
  39. {
  40. m_publish_exit_condition.reset(false, false, false);
  41. m_publish_statu_thread=new std::thread(publish_thread_func,this);
  42. }
  43. return SUCCESS;
  44. }
  45. void PickupProcessTask::reset_msg()
  46. {
  47. m_search_request_msg=message::Parkspace_search_request_msg();
  48. m_search_response_msg=message::Parkspace_search_response_msg();
  49. m_dispatch_request_msg=message::Dispatch_request_msg();
  50. m_dispatch_response_msg=message::Dispatch_response_msg();
  51. m_command_response_msg=message::Pickup_command_response_msg();
  52. }
  53. /*
  54. * 查询车位
  55. */
  56. Error_manager PickupProcessTask::search_space_step()
  57. {
  58. message::Base_info base_info_response;
  59. base_info_response.set_msg_type(message::ePickup_command_response_msg);
  60. base_info_response.set_sender(message::eMain);
  61. base_info_response.set_receiver(message::eTerminor);
  62. m_command_response_msg.mutable_base_info()->CopyFrom(base_info_response);
  63. m_command_response_msg.set_terminal_id(m_terminor_id);
  64. if(Command_manager::get_instance_pointer()->is_paused()==true)
  65. {
  66. return Error_manager(PAUSE, MINOR_ERROR, "系统已急停");
  67. }
  68. message::Entrance_statu statu=Command_manager::get_instance_pointer()->export_statu(m_terminor_id);
  69. if(statu.has_paused()==false) {
  70. return Error_manager(ERROR, MINOR_ERROR, "出口已禁止使用 Disable");
  71. }
  72. if(statu.paused()==true)
  73. {
  74. return Error_manager(ERROR, MINOR_ERROR, "出口已禁止使用 Disable");
  75. }
  76. /*
  77. * 检验汽车信息是否正常
  78. */
  79. if(m_car_info.has_car_width()==false||m_car_info.has_car_height()== false
  80. ||m_car_info.has_license()==false)
  81. {
  82. return Error_manager(INVALID_MESSAGE,CRITICAL_ERROR,"查询车位请求信息错误");
  83. }
  84. /*
  85. * 检查车位管理模块是否正常
  86. */
  87. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  88. if(code!=SUCCESS)
  89. {
  90. return code;
  91. }
  92. message::Base_info base_info_request;
  93. base_info_request.set_msg_type(message::eParkspace_search_request_msg);
  94. base_info_request.set_sender(message::eMain);
  95. base_info_request.set_receiver(message::eParkspace);
  96. base_info_request.set_timeout_ms(2000);
  97. m_search_request_msg.mutable_base_info()->CopyFrom(base_info_request);
  98. m_search_request_msg.mutable_car_info()->CopyFrom(m_car_info);
  99. code = Parkspace_excutor::get_instance_pointer()->search_request(m_search_request_msg,
  100. m_search_response_msg,m_cancel_condition);
  101. return code;
  102. }
  103. /*
  104. * 回退查询车位
  105. */
  106. Error_manager PickupProcessTask::back_search_space_step()
  107. {
  108. return SUCCESS;
  109. }
  110. /*
  111. * 控制流程到下一步
  112. */
  113. Error_manager PickupProcessTask::next_step()
  114. {
  115. if(m_current_step_statu==message::eWaiting || m_current_step_statu== message::eWorking)
  116. return Error_manager(ERROR,MINOR_ERROR,"当前状态禁止改变步骤类型");
  117. switch (m_current_step_type)
  118. {
  119. case message::eSearch_step:
  120. m_current_step_type=(m_current_step_statu==message::eFinished)?message::eDispatch_step:message::eBackComplete;
  121. break;
  122. case message::eDispatch_step:
  123. m_current_step_type=(m_current_step_statu==message::eFinished)?message::eWait_step:message::eBackSearch_step;
  124. break;
  125. case message::eWait_step:
  126. m_current_step_type=(m_current_step_statu==message::eFinished)?message::eRelease_step:message::eBackDispatch_step;
  127. break;
  128. case message::eRelease_step:
  129. m_current_step_type=(m_current_step_statu==message::eFinished)?message::eComplete:message::eBackWait_step;
  130. break;
  131. case message::eComplete:
  132. break;
  133. case message::eBackWait_step:
  134. m_current_step_type=message::eBackDispatch_step;
  135. break;
  136. case message::eBackDispatch_step:
  137. m_current_step_type=message::eBackSearch_step;
  138. break;
  139. case message::eBackSearch_step:
  140. m_current_step_type=message::eBackComplete;
  141. break;
  142. case message::eBackComplete:
  143. break;
  144. default:
  145. LOG(ERROR)<<"------- 取 ------- 下一步失败,当前步骤未知: "<<message::Step_type_Name(m_current_step_type);
  146. break;
  147. }
  148. return SUCCESS;
  149. }
  150. void PickupProcessTask::Main()
  151. {
  152. Error_manager code;
  153. //进入取车流程
  154. while(is_canceled()==false)
  155. {
  156. //第一步查询
  157. if(m_current_step_type==message::eSearch_step)
  158. {
  159. updata_step_statu(message::eWorking);
  160. code=search_space_step();
  161. message::Error_manager error_msg;
  162. error_msg.set_error_code(code.get_error_code());
  163. error_msg.set_error_description(code.get_error_description());
  164. m_command_response_msg.mutable_code()->CopyFrom(error_msg);
  165. Communication_message msg;
  166. msg.reset(m_command_response_msg.base_info(),m_command_response_msg.SerializeAsString());
  167. Message_communicator::get_instance_pointer()->send_msg(&msg);
  168. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  169. LOG_IF(ERROR, code != SUCCESS) << "------- 取 -------查询失败,取车终端:"<<m_terminor_id
  170. <<", 车牌号:"<<m_car_info.license()<<code.get_error_description();
  171. }
  172. if(m_current_step_type== message::eDispatch_step)
  173. {
  174. updata_step_statu(message::eWorking);
  175. code=dispatch_step();
  176. usleep(1000*500);
  177. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  178. LOG_IF(ERROR, code != SUCCESS) << "------- 取 -------取车调度失败,取车终端:"<<m_terminor_id
  179. <<", 车位id:"<<m_search_response_msg.car_position().parkspace_id()
  180. <<", 车位楼层:"<<m_search_response_msg.car_position().floor()
  181. <<", 车位序号:"<<m_search_response_msg.car_position().index()
  182. <<", 车牌号:"<<m_car_info.license()
  183. <<", 库内车牌号:"<<m_search_response_msg.car_position().car_info().license()
  184. <<code.get_error_description();
  185. }
  186. if(m_current_step_type== message::eWait_step)
  187. {
  188. updata_step_statu(message::eWorking);
  189. code=wait_for_leave_step();
  190. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  191. LOG_IF(ERROR, code != SUCCESS) << "------- 取 -------等待车辆离开失败,取车终端:"<<m_terminor_id
  192. <<", 车牌号:"<<m_car_info.license();
  193. }
  194. if(m_current_step_type== message::eRelease_step)
  195. {
  196. updata_step_statu(message::eWorking);
  197. code=release_space_step();
  198. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  199. if(code!=SUCCESS)
  200. {
  201. //提升错误等级为三级
  202. LOG(ERROR)<<"------- 取 ------- 释放车位失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
  203. Exception_solver::get_instance_pointer()->solve_exception(Error_manager(code.get_error_code(),MAJOR_ERROR,code.get_error_description()),this);
  204. continue;
  205. }
  206. }
  207. if(m_current_step_type==message::eComplete) {
  208. //流程结束前,保证至少发送一次流程完成状态
  209. updata_step_statu(message::eFinished);
  210. break;
  211. }
  212. // ----------------------------------------华丽的分割线 ----------------------------------------
  213. if(m_current_step_type== message::eBackWait_step)
  214. {
  215. updata_step_statu(message::eWorking);
  216. code=back_wait_for_leave_step();
  217. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  218. if(code.get_error_level()>=MAJOR_ERROR)
  219. {
  220. //提升错误等级为四级
  221. LOG(ERROR)<<"------- 取 ------- 回退等待失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
  222. Exception_solver::get_instance_pointer()->solve_exception(code,this);
  223. continue;
  224. }
  225. }
  226. if(m_current_step_type== message::eBackDispatch_step)
  227. {
  228. updata_step_statu(message::eWorking);
  229. code=back_dispatch_step();
  230. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  231. if(code.get_error_level()>=MAJOR_ERROR)
  232. {
  233. //提升错误等级为四级
  234. LOG(ERROR)<<"------- 取 ------- 回退调度失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
  235. Exception_solver::get_instance_pointer()->solve_exception(code,this);
  236. continue;
  237. }
  238. }
  239. if(m_current_step_type== message::eBackSearch_step)
  240. {
  241. updata_step_statu(message::eWorking);
  242. code=back_search_space_step();
  243. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  244. if(code.get_error_level()>=MAJOR_ERROR)
  245. {
  246. //提升错误等级为四级
  247. LOG(ERROR)<<"------- 取 ------- 回退调度失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
  248. Exception_solver::get_instance_pointer()->solve_exception(code,this);
  249. continue;
  250. }
  251. }
  252. if(m_current_step_type== message::eBackComplete)
  253. break;
  254. next_step();
  255. }
  256. /*
  257. * 跳出循环后,判断状态,是否正常结束, 循环跳出状态只有可能是 eBackComplete(异常结束),eComplete(正常结束),任务取消状态
  258. */
  259. updata_step_statu(message::eFinished);
  260. publish_step_status();
  261. if(m_cancel_condition.wait_for_millisecond(1)==true) {
  262. LOG(ERROR) << "------- 取 -------取车任务被强制取消,车牌号:" << m_car_info.license()
  263. << ", 终端号:" << m_terminor_id;
  264. usleep(1000*500);
  265. return ;
  266. }
  267. if(m_current_step_type== message::eBackComplete)
  268. {
  269. //异常结束
  270. usleep(1000*500);
  271. LOG(ERROR)<<"------- 取 -------异常取车,回退结束"<<"车牌号:"<<m_car_info.license()
  272. <<",xxxxxxxxxxxxxx 终端:"<<m_terminor_id<<" xxxxxxxxxxxxxx";
  273. }
  274. if(m_current_step_type== message::eComplete)
  275. {
  276. //正常结束
  277. usleep(1000*500);
  278. LOG(INFO)<<"------- 取 -------取车结束,"<<"车牌号:"<<m_car_info.license()
  279. <<",-------------- 终端:"<<m_terminor_id<<" --------------";
  280. }
  281. }
  282. /*
  283. * 执行取车动作请求,并等待执行完成
  284. */
  285. Error_manager PickupProcessTask::dispatch_step()
  286. {
  287. /*
  288. * 检查是否曾经分配过车位
  289. */
  290. if(m_search_response_msg.has_car_position()==false)
  291. {
  292. return Error_manager(FAILED,MINOR_ERROR," 取车流程释放车位请求缺少车位信息");
  293. }
  294. //2,判断调度节点状态
  295. Error_manager code=Dispatch_excutor::get_instance_pointer()->check_export_statu(m_terminor_id);
  296. if(code!=SUCCESS)
  297. return code;
  298. message::Base_info base_info;
  299. base_info.set_msg_type(message::eDispatch_request_msg);
  300. base_info.set_sender(message::eMain);
  301. base_info.set_receiver(message::eDispatch);
  302. base_info.set_timeout_ms(1000*300); //测量超时300s
  303. m_dispatch_request_msg.mutable_base_info()->CopyFrom(base_info);
  304. message::Parkspace_info space_info=m_search_response_msg.car_position();
  305. m_dispatch_request_msg.set_dispatch_motion_direction(message::E_PICKUP_CAR);
  306. m_dispatch_request_msg.set_parkspace_id(space_info.parkspace_id());
  307. m_dispatch_request_msg.set_terminal_id(m_terminor_id);
  308. code=Dispatch_excutor::get_instance_pointer()->dispatch_request(m_dispatch_request_msg,m_dispatch_response_msg,m_cancel_condition);
  309. if(code!=SUCCESS)
  310. return code;
  311. if(m_dispatch_response_msg.error_manager().error_code()==0) {
  312. return SUCCESS;
  313. }
  314. else
  315. return Error_manager(FAILED,MINOR_ERROR,"取车流程调度反馈错误码");
  316. }
  317. /*
  318. * 回退取车调度
  319. */
  320. Error_manager PickupProcessTask::back_dispatch_step()
  321. {
  322. /*
  323. * 关门,搬运将车从出口抓回去
  324. */
  325. /*usleep(1000*1000);
  326. int k=rand()%10;
  327. if(k==0)
  328. return Error_manager(ERROR,MINOR_ERROR,"手动制造错误");*/
  329. return SUCCESS;
  330. }
  331. /*
  332. * 等待车辆离开
  333. */
  334. Error_manager PickupProcessTask::wait_for_leave_step()
  335. {
  336. usleep(1000*200);
  337. /*int k=rand()%10;
  338. if(k==0)
  339. return Error_manager(ERROR,MINOR_ERROR,"手动制造错误");*/
  340. return SUCCESS;
  341. }
  342. /*
  343. * 回退等待车辆离开
  344. */
  345. Error_manager PickupProcessTask::back_wait_for_leave_step()
  346. {
  347. return SUCCESS;
  348. }
  349. /*
  350. * 清除车位表中对应的车位
  351. */
  352. Error_manager PickupProcessTask::release_space_step()
  353. {
  354. /*int k=rand()%10;
  355. if(k==0)
  356. return Error_manager(ERROR,MINOR_ERROR,"手动制造错误");*/
  357. /*
  358. * 检查是否曾经分配过车位
  359. */
  360. if(m_search_response_msg.has_car_position()==false)
  361. {
  362. return Error_manager(FAILED,MINOR_ERROR," 取车流程释放车位请求缺少车位信息");
  363. }
  364. /*
  365. * 检查车位管理模块是否正常
  366. */
  367. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  368. if(code!=SUCCESS)
  369. return code;
  370. message::Base_info base_info;
  371. base_info.set_msg_type(message::eParkspace_release_request_msg);
  372. base_info.set_sender(message::eMain);
  373. base_info.set_receiver(message::eParkspace);
  374. base_info.set_timeout_ms(5000); //测量超时1s
  375. m_release_request_msg.mutable_base_info()->CopyFrom(base_info);
  376. message::Parkspace_info space_info=m_search_response_msg.car_position();
  377. m_release_request_msg.mutable_release_space_info()->CopyFrom(space_info);
  378. code=Parkspace_excutor::get_instance_pointer()->release_request(m_release_request_msg,m_release_response_msg,m_cancel_condition);
  379. if(code!=SUCCESS)
  380. return code;
  381. if(m_release_response_msg.error_manager().error_code()==0) {
  382. return SUCCESS;
  383. }
  384. else
  385. return Error_manager(FAILED,MINOR_ERROR,"取车流程parkspace release response error_code error");
  386. }
  387. void PickupProcessTask::publish_step_status() {
  388. /*
  389. * 通过communicator 发布状态
  390. */
  391. if (Command_accepter::get_instance_pointer()) {
  392. std::lock_guard<std::mutex> lock(m_process_msg_lock);
  393. Command_accepter::get_instance_pointer()->post_process_statu(m_process_msg);
  394. }
  395. }
  396. /*
  397. * 根据当前流程状态,生成状态消息
  398. */
  399. void PickupProcessTask::updata_step_statu(message::Step_statu statu)
  400. {
  401. m_current_step_statu=statu;
  402. std::lock_guard<std::mutex> lock(m_process_msg_lock);
  403. switch (m_current_step_type) {
  404. case message::eSearch_step: {
  405. message::Search_space_step_statu search_step_statu;
  406. search_step_statu.set_step_statu(statu);
  407. m_process_msg.mutable_search_space_step()->CopyFrom(search_step_statu);
  408. break;
  409. }
  410. case message::eDispatch_step: {
  411. message::Dispatch_pick_step_statu dispatch_step_statu;
  412. dispatch_step_statu.set_step_statu(statu);
  413. dispatch_step_statu.mutable_space_info()->CopyFrom(m_search_response_msg.car_position());
  414. m_process_msg.mutable_dispatch_step()->CopyFrom(dispatch_step_statu);
  415. break;
  416. }
  417. case message::eWait_step:
  418. {
  419. message::Waitfor_leave_step_statu waitfor_leave_step;
  420. waitfor_leave_step.set_step_statu(statu);
  421. waitfor_leave_step.mutable_car_info()->CopyFrom(m_car_info);
  422. m_process_msg.mutable_waitfor_leave_step()->CopyFrom(waitfor_leave_step);
  423. break;
  424. }
  425. case message::eRelease_step: {
  426. message::Release_space_step_statu release_step_statu;
  427. release_step_statu.set_step_statu(statu);
  428. release_step_statu.mutable_space_info()->CopyFrom(m_search_response_msg.car_position());
  429. m_process_msg.mutable_release_space_step()->CopyFrom(release_step_statu);
  430. break;
  431. }
  432. case message::eComplete: {
  433. m_process_msg.set_completed(true);
  434. break;
  435. }
  436. case message::eBackWait_step: {
  437. message::Back_waitfor_leave_step_statu back_waitfor_step_statu;
  438. back_waitfor_step_statu.set_step_statu(statu);
  439. back_waitfor_step_statu.mutable_car_info()->CopyFrom(m_car_info);
  440. m_process_msg.mutable_back_waitfor_leave_step()->CopyFrom(back_waitfor_step_statu);
  441. break;
  442. }
  443. case message::eBackDispatch_step: {
  444. message::Back_dispatch_pick_step_statu back_dispatch_step_statu;
  445. back_dispatch_step_statu.set_step_statu(statu);
  446. back_dispatch_step_statu.mutable_space_info()->CopyFrom(m_search_response_msg.car_position());
  447. m_process_msg.mutable_back_dispatch_step()->CopyFrom(back_dispatch_step_statu);
  448. break;
  449. }
  450. case message::eBackSearch_step: {
  451. message::Back_search_space_step_statu back_search_step_statu;
  452. back_search_step_statu.set_step_statu(statu);
  453. back_search_step_statu.mutable_car_info()->CopyFrom(m_car_info);
  454. m_process_msg.mutable_back_search_space_step_statu()->CopyFrom(back_search_step_statu);
  455. break;
  456. }
  457. case message::eBackComplete: {
  458. m_process_msg.set_back_completed(true);
  459. break;
  460. }
  461. default:
  462. break;
  463. }
  464. }