command_manager.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. {
  160. message::Locate_information locate_info = request.locate_information();
  161. if (locate_info.has_locate_correct())
  162. {
  163. if (locate_info.locate_correct() == true)
  164. {
  165. if (locate_info.has_locate_width() && locate_info.has_locate_height()
  166. && locate_info.has_locate_x() && locate_info.has_locate_y()
  167. && locate_info.has_locate_angle() && locate_info.has_locate_wheel_base())
  168. {
  169. /*
  170. * 检查消息完毕,开始处理
  171. */
  172. LOG(INFO) << "------ 停 ------- 收到停车,车牌:" << request.car_info().license() <<
  173. ",终端:" << request.terminal_id() << "............................";
  174. Process_task *ptask = new StoreProcessTask(request.terminal_id(), request.car_info());
  175. StoreProcessTask *pStore_task = (StoreProcessTask *) ptask;
  176. //初始化流程
  177. code = pStore_task->init_task(locate_info);
  178. if (code != SUCCESS)
  179. {
  180. delete pStore_task;
  181. error_msg.set_error_code(code.get_error_code());
  182. error_msg.set_error_description(code.to_string());
  183. response.mutable_code()->CopyFrom(error_msg);
  184. LOG(ERROR) << "xxxx 创建停车流程失败(车位分配失败),终端:" << request.terminal_id() <<
  185. "车牌:" << request.car_info().license() << " " << code.to_string();
  186. Communication_message msg;
  187. msg.reset(response.base_info(), response.SerializeAsString());
  188. Message_communicator::get_instance_pointer()->send_msg(&msg);
  189. return code;
  190. }
  191. m_thread_queue_process->AddTask(pStore_task);
  192. return SUCCESS;
  193. }
  194. }
  195. }
  196. }
  197. error_msg.set_error_code(FAILED);
  198. error_msg.set_error_description("创建停车流程失败...指令缺少必要信息...");
  199. response.mutable_code()->CopyFrom(error_msg);
  200. Communication_message msg;
  201. msg.reset(response.base_info(), response.SerializeAsString());
  202. Message_communicator::get_instance_pointer()->send_msg(&msg);
  203. return Error_manager(FAILED, MINOR_ERROR, "创建停车流程失败...指令缺少必要信息...");
  204. } else {
  205. error_msg.set_error_code(INVALID_MESSAGE);
  206. error_msg.set_error_description("停车请求基本信息错误");
  207. response.mutable_code()->CopyFrom(error_msg);
  208. Communication_message msg;
  209. msg.reset(response.base_info(),response.SerializeAsString());
  210. Message_communicator::get_instance_pointer()->send_msg(&msg);
  211. return Error_manager(INVALID_MESSAGE, MAJOR_ERROR, "停车请求基本信息错误");
  212. }
  213. }
  214. /*
  215. * 执行取车请求
  216. */
  217. Error_manager Command_manager::execute_pickup_command(message::Pickup_command_request_msg& request)
  218. {
  219. message::Error_manager error_msg;
  220. message::Pickup_command_response_msg response;
  221. message::Base_info base_info_response;
  222. base_info_response.set_msg_type(message::ePickup_command_response_msg);
  223. base_info_response.set_sender(message::eMain);
  224. base_info_response.set_receiver(message::eTerminor);
  225. response.mutable_base_info()->CopyFrom(base_info_response);
  226. response.set_terminal_id(request.terminal_id());
  227. if (m_thread_queue_process == nullptr)
  228. {
  229. error_msg.set_error_code(ERROR);
  230. error_msg.set_error_description(" xxxxx bug,线程池未初始化");
  231. response.mutable_code()->CopyFrom(error_msg);
  232. return Error_manager(ERROR, MINOR_ERROR, "线程池未初始化,bug");
  233. }
  234. if (m_system_paused == true)
  235. {
  236. error_msg.set_error_code(PAUSE);
  237. error_msg.set_error_description("急停");
  238. response.mutable_code()->CopyFrom(error_msg);
  239. return Error_manager(PAUSE, MINOR_ERROR, "急停");
  240. }
  241. //判断出口是否开放
  242. if (request.terminal_id() < 0 || request.terminal_id() >= m_system_setting.export_num())
  243. {
  244. error_msg.set_error_code(ERROR);
  245. error_msg.set_error_description(" xxxx 出口 id 设置错误");
  246. response.mutable_code()->CopyFrom(error_msg);
  247. return Error_manager(ERROR, MINOR_ERROR, " xxxxx出口 id 设置错误");
  248. }
  249. if (m_output_entrance_paused[request.terminal_id()] != false)
  250. {
  251. error_msg.set_error_code(ERROR);
  252. error_msg.set_error_description("中控出口已停止使用");
  253. response.mutable_code()->CopyFrom(error_msg);
  254. return Error_manager(ERROR, MINOR_ERROR, "中控出口已停止使用");
  255. }
  256. if (request.base_info().msg_type() == message::ePickup_command_request_msg
  257. && request.base_info().receiver() == message::eMain
  258. && request.base_info().sender() == message::eTerminor
  259. && request.has_car_info())
  260. {
  261. response.set_terminal_id(request.terminal_id());
  262. /*
  263. * 检查各个节点是否正常
  264. */
  265. Error_manager parkspace_code = Parkspace_excutor::get_instance_pointer()->check_statu();
  266. if (parkspace_code != SUCCESS)
  267. {
  268. error_msg.set_error_code(parkspace_code.get_error_code());
  269. error_msg.set_error_description(parkspace_code.get_error_description());
  270. response.mutable_code()->CopyFrom(error_msg);
  271. return parkspace_code;
  272. }
  273. Error_manager dispatch_code = Dispatch_excutor::get_instance_pointer()->check_export_statu(request.terminal_id());
  274. if (dispatch_code != SUCCESS)
  275. {
  276. error_msg.set_error_code(dispatch_code.get_error_code());
  277. error_msg.set_error_description(dispatch_code.get_error_description());
  278. response.mutable_code()->CopyFrom(error_msg);
  279. return dispatch_code;
  280. }
  281. //一切正常,接受指令
  282. Error_manager code;
  283. LOG(WARNING) << "--------- 取 -------收到取车-----------------------------" << request.car_info().license();
  284. Process_task *ptask = new PickupProcessTask(request.terminal_id(), request.car_info());
  285. PickupProcessTask *pPick_task = (PickupProcessTask *) ptask;
  286. //初始化流程
  287. code = pPick_task->init_task();
  288. if (code == SUCCESS)
  289. {
  290. m_thread_queue_process->AddTask(pPick_task);
  291. return SUCCESS;
  292. }
  293. usleep(1000 * 1000);
  294. error_msg.set_error_code(code.get_error_code());
  295. error_msg.set_error_description(code.to_string());
  296. response.mutable_code()->CopyFrom(error_msg);
  297. LOG(ERROR) << "创建取车流程失败(车位查询失败),终端:" << request.terminal_id() <<
  298. "车牌:" << request.car_info().license() << " " << code.to_string();
  299. delete pPick_task;
  300. Communication_message msg;
  301. msg.reset(response.base_info(), response.SerializeAsString());
  302. Message_communicator::get_instance_pointer()->send_msg(&msg);
  303. return code;
  304. }
  305. else
  306. {
  307. error_msg.set_error_code(INVALID_MESSAGE);
  308. error_msg.set_error_description("停车请求信息错误");
  309. response.mutable_code()->CopyFrom(error_msg);
  310. Communication_message msg;
  311. msg.reset(response.base_info(), response.SerializeAsString());
  312. Message_communicator::get_instance_pointer()->send_msg(&msg);
  313. return Error_manager(INVALID_MESSAGE, MAJOR_ERROR, "停车请求信息错误");
  314. }
  315. }
  316. /*
  317. * 控制入口 开放或者关闭
  318. */
  319. Error_manager Command_manager::pause_entrance(int terminal_id,bool paused)
  320. {
  321. if(terminal_id<0 || terminal_id>m_system_setting.entrance_num())
  322. return Error_manager(ERROR,MINOR_ERROR,"xxxx");
  323. m_input_entrance_paused[terminal_id]=paused;
  324. return SUCCESS;
  325. }
  326. message::Entrance_statu Command_manager::entrance_statu(int terminal_id)
  327. {
  328. message::Entrance_statu entrance_statu;
  329. if(terminal_id<0 || terminal_id>=m_system_setting.entrance_num())
  330. return entrance_statu;
  331. message::Module_statu statu=message::eConnected;
  332. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  333. statu=(code==SUCCESS)?message::eConnected:message::eFault;
  334. entrance_statu.set_parkspace_statu(statu);
  335. code=Measure_excutor::get_instance_pointer()->check_statu(terminal_id);
  336. if (code==ERROR) statu=message::eFault;
  337. if(code==DISCONNECT) statu=message::eDisconnected;
  338. entrance_statu.set_measure_statu(statu);
  339. code=Dispatch_excutor::get_instance_pointer()->check_entrance_statu(terminal_id);
  340. if (code==ERROR) statu=message::eFault;
  341. if(code==DISCONNECT) statu=message::eDisconnected;
  342. entrance_statu.set_dispatch_statu(statu);
  343. entrance_statu.set_paused(m_input_entrance_paused[terminal_id]);
  344. return entrance_statu;
  345. }
  346. message::Entrance_statu Command_manager::export_statu(int terminal_id)
  347. {
  348. message::Entrance_statu entrance_statu;
  349. if(terminal_id<0 || terminal_id>=m_system_setting.export_num())
  350. return entrance_statu;
  351. message::Module_statu statu=message::eConnected;
  352. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  353. statu=(code==SUCCESS)?message::eConnected:message::eFault;
  354. entrance_statu.set_parkspace_statu(statu);
  355. code=Dispatch_excutor::get_instance_pointer()->check_entrance_statu(terminal_id);
  356. if (code==ERROR) statu=message::eFault;
  357. if(code==DISCONNECT) statu=message::eDisconnected;
  358. entrance_statu.set_dispatch_statu(statu);
  359. entrance_statu.set_paused(m_output_entrance_paused[terminal_id]);
  360. return entrance_statu;
  361. }
  362. /*
  363. * 控制出口 开放或者关闭
  364. */
  365. Error_manager Command_manager::pause_export(int terminal_id,bool paused)
  366. {
  367. if(terminal_id<0 || terminal_id>m_system_setting.export_num())
  368. return Error_manager(ERROR,MINOR_ERROR,"xxxx");
  369. m_output_entrance_paused[terminal_id]=paused;
  370. return SUCCESS;
  371. }
  372. /*
  373. * pause,系统急停
  374. */
  375. void Command_manager::pause_system()
  376. {
  377. m_system_paused=true;
  378. }
  379. void Command_manager::publish_statu_function(Command_manager* commander)
  380. {
  381. if(commander== nullptr)
  382. return ;
  383. commander->publish_statu();
  384. }
  385. void Command_manager::publish_statu()
  386. {
  387. while(m_publish_exit_condition.wait_for_millisecond(100)==false)
  388. {
  389. message::Central_controller_statu_msg msg;
  390. message::Base_info baseInfo;
  391. baseInfo.set_msg_type(message::eCentral_controller_statu_msg);
  392. baseInfo.set_sender(message::eMain);
  393. baseInfo.set_receiver(message::eEmpty);
  394. msg.mutable_base_info()->CopyFrom(baseInfo);
  395. for(int i=0;i<m_system_setting.entrance_num();++i)
  396. {
  397. message::Entrance_statu entrance_statu;
  398. message::Module_statu statu=message::eConnected;
  399. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  400. statu=(code==SUCCESS)?message::eConnected:message::eFault;
  401. entrance_statu.set_parkspace_statu(statu);
  402. code=Measure_excutor::get_instance_pointer()->check_statu(i);
  403. if (code==ERROR) statu=message::eFault;
  404. if(code==DISCONNECT) statu=message::eDisconnected;
  405. entrance_statu.set_measure_statu(statu);
  406. code=Dispatch_excutor::get_instance_pointer()->check_entrance_statu(i);
  407. if (code==ERROR) statu=message::eFault;
  408. if(code==DISCONNECT) statu=message::eDisconnected;
  409. entrance_statu.set_dispatch_statu(statu);
  410. entrance_statu.set_paused(m_input_entrance_paused[i]);
  411. msg.mutable_entrance_statu_vector()->Add();
  412. msg.mutable_entrance_statu_vector(msg.entrance_statu_vector_size()-1)->CopyFrom(entrance_statu);
  413. }
  414. for(int i=0;i<m_system_setting.export_num();++i)
  415. {
  416. message::Entrance_statu entrance_statu;
  417. message::Module_statu statu=message::eConnected;
  418. Error_manager code=Parkspace_excutor::get_instance_pointer()->check_statu();
  419. statu=(code==SUCCESS)?message::eConnected:message::eFault;
  420. entrance_statu.set_parkspace_statu(statu);
  421. code=Dispatch_excutor::get_instance_pointer()->check_export_statu(i);
  422. if (code==ERROR) statu=message::eFault;
  423. if(code==DISCONNECT) statu=message::eDisconnected;
  424. entrance_statu.set_dispatch_statu(statu);
  425. entrance_statu.set_paused(m_output_entrance_paused[i]);
  426. msg.mutable_export_statu_vector()->Add();
  427. msg.mutable_export_statu_vector(msg.export_statu_vector_size()-1)->CopyFrom(entrance_statu);
  428. }
  429. Command_accepter::get_instance_pointer()->post_central_statu(msg);
  430. }
  431. }