command_manager.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //
  2. // Created by zx on 2020/7/14.
  3. //
  4. #include <sstream>
  5. #include <iomanip>
  6. #include <parkspace_excutor.h>
  7. #include "command_manager.h"
  8. #include "StoreProcessTask.h"
  9. #include "PickupProcessTask.h"
  10. #include "command_accepter.h"
  11. Command_manager::Command_manager()
  12. :m_thread_queue_process(nullptr)
  13. ,m_system_paused(false)
  14. ,m_publish_statu_thread(nullptr)
  15. {
  16. }
  17. Command_manager::~Command_manager()
  18. {
  19. //等待线程池完成
  20. if(m_thread_queue_process!=nullptr) {
  21. m_thread_queue_process->WaitForFinish();
  22. m_thread_queue_process->Stop();
  23. }
  24. //退出发布线程
  25. m_publish_exit_condition.set_pass_ever(true);
  26. if(m_publish_statu_thread!= nullptr)
  27. {
  28. if(m_publish_statu_thread->joinable())
  29. {
  30. m_publish_statu_thread->join();
  31. }
  32. delete m_publish_statu_thread;
  33. m_publish_statu_thread=nullptr;
  34. }
  35. }
  36. Error_manager Command_manager::init(setting::System_setting system_setting) {
  37. /*
  38. * 检查参数
  39. */
  40. if (system_setting.has_bind_ip() == false || system_setting.has_entrance_num() == false
  41. || system_setting.has_export_num() == false) {
  42. return Error_manager(ERROR, MAJOR_ERROR, "系统配置错误");
  43. }
  44. if (system_setting.entrance_num() < 0 || system_setting.export_num() < 0) {
  45. return Error_manager(ERROR, MAJOR_ERROR, "系统配置出入口数量错误");
  46. }
  47. //初始化出入口状态为 Enable
  48. m_input_entrance_paused.resize(system_setting.entrance_num());
  49. for(int i=0;i<system_setting.entrance_num();++i)
  50. m_input_entrance_paused[i]=false;
  51. m_output_entrance_paused.resize(system_setting.export_num());
  52. for(int i=0;i<system_setting.export_num();++i)
  53. m_output_entrance_paused[i]=false;
  54. //创建线程池
  55. if (m_thread_queue_process == nullptr) {
  56. m_thread_queue_process = tq::TQFactory::CreateDefaultQueue();
  57. m_thread_queue_process->Start(48);
  58. }
  59. /*
  60. * 此处添加等待各个通讯模块正常代码
  61. */
  62. std::chrono::system_clock::time_point t_start=std::chrono::system_clock::now();
  63. std::chrono::system_clock::time_point t_end=std::chrono::system_clock::now();
  64. Error_manager parkspace_code=ERROR;
  65. LOG(INFO)<<"初始化车位管理模块...";
  66. do
  67. {
  68. if (parkspace_code != SUCCESS) {
  69. parkspace_code = Parkspace_excutor::get_instance_pointer()->check_statu();
  70. LOG_IF(INFO, parkspace_code == SUCCESS) << "车位管理模块初始化完成!!!";
  71. }
  72. if(parkspace_code==SUCCESS)
  73. break;
  74. t_end=std::chrono::system_clock::now();
  75. usleep(1000*100);
  76. }while(t_end-t_start<std::chrono::seconds(300));
  77. LOG_IF(ERROR,parkspace_code!=SUCCESS)<<"车位管理节点连接超时";
  78. if(parkspace_code!=SUCCESS)
  79. {
  80. return Error_manager(ERROR,MAJOR_ERROR,"车位管理模块初始化超时");
  81. }
  82. //检查节点并创建停取车流程
  83. for(int i=0;i<system_setting.entrance_num();++i)
  84. {
  85. //
  86. // 检查入口 i 的各个节点状态
  87. //
  88. LOG(INFO)<<"初始化停车入口:"<<i<<" 测量及调度模块...";
  89. Error_manager locate_code=ERROR,dispatch_code=ERROR;
  90. do {
  91. if (locate_code != SUCCESS) {
  92. locate_code = Measure_excutor::get_instance_pointer()->check_statu(i);
  93. LOG_IF(INFO, locate_code == SUCCESS) << "停车入口:"<<i<<" 测量模块初始化完成!!!";
  94. }
  95. if (dispatch_code != SUCCESS) {
  96. dispatch_code = Dispatch_excutor::get_instance_pointer()->check_entrance_statu(i);
  97. LOG_IF(INFO, dispatch_code == SUCCESS) << "停车入口:"<<i<<" 调度模块初始化完成!!!";
  98. }
  99. t_end=std::chrono::system_clock::now();
  100. if (locate_code == SUCCESS && dispatch_code == SUCCESS)
  101. break;
  102. usleep(1000*100);
  103. }while(t_end-t_start<std::chrono::seconds(300));
  104. LOG_IF(ERROR,locate_code!=SUCCESS||dispatch_code!=SUCCESS)<<"停车口:"<<i<<" 测量/调度节点连接超时";
  105. if(locate_code!=SUCCESS||dispatch_code!=SUCCESS)
  106. {
  107. return Error_manager(ERROR,MAJOR_ERROR,"停车入口通讯节点初始化超时");
  108. }
  109. }
  110. for(int i=0;i<system_setting.export_num();++i)
  111. {
  112. LOG(INFO)<<"初始化取车出口:"<<i<<" 调度模块...";
  113. Error_manager dispatch_code=ERROR;
  114. do {
  115. if (dispatch_code != SUCCESS) {
  116. dispatch_code = Dispatch_excutor::get_instance_pointer()->check_export_statu(i);
  117. LOG_IF(INFO, dispatch_code == SUCCESS) << "取车出口:" << i << " 调度模块初始化完成!!!";
  118. }
  119. t_end = std::chrono::system_clock::now();
  120. if (dispatch_code == SUCCESS)
  121. break;
  122. usleep(1000*100);
  123. }while(t_end-t_start<std::chrono::seconds(300));
  124. LOG_IF(ERROR,dispatch_code!=SUCCESS)<<"取车出口:"<<i<<" 调度节点连接超时";
  125. if(dispatch_code!=SUCCESS)
  126. {
  127. return Error_manager(ERROR,MAJOR_ERROR,"取车出口通讯节点初始化超时");
  128. }
  129. }
  130. m_system_setting=system_setting;
  131. /*
  132. * 创建发布线程
  133. */
  134. if(m_publish_statu_thread== nullptr)
  135. {
  136. m_publish_exit_condition.reset(false, false, false);
  137. m_publish_statu_thread=new std::thread(publish_statu_function,this);
  138. }
  139. return SUCCESS;
  140. }
  141. /*
  142. * 执行停车请求
  143. */
  144. Error_manager Command_manager::execute_store_command(message::Store_command_request_msg& request)
  145. {
  146. message::Error_manager error_msg;
  147. message::Store_command_response_msg response;
  148. message::Base_info base_info_response;
  149. base_info_response.set_msg_type(message::eStore_command_response_msg);
  150. base_info_response.set_sender(message::eMain);
  151. base_info_response.set_receiver(message::eTerminor);
  152. response.mutable_base_info()->CopyFrom(base_info_response);
  153. response.set_terminal_id(request.terminal_id());
  154. Error_manager code;
  155. if (request.base_info().msg_type() == message::eStore_command_request_msg
  156. && request.base_info().receiver() == message::eMain
  157. && request.base_info().sender() == message::eTerminor) {
  158. if (request.has_locate_information() && request.has_base_info()) {
  159. message::Locate_information locate_info = request.locate_information();
  160. if (locate_info.has_locate_correct()) {
  161. if (locate_info.locate_correct() == true) {
  162. if (locate_info.has_locate_width() && locate_info.has_locate_height()
  163. && locate_info.has_locate_x() && locate_info.has_locate_y()
  164. && locate_info.has_locate_angle() && locate_info.has_locate_wheel_base()) {
  165. /*
  166. * 检查消息完毕,开始处理
  167. */
  168. LOG(INFO) << "------ 停 ------- 收到停车,车牌:" << request.car_info().license() <<
  169. ",终端:" << request.terminal_id() << "............................";
  170. Process_task *ptask = new StoreProcessTask(request.terminal_id(), request.car_info());
  171. StoreProcessTask *pStore_task = (StoreProcessTask *) ptask;
  172. //初始化流程
  173. code = pStore_task->init_task(locate_info);
  174. if (code != SUCCESS) {
  175. delete pStore_task;
  176. error_msg.set_error_code(code.get_error_code());
  177. error_msg.set_error_description(code.to_string());
  178. response.mutable_code()->CopyFrom(error_msg);
  179. LOG(ERROR) << "xxxx 创建停车流程失败(车位分配失败),终端:" << request.terminal_id() <<
  180. "车牌:" << request.car_info().license() << " " << code.to_string();
  181. Communication_message msg;
  182. msg.reset(response.base_info(), response.SerializeAsString());
  183. Message_communicator::get_instance_pointer()->send_msg(&msg);
  184. return code;
  185. }
  186. m_thread_queue_process->AddTask(pStore_task);
  187. return SUCCESS;
  188. }
  189. }
  190. }
  191. }
  192. error_msg.set_error_code(FAILED);
  193. error_msg.set_error_description("创建停车流程失败...指令缺少必要信息...");
  194. response.mutable_code()->CopyFrom(error_msg);
  195. Communication_message msg;
  196. msg.reset(response.base_info(), response.SerializeAsString());
  197. Message_communicator::get_instance_pointer()->send_msg(&msg);
  198. return Error_manager(FAILED, MINOR_ERROR, "创建停车流程失败...指令缺少必要信息...");
  199. } else {
  200. error_msg.set_error_code(INVALID_MESSAGE);
  201. error_msg.set_error_description("停车请求基本信息错误");
  202. response.mutable_code()->CopyFrom(error_msg);
  203. Communication_message msg;
  204. msg.reset(response.base_info(),response.SerializeAsString());
  205. Message_communicator::get_instance_pointer()->send_msg(&msg);
  206. return Error_manager(INVALID_MESSAGE, MAJOR_ERROR, "停车请求基本信息错误");
  207. }
  208. }
  209. /*
  210. * 执行取车请求
  211. */
  212. Error_manager Command_manager::execute_pickup_command(message::Pickup_command_request_msg& request)
  213. {
  214. message::Error_manager error_msg;
  215. message::Pickup_command_response_msg response;
  216. message::Base_info base_info_response;
  217. base_info_response.set_msg_type(message::ePickup_command_response_msg);
  218. base_info_response.set_sender(message::eMain);
  219. base_info_response.set_receiver(message::eTerminor);
  220. response.mutable_base_info()->CopyFrom(base_info_response);
  221. response.set_terminal_id(request.terminal_id());
  222. if (m_thread_queue_process == nullptr) {
  223. error_msg.set_error_code(ERROR);
  224. error_msg.set_error_description(" xxxxx bug,线程池未初始化");
  225. response.mutable_code()->CopyFrom(error_msg);
  226. return Error_manager(ERROR, MINOR_ERROR, "线程池未初始化,bug");
  227. }
  228. if(m_system_paused==true)
  229. {
  230. error_msg.set_error_code(PAUSE);
  231. error_msg.set_error_description("急停");
  232. response.mutable_code()->CopyFrom(error_msg);
  233. return Error_manager(PAUSE, MINOR_ERROR, "急停");
  234. }
  235. //判断出口是否开放
  236. if(request.terminal_id()<0 || request.terminal_id()>=m_system_setting.export_num())
  237. {
  238. error_msg.set_error_code(ERROR);
  239. error_msg.set_error_description(" xxxx 出口 id 设置错误");
  240. response.mutable_code()->CopyFrom(error_msg);
  241. return Error_manager(ERROR, MINOR_ERROR, " xxxxx出口 id 设置错误");
  242. }
  243. if(m_output_entrance_paused[request.terminal_id()]!=false)
  244. {
  245. error_msg.set_error_code(ERROR);
  246. error_msg.set_error_description("中控出口已停止使用");
  247. response.mutable_code()->CopyFrom(error_msg);
  248. return Error_manager(ERROR, MINOR_ERROR, "中控出口已停止使用");
  249. }
  250. if(request.base_info().msg_type()==message::ePickup_command_request_msg
  251. &&request.base_info().receiver()==message::eMain
  252. &&request.base_info().sender()==message::eTerminor
  253. &&request.has_car_info()) {
  254. response.set_terminal_id(request.terminal_id());
  255. /*
  256. * 检查各个节点是否正常
  257. */
  258. Error_manager parkspace_code= Parkspace_excutor::get_instance_pointer()->check_statu();
  259. if(parkspace_code!=SUCCESS)
  260. {
  261. error_msg.set_error_code(parkspace_code.get_error_code());
  262. error_msg.set_error_description(parkspace_code.get_error_description());
  263. response.mutable_code()->CopyFrom(error_msg);
  264. return parkspace_code;
  265. }
  266. Error_manager dispatch_code= Dispatch_excutor::get_instance_pointer()->check_export_statu(request.terminal_id());
  267. if(dispatch_code!=SUCCESS)
  268. {
  269. error_msg.set_error_code(dispatch_code.get_error_code());
  270. error_msg.set_error_description(dispatch_code.get_error_description());
  271. response.mutable_code()->CopyFrom(error_msg);
  272. return dispatch_code;
  273. }
  274. //一切正常,接受指令
  275. Error_manager code;
  276. LOG(WARNING)<<"--------- 取 -------收到取车-----------------------------"<<request.car_info().license();
  277. Process_task* ptask=new PickupProcessTask(request.terminal_id(),request.car_info());
  278. PickupProcessTask* pPick_task=(PickupProcessTask*)ptask;
  279. //初始化流程
  280. code=pPick_task->init_task();
  281. if(code==SUCCESS)
  282. {
  283. m_thread_queue_process->AddTask(pPick_task);
  284. return SUCCESS;
  285. }
  286. usleep(1000*1000);
  287. error_msg.set_error_code(code.get_error_code());
  288. error_msg.set_error_description(code.to_string());
  289. response.mutable_code()->CopyFrom(error_msg);
  290. LOG(ERROR)<<"创建取车流程失败(车位查询失败),终端:"<<request.terminal_id()<<
  291. "车牌:"<<request.car_info().license()<<" "<<code.to_string();
  292. delete pPick_task;
  293. Communication_message msg;
  294. msg.reset(response.base_info(),response.SerializeAsString());
  295. Message_communicator::get_instance_pointer()->send_msg(&msg);
  296. return code;
  297. }
  298. else
  299. {
  300. error_msg.set_error_code(INVALID_MESSAGE);
  301. error_msg.set_error_description("停车请求信息错误");
  302. response.mutable_code()->CopyFrom(error_msg);
  303. Communication_message msg;
  304. msg.reset(response.base_info(),response.SerializeAsString());
  305. Message_communicator::get_instance_pointer()->send_msg(&msg);
  306. return Error_manager(INVALID_MESSAGE,MAJOR_ERROR,"停车请求信息错误");
  307. }
  308. }
  309. /*
  310. * 控制入口 开放或者关闭
  311. */
  312. Error_manager Command_manager::pause_entrance(int terminal_id,bool paused)
  313. {
  314. if(terminal_id<0 || terminal_id>m_system_setting.entrance_num())
  315. return Error_manager(ERROR,MINOR_ERROR,"xxxx");
  316. m_input_entrance_paused[terminal_id]=paused;
  317. return SUCCESS;
  318. }
  319. message::Entrance_statu Command_manager::entrance_statu(int terminal_id)
  320. {
  321. message::Entrance_statu entrance_statu;
  322. if(terminal_id<0 || terminal_id>=m_system_setting.entrance_num())
  323. return entrance_statu;
  324. message::Module_statu statu=message::eConnected;
  325. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  326. statu=(code==SUCCESS)?message::eConnected:message::eFault;
  327. entrance_statu.set_parkspace_statu(statu);
  328. code=Measure_excutor::get_instance_pointer()->check_statu(terminal_id);
  329. if (code==ERROR) statu=message::eFault;
  330. if(code==DISCONNECT) statu=message::eDisconnected;
  331. entrance_statu.set_measure_statu(statu);
  332. code=Dispatch_excutor::get_instance_pointer()->check_entrance_statu(terminal_id);
  333. if (code==ERROR) statu=message::eFault;
  334. if(code==DISCONNECT) statu=message::eDisconnected;
  335. entrance_statu.set_dispatch_statu(statu);
  336. entrance_statu.set_paused(m_input_entrance_paused[terminal_id]);
  337. return entrance_statu;
  338. }
  339. message::Entrance_statu Command_manager::export_statu(int terminal_id)
  340. {
  341. message::Entrance_statu entrance_statu;
  342. if(terminal_id<0 || terminal_id>=m_system_setting.export_num())
  343. return entrance_statu;
  344. message::Module_statu statu=message::eConnected;
  345. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  346. statu=(code==SUCCESS)?message::eConnected:message::eFault;
  347. entrance_statu.set_parkspace_statu(statu);
  348. code=Dispatch_excutor::get_instance_pointer()->check_entrance_statu(terminal_id);
  349. if (code==ERROR) statu=message::eFault;
  350. if(code==DISCONNECT) statu=message::eDisconnected;
  351. entrance_statu.set_dispatch_statu(statu);
  352. entrance_statu.set_paused(m_output_entrance_paused[terminal_id]);
  353. return entrance_statu;
  354. }
  355. /*
  356. * 控制出口 开放或者关闭
  357. */
  358. Error_manager Command_manager::pause_export(int terminal_id,bool paused)
  359. {
  360. if(terminal_id<0 || terminal_id>m_system_setting.export_num())
  361. return Error_manager(ERROR,MINOR_ERROR,"xxxx");
  362. m_output_entrance_paused[terminal_id]=paused;
  363. return SUCCESS;
  364. }
  365. /*
  366. * pause,系统急停
  367. */
  368. void Command_manager::pause_system()
  369. {
  370. m_system_paused=true;
  371. }
  372. void Command_manager::publish_statu_function(Command_manager* commander)
  373. {
  374. if(commander== nullptr)
  375. return ;
  376. commander->publish_statu();
  377. }
  378. void Command_manager::publish_statu()
  379. {
  380. while(m_publish_exit_condition.wait_for_millisecond(100)==false)
  381. {
  382. message::Central_controller_statu_msg msg;
  383. message::Base_info baseInfo;
  384. baseInfo.set_msg_type(message::eCentral_controller_statu_msg);
  385. baseInfo.set_sender(message::eMain);
  386. baseInfo.set_receiver(message::eEmpty);
  387. msg.mutable_base_info()->CopyFrom(baseInfo);
  388. for(int i=0;i<m_system_setting.entrance_num();++i)
  389. {
  390. message::Entrance_statu entrance_statu;
  391. message::Module_statu statu=message::eConnected;
  392. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  393. statu=(code==SUCCESS)?message::eConnected:message::eFault;
  394. entrance_statu.set_parkspace_statu(statu);
  395. code=Measure_excutor::get_instance_pointer()->check_statu(i);
  396. if (code==ERROR) statu=message::eFault;
  397. if(code==DISCONNECT) statu=message::eDisconnected;
  398. entrance_statu.set_measure_statu(statu);
  399. code=Dispatch_excutor::get_instance_pointer()->check_entrance_statu(i);
  400. if (code==ERROR) statu=message::eFault;
  401. if(code==DISCONNECT) statu=message::eDisconnected;
  402. entrance_statu.set_dispatch_statu(statu);
  403. entrance_statu.set_paused(m_input_entrance_paused[i]);
  404. msg.mutable_entrance_statu_vector()->Add();
  405. msg.mutable_entrance_statu_vector(msg.entrance_statu_vector_size()-1)->CopyFrom(entrance_statu);
  406. }
  407. for(int i=0;i<m_system_setting.export_num();++i)
  408. {
  409. message::Entrance_statu entrance_statu;
  410. message::Module_statu statu=message::eConnected;
  411. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  412. statu=(code==SUCCESS)?message::eConnected:message::eFault;
  413. entrance_statu.set_parkspace_statu(statu);
  414. code=Dispatch_excutor::get_instance_pointer()->check_export_statu(i);
  415. if (code==ERROR) statu=message::eFault;
  416. if(code==DISCONNECT) statu=message::eDisconnected;
  417. entrance_statu.set_dispatch_statu(statu);
  418. entrance_statu.set_paused(m_output_entrance_paused[i]);
  419. msg.mutable_export_statu_vector()->Add();
  420. msg.mutable_export_statu_vector(msg.export_statu_vector_size()-1)->CopyFrom(entrance_statu);
  421. }
  422. Command_accepter::get_instance_pointer()->post_central_statu(msg);
  423. }
  424. }