PickupProcessTask.cpp 13 KB

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