PickupProcessTask.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. //
  2. // Created by zx on 2020/7/7.
  3. //
  4. #include <Parkspace_communicator.h>
  5. #include <dispatch_message.pb.h>
  6. #include "PickupProcessTask.h"
  7. #include "process_message.pb.h"
  8. #include "command_manager.h"
  9. #include "system_communicator.h"
  10. #include "uniq_key.h"
  11. PickupProcessTask::PickupProcessTask(unsigned int terminal_id)
  12. :m_publish_statu_thread(nullptr)
  13. {
  14. m_terminor_id=terminal_id;
  15. message::Base_info base_info;
  16. base_info.set_msg_type(message::ePicking_process_statu_msg);
  17. base_info.set_sender(message::eMain);
  18. base_info.set_receiver(message::eEmpty);
  19. m_picking_process_statu_msg.mutable_base_info()->CopyFrom(base_info);
  20. m_picking_process_statu_msg.set_terminal_id(terminal_id);
  21. reset_process_statu();
  22. }
  23. PickupProcessTask::~PickupProcessTask()
  24. {
  25. //退出线程
  26. m_publish_exit_condition.set_pass_ever(true);
  27. if(m_publish_statu_thread!= nullptr)
  28. {
  29. if(m_publish_statu_thread->joinable())
  30. {
  31. m_publish_statu_thread->join();
  32. }
  33. delete m_publish_statu_thread;
  34. m_publish_statu_thread=nullptr;
  35. }
  36. }
  37. Error_manager PickupProcessTask::init_task(message::Car_info car_info)
  38. {
  39. reset_recv_msg();
  40. m_car_info=car_info;
  41. reset_process_statu();
  42. ///创建状态发布线程
  43. if(m_publish_statu_thread== nullptr)
  44. {
  45. m_publish_exit_condition.reset(false, false, false);
  46. m_publish_statu_thread=new std::thread(publish_thread_func,this);
  47. }
  48. return SUCCESS;
  49. }
  50. void PickupProcessTask::reset_recv_msg()
  51. {
  52. m_parcspace_search_response_msg=message::Parkspace_search_response_msg();
  53. m_dispatch_response_msg=message::Dispatch_response_msg();
  54. }
  55. /*
  56. * reset 进度信息
  57. */
  58. void PickupProcessTask::reset_process_statu()
  59. {
  60. message::Check_space_step_statu check_step;
  61. check_step.set_step_statu(message::eWaiting);
  62. message::Dispatch_pick_step_statu pick_step;
  63. pick_step.set_step_statu(message::eWaiting);
  64. message::Release_space_step_statu release_step;
  65. release_step.set_step_statu(message::eWaiting);
  66. message::Waitfor_leave_step_statu waitfor_leave_step;
  67. waitfor_leave_step.set_step_statu(message::eWaiting);
  68. std::lock_guard<std::mutex> lock(m_picking_statu_lock);
  69. m_picking_process_statu_msg.mutable_check_space_step()->CopyFrom(check_step);
  70. m_picking_process_statu_msg.mutable_dispatch_step()->CopyFrom(pick_step);
  71. m_picking_process_statu_msg.mutable_release_space_step()->CopyFrom(release_step);
  72. m_picking_process_statu_msg.mutable_waitfor_leave()->CopyFrom(waitfor_leave_step);
  73. m_picking_process_statu_msg.set_license(m_car_info.license());
  74. }
  75. /*
  76. * 查询车位
  77. */
  78. Error_manager PickupProcessTask::search_space()
  79. {
  80. //更新状态
  81. {
  82. std::lock_guard<std::mutex> lock(m_picking_statu_lock);
  83. m_picking_process_statu_msg.mutable_check_space_step()->set_step_statu(message::eWorking);
  84. }
  85. /*
  86. * 检验汽车信息是否正常
  87. */
  88. if(m_car_info.has_car_width()==false||m_car_info.has_car_height()== false
  89. ||m_car_info.has_license()==false)
  90. {
  91. std::lock_guard<std::mutex> lock(m_picking_statu_lock);
  92. m_picking_process_statu_msg.mutable_check_space_step()->set_step_statu(message::eError);
  93. m_picking_process_statu_msg.mutable_check_space_step()->set_description("查询车位请求汽车信息错误");
  94. return Error_manager(INVALID_MESSAGE,CRITICAL_ERROR,"查询车位请求汽车信息错误");
  95. }
  96. /*
  97. * 检查车位管理模块是否正常
  98. */
  99. Error_manager code=Parkspace_communicator::get_instance_pointer()->check_statu();
  100. std::lock_guard<std::mutex> lock(m_picking_statu_lock);
  101. if(code!=SUCCESS)
  102. {
  103. m_picking_process_statu_msg.mutable_check_space_step()->set_description(code.get_error_description());
  104. m_picking_process_statu_msg.mutable_check_space_step()->set_step_statu(message::eError);
  105. return code;
  106. }
  107. message::Base_info base_info;
  108. base_info.set_msg_type(message::eParkspace_search_request_msg);
  109. base_info.set_sender(message::eMain);
  110. base_info.set_receiver(message::eParkspace);
  111. base_info.set_timeout_ms(1000);
  112. message::Parkspace_search_request_msg request;
  113. request.set_command_key(create_key());
  114. request.mutable_base_info()->CopyFrom(base_info);
  115. request.mutable_car_info()->CopyFrom(m_car_info);
  116. code = Parkspace_communicator::get_instance_pointer()->search_request(request,m_parcspace_search_response_msg);
  117. if(code==SUCCESS)
  118. {
  119. m_picking_process_statu_msg.mutable_check_space_step()->mutable_space_info()->CopyFrom(m_parcspace_search_response_msg.car_position());
  120. m_picking_process_statu_msg.mutable_check_space_step()->set_step_statu(message::eComplete);
  121. }
  122. else {
  123. m_picking_process_statu_msg.mutable_check_space_step()->set_step_statu(message::eError);
  124. m_picking_process_statu_msg.mutable_check_space_step()->set_description(code.get_error_description());
  125. }
  126. return code;
  127. }
  128. void PickupProcessTask::Main()
  129. {
  130. Error_manager code;
  131. //进入取车流程
  132. switch (0)
  133. {
  134. //第一步,执行取车动作
  135. case 0:
  136. {
  137. //更新状态
  138. {
  139. std::lock_guard<std::mutex> lock(m_picking_statu_lock);
  140. m_picking_process_statu_msg.mutable_dispatch_step()->set_step_statu(message::eWorking);
  141. m_picking_process_statu_msg.mutable_dispatch_step()->mutable_space_info()->CopyFrom(
  142. m_parcspace_search_response_msg.car_position());
  143. }
  144. //开始工作
  145. code=pickup_step();
  146. usleep(1000*1000*(rand()%3));
  147. std::lock_guard<std::mutex> lock(m_picking_statu_lock);
  148. if(code!=SUCCESS)
  149. {
  150. LOG(ERROR)<<"取车调度失败,取车终端:"<<m_terminor_id
  151. <<", 车位id:"<<m_parcspace_search_response_msg.car_position().parkspace_id()
  152. <<", 车位楼层:"<<m_parcspace_search_response_msg.car_position().floor()
  153. <<", 车位序号:"<<m_parcspace_search_response_msg.car_position().index()
  154. <<", 车牌号:"<<m_car_info.license()
  155. <<", 库内车牌号:"<<m_parcspace_search_response_msg.car_position().car_info().license();
  156. m_picking_process_statu_msg.mutable_dispatch_step()->set_step_statu(message::eError);
  157. m_picking_process_statu_msg.mutable_dispatch_step()->set_description(code.get_error_description());
  158. break;
  159. }
  160. m_picking_process_statu_msg.mutable_dispatch_step()->set_step_statu(message::eComplete);
  161. }
  162. //第二步,清除车位
  163. case 1:
  164. {
  165. //更新状态
  166. {
  167. std::lock_guard<std::mutex> lock(m_picking_statu_lock);
  168. m_picking_process_statu_msg.mutable_release_space_step()->set_step_statu(message::eWorking);
  169. m_picking_process_statu_msg.mutable_release_space_step()->mutable_space_info()->CopyFrom(
  170. m_parcspace_search_response_msg.car_position());
  171. }
  172. code=release_space_step();
  173. std::lock_guard<std::mutex> lock(m_picking_statu_lock);
  174. if(code!=SUCCESS)
  175. {
  176. m_picking_process_statu_msg.mutable_release_space_step()->set_step_statu(message::eError);
  177. m_picking_process_statu_msg.mutable_release_space_step()->set_description(code.get_error_description());
  178. LOG(ERROR)<<"取车释放车位失败:"<<code.get_error_description();
  179. break;
  180. }
  181. m_picking_process_statu_msg.mutable_release_space_step()->set_step_statu(message::eComplete);
  182. }
  183. //第三步,等待车辆离开
  184. case 2:
  185. {
  186. //更新状态
  187. {
  188. std::lock_guard<std::mutex> lock(m_picking_statu_lock);
  189. m_picking_process_statu_msg.mutable_waitfor_leave()->set_step_statu(message::eWorking);
  190. m_picking_process_statu_msg.mutable_waitfor_leave()->mutable_car_info()->CopyFrom(m_car_info);
  191. }
  192. //等待离开
  193. code=wait_for_leave_step();
  194. std::lock_guard<std::mutex> lock(m_picking_statu_lock);
  195. if(code!=SUCCESS)
  196. {
  197. m_picking_process_statu_msg.mutable_waitfor_leave()->set_step_statu(message::eError);
  198. m_picking_process_statu_msg.mutable_waitfor_leave()->set_description(code.get_error_description());
  199. break;
  200. }
  201. m_picking_process_statu_msg.mutable_waitfor_leave()->set_step_statu(message::eComplete);
  202. LOG(WARNING)<<"取车完成-----------------------------"<<m_car_info.license();
  203. usleep(1000*1000);
  204. return ;
  205. }
  206. default:break;
  207. }
  208. /*
  209. *
  210. */
  211. usleep(1000*1000);
  212. }
  213. /*
  214. * 执行取车动作请求,并等待执行完成
  215. */
  216. Error_manager PickupProcessTask::pickup_step()
  217. {
  218. /*
  219. * 检查是否曾经分配过车位
  220. */
  221. if(m_parcspace_search_response_msg.has_car_position()==false)
  222. {
  223. return Error_manager(FAILED,MINOR_ERROR," 取车流程释放车位请求缺少车位信息");
  224. }
  225. //2,判断调度节点状态
  226. Error_manager code=Dispatch_communicator::get_instance_pointer()->check_export_statu(m_terminor_id);
  227. if(code!=SUCCESS)
  228. return code;
  229. message::Dispatch_request_msg request;
  230. message::Base_info base_info;
  231. base_info.set_msg_type(message::eDispatch_request_msg);
  232. base_info.set_sender(message::eMain);
  233. base_info.set_receiver(message::eDispatch);
  234. base_info.set_timeout_ms(1000*300); //测量超时300s
  235. request.mutable_base_info()->CopyFrom(base_info);
  236. message::Parkspace_info space_info=m_parcspace_search_response_msg.car_position();
  237. request.set_dispatch_motion_direction(message::E_PICKUP_CAR);
  238. request.set_parkspace_id(space_info.parkspace_id());
  239. request.set_terminal_id(m_terminor_id);
  240. request.set_command_key(create_key());
  241. message::Dispatch_response_msg response;
  242. code=Dispatch_communicator::get_instance_pointer()->dispatch_request(request,response);
  243. if(code!=SUCCESS)
  244. return code;
  245. if(response.error_manager().error_code()==0) {
  246. return SUCCESS;
  247. }
  248. else
  249. return Error_manager(FAILED,MINOR_ERROR,"取车流程调度反馈错误码");
  250. }
  251. /*
  252. * 等待车辆离开
  253. */
  254. Error_manager PickupProcessTask::wait_for_leave_step()
  255. {
  256. return SUCCESS;
  257. }
  258. /*
  259. * 清除车位表中对应的车位
  260. */
  261. Error_manager PickupProcessTask::release_space_step()
  262. {
  263. /*
  264. * 检查是否曾经分配过车位
  265. */
  266. if(m_parcspace_search_response_msg.has_car_position()==false)
  267. {
  268. return Error_manager(FAILED,MINOR_ERROR," 取车流程释放车位请求缺少车位信息");
  269. }
  270. /*
  271. * 检查车位管理模块是否正常
  272. */
  273. Error_manager code=Parkspace_communicator::get_instance_pointer()->check_statu();
  274. if(code!=SUCCESS)
  275. return code;
  276. message::Parkspace_release_request_msg request;
  277. message::Base_info base_info;
  278. base_info.set_msg_type(message::eParkspace_release_request_msg);
  279. base_info.set_sender(message::eMain);
  280. base_info.set_receiver(message::eParkspace);
  281. base_info.set_timeout_ms(1000); //测量超时1s
  282. request.mutable_base_info()->CopyFrom(base_info);
  283. message::Parkspace_info space_info=m_parcspace_search_response_msg.car_position();
  284. request.mutable_release_space_info()->CopyFrom(space_info);
  285. request.set_command_key(create_key());
  286. message::Parkspace_release_response_msg release_response;
  287. code=Parkspace_communicator::get_instance_pointer()->release_request(request,release_response);
  288. if(code!=SUCCESS)
  289. return code;
  290. if(release_response.error_manager().error_code()==0) {
  291. return SUCCESS;
  292. }
  293. else
  294. return Error_manager(FAILED,MINOR_ERROR,"取车流程parkspace release response error_code error");
  295. }
  296. /*
  297. * 发布状态线程
  298. */
  299. void PickupProcessTask::publish_thread_func(PickupProcessTask* ptask)
  300. {
  301. if(ptask)
  302. {
  303. ptask->publish_step_status();
  304. }
  305. }
  306. void PickupProcessTask::publish_step_status()
  307. {
  308. //未收到退出信号
  309. while(false==m_publish_exit_condition.wait_for_ex(std::chrono::milliseconds(50)))
  310. {
  311. /*
  312. * 通过communicator 发布状态
  313. */
  314. if(System_communicator::get_instance_pointer())
  315. {
  316. if(m_picking_process_statu_msg.has_base_info()==true)
  317. {
  318. std::lock_guard<std::mutex> lock(m_picking_statu_lock);
  319. System_communicator::get_instance_pointer()->post_entrance_statu(m_picking_process_statu_msg);
  320. }
  321. }
  322. }
  323. }