dispatch_manager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //
  2. // Created by huli on 2020/7/20.
  3. //
  4. #include "dispatch_manager.h"
  5. #include "../tool/proto_tool.h"
  6. #include <cstdlib>
  7. Dispatch_manager::Dispatch_manager()
  8. {
  9. m_dispatch_manager_status = E_DISPATCH_MANAGER_UNKNOW;
  10. m_dispatch_manager_id = -1;
  11. m_dispatch_manager_thread = nullptr;
  12. }
  13. Dispatch_manager::~Dispatch_manager()
  14. {
  15. dispatch_manager_uninit();
  16. }
  17. //调度管理 初始化
  18. Error_manager Dispatch_manager::dispatch_manager_init(int dispatch_manager_id)
  19. {
  20. m_dispatch_manager_id = dispatch_manager_id;
  21. return dispatch_manager_init_from_protobuf(DISPATCH_DEVICE_PARAMETER_PATH);
  22. }
  23. //调度管理 初始化
  24. Error_manager Dispatch_manager::dispatch_manager_init()
  25. {
  26. return dispatch_manager_init_from_protobuf(DISPATCH_DEVICE_PARAMETER_PATH);
  27. }
  28. //初始化 调度管理 模块。从文件读取
  29. Error_manager Dispatch_manager::dispatch_manager_init_from_protobuf(std::string prototxt_path)
  30. {
  31. Dispatch_proto::Dispatch_device_parameter_all t_dispatch_device_parameter_all;
  32. if(! proto_tool::read_proto_param(prototxt_path,t_dispatch_device_parameter_all) )
  33. {
  34. return Error_manager(DISPATCH_MANAGER_READ_PROTOBUF_ERROR,MINOR_ERROR,
  35. "Dispatch_manager read_proto_param failed");
  36. }
  37. return dispatch_manager_init_from_protobuf(t_dispatch_device_parameter_all);
  38. }
  39. //初始化 调度管理 模块。从protobuf读取
  40. Error_manager Dispatch_manager::dispatch_manager_init_from_protobuf(Dispatch_proto::Dispatch_device_parameter_all& dispatch_device_parameter_all)
  41. {
  42. LOG(INFO) << " ----Dispatch_manager::dispatch_manager_init_from_protobuf----- "<< this;
  43. Error_manager t_error;
  44. m_dispatch_plc.dispatch_plc_init(m_dispatch_manager_id);
  45. m_dispatch_ground_lidar[0].dispatch_ground_lidar_init(m_dispatch_manager_id, 0);
  46. m_dispatch_ground_lidar[1].dispatch_ground_lidar_init(m_dispatch_manager_id, 1);
  47. m_dispatch_singlechip[0].dispatch_singlechip_init(m_dispatch_manager_id, 0, 1);
  48. m_dispatch_singlechip[1].dispatch_singlechip_init(m_dispatch_manager_id, 1, 1);
  49. m_dispatch_singlechip[2].dispatch_singlechip_init(m_dispatch_manager_id, 0, 2);
  50. m_dispatch_singlechip[3].dispatch_singlechip_init(m_dispatch_manager_id, 1, 2);
  51. m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  52. // 线程默认开启
  53. m_dispatch_manager_condition.reset(false, true, false);
  54. m_dispatch_manager_thread = new std::thread(&Dispatch_manager::resource_allocation, this);
  55. m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  56. return Error_code::SUCCESS;
  57. }
  58. //调度管理 反初始化
  59. Error_manager Dispatch_manager::dispatch_manager_uninit()
  60. {
  61. if (m_dispatch_manager_thread)
  62. {
  63. m_dispatch_manager_condition.kill_all();
  64. }
  65. if (m_dispatch_manager_thread)
  66. {
  67. m_dispatch_manager_thread->join();
  68. delete m_dispatch_manager_thread;
  69. m_dispatch_manager_thread = NULL;
  70. }
  71. m_dispatch_manager_status = E_DISPATCH_MANAGER_UNKNOW;
  72. m_dispatch_manager_id = -1;
  73. m_dispatch_plc.dispatch_plc_uninit();
  74. m_dispatch_ground_lidar[0].dispatch_ground_lidar_uninit();
  75. m_dispatch_ground_lidar[1].dispatch_ground_lidar_uninit();
  76. m_dispatch_singlechip[0].dispatch_singlechip_uninit();
  77. m_dispatch_singlechip[1].dispatch_singlechip_uninit();
  78. m_dispatch_singlechip[2].dispatch_singlechip_uninit();
  79. m_dispatch_singlechip[3].dispatch_singlechip_uninit();
  80. return Error_code::SUCCESS;
  81. }
  82. //调度管理 设备复位
  83. Error_manager Dispatch_manager::dispatch_manager_device_reset()
  84. {
  85. //以后再写
  86. return Error_code::SUCCESS;
  87. }
  88. //对外的接口函数,负责接受并处理任务单,
  89. Error_manager Dispatch_manager::execute_task(Dispatch_manager::Dispatch_motion_direction dispatch_motion_direction)
  90. {
  91. return Error_code::SUCCESS;
  92. // std::this_thread::sleep_for(std::chrono::seconds(rand()%3+3));
  93. if ( dispatch_motion_direction == E_PICKUP_CAR )
  94. {
  95. return Error_code::SUCCESS;
  96. }
  97. // return Error_code::SUCCESS;
  98. // srand(0);
  99. unsigned int t_probability = rand();
  100. if ( t_probability%100 >=20 )
  101. {
  102. return Error_code::SUCCESS;
  103. }
  104. else
  105. {
  106. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  107. " Dispatch_manager::execute_task() error ");
  108. }
  109. }
  110. //检查能否执行消息指令
  111. Error_manager Dispatch_manager::check_execute_msg(Communication_message* p_msg)
  112. {
  113. Error_manager t_error = Dispatch_manager::get_instance_references().check_status();
  114. if ( t_error == Error_code::SUCCESS )
  115. {
  116. return Error_code::SUCCESS;
  117. }
  118. else if (t_error.get_error_level() == NEGLIGIBLE_ERROR)//一级故障,轻微故障,
  119. {
  120. std::cout << "Dispatch_manager _is_busy , " << std::endl;
  121. //返回繁忙之后, 通信模块1秒后再次调用check
  122. return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  123. }
  124. switch ( p_msg->get_message_type() )
  125. {
  126. case Communication_message::Message_type::eDispatch_request_msg:
  127. {
  128. return Error_code::SUCCESS;
  129. break;
  130. }
  131. case Communication_message::Message_type::eDispatch_plan_response_msg:
  132. {
  133. return Error_code::SUCCESS;
  134. break;
  135. }
  136. case Communication_message::Message_type::eDispatch_control_request_msg:
  137. {
  138. return Error_code::SUCCESS;
  139. break;
  140. }
  141. default :
  142. {
  143. //无效的消息,
  144. return Error_manager(Error_code::INVALID_MESSAGE, Error_level::NEGLIGIBLE_ERROR,
  145. " INVALID_MESSAGE error ");
  146. break;
  147. }
  148. }
  149. return Error_code::SUCCESS;
  150. }
  151. //检查状态
  152. Error_manager Dispatch_manager::check_status()
  153. {
  154. if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_READY )
  155. {
  156. return Error_code::SUCCESS;
  157. }
  158. else if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_STORE || m_dispatch_manager_status == E_DISPATCH_MANAGER_STORE )
  159. {
  160. return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  161. " Dispatch_manager::check_status() error ");
  162. }
  163. else
  164. {
  165. return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_ERROR, Error_level::MINOR_ERROR,
  166. " Dispatch_manager::check_status() error ");
  167. }
  168. return Error_code::SUCCESS;
  169. }
  170. //调度模块 //执行搬运请求(主控->调度管理)
  171. Error_manager Dispatch_manager::execute_for_dispatch_request_msg(message::Dispatch_request_msg& dispatch_request_msg)
  172. {
  173. LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_request_msg--- "<< dispatch_request_msg.DebugString() << " "<< this;
  174. LOG(INFO) << " dispatch_request_msg->m_command_key = "<<dispatch_request_msg.command_key()<< " "<< this;
  175. Error_manager t_error;
  176. std::unique_lock<std::mutex> t_lock(m_lock);
  177. if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  178. {
  179. m_dispatch_request_store_list.push_back(dispatch_request_msg);
  180. }
  181. else
  182. {
  183. m_dispatch_request_pickup_list.push_back(dispatch_request_msg);
  184. }
  185. //need, 目前直接转发给调度plc执行, 后续要存队列,然后增加调度逻辑.
  186. // m_dispatch_plc.execute_for_dispatch_request_msg(dispatch_request_msg);
  187. return Error_code::SUCCESS;
  188. }
  189. //调度模块 答复数据异常
  190. Error_manager Dispatch_manager::send_dispatch_response_msg_with_error(message::Dispatch_request_msg &dispatch_request_msg, Error_manager error)
  191. {
  192. message::Dispatch_response_msg t_dispatch_response_msg;
  193. t_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  194. t_dispatch_response_msg.mutable_base_info()->set_timeout_ms(dispatch_request_msg.base_info().timeout_ms());
  195. t_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  196. t_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  197. t_dispatch_response_msg.set_command_key(dispatch_request_msg.command_key());
  198. t_dispatch_response_msg.mutable_error_manager()->set_error_code(error.get_error_code());
  199. t_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)error.get_error_level());
  200. t_dispatch_response_msg.mutable_error_manager()->set_error_description(error.get_error_description());
  201. System_communication::get_instance_references().encapsulate_msg(t_dispatch_response_msg.SerializeAsString());
  202. return Error_code::SUCCESS;
  203. }
  204. //调度模块 //调度总规划的答复(调度算法->调度管理)
  205. Error_manager Dispatch_manager::execute_for_dispatch_plan_response_msg(message::Dispatch_plan_response_msg &dispatch_plan_response_msg)
  206. {
  207. LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_plan_response_msg--- "<< this;
  208. LOG(INFO) << " dispatch_plan_response_msg->m_command_key = "<<dispatch_plan_response_msg.command_key()<< " "<< this;
  209. Error_manager t_error;
  210. return Error_code::SUCCESS;
  211. }
  212. //调度模块 //调度控制的任务请求(调度算法->调度管理)
  213. Error_manager Dispatch_manager::execute_for_dispatch_control_request_msg(message::Dispatch_control_request_msg &dispatch_control_request_msg)
  214. {
  215. LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_control_request_msg--- "<< this;
  216. LOG(INFO) << " dispatch_control_request_msg->m_command_key = "<<dispatch_control_request_msg.command_key()<< " "<< this;
  217. Error_manager t_error;
  218. return Error_code::SUCCESS;
  219. }
  220. //定时发送 调度管理的状态
  221. Error_manager Dispatch_manager::encapsulate_send_dispatch_manager_status()
  222. {
  223. Error_manager t_error;
  224. int t_dispatch_manager_id = get_dispatch_manager_id();
  225. std::string t_msg1;
  226. //创建一条 调度管理总管理的状态 旧版
  227. message::Dispatch_terminal_status_msg t_dispatch_terminal_status_msg;
  228. t_dispatch_terminal_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_status_msg);
  229. t_dispatch_terminal_status_msg.mutable_base_info()->set_timeout_ms(5000);
  230. t_dispatch_terminal_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  231. t_dispatch_terminal_status_msg.mutable_base_info()->set_receiver(message::Communicator::eEmpty);
  232. t_dispatch_terminal_status_msg.set_terminal_id(t_dispatch_manager_id*2);
  233. t_dispatch_terminal_status_msg.set_terminal_status(message::Terminal_status::E_TERMINAL_READY);
  234. t_dispatch_terminal_status_msg.set_passageway_direction(message::Passageway_direction::E_BILATERAL);
  235. t_msg1 = t_dispatch_terminal_status_msg.SerializeAsString();
  236. System_communication::get_instance_references().encapsulate_msg(t_msg1);
  237. // std::cout << " huli test :::: " << " t_dispatch_terminal_status_msg = " << t_dispatch_terminal_status_msg.DebugString() << std::endl;
  238. t_dispatch_terminal_status_msg.set_terminal_id(t_dispatch_manager_id*2+1);
  239. t_msg1 = t_dispatch_terminal_status_msg.SerializeAsString();
  240. System_communication::get_instance_references().encapsulate_msg(t_msg1);
  241. // std::cout << " huli test :::: " << " t_dispatch_terminal_status_msg = " << t_dispatch_terminal_status_msg.DebugString() << std::endl;
  242. std::string t_msg2;
  243. //创建一条 调度管理总管理的状态 新版
  244. message::Dispatch_manager_status_msg t_dispatch_manager_status_msg;
  245. t_dispatch_manager_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_manager_status_msg);
  246. t_dispatch_manager_status_msg.mutable_base_info()->set_timeout_ms(5000);
  247. t_dispatch_manager_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  248. t_dispatch_manager_status_msg.mutable_base_info()->set_receiver(message::Communicator::eEmpty);
  249. t_dispatch_manager_status_msg.set_dispatch_id(t_dispatch_manager_id*2);
  250. Dispatch_manager::Dispatch_manager_status t_dispatch_manager_status = get_dispatch_manager_status();
  251. t_dispatch_manager_status_msg.set_dispatch_manager_status((message::Dispatch_manager_status)t_dispatch_manager_status);
  252. t_msg2 = t_dispatch_manager_status_msg.SerializeAsString();
  253. System_communication::get_instance_references().encapsulate_msg(t_msg2);
  254. // std::cout << " huli test :::: " << " t_dispatch_manager_status_msg = " << t_dispatch_manager_status_msg.DebugString() << std::endl;
  255. t_dispatch_manager_status_msg.set_dispatch_id(t_dispatch_manager_id*2+1);
  256. t_msg2 = t_dispatch_manager_status_msg.SerializeAsString();
  257. System_communication::get_instance_references().encapsulate_msg(t_msg2);
  258. // std::cout << " huli test :::: " << " t_dispatch_manager_status_msg = " << t_dispatch_manager_status_msg.DebugString() << std::endl;
  259. //std::cout << " huli test :::: " << " ------------------------------------------------- = " << 1 << std::endl;
  260. return Error_code::SUCCESS;
  261. }
  262. //在流程的map 里面释放指定的流程
  263. Error_manager Dispatch_manager::release_dispatch_process(std::string command_key)
  264. {
  265. std::unique_lock<std::mutex> t_lock(m_lock);
  266. return Error_code::SUCCESS;
  267. }
  268. //调度模块 ///地面雷达的状态消息(地面雷达->null)
  269. Error_manager Dispatch_manager::execute_for_ground_status_msg(message::Ground_status_msg ground_status_msg)
  270. {
  271. int t_inlet_id = ground_status_msg.terminal_id() %2;
  272. return m_dispatch_ground_lidar[t_inlet_id].execute_for_ground_status_msg(ground_status_msg);
  273. return Error_code::SUCCESS;
  274. }
  275. //调度模块 ///单片机的状态消息
  276. Error_manager Dispatch_manager::execute_for_singlechip_data_msg(message::Singlechip_data singlechip_data_msg, bool validity)
  277. {
  278. if ( singlechip_data_msg.has_terminalid() && singlechip_data_msg.has_dispatchdirection() )
  279. {
  280. if ( singlechip_data_msg.terminalid() >=0 &&
  281. singlechip_data_msg.terminalid() <=5 &&
  282. singlechip_data_msg.dispatchdirection() >=1 &&
  283. singlechip_data_msg.dispatchdirection() <=2 )
  284. {
  285. int t_temp = (singlechip_data_msg.terminalid() %2) + (singlechip_data_msg.dispatchdirection()-1)*2;
  286. return m_dispatch_singlechip[t_temp].execute_for_singlechip_data_msg(singlechip_data_msg, validity);
  287. }
  288. }
  289. return Error_code::SUCCESS;
  290. }
  291. Dispatch_manager::Dispatch_manager_status Dispatch_manager::get_dispatch_manager_status()
  292. {
  293. return m_dispatch_manager_status;
  294. }
  295. int Dispatch_manager::get_dispatch_manager_id()
  296. {
  297. return m_dispatch_manager_id;
  298. }
  299. void Dispatch_manager::set_dispatch_manager_id(int dispatch_manager_id)
  300. {
  301. m_dispatch_manager_id = dispatch_manager_id;
  302. }
  303. //资源分配
  304. void Dispatch_manager::resource_allocation()
  305. {
  306. LOG(INFO) << " Dispatch_manager::resource_allocation() start " << this;
  307. Error_manager t_error;
  308. while (m_dispatch_manager_condition.is_alive())
  309. {
  310. m_dispatch_manager_condition.wait();
  311. if ( m_dispatch_manager_condition.is_alive() )
  312. {
  313. std::this_thread::sleep_for(std::chrono::microseconds(1));
  314. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  315. // std::this_thread::sleep_for(std::chrono::seconds(1));
  316. std::this_thread::yield();
  317. std::unique_lock<std::mutex> t_lock(m_lock);
  318. if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  319. {
  320. if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP)
  321. {
  322. auto iter = m_dispatch_request_pickup_list.begin();
  323. if ( iter != m_dispatch_request_pickup_list.end() )
  324. {
  325. m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  326. m_dispatch_request_pickup_list.erase(iter);
  327. }
  328. m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  329. }
  330. else if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE )
  331. {
  332. auto iter = m_dispatch_request_store_list.begin();
  333. if ( iter != m_dispatch_request_store_list.end() )
  334. {
  335. m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  336. m_dispatch_request_store_list.erase(iter);
  337. }
  338. m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  339. }
  340. else
  341. {
  342. ///////
  343. }
  344. }
  345. }
  346. }
  347. LOG(INFO) << " Dispatch_manager::resource_allocation() end "<< this;
  348. return;
  349. }