dispatch_manager.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. //
  2. // Created by huli on 2020/7/20.
  3. //
  4. #include "dispatch_manager.h"
  5. #include "../tool/proto_tool.h"
  6. #include <google/protobuf/text_format.h>
  7. #include <cstdlib>
  8. Dispatch_manager::Dispatch_manager()
  9. {
  10. m_dispatch_manager_status = E_DISPATCH_MANAGER_UNKNOW;
  11. m_dispatch_manager_id = -1;
  12. // m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  13. m_dispatch_manager_thread = nullptr;
  14. m_device_floor = 1;
  15. m_count_flag = false;
  16. m_post_flag = false;
  17. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  18. m_plc_floor = 0;
  19. m_car_type = Common_data::Car_type::UNKNOW_CAR_TYPE;
  20. }
  21. Dispatch_manager::~Dispatch_manager()
  22. {
  23. dispatch_manager_uninit();
  24. }
  25. //调度管理 初始化
  26. Error_manager Dispatch_manager::dispatch_manager_init(int dispatch_manager_id)
  27. {
  28. m_dispatch_manager_id = dispatch_manager_id;
  29. return dispatch_manager_init_from_protobuf(DISPATCH_DEVICE_PARAMETER_PATH);
  30. }
  31. //调度管理 初始化
  32. Error_manager Dispatch_manager::dispatch_manager_init()
  33. {
  34. return dispatch_manager_init_from_protobuf(DISPATCH_DEVICE_PARAMETER_PATH);
  35. }
  36. //初始化 调度管理 模块。从文件读取
  37. Error_manager Dispatch_manager::dispatch_manager_init_from_protobuf(std::string prototxt_path)
  38. {
  39. Dispatch_proto::Dispatch_device_parameter_all t_dispatch_device_parameter_all;
  40. if(! proto_tool::read_proto_param(prototxt_path,t_dispatch_device_parameter_all) )
  41. {
  42. return Error_manager(DISPATCH_MANAGER_READ_PROTOBUF_ERROR,MINOR_ERROR,
  43. "Dispatch_manager read_proto_param failed");
  44. }
  45. return dispatch_manager_init_from_protobuf(t_dispatch_device_parameter_all);
  46. }
  47. //初始化 调度管理 模块。从protobuf读取
  48. Error_manager Dispatch_manager::dispatch_manager_init_from_protobuf(Dispatch_proto::Dispatch_device_parameter_all& dispatch_device_parameter_all)
  49. {
  50. LOG(INFO) << " ----Dispatch_manager::dispatch_manager_init_from_protobuf----- "<< this;
  51. Error_manager t_error;
  52. m_dispatch_plc.dispatch_plc_init(m_dispatch_manager_id);
  53. m_dispatch_ground_lidar[0].dispatch_ground_lidar_init(m_dispatch_manager_id, 0);
  54. m_dispatch_ground_lidar[1].dispatch_ground_lidar_init(m_dispatch_manager_id, 1);
  55. m_dispatch_singlechip[0].dispatch_singlechip_init(m_dispatch_manager_id, 0, 1);
  56. m_dispatch_singlechip[1].dispatch_singlechip_init(m_dispatch_manager_id, 1, 1);
  57. m_dispatch_singlechip[2].dispatch_singlechip_init(m_dispatch_manager_id, 0, 2);
  58. m_dispatch_singlechip[3].dispatch_singlechip_init(m_dispatch_manager_id, 1, 2);
  59. // m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  60. // 线程默认开启
  61. m_dispatch_manager_condition.reset(false, true, false);
  62. // m_dispatch_manager_thread = new std::thread(&Dispatch_manager::resource_allocation, this);
  63. // m_dispatch_manager_thread = new std::thread(&Dispatch_manager::process_control, this);
  64. m_dispatch_manager_thread = new std::thread(&Dispatch_manager::process_sql, this);
  65. m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  66. return Error_code::SUCCESS;
  67. }
  68. //调度管理 反初始化
  69. Error_manager Dispatch_manager::dispatch_manager_uninit()
  70. {
  71. if (m_dispatch_manager_thread)
  72. {
  73. m_dispatch_manager_condition.kill_all();
  74. }
  75. if (m_dispatch_manager_thread)
  76. {
  77. m_dispatch_manager_thread->join();
  78. delete m_dispatch_manager_thread;
  79. m_dispatch_manager_thread = NULL;
  80. }
  81. m_dispatch_manager_status = E_DISPATCH_MANAGER_UNKNOW;
  82. m_dispatch_manager_id = -1;
  83. m_dispatch_plc.dispatch_plc_uninit();
  84. m_dispatch_ground_lidar[0].dispatch_ground_lidar_uninit();
  85. m_dispatch_ground_lidar[1].dispatch_ground_lidar_uninit();
  86. m_dispatch_singlechip[0].dispatch_singlechip_uninit();
  87. m_dispatch_singlechip[1].dispatch_singlechip_uninit();
  88. m_dispatch_singlechip[2].dispatch_singlechip_uninit();
  89. m_dispatch_singlechip[3].dispatch_singlechip_uninit();
  90. return Error_code::SUCCESS;
  91. }
  92. //调度管理 设备复位
  93. Error_manager Dispatch_manager::dispatch_manager_device_reset()
  94. {
  95. //以后再写
  96. return Error_code::SUCCESS;
  97. }
  98. //新的对外接口, 负责接受Rabbitmq_message,然后内部处理
  99. Error_manager Dispatch_manager::execute_rabbitmq_message_new(Rabbitmq_message* p_msg)
  100. {
  101. std::unique_lock<std::mutex> t_lock(m_lock);
  102. //通过key来判断消息类型
  103. if ( p_msg->m_routing_key == "measure_1_statu_port" ||
  104. p_msg->m_routing_key == "measure_3_statu_port" ||
  105. p_msg->m_routing_key == "measure_5_statu_port" )
  106. {
  107. m_measure_statu_msg_1 = *p_msg;
  108. if(google::protobuf::TextFormat::ParseFromString(m_measure_statu_msg_1.m_message_buf, &m_measure_info_1))
  109. {
  110. m_dispatch_ground_lidar[0].execute_for_ground_status_msg_new(m_measure_info_1);
  111. }
  112. }
  113. else if ( p_msg->m_routing_key == "measure_2_statu_port" ||
  114. p_msg->m_routing_key == "measure_4_statu_port" ||
  115. p_msg->m_routing_key == "measure_6_statu_port" )
  116. {
  117. // if ( p_msg->m_routing_key == "measure_2_statu_port" )
  118. // {
  119. // std::cout << " huli test :::: " << " nnnnnnnnnnnnnnnnnnnnnhhhhhhhhhhhhhhhhhhhhhhhhhh = " << p_msg->m_message_buf << std::endl;
  120. // }
  121. m_measure_statu_msg_2 = *p_msg;
  122. if(google::protobuf::TextFormat::ParseFromString(m_measure_statu_msg_2.m_message_buf, &m_measure_info_2))
  123. {
  124. m_dispatch_ground_lidar[1].execute_for_ground_status_msg_new(m_measure_info_2);
  125. }
  126. }
  127. else if ( p_msg->m_routing_key == "in_mcpu_1_statu_port" ||
  128. p_msg->m_routing_key == "in_mcpu_3_statu_port" ||
  129. p_msg->m_routing_key == "in_mcpu_5_statu_port" )
  130. {
  131. // std::cout << " huli test :::: " << " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = " << p_msg->m_message_buf << std::endl;
  132. // if ( p_msg->m_routing_key == "in_mcpu_3_statu_port" )
  133. // {
  134. // std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbffffffffffffffffff in_mcpu_3_statu_port= " << p_msg->m_message_buf << std::endl;
  135. //
  136. // }
  137. m_in_mcpu_statu_msg_1 = *p_msg;
  138. if(google::protobuf::TextFormat::ParseFromString(m_in_mcpu_statu_msg_1.m_message_buf, &m_in_mcpu_statu_1))
  139. {
  140. m_dispatch_singlechip[0].execute_for_singlechip_data_msg_new(m_in_mcpu_statu_1, true);
  141. }
  142. }
  143. else if ( p_msg->m_routing_key == "in_mcpu_2_statu_port" ||
  144. p_msg->m_routing_key == "in_mcpu_4_statu_port" ||
  145. p_msg->m_routing_key == "in_mcpu_6_statu_port" )
  146. {
  147. // std::cout << " huli test :::: " << " cccccccccccccccccccccccccccccccccccccccccccccccccc = " << p_msg->m_message_buf << std::endl;
  148. // if ( p_msg->m_routing_key == "in_mcpu_4_statu_port" )
  149. // {
  150. // std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbffffffffffffffffff in_mcpu_4_statu_port= " << p_msg->m_message_buf << std::endl;
  151. // }
  152. m_in_mcpu_statu_msg_2 = *p_msg;
  153. if(google::protobuf::TextFormat::ParseFromString(m_in_mcpu_statu_msg_2.m_message_buf, &m_in_mcpu_statu_2))
  154. {
  155. m_dispatch_singlechip[1].execute_for_singlechip_data_msg_new(m_in_mcpu_statu_2, true);
  156. }
  157. }
  158. else if ( p_msg->m_routing_key == "out_mcpu_1_statu_port" ||
  159. p_msg->m_routing_key == "out_mcpu_3_statu_port" ||
  160. p_msg->m_routing_key == "out_mcpu_5_statu_port" )
  161. {
  162. // if ( p_msg->m_routing_key == "out_mcpu_1_statu_port" )
  163. // {
  164. // std::cout << " huli test :::: " << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv out_mcpu_1_statu_port= " << p_msg->m_message_buf << std::endl;
  165. // }
  166. // std::cout << " huli test :::: " << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv = " << p_msg->m_message_buf << std::endl;
  167. m_out_mcpu_statu_msg_1 = *p_msg;
  168. if(google::protobuf::TextFormat::ParseFromString(m_out_mcpu_statu_msg_1.m_message_buf, &m_out_mcpu_statu_1))
  169. {
  170. m_dispatch_singlechip[2].execute_for_singlechip_data_msg_new(m_out_mcpu_statu_1, true);
  171. }
  172. }
  173. else if ( p_msg->m_routing_key == "out_mcpu_2_statu_port" ||
  174. p_msg->m_routing_key == "out_mcpu_4_statu_port" ||
  175. p_msg->m_routing_key == "out_mcpu_6_statu_port" )
  176. {
  177. // if ( p_msg->m_routing_key == "out_mcpu_2_statu_port" )
  178. // {
  179. // std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb out_mcpu_2_statu_port= " << p_msg->m_message_buf << std::endl;
  180. // }
  181. // std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = " << p_msg->m_message_buf << std::endl;
  182. m_out_mcpu_statu_msg_2 = *p_msg;
  183. if(google::protobuf::TextFormat::ParseFromString(m_out_mcpu_statu_msg_2.m_message_buf, &m_out_mcpu_statu_2))
  184. {
  185. m_dispatch_singlechip[3].execute_for_singlechip_data_msg_new(m_out_mcpu_statu_2, true);
  186. }
  187. }
  188. else
  189. {
  190. LOG(INFO) << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx error Rabbitmq_message = "<< p_msg->m_message_buf << " --- " << this;
  191. // park_table t_park_table;
  192. // if(google::protobuf::TextFormat::ParseFromString(p_msg->m_message_buf, &t_park_table))
  193. // {
  194. // std::string t_str = t_park_table.DebugString();
  195. // std::cout << " huli test :::: " << " t_str = " << t_str << std::endl;
  196. //
  197. // std::string t_str1 = t_park_table.statu().statu_description();
  198. // std::cout << " huli test :::: " << " t_str1 = " << t_str1 << std::endl;
  199. //
  200. // std::string t_str2 = t_park_table.car_number();
  201. // std::cout << " huli test :::: " << " t_str2 = " << t_str2 << std::endl;
  202. //
  203. // }
  204. }
  205. return Error_code::SUCCESS;
  206. }
  207. //给dispatch_plc底层提供接口, ack答复服务器,
  208. Error_manager Dispatch_manager::ack_rabbitmq_message_new()
  209. {
  210. if ( m_count_flag )
  211. {
  212. m_count_flag = false;
  213. System_communication::get_instance_references().ack_msg(&m_count_command_signal_msg);
  214. }
  215. if ( m_post_flag )
  216. {
  217. m_post_flag = false;
  218. System_communication::get_instance_references().ack_msg(&m_post_command_msg);
  219. }
  220. m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  221. return Error_code::SUCCESS;
  222. }
  223. //对外的接口函数,负责接受并处理任务单,
  224. //Error_manager Dispatch_manager::execute_task(Dispatch_manager::Dispatch_motion_direction dispatch_motion_direction)
  225. //{
  226. // return Error_code::SUCCESS;
  227. //// std::this_thread::sleep_for(std::chrono::seconds(rand()%3+3));
  228. //
  229. // if ( dispatch_motion_direction == E_PICKUP_CAR )
  230. // {
  231. // return Error_code::SUCCESS;
  232. // }
  233. //
  234. //// return Error_code::SUCCESS;
  235. //
  236. //// srand(0);
  237. // unsigned int t_probability = rand();
  238. //
  239. // if ( t_probability%100 >=20 )
  240. // {
  241. // return Error_code::SUCCESS;
  242. // }
  243. // else
  244. // {
  245. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  246. // " Dispatch_manager::execute_task() error ");
  247. // }
  248. //}
  249. //检查能否执行消息指令
  250. Error_manager Dispatch_manager::check_execute_msg(Rabbitmq_message* p_msg)
  251. {
  252. return Error_code::SUCCESS;
  253. /*
  254. Error_manager t_error = Dispatch_manager::get_instance_references().check_status();
  255. if ( t_error == Error_code::SUCCESS )
  256. {
  257. return Error_code::SUCCESS;
  258. }
  259. else if (t_error.get_error_level() == NEGLIGIBLE_ERROR)//一级故障,轻微故障,
  260. {
  261. std::cout << "Dispatch_manager _is_busy , " << std::endl;
  262. //返回繁忙之后, 通信模块1秒后再次调用check
  263. return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  264. }
  265. switch ( p_msg->get_message_type() )
  266. {
  267. case Communication_message::Message_type::eDispatch_request_msg:
  268. {
  269. return Error_code::SUCCESS;
  270. break;
  271. }
  272. case Communication_message::Message_type::eDispatch_plan_response_msg:
  273. {
  274. return Error_code::SUCCESS;
  275. break;
  276. }
  277. case Communication_message::Message_type::eDispatch_control_request_msg:
  278. {
  279. return Error_code::SUCCESS;
  280. break;
  281. }
  282. default :
  283. {
  284. //无效的消息,
  285. return Error_manager(Error_code::INVALID_MESSAGE, Error_level::NEGLIGIBLE_ERROR,
  286. " INVALID_MESSAGE error ");
  287. break;
  288. }
  289. }
  290. */
  291. return Error_code::SUCCESS;
  292. }
  293. //检查状态
  294. Error_manager Dispatch_manager::check_status()
  295. {
  296. if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_READY )
  297. {
  298. return Error_code::SUCCESS;
  299. }
  300. else if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_STORE || m_dispatch_manager_status == E_DISPATCH_MANAGER_PICKUP )
  301. {
  302. return Error_code::SUCCESS;
  303. //无论存取, 状态都是返回正常,运行接受下一个任务
  304. // return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  305. // " Dispatch_manager::check_status() error ");
  306. }
  307. else
  308. {
  309. return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_ERROR, Error_level::MINOR_ERROR,
  310. " Dispatch_manager::check_status() error ");
  311. }
  312. return Error_code::SUCCESS;
  313. }
  314. //调度模块 //执行搬运请求(主控->调度管理)
  315. //Error_manager Dispatch_manager::execute_for_dispatch_request_msg(message::Dispatch_request_msg& dispatch_request_msg)
  316. //{
  317. // LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_request_msg--- "<< dispatch_request_msg.DebugString() << " "<< this;
  318. // LOG(INFO) << " dispatch_request_msg->m_command_key = "<<dispatch_request_msg.command_key()<< " "<< this;
  319. //
  320. // Error_manager t_error;
  321. //
  322. // std::unique_lock<std::mutex> t_lock(m_lock);
  323. // if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  324. // {
  325. // m_dispatch_request_store_list.push_back(dispatch_request_msg);
  326. // m_store_updata_time = std::chrono::system_clock::now();
  327. // }
  328. // else
  329. // {
  330. // m_dispatch_request_pickup_list.push_back(dispatch_request_msg);
  331. // m_pickup_updata_time = std::chrono::system_clock::now();
  332. // }
  333. //
  334. //
  335. //
  336. // //need, 目前直接转发给调度plc执行, 后续要存队列,然后增加调度逻辑.
  337. //
  338. //// m_dispatch_plc.execute_for_dispatch_request_msg(dispatch_request_msg);
  339. //
  340. //
  341. //
  342. // return Error_code::SUCCESS;
  343. //}
  344. ////调度模块 答复数据异常
  345. //Error_manager Dispatch_manager::send_dispatch_response_msg_with_error(message::Dispatch_request_msg &dispatch_request_msg, Error_manager error)
  346. //{
  347. // message::Dispatch_response_msg t_dispatch_response_msg;
  348. // t_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  349. // t_dispatch_response_msg.mutable_base_info()->set_timeout_ms(dispatch_request_msg.base_info().timeout_ms());
  350. // t_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  351. // t_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  352. // t_dispatch_response_msg.set_command_key(dispatch_request_msg.command_key());
  353. //
  354. // t_dispatch_response_msg.mutable_error_manager()->set_error_code(error.get_error_code());
  355. // t_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)error.get_error_level());
  356. // t_dispatch_response_msg.mutable_error_manager()->set_error_description(error.get_error_description());
  357. //
  358. //// System_communication::get_instance_references().encapsulate_msg(t_dispatch_response_msg.SerializeAsString());
  359. // System_communication::get_instance_references().encapsulate_task_msg(t_dispatch_response_msg.SerializeAsString());
  360. // return Error_code::SUCCESS;
  361. //}
  362. //
  363. ////调度模块 //调度总规划的答复(调度算法->调度管理)
  364. //Error_manager Dispatch_manager::execute_for_dispatch_plan_response_msg(message::Dispatch_plan_response_msg &dispatch_plan_response_msg)
  365. //{
  366. // LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_plan_response_msg--- "<< this;
  367. // LOG(INFO) << " dispatch_plan_response_msg->m_command_key = "<<dispatch_plan_response_msg.command_key()<< " "<< this;
  368. //
  369. // Error_manager t_error;
  370. //
  371. // return Error_code::SUCCESS;
  372. //}
  373. ////调度模块 //调度控制的任务请求(调度算法->调度管理)
  374. //Error_manager Dispatch_manager::execute_for_dispatch_control_request_msg(message::Dispatch_control_request_msg &dispatch_control_request_msg)
  375. //{
  376. // LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_control_request_msg--- "<< this;
  377. // LOG(INFO) << " dispatch_control_request_msg->m_command_key = "<<dispatch_control_request_msg.command_key()<< " "<< this;
  378. //
  379. // Error_manager t_error;
  380. //
  381. // return Error_code::SUCCESS;
  382. //}
  383. //定时发送 调度管理的状态
  384. Error_manager Dispatch_manager::encapsulate_send_dispatch_manager_status()
  385. {
  386. // Error_manager t_error;
  387. //
  388. // int t_dispatch_manager_id = get_dispatch_manager_id();
  389. //
  390. // std::string t_msg1;
  391. // //创建一条 调度管理总管理的状态 旧版
  392. // message::Dispatch_terminal_status_msg t_dispatch_terminal_status_msg;
  393. // t_dispatch_terminal_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_status_msg);
  394. // t_dispatch_terminal_status_msg.mutable_base_info()->set_timeout_ms(5000);
  395. // t_dispatch_terminal_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  396. // t_dispatch_terminal_status_msg.mutable_base_info()->set_receiver(message::Communicator::eEmpty);
  397. //
  398. // t_dispatch_terminal_status_msg.mutable_id_struct()->set_unit_id(t_dispatch_manager_id);
  399. // t_dispatch_terminal_status_msg.set_terminal_status(message::Terminal_status(m_dispatch_manager_status));
  400. // t_dispatch_terminal_status_msg.set_passageway_direction(message::Passageway_direction::E_BILATERAL);
  401. //
  402. //
  403. // //E_DISPATCH_POCESS_OVER
  404. // {
  405. // std::unique_lock<std::mutex> t_lock(m_lock);
  406. // for (auto iter = m_dispatch_response_store_map.begin(); iter != m_dispatch_response_store_map.end(); ++iter)
  407. // {
  408. // message::Dispatch_process_information * tp_dispatch_process_information =
  409. // t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
  410. // tp_dispatch_process_information->set_car_numberplate(iter->second.parkspace_info_ex(0).car_info().car_numberplate());
  411. // tp_dispatch_process_information->set_command_key(iter->second.command_key());
  412. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->second.dispatch_motion_direction());
  413. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->second.id_struct());
  414. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->second.locate_information());
  415. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->second.parkspace_info_ex());
  416. // tp_dispatch_process_information->set_car_type(iter->second.car_type());
  417. //
  418. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_OVER);
  419. // }
  420. // for (auto iter = m_dispatch_response_pickup_map.begin(); iter != m_dispatch_response_pickup_map.end(); ++iter)
  421. // {
  422. // message::Dispatch_process_information * tp_dispatch_process_information =
  423. // t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
  424. // tp_dispatch_process_information->set_car_numberplate(iter->second.parkspace_info_ex(0).car_info().car_numberplate());
  425. // tp_dispatch_process_information->set_command_key(iter->second.command_key());
  426. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->second.dispatch_motion_direction());
  427. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->second.id_struct());
  428. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->second.locate_information());
  429. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->second.parkspace_info_ex());
  430. // tp_dispatch_process_information->set_car_type(iter->second.car_type());
  431. //
  432. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_OVER);
  433. // }
  434. // }
  435. //
  436. //
  437. // //E_DISPATCH_POCESS_WORKING
  438. // {
  439. // if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST ||
  440. // m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_WORKING||
  441. // m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_BUSY)
  442. // {
  443. // message::Dispatch_request_msg t_dispatch_request_msg = m_dispatch_plc.get_dispatch_request_msg();
  444. // if ( t_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  445. // {
  446. // message::Dispatch_process_information * tp_dispatch_process_information =
  447. // t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
  448. // tp_dispatch_process_information->set_car_numberplate(t_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate());
  449. // tp_dispatch_process_information->set_command_key(t_dispatch_request_msg.command_key());
  450. // tp_dispatch_process_information->set_dispatch_motion_direction(t_dispatch_request_msg.dispatch_motion_direction());
  451. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(t_dispatch_request_msg.id_struct());
  452. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(t_dispatch_request_msg.locate_information());
  453. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(t_dispatch_request_msg.parkspace_info_ex());
  454. // tp_dispatch_process_information->set_car_type(t_dispatch_request_msg.car_type());
  455. //
  456. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WORKING);
  457. // tp_dispatch_process_information->set_working_total_time(m_dispatch_plc.get_response_working_total_time());
  458. // tp_dispatch_process_information->set_working_remaining_time(m_dispatch_plc.get_response_working_remaining_time());
  459. // }
  460. // else
  461. // {
  462. // message::Dispatch_process_information * tp_dispatch_process_information =
  463. // t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
  464. // tp_dispatch_process_information->set_car_numberplate(t_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate());
  465. // tp_dispatch_process_information->set_command_key(t_dispatch_request_msg.command_key());
  466. // tp_dispatch_process_information->set_dispatch_motion_direction(t_dispatch_request_msg.dispatch_motion_direction());
  467. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(t_dispatch_request_msg.id_struct());
  468. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(t_dispatch_request_msg.locate_information());
  469. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(t_dispatch_request_msg.parkspace_info_ex());
  470. // tp_dispatch_process_information->set_car_type(t_dispatch_request_msg.car_type());
  471. //
  472. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WORKING);
  473. // tp_dispatch_process_information->set_working_total_time(m_dispatch_plc.get_response_working_total_time());
  474. // tp_dispatch_process_information->set_working_remaining_time(m_dispatch_plc.get_response_working_remaining_time());
  475. // }
  476. // }
  477. // }
  478. //
  479. // //E_DISPATCH_POCESS_WAIT
  480. // {
  481. // std::unique_lock<std::mutex> t_lock(m_lock);
  482. // int t_store_wait_number = 1;
  483. // for (auto iter = m_dispatch_request_store_list.begin(); iter != m_dispatch_request_store_list.end(); ++iter)
  484. // {
  485. // message::Dispatch_process_information * tp_dispatch_process_information =
  486. // t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
  487. // tp_dispatch_process_information->set_car_numberplate(iter->parkspace_info_ex(0).car_info().car_numberplate());
  488. // tp_dispatch_process_information->set_command_key(iter->command_key());
  489. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->dispatch_motion_direction());
  490. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->id_struct());
  491. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->locate_information());
  492. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->parkspace_info_ex());
  493. // tp_dispatch_process_information->set_car_type(iter->car_type());
  494. //
  495. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WAIT);
  496. // tp_dispatch_process_information->set_wait_number(t_store_wait_number);
  497. // t_store_wait_number++;
  498. // }
  499. // int t_pickup_wait_number = 1;
  500. // for (auto iter = m_dispatch_request_pickup_list.begin(); iter != m_dispatch_request_pickup_list.end(); ++iter)
  501. // {
  502. // message::Dispatch_process_information * tp_dispatch_process_information =
  503. // t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
  504. // tp_dispatch_process_information->set_car_numberplate(iter->parkspace_info_ex(0).car_info().car_numberplate());
  505. // tp_dispatch_process_information->set_command_key(iter->command_key());
  506. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->dispatch_motion_direction());
  507. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->id_struct());
  508. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->locate_information());
  509. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->parkspace_info_ex());
  510. // tp_dispatch_process_information->set_car_type(iter->car_type());
  511. //
  512. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WAIT);
  513. // tp_dispatch_process_information->set_wait_number(t_pickup_wait_number);
  514. // t_pickup_wait_number++;
  515. // }
  516. // }
  517. //
  518. // std::string t_store_updata_time = Time_tool::get_instance_references().get_time_string_millisecond(m_store_updata_time);
  519. // std::string t_pickup_updata_time = Time_tool::get_instance_references().get_time_string_millisecond(m_pickup_updata_time);
  520. //
  521. // t_dispatch_terminal_status_msg.set_dispatch_store_process_information_updata_time(t_store_updata_time);
  522. // t_dispatch_terminal_status_msg.set_dispatch_pickup_process_information_updata_time(t_pickup_updata_time);
  523. //
  524. //
  525. // t_msg1 = t_dispatch_terminal_status_msg.SerializeAsString();
  526. // System_communication::get_instance_references().encapsulate_status_msg(t_msg1);
  527. //
  528. return Error_code::SUCCESS;
  529. }
  530. //定时发送 调度管理的状态
  531. Error_manager Dispatch_manager::encapsulate_send_dispatch_manager_status_new()
  532. {
  533. std::unique_lock<std::mutex> t_lock(m_lock);
  534. dispatch_node_statu t_dispatch_node_statu_msg;
  535. if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_READY )
  536. {
  537. t_dispatch_node_statu_msg.set_statu(eIdle);
  538. // t_dispatch_node_statu_msg.set_statu(eBusy);
  539. }
  540. else if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_UNKNOW ||
  541. m_dispatch_manager_status == E_DISPATCH_MANAGER_DISCONNECT ||
  542. m_dispatch_manager_status == E_DISPATCH_MANAGER_FAULT )
  543. {
  544. t_dispatch_node_statu_msg.set_statu(eFault);
  545. }
  546. else
  547. {
  548. t_dispatch_node_statu_msg.set_statu(eBusy);
  549. }
  550. t_dispatch_node_statu_msg.set_idle_stop_floor(m_device_floor);
  551. if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE ||
  552. m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REALLOCATE)
  553. {
  554. t_dispatch_node_statu_msg.mutable_running_pack_info()->CopyFrom(m_park_table_msg);
  555. }
  556. else if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP ||
  557. m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REVOCATION )
  558. {
  559. t_dispatch_node_statu_msg.mutable_running_pick_info()->CopyFrom(m_pick_table_msg);
  560. }
  561. else
  562. {
  563. }
  564. t_dispatch_node_statu_msg.set_unit_id(m_dispatch_manager_id+1);
  565. t_dispatch_node_statu_msg.set_plc_heartbeat(m_dispatch_plc.m_plc_heartbeat);
  566. t_dispatch_node_statu_msg.set_plc_status_info(m_dispatch_plc.m_plc_status_info);
  567. dispatch_region_info * p_dispatch_region_info_1 = t_dispatch_node_statu_msg.mutable_dispatch_region_info_vector()->Add();
  568. p_dispatch_region_info_1->set_terminal_id(m_dispatch_manager_id*2+1);
  569. p_dispatch_region_info_1->set_turnplate_angle_min(m_dispatch_plc.m_turnplate_angle_min1);
  570. p_dispatch_region_info_1->set_turnplate_angle_max(m_dispatch_plc.m_turnplate_angle_max1);
  571. dispatch_region_info * p_dispatch_region_info_2 = t_dispatch_node_statu_msg.mutable_dispatch_region_info_vector()->Add();
  572. p_dispatch_region_info_2->set_terminal_id(m_dispatch_manager_id*2+2);
  573. p_dispatch_region_info_2->set_turnplate_angle_min(m_dispatch_plc.m_turnplate_angle_min2);
  574. p_dispatch_region_info_2->set_turnplate_angle_max(m_dispatch_plc.m_turnplate_angle_max2);
  575. std::string t_msg = t_dispatch_node_statu_msg.DebugString();
  576. // std::cout << " huli test t_dispatch_node_statu_msg:::: " << " t_msg.size() = " << t_msg.size() << std::endl;
  577. // std::cout << " huli test t_dispatch_node_statu_msg:::: " << " t_msg = " << t_msg << std::endl;
  578. System_communication::get_instance_references().encapsulate_status_msg(t_msg);
  579. return Error_code::SUCCESS;
  580. }
  581. //在流程的map 里面释放指定的流程
  582. Error_manager Dispatch_manager::release_dispatch_process(std::string command_key)
  583. {
  584. std::unique_lock<std::mutex> t_lock(m_lock);
  585. return Error_code::SUCCESS;
  586. }
  587. //调度模块 ///地面雷达的状态消息(地面雷达->null)
  588. //Error_manager Dispatch_manager::execute_for_ground_status_msg(message::Ground_status_msg ground_status_msg)
  589. //{
  590. // int t_inlet_id = ground_status_msg.mutable_id_struct()->terminal_id() %2;
  591. // return m_dispatch_ground_lidar[t_inlet_id].execute_for_ground_status_msg(ground_status_msg);
  592. //
  593. // return Error_code::SUCCESS;
  594. //}
  595. //调度模块 ///单片机的状态消息
  596. //Error_manager Dispatch_manager::execute_for_singlechip_data_msg(message::Singlechip_data singlechip_data_msg, bool validity)
  597. //{
  598. // if ( singlechip_data_msg.has_terminalid() && singlechip_data_msg.has_dispatchdirection() )
  599. // {
  600. // if ( singlechip_data_msg.terminalid() >=0 &&
  601. // singlechip_data_msg.terminalid() <=5 &&
  602. // singlechip_data_msg.dispatchdirection() >=1 &&
  603. // singlechip_data_msg.dispatchdirection() <=2 )
  604. // {
  605. // int t_temp = (singlechip_data_msg.terminalid() %2) + (singlechip_data_msg.dispatchdirection()-1)*2;
  606. // return m_dispatch_singlechip[t_temp].execute_for_singlechip_data_msg(singlechip_data_msg, validity);
  607. // }
  608. // }
  609. //
  610. // return Error_code::SUCCESS;
  611. //}
  612. //获取空闲的出口,返回0表示没有空闲出口, 返回1~6表示对应的出口
  613. int Dispatch_manager::get_outlet_for_ready()
  614. {
  615. std::cout << " huli test 111:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  616. //检查出口, 并删除取车完成 //判断2个出口是否空闲
  617. if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
  618. {
  619. std::cout << " huli test 222:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  620. if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+1) == Error_code::SUCCESS )
  621. {
  622. std::cout << " huli test 333:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  623. return m_dispatch_manager_id*2+1;
  624. }
  625. }
  626. else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  627. {
  628. std::cout << " huli test 444:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  629. if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+2) == Error_code::SUCCESS )
  630. {
  631. std::cout << " huli test 555:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  632. return m_dispatch_manager_id*2+2;
  633. }
  634. }
  635. else
  636. {
  637. return 0;
  638. }
  639. return 0;
  640. }
  641. Dispatch_manager::Dispatch_manager_status Dispatch_manager::get_dispatch_manager_status()
  642. {
  643. return m_dispatch_manager_status;
  644. }
  645. int Dispatch_manager::get_dispatch_manager_id()
  646. {
  647. return m_dispatch_manager_id;
  648. }
  649. void Dispatch_manager::set_dispatch_manager_id(int dispatch_manager_id)
  650. {
  651. m_dispatch_manager_id = dispatch_manager_id;
  652. }
  653. //资源分配
  654. //void Dispatch_manager::resource_allocation()
  655. //{
  656. // return;
  657. //
  658. // LOG(INFO) << " Dispatch_manager::resource_allocation() start " << this;
  659. // Error_manager t_error;
  660. //
  661. //
  662. // while (m_dispatch_manager_condition.is_alive())
  663. // {
  664. // m_dispatch_manager_condition.wait();
  665. // if ( m_dispatch_manager_condition.is_alive() )
  666. // {
  667. // std::this_thread::sleep_for(std::chrono::microseconds(1));
  668. // std::this_thread::sleep_for(std::chrono::milliseconds(100));
  669. //// std::this_thread::sleep_for(std::chrono::seconds(1));
  670. // std::this_thread::yield();
  671. // std::unique_lock<std::mutex> t_lock(m_lock);
  672. //
  673. // //排序和资源分配
  674. // if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  675. // {
  676. // //补全 id_struct
  677. // if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP)
  678. // {
  679. // //判断2个出口是否空闲, 在资源分配时,提前分配好出口终端号
  680. // if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
  681. // {
  682. // auto iter = m_dispatch_request_pickup_list.begin();
  683. // if ( iter != m_dispatch_request_pickup_list.end() )
  684. // {
  685. // iter->mutable_id_struct()->set_terminal_id(m_dispatch_manager_id*2);
  686. // m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  687. // m_dispatch_manager_status = E_DISPATCH_MANAGER_PICKUP;
  688. // m_dispatch_request_pickup_list.erase(iter);
  689. // m_pickup_updata_time = std::chrono::system_clock::now();
  690. // }
  691. // }
  692. // else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  693. // {
  694. // auto iter = m_dispatch_request_pickup_list.begin();
  695. // if ( iter != m_dispatch_request_pickup_list.end() )
  696. // {
  697. // iter->mutable_id_struct()->set_terminal_id(m_dispatch_manager_id*2+1);
  698. // m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  699. // m_dispatch_manager_status = E_DISPATCH_MANAGER_PICKUP;
  700. // m_dispatch_request_pickup_list.erase(iter);
  701. // m_pickup_updata_time = std::chrono::system_clock::now();
  702. // }
  703. // }
  704. // //else什么也不做,直接切换到存车就行
  705. //
  706. // m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  707. // }
  708. // else if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE )
  709. // {
  710. // auto iter = m_dispatch_request_store_list.begin();
  711. // if ( iter != m_dispatch_request_store_list.end() )
  712. // {
  713. // iter->mutable_id_struct()->set_unit_id(m_dispatch_manager_id );
  714. // m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  715. // m_dispatch_manager_status = E_DISPATCH_MANAGER_STORE;
  716. // m_dispatch_request_store_list.erase(iter);
  717. // m_store_updata_time = std::chrono::system_clock::now();
  718. // }
  719. // m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  720. // }
  721. // else
  722. // {
  723. // ///////
  724. // }
  725. // }
  726. // else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY)
  727. // {
  728. // m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  729. // }
  730. // else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_BUSY)
  731. // {
  732. // m_dispatch_manager_status = E_DISPATCH_MANAGER_BUSY;
  733. // }
  734. // else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT)
  735. // {
  736. // m_dispatch_manager_status = E_DISPATCH_MANAGER_DISCONNECT;
  737. // }
  738. // else
  739. // {
  740. // m_dispatch_manager_status = E_DISPATCH_MANAGER_FAULT;
  741. // }
  742. //
  743. //
  744. // //检查时间, 并删除存车完成
  745. // for (auto iter = m_dispatch_response_store_map.begin(); iter != m_dispatch_response_store_map.end();)
  746. // {
  747. // if ( std::chrono::system_clock::now() - iter->first > std::chrono::seconds(10) )
  748. // {
  749. // iter = m_dispatch_response_store_map.erase(iter);
  750. // m_store_updata_time = std::chrono::system_clock::now();
  751. // }
  752. // else
  753. // {
  754. // ++iter;
  755. // }
  756. // }
  757. //
  758. // //检查出口, 并删除取车完成 //判断2个出口是否空闲
  759. // if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
  760. // {
  761. // if ( m_dispatch_response_pickup_map.find(m_dispatch_manager_id*2) != m_dispatch_response_pickup_map.end() )
  762. // {
  763. // m_dispatch_response_pickup_map.erase(m_dispatch_manager_id*2);
  764. // m_pickup_updata_time = std::chrono::system_clock::now();
  765. // }
  766. // }
  767. // else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  768. // {
  769. // if ( m_dispatch_response_pickup_map.find(m_dispatch_manager_id*2+1) != m_dispatch_response_pickup_map.end() )
  770. // {
  771. // m_dispatch_response_pickup_map.erase(m_dispatch_manager_id*2+1);
  772. // m_pickup_updata_time = std::chrono::system_clock::now();
  773. // }
  774. // }
  775. //
  776. // }
  777. // }
  778. //
  779. // LOG(INFO) << " Dispatch_manager::resource_allocation() end "<< this;
  780. //
  781. // return;
  782. //}
  783. //
  784. ////新版流程控制
  785. //void Dispatch_manager::process_control()
  786. //{
  787. // LOG(INFO) << " Dispatch_manager::process_control() start " << this;
  788. // Error_manager t_error;
  789. //
  790. // while (m_dispatch_manager_condition.is_alive())
  791. // {
  792. // m_dispatch_manager_condition.wait();
  793. // if (m_dispatch_manager_condition.is_alive())
  794. // {
  795. // std::this_thread::sleep_for(std::chrono::microseconds(1));
  796. // std::this_thread::sleep_for(std::chrono::milliseconds(1));
  797. //// std::this_thread::sleep_for(std::chrono::seconds(1));
  798. // std::this_thread::yield();
  799. // std::unique_lock<std::mutex> t_lock(m_lock);
  800. //
  801. // static int t_count = 0;
  802. // if (t_count % 1000 == 0 )
  803. // {
  804. // std::cout << " huli test :::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  805. // std::cout << " huli test :::: " << " m_dispatch_manager_status = " << m_dispatch_manager_status << std::endl;
  806. // }
  807. // t_count++;
  808. //
  809. // switch ( m_dispatch_manager_status )
  810. // {
  811. // case E_DISPATCH_MANAGER_READY:
  812. // {
  813. // //等待 count_command_signal_1_port 的消息
  814. // if ( m_count_flag )
  815. // {
  816. // m_dispatch_manager_status = E_DISPATCH_MANAGER_COUNT_NEW;
  817. // }
  818. // if ( m_post_flag )
  819. // {
  820. // m_dispatch_manager_status = E_DISPATCH_MANAGER_POST_NEW;
  821. // }
  822. // break;
  823. // }
  824. // case E_DISPATCH_MANAGER_COUNT_NEW:
  825. // {
  826. // //目前不用解析 m_count_command_signal_msg ,只要收到 count_command_signal_1_port, 直接进行下一步
  827. // //发送调度任务申请
  828. // char t_buf[256] = {0};
  829. // sprintf(t_buf, "%d-%d", m_dispatch_manager_id+1, m_device_floor);
  830. // std::string t_string(t_buf);
  831. // System_communication::get_instance_references().encapsulate_task_msg(t_string, 0);
  832. // std::cout << " huli test :::: " << " m_request_command_msg = " << t_string << std::endl;
  833. // m_dispatch_manager_status = E_DISPATCH_MANAGER_REQUEST_NEW;
  834. // break;
  835. // }
  836. // case E_DISPATCH_MANAGER_REQUEST_NEW:
  837. // {
  838. // //等待 post_command_1_port 的消息
  839. // if ( m_post_flag )
  840. // {
  841. // m_dispatch_manager_status = E_DISPATCH_MANAGER_POST_NEW;
  842. // }
  843. // break;
  844. // }
  845. // case E_DISPATCH_MANAGER_POST_NEW:
  846. // {
  847. // //解析 m_post_command_msg
  848. // std::string t_string = m_post_command_msg.m_message_buf;
  849. // //检索字符串的内容,
  850. // if ( t_string.find("entrance_measure_info") != std::string::npos )
  851. // {
  852. // //entrance_measure_info 是 park_table 特有的
  853. // if(! google::protobuf::TextFormat::ParseFromString(t_string, &m_park_table_msg))
  854. // {
  855. // //答复错误
  856. // }
  857. // else
  858. // {
  859. //// LOG(INFO) << " m_park_table_msg = "<< m_park_table_msg.DebugString() << " --- " << this;
  860. // m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
  861. //
  862. // //根据雷达x坐标, 计算入口id
  863. // int t_terminal = 0;
  864. // if (m_park_table_msg.entrance_measure_info().cx()<0)
  865. // {
  866. // t_terminal = m_dispatch_manager_id*2+1;
  867. // }
  868. // else if(m_park_table_msg.entrance_measure_info().cx()>0)
  869. // {
  870. // t_terminal = m_dispatch_manager_id*2+2;
  871. // }
  872. // m_park_table_msg.set_terminal_id(t_terminal);
  873. // m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
  874. // m_device_floor = m_park_table_msg.allocated_space_info().floor();
  875. // m_dispatch_manager_status = E_DISPATCH_MANAGER_COMPLETED_NEW;
  876. // }
  877. // }
  878. // else
  879. // {
  880. // if ( get_outlet_for_ready() !=0 )
  881. // {
  882. // if(! google::protobuf::TextFormat::ParseFromString(t_string, &m_pick_table_msg))
  883. // {
  884. // //答复错误
  885. // }
  886. // else
  887. // {
  888. // m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
  889. // m_pick_table_msg.set_terminal_id(get_outlet_for_ready());
  890. // m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg, m_dispatch_process_type, m_car_type);
  891. // m_device_floor = 1;
  892. // m_dispatch_manager_status = E_DISPATCH_MANAGER_COMPLETED_NEW;
  893. // }
  894. // }
  895. // //else 没有空闲出口,就无限等待
  896. // }
  897. // break;
  898. // }
  899. // case E_DISPATCH_MANAGER_COMPLETED_NEW:
  900. // {
  901. // //等待dispatch_plc执行,然后ack里面切回ready
  902. // break;
  903. // }
  904. // default:
  905. // {
  906. // std::cout << " huli test :::: " << " Dispatch_manager::process_control() error, m_dispatch_manager_status = "
  907. // << m_dispatch_manager_status << std::endl;
  908. // break;
  909. // }
  910. // }
  911. // }
  912. // }
  913. // LOG(INFO) << " Dispatch_manager::process_control() end "<< this;
  914. // return;
  915. //}
  916. //新版流程控制
  917. void Dispatch_manager::process_sql()
  918. {
  919. LOG(INFO) << " Dispatch_manager::process_sql() start " << this;
  920. Error_manager t_error;
  921. while (m_dispatch_manager_condition.is_alive())
  922. {
  923. m_dispatch_manager_condition.wait();
  924. if (m_dispatch_manager_condition.is_alive())
  925. {
  926. std::this_thread::sleep_for(std::chrono::microseconds(1));
  927. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  928. // std::this_thread::sleep_for(std::chrono::seconds(1));
  929. std::this_thread::yield();
  930. std::unique_lock<std::mutex> t_lock(m_lock);
  931. static auto t_update_time = std::chrono::system_clock::now();
  932. if(std::chrono::system_clock::now() - t_update_time > std::chrono::seconds(1))
  933. {
  934. t_update_time = std::chrono::system_clock::now();
  935. std::cout << " huli test :::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  936. std::cout << " huli test :::: " << " m_dispatch_manager_status = " << m_dispatch_manager_status << std::endl;
  937. std::cout << " huli test :::: " << " xxxxxxxxx get_outlet_for_ready() = " << get_outlet_for_ready() << std::endl;
  938. }
  939. switch ( m_dispatch_manager_status )
  940. {
  941. case E_DISPATCH_MANAGER_READY:
  942. case E_DISPATCH_MANAGER_REQUEST:
  943. {
  944. //循环请求, 请求周期1秒
  945. // std::this_thread::sleep_for(std::chrono::seconds(1));
  946. if ( std::chrono::system_clock::now() - m_request_updata_time > std::chrono::seconds(1) )
  947. {
  948. m_request_updata_time = std::chrono::system_clock::now();
  949. //调度开始前, 向数据库发送请求的相关操作, 输入 穿梭机所在的楼层, 调度id 0~2, 空闲出口id, 如果不是0,就表示有空闲的出口
  950. t_error = m_dispatch_command.dispatch_request_to_sql(m_device_floor, m_dispatch_manager_id, get_outlet_for_ready());
  951. //std::cout << " huli test :::: " << " sssssssssssssssssssssssssssssssssss dispatch_request_to_sql = " << t_error << std::endl;
  952. if ( t_error == Error_code::SUCCESS )
  953. {
  954. //如果找到了合适的指令, 那么交给plc来执行
  955. if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  956. {
  957. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
  958. m_park_table_msg = m_dispatch_command.m_park_table_msg;
  959. m_car_type = m_dispatch_command.m_car_type;
  960. m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
  961. m_device_floor = m_park_table_msg.allocated_space_info().floor();
  962. }
  963. else if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  964. {
  965. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
  966. m_pick_table_msg = m_dispatch_command.m_pick_table_msg;
  967. m_car_type = m_dispatch_command.m_car_type;
  968. m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg, m_dispatch_process_type, m_car_type);
  969. m_device_floor = 1;
  970. }
  971. //切换到工作状态
  972. m_dispatch_manager_status = E_DISPATCH_MANAGER_WORKING;
  973. }
  974. else if(t_error != Error_code::NODATA)
  975. {
  976. LOG(INFO) << " dispatch_request_to_sql error = " << t_error << this;
  977. }
  978. //else NODATA 无限循环
  979. }
  980. //else 等待
  981. break;
  982. }
  983. case E_DISPATCH_MANAGER_WORKING:
  984. {
  985. //等待dispatch_plc执行, 完成后切换到 E_DISPATCH_MANAGER_RESPONSE
  986. if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::DISPATCH_PLC_READY ||
  987. m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::DISPATCH_PLC_FAULT)
  988. {
  989. if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_MIN_CAR )
  990. {
  991. //切换到 重新分配
  992. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  993. m_car_type = Common_data::Car_type::MIN_CAR;
  994. }
  995. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_MID_CAR )
  996. {
  997. //切换到 重新分配
  998. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  999. m_car_type = Common_data::Car_type::MID_CAR;
  1000. }
  1001. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_BIG_CAR )
  1002. {
  1003. //切换到 重新分配
  1004. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1005. m_car_type = Common_data::Car_type::BIG_CAR;
  1006. }
  1007. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_HUGE_CAR )
  1008. {
  1009. //切换到 重新分配
  1010. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1011. m_car_type = Common_data::Car_type::HUGE_CAR;
  1012. }
  1013. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_FAULT_CAR )
  1014. {
  1015. //切换到 重新分配
  1016. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1017. m_car_type = Common_data::Car_type::FAULT_CAR;
  1018. }
  1019. else
  1020. {
  1021. //切换到答复状态
  1022. m_dispatch_manager_status = E_DISPATCH_MANAGER_RESPONSE;
  1023. }
  1024. }
  1025. //else 无限等待
  1026. break;
  1027. }
  1028. case E_DISPATCH_MANAGER_REALLOCATE:
  1029. {
  1030. //调度 , 向数据库 重新分配车位,
  1031. t_error = m_dispatch_command.dispatch_reallocate_to_sql(m_car_type, get_outlet_for_ready());
  1032. if ( t_error == Error_code::SUCCESS )
  1033. {
  1034. //如果找到了合适的车位, 那么交给plc来执行
  1035. if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  1036. {
  1037. m_dispatch_process_type = m_dispatch_command.m_dispatch_process_type;
  1038. m_park_table_msg = m_dispatch_command.m_park_table_msg;
  1039. m_car_type = m_dispatch_command.m_car_type;
  1040. m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
  1041. m_device_floor = m_park_table_msg.allocated_space_info().floor();
  1042. }
  1043. //没有找到合适的车位, 那么放到出口
  1044. else if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  1045. {
  1046. m_dispatch_process_type = m_dispatch_command.m_dispatch_process_type;
  1047. m_pick_table_msg = m_dispatch_command.m_pick_table_msg;
  1048. m_car_type = m_dispatch_command.m_car_type;
  1049. m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg,m_dispatch_process_type, m_car_type);
  1050. m_device_floor = 1;
  1051. }
  1052. //切换到工作状态
  1053. m_dispatch_manager_status = E_DISPATCH_MANAGER_WORKING;
  1054. }
  1055. else if(t_error != Error_code::NODATA)
  1056. {
  1057. LOG(INFO) << " dispatch_request_to_sql error = " << t_error << this;
  1058. }
  1059. //else NODATA 无限循环
  1060. break;
  1061. break;
  1062. }
  1063. case E_DISPATCH_MANAGER_RESPONSE:
  1064. {
  1065. //调度完成后, 向数据库发送答复的相关操作
  1066. t_error = m_dispatch_command.dispatch_response_to_sql(m_dispatch_plc.get_result());
  1067. if(t_error != Error_code::SUCCESS)
  1068. {
  1069. LOG(INFO) << " dispatch_response_to_sql error = " << t_error << this;
  1070. }
  1071. //切换到ready状态
  1072. m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  1073. break;
  1074. }
  1075. default:
  1076. {
  1077. std::cout << " huli test :::: " << " Dispatch_manager::process_control() error, m_dispatch_manager_status = "
  1078. << m_dispatch_manager_status << std::endl;
  1079. break;
  1080. }
  1081. }
  1082. }
  1083. }
  1084. LOG(INFO) << " Dispatch_manager::process_sql() end "<< this;
  1085. return;
  1086. }