StoreProcessTask.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //
  2. // Created by zx on 2020/7/7.
  3. //
  4. #include <Parkspace_communicator.h>
  5. #include <dispatch_message.pb.h>
  6. #include "dispatch_communicator.h"
  7. #include "StoreProcessTask.h"
  8. #include "process_message.pb.h"
  9. #include "command_manager.h"
  10. #include "system_communicator.h"
  11. StoreProcessTask::StoreProcessTask(unsigned int terminor_id)
  12. :m_publish_statu_thread(nullptr)
  13. {
  14. m_command_id=-1;
  15. m_terminor_id=terminor_id;
  16. message::Base_info base_info;
  17. base_info.set_msg_type(message::eStoring_process_statu_msg);
  18. base_info.set_sender(message::eMain);
  19. base_info.set_receiver(message::eEmpty);
  20. m_storing_process_statu_msg.mutable_base_info()->CopyFrom(base_info);
  21. m_storing_process_statu_msg.set_terminal_id(terminor_id);
  22. reset_process_statu();
  23. }
  24. StoreProcessTask::~StoreProcessTask()
  25. {
  26. //退出线程
  27. m_publish_exit_condition.set_pass_ever(true);
  28. if(m_publish_statu_thread!= nullptr)
  29. {
  30. if(m_publish_statu_thread->joinable())
  31. {
  32. m_publish_statu_thread->join();
  33. }
  34. delete m_publish_statu_thread;
  35. m_publish_statu_thread=nullptr;
  36. }
  37. }
  38. Error_manager StoreProcessTask::init_task( unsigned int command_id,
  39. message::Locate_information locate_info,message::Car_info car_info)
  40. {
  41. m_command_id=command_id;
  42. m_car_info=car_info;
  43. m_locate_info=locate_info;
  44. ///创建状态发布线程
  45. if(m_publish_statu_thread== nullptr)
  46. {
  47. m_publish_exit_condition.reset(false, false, false);
  48. m_publish_statu_thread=new std::thread(publish_thread_func,this);
  49. }
  50. if(is_ready()) {
  51. return SUCCESS;
  52. }
  53. else
  54. {
  55. return Error_manager(ERROR,MINOR_ERROR,"终端未准备!!!");
  56. }
  57. }
  58. Error_manager StoreProcessTask::locate_step() {
  59. Error_manager code;
  60. //检查测量模块状态
  61. code=Locate_communicator::get_instance_pointer()->check_statu();
  62. if(code!=SUCCESS)
  63. return code;
  64. message::Measure_request_msg request;
  65. message::Base_info base_info;
  66. base_info.set_msg_type(message::eLocate_request_msg);
  67. base_info.set_sender(message::eMain);
  68. base_info.set_receiver(message::eMeasurer);
  69. base_info.set_timeout_ms(20000); //测量超时5s
  70. request.mutable_base_info()->CopyFrom(base_info);
  71. request.set_command_id(m_command_id);
  72. request.set_terminal_id(m_terminor_id);
  73. code=Locate_communicator::get_instance_pointer()->locate_request(request,m_measure_response_msg);
  74. if(code!=SUCCESS)
  75. return code;
  76. if(m_measure_response_msg.error_manager().error_code()==0) {
  77. return SUCCESS;
  78. }
  79. else
  80. return Error_manager(FAILED,MINOR_ERROR,m_measure_response_msg.error_manager().error_description().c_str());
  81. }
  82. /*
  83. * 调度
  84. */
  85. Error_manager StoreProcessTask::dispatch_step()
  86. {
  87. Error_manager code;
  88. /*
  89. * 判断调度所需的数据是否都正常
  90. */
  91. //1,测量信息是否存在
  92. if(m_measure_response_msg.has_base_info()== false
  93. ||m_measure_response_msg.has_locate_information()==false
  94. ||m_parcspace_alloc_response_msg.has_base_info()== false
  95. ||m_parcspace_alloc_response_msg.has_allocated_space_info()==false)
  96. {
  97. return Error_manager(ERROR,MAJOR_ERROR,"调度所需的前置数据(测量,车位)不存在");
  98. }
  99. //2,判断调度节点状态
  100. code=Dispatch_communicator::get_instance_pointer()->check_statu();
  101. if(code!=SUCCESS)
  102. return code;
  103. message::Dispatch_request_msg request;
  104. message::Base_info base_info;
  105. base_info.set_msg_type(message::eDispatch_request_msg);
  106. base_info.set_sender(message::eMain);
  107. base_info.set_receiver(message::eDispatch);
  108. base_info.set_timeout_ms(1000*15); //测量超时15s
  109. request.mutable_base_info()->CopyFrom(base_info);
  110. request.set_command_id(m_command_id);
  111. request.set_terminal_id(m_terminor_id);
  112. request.set_dispatch_motion_direction(message::E_STORE_CAR);
  113. request.set_parkspace_id(m_parcspace_alloc_response_msg.allocated_space_info().parkspace_id());
  114. code=Dispatch_communicator::get_instance_pointer()->dispatch_request(request,m_dispatch_response_msg);
  115. if(code!=SUCCESS)
  116. return code;
  117. if(m_dispatch_response_msg.error_manager().error_code()==0) {
  118. return SUCCESS;
  119. }
  120. else
  121. return Error_manager(FAILED,MINOR_ERROR,"dispatch response error_code error");
  122. }
  123. /*
  124. * reset 进度信息
  125. */
  126. void StoreProcessTask::reset_process_statu()
  127. {
  128. message::Alloc_space_step_statu alloc_step;
  129. alloc_step.set_step_statu(message::eWaiting);
  130. message::Measure_step_statu measure_step;
  131. measure_step.set_step_statu(message::eWaiting);
  132. message::Dispatch_store_step_statu store_step;
  133. store_step.set_step_statu(message::eWaiting);
  134. message::Confirm_space_step_statu confirm_step;
  135. confirm_step.set_step_statu(message::eWaiting);
  136. message::Release_space_step_statu release_step;
  137. release_step.set_step_statu(message::eWaiting);
  138. m_storing_process_statu_msg.mutable_alloc_space_step()->CopyFrom(alloc_step);
  139. m_storing_process_statu_msg.mutable_measure_step()->CopyFrom(measure_step);
  140. m_storing_process_statu_msg.mutable_dispatch_step()->CopyFrom(store_step);
  141. m_storing_process_statu_msg.mutable_confirm_space_step()->CopyFrom(confirm_step);
  142. m_storing_process_statu_msg.mutable_failed_release_space_step()->CopyFrom(release_step);
  143. }
  144. /*
  145. * 检查当前任务是否处于空闲准备状态
  146. */
  147. bool StoreProcessTask::is_ready()
  148. {
  149. bool cond_alloc_space = m_storing_process_statu_msg.alloc_space_step().step_statu()==message::eWaiting
  150. ||m_storing_process_statu_msg.alloc_space_step().step_statu()==message::eComplete;
  151. bool cond_measure = m_storing_process_statu_msg.measure_step().step_statu()==message::eWaiting
  152. ||m_storing_process_statu_msg.measure_step().step_statu()==message::eComplete;
  153. bool cond_dispatch = m_storing_process_statu_msg.dispatch_step().step_statu()==message::eWaiting
  154. ||m_storing_process_statu_msg.dispatch_step().step_statu()==message::eComplete;
  155. bool cond_confirm_space = m_storing_process_statu_msg.confirm_space_step().step_statu()==message::eWaiting
  156. ||m_storing_process_statu_msg.confirm_space_step().step_statu()==message::eComplete;
  157. bool cond_release_space = m_storing_process_statu_msg.failed_release_space_step().step_statu()==message::eWaiting
  158. ||m_storing_process_statu_msg.failed_release_space_step().step_statu()==message::eComplete;
  159. return cond_alloc_space && cond_measure && cond_dispatch && cond_confirm_space && cond_release_space;
  160. }
  161. /*
  162. * 分配车位
  163. */
  164. Error_manager StoreProcessTask::alloc_space()
  165. {
  166. //更新车位分配步骤状态
  167. m_storing_process_statu_msg.mutable_alloc_space_step()->mutable_car_info()->CopyFrom(m_locate_info);
  168. m_storing_process_statu_msg.mutable_alloc_space_step()->set_step_statu(message::eWorking);
  169. /*
  170. * 检查是否有测量数据
  171. */
  172. if(m_locate_info.has_locate_height()==false||m_locate_info.has_locate_width()==false)
  173. {
  174. m_storing_process_statu_msg.mutable_alloc_space_step()->set_step_statu(message::eError);
  175. m_storing_process_statu_msg.mutable_alloc_space_step()->set_description("停车请求缺少车辆高度和宽度信息");
  176. return Error_manager(FAILED,MINOR_ERROR,"停车请求缺少车辆高度和宽度信息");
  177. }
  178. /*
  179. * 检查车位管理模块是否正常
  180. */
  181. Error_manager code=Parkspace_communicator::get_instance_pointer()->check_statu();
  182. if(code!=SUCCESS)
  183. {
  184. m_storing_process_statu_msg.mutable_alloc_space_step()->set_step_statu(message::eError);
  185. m_storing_process_statu_msg.mutable_alloc_space_step()->set_description(code.get_error_description());
  186. return code;
  187. }
  188. //发送分配请求
  189. message::Parkspace_allocation_request_msg request;
  190. message::Base_info base_info;
  191. base_info.set_msg_type(message::eParkspace_allocation_request_msg);
  192. base_info.set_sender(message::eMain);
  193. base_info.set_receiver(message::eParkspace);
  194. base_info.set_timeout_ms(1000); //测量超时1s
  195. request.mutable_base_info()->CopyFrom(base_info);
  196. request.mutable_car_info()->CopyFrom(m_car_info);
  197. request.set_command_id(m_command_id);
  198. request.set_terminal_id(m_terminor_id);
  199. code=Parkspace_communicator::get_instance_pointer()->alloc_request(request,m_parcspace_alloc_response_msg);
  200. if(code!=SUCCESS)
  201. {
  202. m_storing_process_statu_msg.mutable_alloc_space_step()->set_step_statu(message::eError);
  203. m_storing_process_statu_msg.mutable_alloc_space_step()->set_description(code.get_error_description());
  204. return code;
  205. }
  206. if(m_parcspace_alloc_response_msg.error_manager().error_code()==0)
  207. {
  208. m_storing_process_statu_msg.mutable_alloc_space_step()->set_step_statu(message::eComplete);
  209. return SUCCESS;
  210. }
  211. else
  212. {
  213. m_storing_process_statu_msg.mutable_alloc_space_step()->set_step_statu(message::eError);
  214. m_storing_process_statu_msg.mutable_alloc_space_step()->set_description("分配车位反馈结果错误");
  215. return Error_manager(FAILED,MINOR_ERROR,"分配车位反馈结果错误");
  216. }
  217. }
  218. /*
  219. * 车位占用确认
  220. */
  221. Error_manager StoreProcessTask::confirm_space_step()
  222. {
  223. /*
  224. * 检查是否曾经分配过车位
  225. */
  226. if(m_parcspace_alloc_response_msg.has_allocated_space_info()==false)
  227. {
  228. return Error_manager(FAILED,MINOR_ERROR," parkspace confirm request without space info");
  229. }
  230. /*
  231. * 检查车位管理模块是否正常
  232. */
  233. Error_manager code=Parkspace_communicator::get_instance_pointer()->check_statu();
  234. if(code!=SUCCESS)
  235. return code;
  236. message::Parkspace_confirm_alloc_request_msg request;
  237. message::Base_info base_info;
  238. base_info.set_msg_type(message::eParkspace_confirm_alloc_request_msg);
  239. base_info.set_sender(message::eMain);
  240. base_info.set_receiver(message::eParkspace);
  241. base_info.set_timeout_ms(1000); //测量超时1s
  242. request.mutable_base_info()->CopyFrom(base_info);
  243. message::Parkspace_info space_info=m_parcspace_alloc_response_msg.allocated_space_info();
  244. request.mutable_confirm_space_info()->CopyFrom(space_info);
  245. request.set_command_id(m_command_id);
  246. message::Parkspace_confirm_alloc_response_msg confirm_response;
  247. code=Parkspace_communicator::get_instance_pointer()->confirm_request(request,confirm_response);
  248. if(code!=SUCCESS)
  249. return code;
  250. if(m_parcspace_alloc_response_msg.allocated_space_info().parkspace_id()!=
  251. confirm_response.confirm_alloc_space_info().parkspace_id())
  252. {
  253. return Error_manager(ERROR,MINOR_ERROR,"占用车位与分配车位不一致");
  254. }
  255. if(confirm_response.error_manager().error_code()==0) {
  256. LOG(INFO)<<"停车流程正常,确认占用车位成功,停车终端:"<<m_terminor_id
  257. <<", 指令id:"<<m_command_id
  258. <<", 车位楼层:"<<confirm_response.confirm_alloc_space_info().floor()
  259. <<", 车位序号:"<<confirm_response.confirm_alloc_space_info().index()
  260. <<", 车牌号:"<<confirm_response.confirm_alloc_space_info().car_info().license();
  261. return SUCCESS;
  262. }
  263. else
  264. return Error_manager(FAILED,MINOR_ERROR,"parkspace confirm response error_code error");
  265. }
  266. /*
  267. * 车位解锁,当停车失败时需要车位解锁
  268. */
  269. Error_manager StoreProcessTask::release_space_step()
  270. {
  271. /*
  272. * 检查是否曾经分配过车位
  273. */
  274. if(m_parcspace_alloc_response_msg.has_allocated_space_info()==false)
  275. {
  276. return Error_manager(FAILED,MINOR_ERROR," parkspace release request without space info");
  277. }
  278. /*
  279. * 检查车位管理模块是否正常
  280. */
  281. Error_manager code=Parkspace_communicator::get_instance_pointer()->check_statu();
  282. if(code!=SUCCESS)
  283. return code;
  284. message::Parkspace_release_request_msg request;
  285. message::Base_info base_info;
  286. base_info.set_msg_type(message::eParkspace_release_request_msg);
  287. base_info.set_sender(message::eMain);
  288. base_info.set_receiver(message::eParkspace);
  289. base_info.set_timeout_ms(1000); //测量超时1s
  290. request.mutable_base_info()->CopyFrom(base_info);
  291. message::Parkspace_info space_info=m_parcspace_alloc_response_msg.allocated_space_info();
  292. request.mutable_release_space_info()->CopyFrom(space_info);
  293. request.set_command_id(m_command_id);
  294. message::Parkspace_release_response_msg release_response;
  295. code=Parkspace_communicator::get_instance_pointer()->release_request(request,release_response);
  296. if(code!=SUCCESS)
  297. return code;
  298. if(release_response.error_manager().error_code()==0) {
  299. LOG(WARNING)<<"停车流程异常,释放车位成功,停车终端:"<<m_terminor_id
  300. <<", 指令id:"<<m_command_id
  301. <<", 车位楼层:"<<m_parcspace_alloc_response_msg.allocated_space_info().floor()
  302. <<", 车位序号:"<<m_parcspace_alloc_response_msg.allocated_space_info().index()
  303. <<", 车牌号:"<<m_parcspace_alloc_response_msg.allocated_space_info().car_info().license();
  304. return SUCCESS;
  305. }
  306. else
  307. return Error_manager(FAILED,MINOR_ERROR,"parkspace release response error_code error");
  308. }
  309. void StoreProcessTask::Main()
  310. {
  311. /*
  312. * 外部已经分配好车位,进入到此流程说明车位已经分配好, 存放在 m_parcspace_alloc_response_msg
  313. */
  314. Error_manager code;
  315. //开始执行停车指令
  316. //第一步测量
  317. switch (0)
  318. {
  319. //第一步,测量
  320. case 0:
  321. {
  322. //更新状态信息
  323. m_storing_process_statu_msg.mutable_measure_step()->set_step_statu(message::eWorking);
  324. //开始定位
  325. code=locate_step();
  326. if(code!=SUCCESS)
  327. {
  328. m_storing_process_statu_msg.mutable_measure_step()->set_step_statu(message::eError);
  329. m_storing_process_statu_msg.mutable_measure_step()->set_description(code.get_error_description());
  330. LOG(ERROR)<<"测量失败:"<<code.get_error_description();
  331. break;
  332. }
  333. //更新状态信息
  334. m_storing_process_statu_msg.mutable_measure_step()->mutable_locate_info()->CopyFrom(m_measure_response_msg.locate_information());
  335. m_storing_process_statu_msg.mutable_measure_step()->set_step_statu(message::eComplete);
  336. }
  337. //第二步,调度
  338. case 1:
  339. {
  340. //更新状态信息
  341. m_storing_process_statu_msg.mutable_dispatch_step()->set_step_statu(message::eWorking);
  342. m_storing_process_statu_msg.mutable_dispatch_step()->mutable_locate_info()->CopyFrom(m_measure_response_msg.locate_information());
  343. m_storing_process_statu_msg.mutable_dispatch_step()->mutable_space_info()->CopyFrom(m_parcspace_alloc_response_msg.allocated_space_info());
  344. //开始调度
  345. code=dispatch_step();
  346. if(code!=SUCCESS)
  347. {
  348. m_storing_process_statu_msg.mutable_dispatch_step()->set_step_statu(message::eError);
  349. m_storing_process_statu_msg.mutable_dispatch_step()->set_description(code.get_error_description());
  350. LOG(ERROR)<<"调度失败:"<<code.get_error_description();
  351. break;
  352. }
  353. //更新状态信息
  354. m_storing_process_statu_msg.mutable_dispatch_step()->set_step_statu(message::eComplete);
  355. }
  356. //第三步,占据车位
  357. case 2:
  358. {
  359. //更新状态信息
  360. m_storing_process_statu_msg.mutable_confirm_space_step()->set_step_statu(message::eWorking);
  361. m_storing_process_statu_msg.mutable_confirm_space_step()->mutable_space_info()->CopyFrom(m_parcspace_alloc_response_msg.allocated_space_info());
  362. //开始工作
  363. code=confirm_space_step();
  364. if(code!=SUCCESS)
  365. {
  366. m_storing_process_statu_msg.mutable_confirm_space_step()->set_step_statu(message::eError);
  367. m_storing_process_statu_msg.mutable_confirm_space_step()->set_description(code.get_error_description());
  368. LOG(ERROR)<<"停车流程:"<<code.get_error_description();
  369. break;
  370. }
  371. //更新状态信息
  372. m_storing_process_statu_msg.mutable_confirm_space_step()->set_step_statu(message::eComplete);
  373. }
  374. //第四步,打印... 日志 .... 记录.....
  375. case 3:
  376. {
  377. LOG(INFO)<<"停车成功,停车终端:"<<m_terminor_id<<"指令id:"<<m_command_id
  378. <<", 车位id:"<<m_parcspace_alloc_response_msg.allocated_space_info().parkspace_id()
  379. <<", 车位楼层:"<<m_parcspace_alloc_response_msg.allocated_space_info().floor()
  380. <<", 车位序号:"<<m_parcspace_alloc_response_msg.allocated_space_info().index()
  381. <<", 车牌号:"<<m_car_info.license()
  382. <<", 库内车牌号:"<<m_parcspace_alloc_response_msg.allocated_space_info().car_info().license();
  383. return ;
  384. }
  385. }
  386. /*
  387. * switch 语句break,说明停车故障,需清除车位
  388. */
  389. //失败,清理车位
  390. m_storing_process_statu_msg.mutable_failed_release_space_step()->set_step_statu(message::eWorking);
  391. code=release_space_step();
  392. if(code!=SUCCESS)
  393. {
  394. m_storing_process_statu_msg.mutable_failed_release_space_step()->set_step_statu(message::eError);
  395. m_storing_process_statu_msg.mutable_failed_release_space_step()->set_description(code.get_error_description());
  396. LOG(ERROR)<<"致命故障,停车失败,清理车位故障:"<<code.get_error_description();
  397. }
  398. else
  399. {
  400. m_storing_process_statu_msg.mutable_failed_release_space_step()->set_step_statu(message::eComplete);
  401. }
  402. /*
  403. * 流程异常,此处应暂停系统,待管理员介入
  404. */
  405. }
  406. /*
  407. * 发布状态线程
  408. */
  409. void StoreProcessTask::publish_thread_func(StoreProcessTask* ptask)
  410. {
  411. if(ptask)
  412. {
  413. ptask->publish_step_status();
  414. }
  415. }
  416. void StoreProcessTask::publish_step_status()
  417. {
  418. //未收到退出信号
  419. while(false==m_publish_exit_condition.wait_for_ex(std::chrono::milliseconds(200)))
  420. {
  421. /*
  422. * 通过communicator 发布状态
  423. */
  424. if(System_communicator::get_instance_pointer())
  425. {
  426. if(m_storing_process_statu_msg.has_base_info()==true)
  427. {
  428. System_communicator::get_instance_pointer()->post_entrance_statu(m_storing_process_statu_msg);
  429. }
  430. }
  431. }
  432. }