StoreProcessTask.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. //
  2. // Created by zx on 2020/7/7.
  3. //
  4. #include <parkspace_excutor.h>
  5. #include <dispatch_message.pb.h>
  6. #include "dispatch_excutor.h"
  7. #include "StoreProcessTask.h"
  8. #include "process_message.pb.h"
  9. #include "command_manager.h"
  10. #include "command_accepter.h"
  11. #include "exception_solver.h"
  12. #include "uniq_key.h"
  13. StoreProcessTask::StoreProcessTask(unsigned int terminor_id,message::Car_info car_info)
  14. :Process_task(terminor_id,car_info)
  15. {
  16. }
  17. StoreProcessTask::~StoreProcessTask()
  18. {
  19. Exception_solver::get_instance_pointer()->delete_task_cancel_condition(m_car_info.license());
  20. }
  21. Error_manager StoreProcessTask::init_task(message::Locate_information locate_info)
  22. {
  23. reset_msg();
  24. m_locate_info=locate_info;
  25. //添加当前流程的任务取消标志位到异常处理模块
  26. m_cancel_condition.reset(false, false, false);
  27. Error_manager code=Exception_solver::get_instance_pointer()->add_task_cancel_condition(m_car_info.license(),this);
  28. if(code!=SUCCESS)
  29. return code;
  30. //初始化进度状态消息基本信息
  31. message::Base_info base_info;
  32. base_info.set_msg_type(message::eStoring_process_statu_msg);
  33. base_info.set_sender(message::eMain);
  34. base_info.set_receiver(message::eEmpty);
  35. m_process_msg.mutable_base_info()->CopyFrom(base_info);
  36. m_process_msg.set_terminal_id(m_terminor_id);
  37. m_process_msg.set_license(m_car_info.license());
  38. m_current_step_type=message::eAlloc_step;
  39. ///创建状态发布线程
  40. if(m_publish_statu_thread== nullptr)
  41. {
  42. m_publish_exit_condition.reset(false, false, false);
  43. m_publish_statu_thread=new std::thread(publish_thread_func,this);
  44. }
  45. return SUCCESS;
  46. }
  47. Error_manager StoreProcessTask::locate_step() {
  48. Error_manager code;
  49. //检查测量模块状态
  50. code=Measure_excutor::get_instance_pointer()->check_statu(m_terminor_id);
  51. if(code!=SUCCESS)
  52. return code;
  53. message::Base_info base_info;
  54. base_info.set_msg_type(message::eLocate_request_msg);
  55. base_info.set_sender(message::eMain);
  56. base_info.set_receiver(message::eMeasurer);
  57. base_info.set_timeout_ms(20000); //测量超时5s
  58. m_measure_request_msg.mutable_base_info()->CopyFrom(base_info);
  59. m_measure_request_msg.set_command_key(create_key());
  60. m_measure_request_msg.set_terminal_id(m_terminor_id);
  61. code=Measure_excutor::get_instance_pointer()->locate_request(m_measure_request_msg,
  62. m_measure_response_msg,m_cancel_condition);
  63. if(code!=SUCCESS)
  64. return code;
  65. if(m_measure_response_msg.error_manager().error_code()==0) {
  66. return SUCCESS;
  67. }
  68. else {
  69. Error_code t_code=(Error_code)m_measure_response_msg.error_manager().error_code();
  70. Error_level t_level=(Error_level)m_measure_response_msg.error_manager().error_level();
  71. return Error_manager(t_code, t_level, m_measure_response_msg.error_manager().error_description().c_str());
  72. }
  73. }
  74. /*
  75. * 回退定位
  76. */
  77. Error_manager StoreProcessTask::back_locate_step()
  78. {
  79. m_measure_request_msg=message::Measure_request_msg();
  80. m_measure_response_msg=message::Measure_response_msg();
  81. int k=rand()%5;
  82. if(k==0)
  83. return Error_manager(ERROR,CRITICAL_ERROR,"回退测量对比失败");
  84. return SUCCESS;
  85. }
  86. /*
  87. * 检验结果
  88. */
  89. Error_manager StoreProcessTask::compare_step()
  90. {
  91. return SUCCESS;
  92. }
  93. /*
  94. * 回退检验
  95. */
  96. Error_manager StoreProcessTask::back_compare_step()
  97. {
  98. int k=rand()%5;
  99. if(k==0)
  100. return Error_manager(ERROR,CRITICAL_ERROR,"回退检验对比失败");
  101. return SUCCESS;
  102. }
  103. /*
  104. * 调度
  105. */
  106. Error_manager StoreProcessTask::dispatch_step()
  107. {
  108. Error_manager code;
  109. /*
  110. * 判断调度所需的数据是否都正常
  111. */
  112. //1,测量信息是否存在
  113. if(m_measure_response_msg.has_base_info()== false
  114. ||m_measure_response_msg.has_locate_information()==false
  115. ||m_parcspace_alloc_response_msg.has_base_info()== false
  116. ||m_parcspace_alloc_response_msg.has_allocated_space_info()==false)
  117. {
  118. return Error_manager(ERROR,MAJOR_ERROR,"调度所需的前置数据(测量,车位)不存在");
  119. }
  120. //2,判断调度节点状态
  121. code=Dispatch_excutor::get_instance_pointer()->check_entrance_statu(m_terminor_id);
  122. if(code!=SUCCESS)
  123. return code;
  124. message::Base_info base_info;
  125. base_info.set_msg_type(message::eDispatch_request_msg);
  126. base_info.set_sender(message::eMain);
  127. base_info.set_receiver(message::eDispatch);
  128. base_info.set_timeout_ms(1000*15); //超时15s
  129. m_dispatch_request_msg.mutable_base_info()->CopyFrom(base_info);
  130. std::string key=create_key();
  131. m_dispatch_request_msg.set_command_key(key);
  132. m_dispatch_request_msg.set_terminal_id(m_terminor_id);
  133. m_dispatch_request_msg.set_dispatch_motion_direction(message::E_STORE_CAR);
  134. m_dispatch_request_msg.set_parkspace_id(m_parcspace_alloc_response_msg.allocated_space_info().parkspace_id());
  135. code=Dispatch_excutor::get_instance_pointer()->dispatch_request(m_dispatch_request_msg,
  136. m_dispatch_response_msg,m_cancel_condition);
  137. if(code!=SUCCESS)
  138. return code;
  139. if(m_dispatch_response_msg.error_manager().error_code()==0) {
  140. return SUCCESS;
  141. }
  142. else
  143. {
  144. Error_code t_code=(Error_code)m_dispatch_response_msg.error_manager().error_code();
  145. Error_level t_level=(Error_level)m_dispatch_response_msg.error_manager().error_level();
  146. return Error_manager(t_code,t_level,"dispatch response error_code error");
  147. }
  148. }
  149. /*
  150. * 回退调度
  151. */
  152. Error_manager StoreProcessTask::back_dispatch_step()
  153. {
  154. //reset 调度请求数据
  155. m_dispatch_request_msg=message::Dispatch_request_msg();
  156. m_dispatch_response_msg=message::Dispatch_response_msg();
  157. return SUCCESS;
  158. }
  159. /*
  160. * 分配车位
  161. */
  162. Error_manager StoreProcessTask::alloc_space_step()
  163. {
  164. message::Base_info base_info_response;
  165. base_info_response.set_msg_type(message::eStore_command_response_msg);
  166. base_info_response.set_sender(message::eMain);
  167. base_info_response.set_receiver(message::eTerminor);
  168. m_command_response_msg.mutable_base_info()->CopyFrom(base_info_response);
  169. m_command_response_msg.set_terminal_id(m_terminor_id);
  170. if(Command_manager::get_instance_pointer()->is_paused()==true)
  171. {
  172. return Error_manager(PAUSE, MINOR_ERROR, "系统已急停");
  173. }
  174. //
  175. message::Entrance_statu statu=Command_manager::get_instance_pointer()->entrance_statu(m_terminor_id);
  176. if(statu.has_paused()==false) {
  177. return Error_manager(ERROR, MINOR_ERROR, "入口已禁止使用 Disable");
  178. }
  179. if(statu.paused()==true)
  180. {
  181. return Error_manager(ERROR, MINOR_ERROR, "入口已禁止使用 Disable");
  182. }
  183. /*
  184. * 检查是否有测量数据
  185. */
  186. if(m_locate_info.has_locate_height()==false||m_locate_info.has_locate_width()==false)
  187. {
  188. return Error_manager(FAILED,MINOR_ERROR,"停车请求缺少车辆高度和宽度信息");
  189. }
  190. /*
  191. * 检查车位管理模块是否正常
  192. */
  193. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  194. if(code!=SUCCESS)
  195. {
  196. return code;
  197. }
  198. //发送分配请求
  199. message::Base_info base_info_request;
  200. base_info_request.set_msg_type(message::eParkspace_allocation_request_msg);
  201. base_info_request.set_sender(message::eMain);
  202. base_info_request.set_receiver(message::eParkspace);
  203. base_info_request.set_timeout_ms(5000); //超时1s
  204. m_alloc_request_msg.mutable_base_info()->CopyFrom(base_info_request);
  205. m_alloc_request_msg.mutable_car_info()->CopyFrom(m_car_info);
  206. m_alloc_request_msg.set_command_key(create_key());
  207. m_alloc_request_msg.set_terminal_id(m_terminor_id);
  208. code=Parkspace_excutor::get_instance_pointer()->alloc_request(m_alloc_request_msg,
  209. m_parcspace_alloc_response_msg,m_cancel_condition);
  210. if(code!=SUCCESS)
  211. {
  212. return code;
  213. }
  214. if(m_parcspace_alloc_response_msg.error_manager().error_code()==0)
  215. {
  216. message::Car_info alloc_car_info=m_parcspace_alloc_response_msg.allocated_space_info().car_info();
  217. if(alloc_car_info.license()!=m_car_info.license())
  218. {
  219. return Error_manager(ERROR,MINOR_ERROR,"分配车位反馈的车辆信息不匹配");
  220. }
  221. return SUCCESS;
  222. }
  223. else
  224. {
  225. Error_code t_code=(Error_code)m_parcspace_alloc_response_msg.error_manager().error_code();
  226. Error_level t_level=(Error_level)m_parcspace_alloc_response_msg.error_manager().error_level();
  227. return Error_manager(t_code,t_level,"分配车位反馈结果错误");
  228. }
  229. }
  230. /*
  231. * 车位占用确认
  232. */
  233. Error_manager StoreProcessTask::confirm_space_step()
  234. {
  235. /*
  236. * 检查车位管理模块是否正常
  237. */
  238. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  239. if(code!=SUCCESS)
  240. return code;
  241. message::Parkspace_confirm_alloc_request_msg request;
  242. message::Base_info base_info;
  243. base_info.set_msg_type(message::eParkspace_confirm_alloc_request_msg);
  244. base_info.set_sender(message::eMain);
  245. base_info.set_receiver(message::eParkspace);
  246. base_info.set_timeout_ms(5000); //测量超时1s
  247. request.mutable_base_info()->CopyFrom(base_info);
  248. message::Parkspace_info space_info=m_parcspace_alloc_response_msg.allocated_space_info();
  249. request.mutable_confirm_space_info()->CopyFrom(space_info);
  250. request.set_command_key(create_key());
  251. message::Parkspace_confirm_alloc_response_msg confirm_response;
  252. code=Parkspace_excutor::get_instance_pointer()->confirm_request(request,confirm_response,m_cancel_condition);
  253. if(code!=SUCCESS)
  254. return code;
  255. if(m_parcspace_alloc_response_msg.allocated_space_info().parkspace_id()!=
  256. confirm_response.confirm_alloc_space_info().parkspace_id())
  257. {
  258. return Error_manager(ERROR,MINOR_ERROR,"占用车位与分配车位不一致");
  259. }
  260. if(confirm_response.error_manager().error_code()==0) {
  261. /*LOG(INFO)<<"停车流程正常,确认占用车位成功,停车终端:"<<m_terminor_id
  262. <<", 指令id:"<<m_command_info.place()+m_command_info.time()
  263. <<", 车位楼层:"<<confirm_response.confirm_alloc_space_info().floor()
  264. <<", 车位序号:"<<confirm_response.confirm_alloc_space_info().index()
  265. <<", 车牌号:"<<confirm_response.confirm_alloc_space_info().car_info().license();*/
  266. return SUCCESS;
  267. }
  268. else
  269. {
  270. Error_code t_code=(Error_code)confirm_response.error_manager().error_code();
  271. Error_level t_level=(Error_level)confirm_response.error_manager().error_level();
  272. return Error_manager(t_code,t_level,"parkspace confirm response error_code error");
  273. }
  274. }
  275. /*
  276. * 回退车位分配
  277. */
  278. Error_manager StoreProcessTask::back_alloc_space_step()
  279. {
  280. int k=rand()%5;
  281. if(k==0)
  282. return Error_manager(ERROR,CRITICAL_ERROR,"回退车位分配失败");
  283. /*
  284. * 检查是否曾经分配过车位
  285. */
  286. if(m_parcspace_alloc_response_msg.has_allocated_space_info()==false)
  287. {
  288. return Error_manager(FAILED,CRITICAL_ERROR," parkspace release request without space info");
  289. }
  290. /*
  291. * 检查车位管理模块是否正常
  292. */
  293. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  294. if(code!=SUCCESS)
  295. return Error_manager(code.get_error_code(),CRITICAL_ERROR,code.get_error_description());
  296. message::Parkspace_release_request_msg request;
  297. message::Base_info base_info;
  298. base_info.set_msg_type(message::eParkspace_release_request_msg);
  299. base_info.set_sender(message::eMain);
  300. base_info.set_receiver(message::eParkspace);
  301. base_info.set_timeout_ms(5000); //测量超时1s
  302. request.mutable_base_info()->CopyFrom(base_info);
  303. message::Parkspace_info space_info=m_parcspace_alloc_response_msg.allocated_space_info();
  304. request.mutable_release_space_info()->CopyFrom(space_info);
  305. request.set_command_key(create_key());
  306. message::Parkspace_release_response_msg release_response;
  307. code=Parkspace_excutor::get_instance_pointer()->release_request(request,release_response,m_cancel_condition);
  308. if(code!=SUCCESS)
  309. return Error_manager(code.get_error_code(),CRITICAL_ERROR,code.get_error_description());
  310. if(release_response.error_manager().error_code()==0) {
  311. /*LOG(WARNING)<<"停车流程异常,释放车位成功,停车终端:"<<m_terminor_id
  312. <<", 指令id:"<<m_command_info.place()+m_command_info.time()
  313. <<", 车位楼层:"<<m_parcspace_alloc_response_msg.allocated_space_info().floor()
  314. <<", 车位序号:"<<m_parcspace_alloc_response_msg.allocated_space_info().index()
  315. <<", 车牌号:"<<m_parcspace_alloc_response_msg.allocated_space_info().car_info().license();*/
  316. m_alloc_request_msg=message::Parkspace_allocation_request_msg();
  317. m_parcspace_alloc_response_msg=message::Parkspace_allocation_response_msg();
  318. return SUCCESS;
  319. }
  320. else
  321. {
  322. Error_code t_code=(Error_code)release_response.error_manager().error_code();
  323. Error_level t_level=(Error_level)release_response.error_manager().error_level();
  324. return Error_manager(t_code,CRITICAL_ERROR,"back alloc response error_code error");
  325. }
  326. }
  327. /*
  328. * 控制流程到下一步
  329. */
  330. Error_manager StoreProcessTask::next_step()
  331. {
  332. if(m_current_step_statu==message::eWaiting || m_current_step_statu== message::eWorking)
  333. return Error_manager(ERROR,MINOR_ERROR,"当前状态禁止改变步骤类型");
  334. switch (m_current_step_type)
  335. {
  336. case message::eAlloc_step:
  337. m_current_step_type=(m_current_step_statu==message::eFinished)?message::eMeasure_step:message::eBackComplete;
  338. break;
  339. case message::eMeasure_step:
  340. m_current_step_type=(m_current_step_statu==message::eFinished)?message::eCompare_step:message::eBackAlloc_step;
  341. break;
  342. case message::eCompare_step:
  343. m_current_step_type=(m_current_step_statu==message::eFinished)?message::eDispatch_step:message::eBackMeasure_step;
  344. break;
  345. case message::eDispatch_step:
  346. m_current_step_type=(m_current_step_statu==message::eFinished)?message::eConfirm_step:message::eBack_compare_step;
  347. break;
  348. case message::eConfirm_step:
  349. m_current_step_type=(m_current_step_statu==message::eFinished)?message::eComplete:message::eBackDispatch_step;
  350. break;
  351. case message::eComplete:
  352. break;
  353. case message::eBackDispatch_step:
  354. m_current_step_type=message::eBack_compare_step;
  355. break;
  356. case message::eBack_compare_step:
  357. m_current_step_type=message::eBackMeasure_step;
  358. break;
  359. case message::eBackMeasure_step:
  360. m_current_step_type=message::eBackAlloc_step;
  361. break;
  362. case message::eBackAlloc_step:
  363. m_current_step_type=message::eBackComplete;
  364. break;
  365. case message::eBackComplete:
  366. break;
  367. }
  368. return SUCCESS;
  369. }
  370. /*
  371. * 流程函数
  372. */
  373. void StoreProcessTask::Main()
  374. {
  375. /*
  376. * 外部已经分配好车位,进入到此流程说明车位已经分配好, 存放在 m_parcspace_alloc_response_msg
  377. */
  378. Error_manager code;
  379. //开始执行停车指令
  380. while(is_canceled()==false)
  381. {
  382. //第一步分配车位
  383. if(m_current_step_type == message::eAlloc_step)
  384. {
  385. updata_step_statu(message::eWorking);
  386. code=alloc_space_step();
  387. //发送反馈
  388. message::Error_manager error_msg;
  389. error_msg.set_error_code(code.get_error_code());
  390. error_msg.set_error_description(code.get_error_description());
  391. m_command_response_msg.mutable_code()->CopyFrom(error_msg);
  392. Communication_message msg;
  393. msg.reset(m_command_response_msg.base_info(),m_command_response_msg.SerializeAsString());
  394. Message_communicator::get_instance_pointer()->send_msg(&msg);
  395. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  396. LOG_IF(ERROR, code != SUCCESS) << "分配失败:" <<m_car_info.license()<< code.get_error_description();
  397. }
  398. //第二步,测量
  399. if (m_current_step_type == message::eMeasure_step)
  400. {
  401. //开始定位
  402. updata_step_statu(message::eWorking);
  403. code = locate_step();
  404. usleep(1000 * 1000 );
  405. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  406. LOG_IF(ERROR, code != SUCCESS) << "测量失败:" <<m_car_info.license()<< code.get_error_description();
  407. }
  408. if(m_current_step_type== message::eCompare_step)
  409. {
  410. updata_step_statu(message::eWorking);
  411. code=compare_step();
  412. usleep(500*1000);
  413. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  414. LOG_IF(ERROR, code != SUCCESS) << "检验失败:"<<m_car_info.license() << code.get_error_description();
  415. }
  416. //第二步,调度
  417. if (m_current_step_type == message::eDispatch_step)
  418. {
  419. //开始调度
  420. updata_step_statu(message::eWorking);
  421. code = dispatch_step();
  422. usleep(1000 * 2000 );
  423. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  424. LOG_IF(ERROR, code != SUCCESS) << "调度失败:"<<m_car_info.license() << code.get_error_description();
  425. }
  426. //第三步,占据车位
  427. if (m_current_step_type == message::eConfirm_step)
  428. {
  429. updata_step_statu(message::eWorking);
  430. code = confirm_space_step();
  431. usleep(1000 * 1000);
  432. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  433. LOG_IF(ERROR, code != SUCCESS) << "终端号:" << m_terminor_id << "停车流程:" << code.get_error_description() <<
  434. " 车位id :"
  435. << m_parcspace_alloc_response_msg.allocated_space_info().parkspace_id()
  436. << ",车牌:" << m_car_info.license()<<code.to_string();
  437. }
  438. //第四步,完成,退出循环
  439. if (m_current_step_type == message::eComplete)
  440. {
  441. //流程结束前,保证至少发送一次流程完成状态
  442. updata_step_statu(message::eFinished);
  443. break;
  444. }
  445. //回退confirm ------------------------------------华丽的分割线------------------------------------------
  446. if (m_current_step_type == message::eBackConfirm_step)
  447. {
  448. updata_step_statu(message::eWorking);
  449. usleep(1000*1000);
  450. updata_step_statu(message::eFinished);
  451. }
  452. if(m_current_step_type== message::eBackDispatch_step)
  453. {
  454. updata_step_statu(message::eWorking);
  455. code=back_dispatch_step();
  456. usleep(1000*1000);
  457. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  458. if(code.get_error_level()>=MAJOR_ERROR)
  459. {
  460. //提升错误等级为四级
  461. LOG(ERROR)<<" 回退调度失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
  462. Exception_solver::get_instance_pointer()->solve_exception(code,this);
  463. continue;
  464. }
  465. }
  466. if(m_current_step_type== message::eBack_compare_step)
  467. {
  468. updata_step_statu(message::eWorking);
  469. code=back_compare_step();
  470. usleep(1000*1000);
  471. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  472. if(code.get_error_level()>=MAJOR_ERROR)
  473. {
  474. LOG(ERROR)<<" 回退对比失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
  475. Exception_solver::get_instance_pointer()->solve_exception(code,this);
  476. LOG(WARNING)<<" 手动处理 结果对比, 继续 ..........车牌:"<<m_car_info.license();
  477. continue;
  478. }
  479. }
  480. if(m_current_step_type== message::eBackMeasure_step)
  481. {
  482. updata_step_statu(message::eWorking);
  483. code=back_locate_step();
  484. usleep(1000*1000);
  485. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  486. if(code.get_error_level()>=MAJOR_ERROR)
  487. {
  488. //提升错误等级为四级
  489. LOG(ERROR)<<" 回退测量失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
  490. Exception_solver::get_instance_pointer()->solve_exception(code,this);
  491. continue;
  492. }
  493. }
  494. if(m_current_step_type== message::eBackAlloc_step)
  495. {
  496. updata_step_statu(message::eWorking);
  497. code=back_alloc_space_step();
  498. usleep(1000*1000);
  499. code!=SUCCESS?updata_step_statu(message::eError):updata_step_statu(message::eFinished);
  500. if(code.get_error_level()>=MAJOR_ERROR)
  501. {
  502. //提升错误等级为四级
  503. LOG(ERROR)<<" 回退分配车位失败 ------进入异常处理, 车牌号:"<<m_car_info.license();
  504. Exception_solver::get_instance_pointer()->solve_exception(code,this);
  505. continue;
  506. }
  507. }
  508. if(m_current_step_type== message::eBackComplete)
  509. {
  510. break;
  511. }
  512. next_step();
  513. }
  514. /*
  515. * 跳出循环后,判断状态,是否正常结束, 循环跳出状态只有可能是 eBackComplete(异常结束),eComplete(正常结束),任务取消状态
  516. */
  517. updata_step_statu(message::eFinished);
  518. publish_step_status();
  519. if(m_cancel_condition.wait_for_millisecond(1)==true) {
  520. LOG(ERROR) << "停车任务被强制取消,车牌号:" << m_car_info.license()
  521. << ", 终端号:" << m_terminor_id;
  522. usleep(1000*200);
  523. return ;
  524. }
  525. if(m_current_step_type== message::eBackComplete)
  526. {
  527. //异常结束
  528. usleep(1000*200);
  529. LOG(ERROR)<<"异常停车,回退结束"<<"车牌号:"<<m_car_info.license()
  530. <<",xxxxxxxxxxxxxx 终端:"<<m_terminor_id<<" xxxxxxxxxxxxxx";
  531. }
  532. if(m_current_step_type== message::eComplete)
  533. {
  534. //正常结束
  535. usleep(1000*200);
  536. LOG(INFO)<<"停车结束,"<<"车牌号:"<<m_car_info.license()
  537. <<",-------------- 终端:"<<m_terminor_id<<" --------------";
  538. }
  539. }
  540. void StoreProcessTask::publish_step_status() {
  541. /*
  542. * 通过communicator 发布状态
  543. */
  544. if (Command_accepter::get_instance_pointer()) {
  545. std::lock_guard<std::mutex> lock(m_process_msg_lock);
  546. Command_accepter::get_instance_pointer()->post_process_statu(m_process_msg);
  547. }
  548. }
  549. /*
  550. * 根据当前流程状态,并修改状态消息
  551. */
  552. void StoreProcessTask::updata_step_statu(message::Step_statu statu)
  553. {
  554. m_current_step_statu=statu;
  555. std::lock_guard<std::mutex> lock(m_process_msg_lock);
  556. switch (m_current_step_type) {
  557. case message::eAlloc_step: {
  558. message::Alloc_space_step_statu alloc_step_statu;
  559. alloc_step_statu.set_step_statu(statu);
  560. m_process_msg.mutable_alloc_space_step()->CopyFrom(alloc_step_statu);
  561. break;
  562. }
  563. case message::eMeasure_step: {
  564. message::Measure_step_statu measure_step_statu;
  565. measure_step_statu.set_step_statu(statu);
  566. measure_step_statu.mutable_locate_info()->CopyFrom(m_locate_info);
  567. m_process_msg.mutable_measure_step()->CopyFrom(measure_step_statu);
  568. break;
  569. }
  570. case message::eCompare_step:{
  571. message::Compare_step_statu compare_step;
  572. compare_step.mutable_locate_info_wj()->CopyFrom(m_locate_info);
  573. compare_step.mutable_locate_info_dj()->CopyFrom(m_measure_response_msg.locate_information());
  574. compare_step.mutable_locate_info_result()->CopyFrom(m_compare_location_data);
  575. compare_step.set_step_statu(statu);
  576. m_process_msg.mutable_compare_step()->CopyFrom(compare_step);
  577. break;
  578. }
  579. case message::eDispatch_step: {
  580. message::Dispatch_store_step_statu dispatch_step_statu;
  581. dispatch_step_statu.set_step_statu(statu);
  582. dispatch_step_statu.mutable_locate_info()->CopyFrom(m_locate_info);
  583. dispatch_step_statu.mutable_space_info()->CopyFrom(m_parcspace_alloc_response_msg.allocated_space_info());
  584. m_process_msg.mutable_dispatch_step()->CopyFrom(dispatch_step_statu);
  585. break;
  586. }
  587. case message::eConfirm_step: {
  588. message::Confirm_space_step_statu confirm_step_type;
  589. confirm_step_type.set_step_statu(statu);
  590. confirm_step_type.mutable_space_info()->CopyFrom(m_parcspace_alloc_response_msg.allocated_space_info());
  591. m_process_msg.mutable_confirm_space_step()->CopyFrom(confirm_step_type);
  592. break;
  593. }
  594. case message::eComplete: {
  595. m_process_msg.set_completed(true);
  596. break;
  597. }
  598. case message::eBackConfirm_step: {
  599. message::Back_confirm_space_step_statu back_confirm_step_type;
  600. back_confirm_step_type.set_step_statu(statu);
  601. m_process_msg.mutable_back_confirm_step()->CopyFrom(back_confirm_step_type);
  602. break;
  603. }
  604. case message::eBackDispatch_step: {
  605. message::Back_dispatch_store_step_statu back_dispatch_step_statu;
  606. back_dispatch_step_statu.set_step_statu(statu);
  607. back_dispatch_step_statu.mutable_space_info()->CopyFrom(m_parcspace_alloc_response_msg.allocated_space_info());
  608. back_dispatch_step_statu.mutable_locate_info()->CopyFrom(m_compare_location_data);
  609. m_process_msg.mutable_back_dispatch_step()->CopyFrom(back_dispatch_step_statu);
  610. break;
  611. }
  612. case message::eBack_compare_step:{
  613. message::Back_compare_step_statu back_compare_step_statu;
  614. back_compare_step_statu.set_step_statu(statu);
  615. back_compare_step_statu.mutable_locate_info_wj()->CopyFrom(m_locate_info);
  616. back_compare_step_statu.mutable_locate_info_dj()->CopyFrom(m_measure_response_msg.locate_information());
  617. back_compare_step_statu.mutable_locate_info_result()->CopyFrom(m_compare_location_data);
  618. m_process_msg.mutable_back_compare_step()->CopyFrom(back_compare_step_statu);
  619. break;
  620. }
  621. case message::eBackMeasure_step: {
  622. message::Back_measure_step_statu back_measure_step_statu;
  623. back_measure_step_statu.set_step_statu(statu);
  624. m_process_msg.mutable_back_measure_step()->CopyFrom(back_measure_step_statu);
  625. break;
  626. }
  627. case message::eBackAlloc_step: {
  628. message::Back_alloc_space_step_statu back_alloc_step_statu;
  629. back_alloc_step_statu.set_step_statu(statu);
  630. back_alloc_step_statu.mutable_space_info()->CopyFrom(m_parcspace_alloc_response_msg.allocated_space_info());
  631. m_process_msg.mutable_back_alloc_space_step()->CopyFrom(back_alloc_step_statu);
  632. break;
  633. }
  634. case message::eBackComplete: {
  635. m_process_msg.set_back_completed(true);
  636. break;
  637. }
  638. default:
  639. break;
  640. }
  641. }
  642. /*
  643. * 初始化 接收到的消息
  644. */
  645. void StoreProcessTask::reset_msg() {
  646. m_alloc_request_msg = message::Parkspace_allocation_request_msg();
  647. m_measure_request_msg = message::Measure_request_msg();
  648. m_dispatch_request_msg = message::Dispatch_request_msg();
  649. m_confirm_request_msg = message::Parkspace_confirm_alloc_request_msg();
  650. m_command_response_msg=message::Store_command_response_msg();
  651. m_locate_info = message::Locate_information();
  652. m_measure_response_msg = message::Measure_response_msg(); //测量模块的测量数据
  653. m_parcspace_alloc_response_msg = message::Parkspace_allocation_response_msg(); //分配的车位数据
  654. m_dispatch_response_msg = message::Dispatch_response_msg(); //调度模块的反馈数据
  655. }