system_executor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //
  2. // Created by huli on 2020/7/2.
  3. //
  4. #include "system_executor.h"
  5. #include "../message/measure_message.pb.h"
  6. //#include "../laser/laser_manager.h"
  7. //#include "../locate/locate_manager.h"
  8. #include "../system/system_communication.h"
  9. #include "../message/dispatch_message.pb.h"
  10. #include "../dispatch/dispatch_manager.h"
  11. System_executor::System_executor()
  12. {
  13. }
  14. System_executor::~System_executor()
  15. {
  16. system_executor_uninit();
  17. }
  18. //初始化
  19. Error_manager System_executor::system_executor_init(int threads_size)
  20. {
  21. m_thread_pool.thread_pool_init(threads_size);
  22. m_system_executor_status = SYSTEM_EXECUTOR_READY;
  23. return Error_code::SUCCESS;
  24. }
  25. //反初始化
  26. Error_manager System_executor::system_executor_uninit()
  27. {
  28. m_thread_pool.thread_pool_uninit();
  29. m_system_executor_status = SYSTEM_EXECUTOR_UNKNOW;
  30. return Error_code::SUCCESS;
  31. }
  32. //检查消息是否有效, 主要检查消息类型和接受者, 判断这条消息是不是给我的.
  33. Error_manager System_executor::check_msg(Communication_message* p_msg)
  34. {
  35. if ( p_msg == NULL )
  36. {
  37. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  38. " POINTER IS NULL ");
  39. }
  40. //检查消息类型
  41. switch ( p_msg->get_message_type() )
  42. {
  43. case Communication_message::Message_type::eDispatch_request_msg:
  44. {
  45. //检查接受人
  46. if ( p_msg->get_receiver() == Communication_message::Communicator::eDispatch )
  47. {
  48. message::Dispatch_request_msg t_dispatch_request_msg;
  49. //针对消息类型, 对消息进行二次解析
  50. if (t_dispatch_request_msg.ParseFromString(p_msg->get_message_buf()))
  51. {
  52. int t_dispatch_id = Dispatch_manager::get_instance_references().get_dispatch_id();
  53. //检查终端id
  54. if ( t_dispatch_request_msg.terminal_id() == t_dispatch_id*2+0 || t_dispatch_request_msg.terminal_id() == t_dispatch_id*2+1)
  55. {
  56. return Error_code::SUCCESS;
  57. }
  58. }
  59. }
  60. break;
  61. }
  62. default :
  63. ;
  64. break;
  65. }
  66. //无效的消息,
  67. return Error_manager(Error_code::INVALID_MESSAGE, Error_level::NEGLIGIBLE_ERROR,
  68. " INVALID_MESSAGE error ");
  69. }
  70. //检查执行者的状态, 判断能否处理这条消息,
  71. Error_manager System_executor::check_executer(Communication_message* p_msg)
  72. {
  73. if ( p_msg == NULL )
  74. {
  75. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  76. " POINTER IS NULL ");
  77. }
  78. Error_manager t_error;
  79. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 找到处理模块的实例对象, 查询执行人是否可以处理这条消息
  80. switch ( p_msg->get_message_type() )
  81. {
  82. case Communication_message::Message_type::eDispatch_request_msg:
  83. {
  84. Error_manager t_dispatch_result = Dispatch_manager::get_instance_references().check_status();
  85. Error_manager t_executor_result = System_executor::get_instance_references().check_status();
  86. if (t_dispatch_result == SUCCESS
  87. && t_executor_result == SUCCESS)
  88. {
  89. return Error_code::SUCCESS;
  90. }
  91. else
  92. {
  93. //整合所有的错误码
  94. t_error.compare_and_cover_error(t_dispatch_result);
  95. t_error.compare_and_cover_error(t_executor_result);
  96. if (t_error.get_error_level() == NEGLIGIBLE_ERROR)//一级故障,轻微故障,
  97. {
  98. std::cout << "executer_is_busy , " << std::endl;
  99. //返回繁忙之后, 通信模块1秒后再次调用check
  100. return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  101. }
  102. else//返回二级故障,可以封装一条答复信息, 返回错误码
  103. {
  104. message::Dispatch_request_msg t_dispatch_request_msg;
  105. //针对消息类型, 对消息进行二次解析
  106. if (t_dispatch_request_msg.ParseFromString(p_msg->get_message_buf()))
  107. {
  108. //创建一条答复消息
  109. message::Dispatch_response_msg t_dispatch_response_msg;
  110. t_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  111. t_dispatch_response_msg.mutable_base_info()->set_timeout_ms(5000);
  112. t_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch);
  113. t_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  114. t_dispatch_response_msg.set_command_key(t_dispatch_request_msg.command_key());
  115. t_dispatch_response_msg.mutable_error_manager()->set_error_code(t_error.get_error_code());
  116. t_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)t_error.get_error_level());
  117. t_dispatch_response_msg.mutable_error_manager()->set_error_description(t_error.get_error_description());
  118. std::string t_msg = t_dispatch_response_msg.SerializeAsString();
  119. System_communication::get_instance_references().encapsulate_msg(t_msg);
  120. LOG(INFO) << " System_executor::check_executer executer status error "<< this;
  121. return t_error;
  122. }
  123. else
  124. {
  125. LOG(INFO) << " System_executor::check_executer Second analysis ERROR "<< this;
  126. return Error_manager(Error_code::SYSTEM_EXECUTOR_PARSE_ERROR, Error_level::MINOR_ERROR,
  127. " message::Measure_request_msg ParseFromString error ");
  128. }
  129. }
  130. }
  131. break;
  132. }
  133. default :
  134. ;
  135. break;
  136. }
  137. return t_error;
  138. }
  139. //处理消息的执行函数
  140. Error_manager System_executor::execute_msg(Communication_message* p_msg)
  141. {
  142. if ( p_msg == NULL )
  143. {
  144. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  145. " POINTER IS NULL ");
  146. }
  147. switch ( p_msg->get_message_type() )
  148. {
  149. case Communication_message::Message_type::eLocate_request_msg:
  150. {
  151. message::Measure_request_msg t_measure_request_msg;
  152. //针对消息类型, 对消息进行二次解析
  153. if (t_measure_request_msg.ParseFromString(p_msg->get_message_buf()))
  154. {
  155. //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
  156. m_thread_pool.enqueue(&System_executor::execute_for_measure, this,
  157. t_measure_request_msg.command_key(), t_measure_request_msg.terminal_id());
  158. }
  159. else
  160. {
  161. return Error_manager(Error_code::SYSTEM_EXECUTOR_PARSE_ERROR, Error_level::MINOR_ERROR,
  162. " message::Measure_request_msg ParseFromString error ");
  163. }
  164. break;
  165. }
  166. case Communication_message::Message_type::eDispatch_request_msg:
  167. {
  168. message::Dispatch_request_msg t_dispatch_request_msg;
  169. //针对消息类型, 对消息进行二次解析
  170. if (t_dispatch_request_msg.ParseFromString(p_msg->get_message_buf()))
  171. {
  172. Locate_information t_locate_information;
  173. if ( t_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR
  174. && t_dispatch_request_msg.has_locate_information())
  175. {
  176. t_locate_information.locate_x = t_dispatch_request_msg.locate_information().locate_x();
  177. t_locate_information.locate_y = t_dispatch_request_msg.locate_information().locate_y();
  178. t_locate_information.locate_angle = t_dispatch_request_msg.locate_information().locate_angle();
  179. t_locate_information.locate_length = t_dispatch_request_msg.locate_information().locate_length();
  180. t_locate_information.locate_width = t_dispatch_request_msg.locate_information().locate_width();
  181. t_locate_information.locate_height = t_dispatch_request_msg.locate_information().locate_height();
  182. t_locate_information.locate_wheel_base = t_dispatch_request_msg.locate_information().locate_wheel_base();
  183. t_locate_information.locate_wheel_width = t_dispatch_request_msg.locate_information().locate_wheel_width();
  184. t_locate_information.locate_correct = t_dispatch_request_msg.locate_information().locate_correct();
  185. }
  186. //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
  187. m_thread_pool.enqueue(&System_executor::execute_for_dispatch, this,
  188. t_dispatch_request_msg.command_key(),
  189. (Dispatch_manager::Dispatch_motion_direction)t_dispatch_request_msg.dispatch_motion_direction(),
  190. t_dispatch_request_msg.terminal_id(),
  191. t_dispatch_request_msg.parkspace_info().parkspace_id(),
  192. &t_locate_information );
  193. }
  194. else
  195. {
  196. return Error_manager(Error_code::SYSTEM_EXECUTOR_PARSE_ERROR, Error_level::MINOR_ERROR,
  197. " message::Dispatch_request_msg ParseFromString error ");
  198. }
  199. break;
  200. }
  201. default:
  202. break;
  203. }
  204. return Error_code::SUCCESS;
  205. }
  206. //检查状态
  207. Error_manager System_executor::check_status()
  208. {
  209. if ( m_system_executor_status == SYSTEM_EXECUTOR_READY )
  210. {
  211. if ( m_thread_pool.thread_is_full_load() == false )
  212. {
  213. return Error_code::SUCCESS;
  214. }
  215. else
  216. {
  217. return Error_manager(Error_code::SYSTEM_EXECUTOR_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  218. " System_executor::check_status error ");
  219. }
  220. }
  221. else
  222. {
  223. return Error_manager(Error_code::SYSTEM_EXECUTOR_STATUS_ERROR, Error_level::MINOR_ERROR,
  224. " System_executor::check_status error ");
  225. }
  226. }
  227. //定时发送状态信息
  228. Error_manager System_executor::encapsulate_send_status()
  229. {
  230. Error_manager t_error;
  231. int t_dispatch_id = Dispatch_manager::get_instance_references().get_dispatch_id();
  232. std::string t_msg;
  233. /*
  234. //创建一条 调度管理总管理的状态
  235. message::Dispatch_manager_status_msg t_dispatch_manager_status_msg;
  236. t_dispatch_manager_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_manager_status_msg);
  237. t_dispatch_manager_status_msg.mutable_base_info()->set_timeout_ms(5000);
  238. t_dispatch_manager_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch);
  239. t_dispatch_manager_status_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  240. t_dispatch_manager_status_msg.set_dispatch_id(t_dispatch_id);
  241. Dispatch_manager::Dispatch_manager_status t_dispatch_manager_status = Dispatch_manager::get_instance_references().get_dispatch_manager_status();
  242. t_dispatch_manager_status_msg.set_dispatch_manager_status((message::Dispatch_manager_status)t_dispatch_manager_status);
  243. t_msg = t_dispatch_manager_status_msg.SerializeAsString();
  244. System_communication::get_instance_references().encapsulate_msg(t_msg);
  245. */
  246. //创建4条 调度模块终端出入口的状态
  247. message::Dispatch_terminal_status_msg t_dispatch_terminal_status_msg;
  248. t_dispatch_terminal_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_status_msg);
  249. t_dispatch_terminal_status_msg.mutable_base_info()->set_timeout_ms(5000);
  250. t_dispatch_terminal_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch);
  251. t_dispatch_terminal_status_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  252. t_dispatch_terminal_status_msg.set_terminal_id(t_dispatch_id*2+0);
  253. t_dispatch_terminal_status_msg.set_terminal_status(message::Terminal_status::E_TERMINAL_READY);
  254. t_dispatch_terminal_status_msg.set_passageway_direction(message::Passageway_direction::E_INLET);
  255. t_msg = t_dispatch_terminal_status_msg.SerializeAsString();
  256. System_communication::get_instance_references().encapsulate_msg(t_msg);
  257. t_dispatch_terminal_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_status_msg);
  258. t_dispatch_terminal_status_msg.mutable_base_info()->set_timeout_ms(5000);
  259. t_dispatch_terminal_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch);
  260. t_dispatch_terminal_status_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  261. t_dispatch_terminal_status_msg.set_terminal_id(t_dispatch_id*2+0);
  262. t_dispatch_terminal_status_msg.set_terminal_status(message::Terminal_status::E_TERMINAL_READY);
  263. t_dispatch_terminal_status_msg.set_passageway_direction(message::Passageway_direction::E_OUTLET);
  264. t_msg = t_dispatch_terminal_status_msg.SerializeAsString();
  265. System_communication::get_instance_references().encapsulate_msg(t_msg);
  266. t_dispatch_terminal_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_status_msg);
  267. t_dispatch_terminal_status_msg.mutable_base_info()->set_timeout_ms(5000);
  268. t_dispatch_terminal_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch);
  269. t_dispatch_terminal_status_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  270. t_dispatch_terminal_status_msg.set_terminal_id(t_dispatch_id*2+1);
  271. t_dispatch_terminal_status_msg.set_terminal_status(message::Terminal_status::E_TERMINAL_READY);
  272. t_dispatch_terminal_status_msg.set_passageway_direction(message::Passageway_direction::E_INLET);
  273. t_msg = t_dispatch_terminal_status_msg.SerializeAsString();
  274. System_communication::get_instance_references().encapsulate_msg(t_msg);
  275. t_dispatch_terminal_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_status_msg);
  276. t_dispatch_terminal_status_msg.mutable_base_info()->set_timeout_ms(5000);
  277. t_dispatch_terminal_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch);
  278. t_dispatch_terminal_status_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  279. t_dispatch_terminal_status_msg.set_terminal_id(t_dispatch_id*2+1);
  280. t_dispatch_terminal_status_msg.set_terminal_status(message::Terminal_status::E_TERMINAL_READY);
  281. t_dispatch_terminal_status_msg.set_passageway_direction(message::Passageway_direction::E_OUTLET);
  282. t_msg = t_dispatch_terminal_status_msg.SerializeAsString();
  283. System_communication::get_instance_references().encapsulate_msg(t_msg);
  284. return Error_code::SUCCESS;
  285. }
  286. //判断是否为待机,如果已经准备好,则可以执行任务。
  287. bool System_executor::is_ready()
  288. {
  289. if ( m_system_executor_status == SYSTEM_EXECUTOR_READY && m_thread_pool.thread_is_full_load() == false )
  290. {
  291. return true;
  292. }
  293. else
  294. {
  295. return false;
  296. }
  297. }
  298. System_executor::System_executor_status System_executor::get_system_executor_status()
  299. {
  300. return m_system_executor_status;
  301. }
  302. //雷达感测定位 的处理函数
  303. //input::command_id, 消息指令id, 由主控制系统生成的唯一码
  304. //input::command_id, 终端id, 对应具体的某个车位
  305. //return::void, 没有返回, 执行结果直接生成一条答复消息, 然后通过通信返回
  306. void System_executor::execute_for_measure(std::string command_key, int terminal_id)
  307. {
  308. Error_manager t_error;
  309. LOG(INFO) << " System_executor::execute_for_measure run "<< this;
  310. //这里要处理.......以后再写
  311. //创建一条答复消息
  312. message::Measure_response_msg t_measure_response_msg;
  313. t_measure_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eLocate_response_msg);
  314. t_measure_response_msg.mutable_base_info()->set_timeout_ms(5000);
  315. t_measure_response_msg.mutable_base_info()->set_sender(message::Communicator::eMeasurer);
  316. t_measure_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  317. t_measure_response_msg.set_command_key(command_key);
  318. t_measure_response_msg.set_terminal_id(terminal_id);
  319. t_measure_response_msg.mutable_error_manager()->set_error_code(t_error.get_error_code());
  320. t_measure_response_msg.mutable_error_manager()->set_error_level((message::Error_level)t_error.get_error_level());
  321. t_measure_response_msg.mutable_error_manager()->set_error_description(t_error.get_error_description(), t_error.get_description_length());
  322. t_measure_response_msg.mutable_locate_information()->set_locate_x(0);
  323. t_measure_response_msg.mutable_locate_information()->set_locate_y(0);
  324. t_measure_response_msg.mutable_locate_information()->set_locate_angle(0);
  325. t_measure_response_msg.mutable_locate_information()->set_locate_length(0);
  326. t_measure_response_msg.mutable_locate_information()->set_locate_width(0);
  327. t_measure_response_msg.mutable_locate_information()->set_locate_height(0);
  328. t_measure_response_msg.mutable_locate_information()->set_locate_wheel_base(0);
  329. t_measure_response_msg.mutable_locate_information()->set_locate_wheel_width(0);
  330. t_measure_response_msg.mutable_locate_information()->set_locate_correct(0);
  331. std::string t_msg = t_measure_response_msg.SerializeAsString();
  332. System_communication::get_instance_references().encapsulate_msg(t_msg);
  333. LOG(INFO) << " System_executor::execute_for_measure end "<< this;
  334. return ;
  335. }
  336. //调度模块的处理函数
  337. void System_executor::execute_for_dispatch(std::string command_key, Dispatch_manager::Dispatch_motion_direction dispatch_motion_direction,
  338. int terminal_id, int parkspace_id, Locate_information * p_locate_information)
  339. {
  340. Error_manager t_error;
  341. LOG(INFO) << " System_executor::execute_for_dispatch run "<< this;
  342. //这里要处理.......以后再写
  343. t_error = Dispatch_manager::get_instance_references().execute_task(dispatch_motion_direction);
  344. //创建一条答复消息
  345. message::Dispatch_response_msg t_dispatch_response_msg;
  346. t_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  347. t_dispatch_response_msg.mutable_base_info()->set_timeout_ms(5000);
  348. t_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch);
  349. t_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  350. t_dispatch_response_msg.set_command_key(command_key);
  351. t_dispatch_response_msg.mutable_error_manager()->set_error_code(t_error.get_error_code());
  352. t_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)t_error.get_error_level());
  353. t_dispatch_response_msg.mutable_error_manager()->set_error_description(t_error.get_error_description(), t_error.get_description_length());
  354. std::string t_msg = t_dispatch_response_msg.SerializeAsString();
  355. System_communication::get_instance_references().encapsulate_msg(t_msg);
  356. std::cout << "huli test dispatch_motion_direction = " << dispatch_motion_direction << std::endl;
  357. std::cout << "huli test error = " << t_error << std::endl;
  358. LOG(INFO) << " System_executor::execute_for_dispatch end "<< this;
  359. return ;
  360. }