dispatch_manager.cpp 14 KB

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