dispatch_manager.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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(SETTING_PATH DISPATCH_DEVICE_PARAMETER_PATH);
  30. }
  31. //调度管理 初始化
  32. Error_manager Dispatch_manager::dispatch_manager_init()
  33. {
  34. return dispatch_manager_init_from_protobuf(SETTING_PATH 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. #ifdef CHUTIAN_PROJECT_PROJECT
  54. m_dispatch_ground_lidar[0].dispatch_ground_lidar_init(m_dispatch_manager_id, 0);
  55. m_dispatch_ground_lidar[1].dispatch_ground_lidar_init(m_dispatch_manager_id, 1);
  56. m_dispatch_singlechip[0].dispatch_singlechip_init(m_dispatch_manager_id, 0, 1);
  57. m_dispatch_singlechip[1].dispatch_singlechip_init(m_dispatch_manager_id, 1, 1);
  58. m_dispatch_singlechip[2].dispatch_singlechip_init(m_dispatch_manager_id, 0, 2);
  59. m_dispatch_singlechip[3].dispatch_singlechip_init(m_dispatch_manager_id, 1, 2);
  60. #endif
  61. // m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  62. // 线程默认开启
  63. m_dispatch_manager_condition.reset(false, true, false);
  64. // m_dispatch_manager_thread = new std::thread(&Dispatch_manager::resource_allocation, this);
  65. // m_dispatch_manager_thread = new std::thread(&Dispatch_manager::process_control, this);
  66. #ifdef CHUTIAN_PROJECT_PROJECT
  67. m_dispatch_manager_thread = new std::thread(&Dispatch_manager::process_sql, this);
  68. #endif//CHUTIAN_PROJECT_PROJECT
  69. #ifdef SHANGGUJIE_PROJECT_PROJECT
  70. m_dispatch_manager_thread = new std::thread(&Dispatch_manager::process_sql_shanggujie, this);
  71. #endif//SHANGGUJIE_PROJECT_PROJECT
  72. m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  73. return Error_code::SUCCESS;
  74. }
  75. //调度管理 反初始化
  76. Error_manager Dispatch_manager::dispatch_manager_uninit()
  77. {
  78. if (m_dispatch_manager_thread)
  79. {
  80. m_dispatch_manager_condition.kill_all();
  81. }
  82. if (m_dispatch_manager_thread)
  83. {
  84. m_dispatch_manager_thread->join();
  85. delete m_dispatch_manager_thread;
  86. m_dispatch_manager_thread = NULL;
  87. }
  88. m_dispatch_manager_status = E_DISPATCH_MANAGER_UNKNOW;
  89. m_dispatch_manager_id = -1;
  90. m_dispatch_plc.dispatch_plc_uninit();
  91. #ifdef CHUTIAN_PROJECT_PROJECT
  92. m_dispatch_ground_lidar[0].dispatch_ground_lidar_uninit();
  93. m_dispatch_ground_lidar[1].dispatch_ground_lidar_uninit();
  94. m_dispatch_singlechip[0].dispatch_singlechip_uninit();
  95. m_dispatch_singlechip[1].dispatch_singlechip_uninit();
  96. m_dispatch_singlechip[2].dispatch_singlechip_uninit();
  97. m_dispatch_singlechip[3].dispatch_singlechip_uninit();
  98. #endif
  99. return Error_code::SUCCESS;
  100. }
  101. //调度管理 设备复位
  102. Error_manager Dispatch_manager::dispatch_manager_device_reset()
  103. {
  104. //以后再写
  105. return Error_code::SUCCESS;
  106. }
  107. //新的对外接口, 负责接受Rabbitmq_message,然后内部处理
  108. Error_manager Dispatch_manager::execute_rabbitmq_message_new(Rabbitmq_message* p_msg)
  109. {
  110. std::unique_lock<std::mutex> t_lock(m_lock);
  111. #ifdef CHUTIAN_PROJECT_PROJECT
  112. //通过key来判断消息类型
  113. if ( p_msg->m_routing_key == "measure_1_statu_port" ||
  114. p_msg->m_routing_key == "measure_3_statu_port" ||
  115. p_msg->m_routing_key == "measure_5_statu_port" )
  116. {
  117. m_measure_statu_msg_1 = *p_msg;
  118. if(google::protobuf::TextFormat::ParseFromString(m_measure_statu_msg_1.m_message_buf, &m_measure_info_1))
  119. {
  120. m_dispatch_ground_lidar[0].execute_for_ground_status_msg_new(m_measure_info_1);
  121. }
  122. }
  123. else if ( p_msg->m_routing_key == "measure_2_statu_port" ||
  124. p_msg->m_routing_key == "measure_4_statu_port" ||
  125. p_msg->m_routing_key == "measure_6_statu_port" )
  126. {
  127. // if ( p_msg->m_routing_key == "measure_2_statu_port" )
  128. // {
  129. // std::cout << " huli test :::: " << " nnnnnnnnnnnnnnnnnnnnnhhhhhhhhhhhhhhhhhhhhhhhhhh = " << p_msg->m_message_buf << std::endl;
  130. // }
  131. m_measure_statu_msg_2 = *p_msg;
  132. if(google::protobuf::TextFormat::ParseFromString(m_measure_statu_msg_2.m_message_buf, &m_measure_info_2))
  133. {
  134. m_dispatch_ground_lidar[1].execute_for_ground_status_msg_new(m_measure_info_2);
  135. }
  136. }
  137. else if ( p_msg->m_routing_key == "in_mcpu_1_statu_port" ||
  138. p_msg->m_routing_key == "in_mcpu_3_statu_port" ||
  139. p_msg->m_routing_key == "in_mcpu_5_statu_port" )
  140. {
  141. // std::cout << " huli test :::: " << " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = " << p_msg->m_message_buf << std::endl;
  142. // if ( p_msg->m_routing_key == "in_mcpu_3_statu_port" )
  143. // {
  144. // std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbffffffffffffffffff in_mcpu_3_statu_port= " << p_msg->m_message_buf << std::endl;
  145. //
  146. // }
  147. m_in_mcpu_statu_msg_1 = *p_msg;
  148. if(google::protobuf::TextFormat::ParseFromString(m_in_mcpu_statu_msg_1.m_message_buf, &m_in_mcpu_statu_1))
  149. {
  150. m_dispatch_singlechip[0].execute_for_singlechip_data_msg_new(m_in_mcpu_statu_1, true);
  151. }
  152. }
  153. else if ( p_msg->m_routing_key == "in_mcpu_2_statu_port" ||
  154. p_msg->m_routing_key == "in_mcpu_4_statu_port" ||
  155. p_msg->m_routing_key == "in_mcpu_6_statu_port" )
  156. {
  157. // std::cout << " huli test :::: " << " cccccccccccccccccccccccccccccccccccccccccccccccccc = " << p_msg->m_message_buf << std::endl;
  158. // if ( p_msg->m_routing_key == "in_mcpu_4_statu_port" )
  159. // {
  160. // std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbffffffffffffffffff in_mcpu_4_statu_port= " << p_msg->m_message_buf << std::endl;
  161. // }
  162. m_in_mcpu_statu_msg_2 = *p_msg;
  163. if(google::protobuf::TextFormat::ParseFromString(m_in_mcpu_statu_msg_2.m_message_buf, &m_in_mcpu_statu_2))
  164. {
  165. m_dispatch_singlechip[1].execute_for_singlechip_data_msg_new(m_in_mcpu_statu_2, true);
  166. }
  167. }
  168. else if ( p_msg->m_routing_key == "out_mcpu_1_statu_port" ||
  169. p_msg->m_routing_key == "out_mcpu_3_statu_port" ||
  170. p_msg->m_routing_key == "out_mcpu_5_statu_port" )
  171. {
  172. // if ( p_msg->m_routing_key == "out_mcpu_1_statu_port" )
  173. // {
  174. // std::cout << " huli test :::: " << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv out_mcpu_1_statu_port= " << p_msg->m_message_buf << std::endl;
  175. // }
  176. // std::cout << " huli test :::: " << " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv = " << p_msg->m_message_buf << std::endl;
  177. m_out_mcpu_statu_msg_1 = *p_msg;
  178. if(google::protobuf::TextFormat::ParseFromString(m_out_mcpu_statu_msg_1.m_message_buf, &m_out_mcpu_statu_1))
  179. {
  180. m_dispatch_singlechip[2].execute_for_singlechip_data_msg_new(m_out_mcpu_statu_1, true);
  181. }
  182. }
  183. else if ( p_msg->m_routing_key == "out_mcpu_2_statu_port" ||
  184. p_msg->m_routing_key == "out_mcpu_4_statu_port" ||
  185. p_msg->m_routing_key == "out_mcpu_6_statu_port" )
  186. {
  187. // if ( p_msg->m_routing_key == "out_mcpu_2_statu_port" )
  188. // {
  189. // std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb out_mcpu_2_statu_port= " << p_msg->m_message_buf << std::endl;
  190. // }
  191. // std::cout << " huli test :::: " << " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = " << p_msg->m_message_buf << std::endl;
  192. m_out_mcpu_statu_msg_2 = *p_msg;
  193. if(google::protobuf::TextFormat::ParseFromString(m_out_mcpu_statu_msg_2.m_message_buf, &m_out_mcpu_statu_2))
  194. {
  195. m_dispatch_singlechip[3].execute_for_singlechip_data_msg_new(m_out_mcpu_statu_2, true);
  196. }
  197. }
  198. #endif //#ifdef CHUTIAN_PROJECT_PROJECT
  199. #ifdef SHANGGUJIE_PROJECT_PROJECT
  200. LOG(INFO) << " zzzzzzzzzzzzzzzzzzzzzzzzzzz = "<< p_msg->m_message_buf << " --- " << this;
  201. if ( p_msg->m_routing_key == "park_command_request_port" )
  202. {
  203. park_table t_park_table;
  204. if(google::protobuf::TextFormat::ParseFromString(p_msg->m_message_buf, &t_park_table))
  205. {
  206. //存入数据库
  207. //need
  208. LOG(INFO) << " t_park_table = "<< t_park_table.DebugString() << " --- " << this;
  209. if ( t_park_table.statu().execute_statu() == eNormal )
  210. {
  211. m_dispatch_command.insert_park_command(t_park_table);
  212. }
  213. //else
  214. }
  215. System_communication::get_instance_references().ack_msg(p_msg);
  216. }
  217. else if ( p_msg->m_routing_key == "pick_command_request_port" )
  218. {
  219. pick_table t_pick_table;
  220. if(google::protobuf::TextFormat::ParseFromString(p_msg->m_message_buf, &t_pick_table))
  221. {
  222. //存入数据库
  223. //need
  224. LOG(INFO) << " t_pick_table = "<< t_pick_table.DebugString() << " --- " << this;
  225. if ( t_pick_table.statu().execute_statu() == eNormal )
  226. {
  227. m_dispatch_command.insert_pick_command(t_pick_table);
  228. }
  229. //else
  230. }
  231. System_communication::get_instance_references().ack_msg(p_msg);
  232. }
  233. #endif//SHANGGUJIE_PROJECT_PROJECT
  234. else
  235. {
  236. LOG(INFO) << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx error Rabbitmq_message = "<< p_msg->m_message_buf << " --- " << this;
  237. // park_table t_park_table;
  238. // if(google::protobuf::TextFormat::ParseFromString(p_msg->m_message_buf, &t_park_table))
  239. // {
  240. // std::string t_str = t_park_table.DebugString();
  241. // std::cout << " huli test :::: " << " t_str = " << t_str << std::endl;
  242. //
  243. // std::string t_str1 = t_park_table.statu().statu_description();
  244. // std::cout << " huli test :::: " << " t_str1 = " << t_str1 << std::endl;
  245. //
  246. // std::string t_str2 = t_park_table.car_number();
  247. // std::cout << " huli test :::: " << " t_str2 = " << t_str2 << std::endl;
  248. //
  249. // }
  250. }
  251. return Error_code::SUCCESS;
  252. }
  253. //给dispatch_plc底层提供接口, ack答复服务器,
  254. Error_manager Dispatch_manager::ack_rabbitmq_message_new()
  255. {
  256. if ( m_count_flag )
  257. {
  258. m_count_flag = false;
  259. System_communication::get_instance_references().ack_msg(&m_count_command_signal_msg);
  260. }
  261. if ( m_post_flag )
  262. {
  263. m_post_flag = false;
  264. System_communication::get_instance_references().ack_msg(&m_post_command_msg);
  265. }
  266. m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  267. return Error_code::SUCCESS;
  268. }
  269. //网络通信的对外接口. 对单片机通信
  270. Error_manager Dispatch_manager::execute_network_message_new(Network_message* p_msg)
  271. {
  272. std::unique_lock<std::mutex> t_lock(m_lock);
  273. #ifdef CHUTIAN_PROJECT_PROJECT
  274. //根据自定义的socket_id, 分发给4个单片机模块.
  275. if ( 0 <= p_msg->m_socket_id && p_msg->m_socket_id < 4 )
  276. {
  277. return m_dispatch_singlechip[p_msg->m_socket_id].execute_for_singlechip_data_msg_new(p_msg->m_message_buf);
  278. }
  279. #endif//CHUTIAN_PROJECT_PROJECT
  280. return Error_code::SUCCESS;
  281. }
  282. //对外的接口函数,负责接受并处理任务单,
  283. //Error_manager Dispatch_manager::execute_task(Dispatch_manager::Dispatch_motion_direction dispatch_motion_direction)
  284. //{
  285. // return Error_code::SUCCESS;
  286. //// std::this_thread::sleep_for(std::chrono::seconds(rand()%3+3));
  287. //
  288. // if ( dispatch_motion_direction == E_PICKUP_CAR )
  289. // {
  290. // return Error_code::SUCCESS;
  291. // }
  292. //
  293. //// return Error_code::SUCCESS;
  294. //
  295. //// srand(0);
  296. // unsigned int t_probability = rand();
  297. //
  298. // if ( t_probability%100 >=20 )
  299. // {
  300. // return Error_code::SUCCESS;
  301. // }
  302. // else
  303. // {
  304. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  305. // " Dispatch_manager::execute_task() error ");
  306. // }
  307. //}
  308. //检查能否执行消息指令
  309. Error_manager Dispatch_manager::check_execute_msg(Rabbitmq_message* p_msg)
  310. {
  311. return Error_code::SUCCESS;
  312. /*
  313. Error_manager t_error = Dispatch_manager::get_instance_references().check_status();
  314. if ( t_error == Error_code::SUCCESS )
  315. {
  316. return Error_code::SUCCESS;
  317. }
  318. else if (t_error.get_error_level() == NEGLIGIBLE_ERROR)//一级故障,轻微故障,
  319. {
  320. std::cout << "Dispatch_manager _is_busy , " << std::endl;
  321. //返回繁忙之后, 通信模块1秒后再次调用check
  322. return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  323. }
  324. switch ( p_msg->get_message_type() )
  325. {
  326. case Communication_message::Message_type::eDispatch_request_msg:
  327. {
  328. return Error_code::SUCCESS;
  329. break;
  330. }
  331. case Communication_message::Message_type::eDispatch_plan_response_msg:
  332. {
  333. return Error_code::SUCCESS;
  334. break;
  335. }
  336. case Communication_message::Message_type::eDispatch_control_request_msg:
  337. {
  338. return Error_code::SUCCESS;
  339. break;
  340. }
  341. default :
  342. {
  343. //无效的消息,
  344. return Error_manager(Error_code::INVALID_MESSAGE, Error_level::NEGLIGIBLE_ERROR,
  345. " INVALID_MESSAGE error ");
  346. break;
  347. }
  348. }
  349. */
  350. return Error_code::SUCCESS;
  351. }
  352. //检查状态
  353. Error_manager Dispatch_manager::check_status()
  354. {
  355. if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_READY )
  356. {
  357. return Error_code::SUCCESS;
  358. }
  359. else if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_STORE || m_dispatch_manager_status == E_DISPATCH_MANAGER_PICKUP )
  360. {
  361. return Error_code::SUCCESS;
  362. //无论存取, 状态都是返回正常,运行接受下一个任务
  363. // return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  364. // " Dispatch_manager::check_status() error ");
  365. }
  366. else
  367. {
  368. return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_ERROR, Error_level::MINOR_ERROR,
  369. " Dispatch_manager::check_status() error ");
  370. }
  371. return Error_code::SUCCESS;
  372. }
  373. //调度模块 //执行搬运请求(主控->调度管理)
  374. //Error_manager Dispatch_manager::execute_for_dispatch_request_msg(message::Dispatch_request_msg& dispatch_request_msg)
  375. //{
  376. // LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_request_msg--- "<< dispatch_request_msg.DebugString() << " "<< this;
  377. // LOG(INFO) << " dispatch_request_msg->m_command_key = "<<dispatch_request_msg.command_key()<< " "<< this;
  378. //
  379. // Error_manager t_error;
  380. //
  381. // std::unique_lock<std::mutex> t_lock(m_lock);
  382. // if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  383. // {
  384. // m_dispatch_request_store_list.push_back(dispatch_request_msg);
  385. // m_store_updata_time = std::chrono::system_clock::now();
  386. // }
  387. // else
  388. // {
  389. // m_dispatch_request_pickup_list.push_back(dispatch_request_msg);
  390. // m_pickup_updata_time = std::chrono::system_clock::now();
  391. // }
  392. //
  393. //
  394. //
  395. // //need, 目前直接转发给调度plc执行, 后续要存队列,然后增加调度逻辑.
  396. //
  397. //// m_dispatch_plc.execute_for_dispatch_request_msg(dispatch_request_msg);
  398. //
  399. //
  400. //
  401. // return Error_code::SUCCESS;
  402. //}
  403. ////调度模块 答复数据异常
  404. //Error_manager Dispatch_manager::send_dispatch_response_msg_with_error(message::Dispatch_request_msg &dispatch_request_msg, Error_manager error)
  405. //{
  406. // message::Dispatch_response_msg t_dispatch_response_msg;
  407. // t_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  408. // t_dispatch_response_msg.mutable_base_info()->set_timeout_ms(dispatch_request_msg.base_info().timeout_ms());
  409. // t_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  410. // t_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  411. // t_dispatch_response_msg.set_command_key(dispatch_request_msg.command_key());
  412. //
  413. // t_dispatch_response_msg.mutable_error_manager()->set_error_code(error.get_error_code());
  414. // t_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)error.get_error_level());
  415. // t_dispatch_response_msg.mutable_error_manager()->set_error_description(error.get_error_description());
  416. //
  417. //// System_communication::get_instance_references().encapsulate_msg(t_dispatch_response_msg.SerializeAsString());
  418. // System_communication::get_instance_references().encapsulate_task_msg(t_dispatch_response_msg.SerializeAsString());
  419. // return Error_code::SUCCESS;
  420. //}
  421. //
  422. ////调度模块 //调度总规划的答复(调度算法->调度管理)
  423. //Error_manager Dispatch_manager::execute_for_dispatch_plan_response_msg(message::Dispatch_plan_response_msg &dispatch_plan_response_msg)
  424. //{
  425. // LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_plan_response_msg--- "<< this;
  426. // LOG(INFO) << " dispatch_plan_response_msg->m_command_key = "<<dispatch_plan_response_msg.command_key()<< " "<< this;
  427. //
  428. // Error_manager t_error;
  429. //
  430. // return Error_code::SUCCESS;
  431. //}
  432. ////调度模块 //调度控制的任务请求(调度算法->调度管理)
  433. //Error_manager Dispatch_manager::execute_for_dispatch_control_request_msg(message::Dispatch_control_request_msg &dispatch_control_request_msg)
  434. //{
  435. // LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_control_request_msg--- "<< this;
  436. // LOG(INFO) << " dispatch_control_request_msg->m_command_key = "<<dispatch_control_request_msg.command_key()<< " "<< this;
  437. //
  438. // Error_manager t_error;
  439. //
  440. // return Error_code::SUCCESS;
  441. //}
  442. //定时发送 调度管理的状态
  443. Error_manager Dispatch_manager::encapsulate_send_dispatch_manager_status()
  444. {
  445. // Error_manager t_error;
  446. //
  447. // int t_dispatch_manager_id = get_dispatch_manager_id();
  448. //
  449. // std::string t_msg1;
  450. // //创建一条 调度管理总管理的状态 旧版
  451. // message::Dispatch_terminal_status_msg t_dispatch_terminal_status_msg;
  452. // t_dispatch_terminal_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_status_msg);
  453. // t_dispatch_terminal_status_msg.mutable_base_info()->set_timeout_ms(5000);
  454. // t_dispatch_terminal_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  455. // t_dispatch_terminal_status_msg.mutable_base_info()->set_receiver(message::Communicator::eEmpty);
  456. //
  457. // t_dispatch_terminal_status_msg.mutable_id_struct()->set_unit_id(t_dispatch_manager_id);
  458. // t_dispatch_terminal_status_msg.set_terminal_status(message::Terminal_status(m_dispatch_manager_status));
  459. // t_dispatch_terminal_status_msg.set_passageway_direction(message::Passageway_direction::E_BILATERAL);
  460. //
  461. //
  462. // //E_DISPATCH_POCESS_OVER
  463. // {
  464. // std::unique_lock<std::mutex> t_lock(m_lock);
  465. // for (auto iter = m_dispatch_response_store_map.begin(); iter != m_dispatch_response_store_map.end(); ++iter)
  466. // {
  467. // message::Dispatch_process_information * tp_dispatch_process_information =
  468. // t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
  469. // tp_dispatch_process_information->set_car_numberplate(iter->second.parkspace_info_ex(0).car_info().car_numberplate());
  470. // tp_dispatch_process_information->set_command_key(iter->second.command_key());
  471. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->second.dispatch_motion_direction());
  472. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->second.id_struct());
  473. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->second.locate_information());
  474. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->second.parkspace_info_ex());
  475. // tp_dispatch_process_information->set_car_type(iter->second.car_type());
  476. //
  477. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_OVER);
  478. // }
  479. // for (auto iter = m_dispatch_response_pickup_map.begin(); iter != m_dispatch_response_pickup_map.end(); ++iter)
  480. // {
  481. // message::Dispatch_process_information * tp_dispatch_process_information =
  482. // t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
  483. // tp_dispatch_process_information->set_car_numberplate(iter->second.parkspace_info_ex(0).car_info().car_numberplate());
  484. // tp_dispatch_process_information->set_command_key(iter->second.command_key());
  485. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->second.dispatch_motion_direction());
  486. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->second.id_struct());
  487. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->second.locate_information());
  488. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->second.parkspace_info_ex());
  489. // tp_dispatch_process_information->set_car_type(iter->second.car_type());
  490. //
  491. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_OVER);
  492. // }
  493. // }
  494. //
  495. //
  496. // //E_DISPATCH_POCESS_WORKING
  497. // {
  498. // if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST ||
  499. // m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_WORKING||
  500. // m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_BUSY)
  501. // {
  502. // message::Dispatch_request_msg t_dispatch_request_msg = m_dispatch_plc.get_dispatch_request_msg();
  503. // if ( t_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  504. // {
  505. // message::Dispatch_process_information * tp_dispatch_process_information =
  506. // t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
  507. // tp_dispatch_process_information->set_car_numberplate(t_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate());
  508. // tp_dispatch_process_information->set_command_key(t_dispatch_request_msg.command_key());
  509. // tp_dispatch_process_information->set_dispatch_motion_direction(t_dispatch_request_msg.dispatch_motion_direction());
  510. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(t_dispatch_request_msg.id_struct());
  511. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(t_dispatch_request_msg.locate_information());
  512. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(t_dispatch_request_msg.parkspace_info_ex());
  513. // tp_dispatch_process_information->set_car_type(t_dispatch_request_msg.car_type());
  514. //
  515. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WORKING);
  516. // tp_dispatch_process_information->set_working_total_time(m_dispatch_plc.get_response_working_total_time());
  517. // tp_dispatch_process_information->set_working_remaining_time(m_dispatch_plc.get_response_working_remaining_time());
  518. // }
  519. // else
  520. // {
  521. // message::Dispatch_process_information * tp_dispatch_process_information =
  522. // t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
  523. // tp_dispatch_process_information->set_car_numberplate(t_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate());
  524. // tp_dispatch_process_information->set_command_key(t_dispatch_request_msg.command_key());
  525. // tp_dispatch_process_information->set_dispatch_motion_direction(t_dispatch_request_msg.dispatch_motion_direction());
  526. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(t_dispatch_request_msg.id_struct());
  527. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(t_dispatch_request_msg.locate_information());
  528. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(t_dispatch_request_msg.parkspace_info_ex());
  529. // tp_dispatch_process_information->set_car_type(t_dispatch_request_msg.car_type());
  530. //
  531. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WORKING);
  532. // tp_dispatch_process_information->set_working_total_time(m_dispatch_plc.get_response_working_total_time());
  533. // tp_dispatch_process_information->set_working_remaining_time(m_dispatch_plc.get_response_working_remaining_time());
  534. // }
  535. // }
  536. // }
  537. //
  538. // //E_DISPATCH_POCESS_WAIT
  539. // {
  540. // std::unique_lock<std::mutex> t_lock(m_lock);
  541. // int t_store_wait_number = 1;
  542. // for (auto iter = m_dispatch_request_store_list.begin(); iter != m_dispatch_request_store_list.end(); ++iter)
  543. // {
  544. // message::Dispatch_process_information * tp_dispatch_process_information =
  545. // t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
  546. // tp_dispatch_process_information->set_car_numberplate(iter->parkspace_info_ex(0).car_info().car_numberplate());
  547. // tp_dispatch_process_information->set_command_key(iter->command_key());
  548. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->dispatch_motion_direction());
  549. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->id_struct());
  550. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->locate_information());
  551. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->parkspace_info_ex());
  552. // tp_dispatch_process_information->set_car_type(iter->car_type());
  553. //
  554. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WAIT);
  555. // tp_dispatch_process_information->set_wait_number(t_store_wait_number);
  556. // t_store_wait_number++;
  557. // }
  558. // int t_pickup_wait_number = 1;
  559. // for (auto iter = m_dispatch_request_pickup_list.begin(); iter != m_dispatch_request_pickup_list.end(); ++iter)
  560. // {
  561. // message::Dispatch_process_information * tp_dispatch_process_information =
  562. // t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
  563. // tp_dispatch_process_information->set_car_numberplate(iter->parkspace_info_ex(0).car_info().car_numberplate());
  564. // tp_dispatch_process_information->set_command_key(iter->command_key());
  565. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->dispatch_motion_direction());
  566. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->id_struct());
  567. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->locate_information());
  568. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->parkspace_info_ex());
  569. // tp_dispatch_process_information->set_car_type(iter->car_type());
  570. //
  571. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WAIT);
  572. // tp_dispatch_process_information->set_wait_number(t_pickup_wait_number);
  573. // t_pickup_wait_number++;
  574. // }
  575. // }
  576. //
  577. // std::string t_store_updata_time = Time_tool::get_instance_references().get_time_string_millisecond(m_store_updata_time);
  578. // std::string t_pickup_updata_time = Time_tool::get_instance_references().get_time_string_millisecond(m_pickup_updata_time);
  579. //
  580. // t_dispatch_terminal_status_msg.set_dispatch_store_process_information_updata_time(t_store_updata_time);
  581. // t_dispatch_terminal_status_msg.set_dispatch_pickup_process_information_updata_time(t_pickup_updata_time);
  582. //
  583. //
  584. // t_msg1 = t_dispatch_terminal_status_msg.SerializeAsString();
  585. // System_communication::get_instance_references().encapsulate_status_msg(t_msg1);
  586. //
  587. return Error_code::SUCCESS;
  588. }
  589. //定时发送 调度管理的状态
  590. Error_manager Dispatch_manager::encapsulate_send_dispatch_manager_status_new()
  591. {
  592. std::unique_lock<std::mutex> t_lock(m_lock);
  593. dispatch_node_statu t_dispatch_node_statu_msg;
  594. if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_READY )
  595. {
  596. t_dispatch_node_statu_msg.set_statu(eIdle);
  597. // t_dispatch_node_statu_msg.set_statu(eBusy);
  598. }
  599. else if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_UNKNOW ||
  600. m_dispatch_manager_status == E_DISPATCH_MANAGER_DISCONNECT ||
  601. m_dispatch_manager_status == E_DISPATCH_MANAGER_FAULT )
  602. {
  603. t_dispatch_node_statu_msg.set_statu(eFault);
  604. }
  605. else
  606. {
  607. t_dispatch_node_statu_msg.set_statu(eBusy);
  608. }
  609. t_dispatch_node_statu_msg.set_idle_stop_floor(m_device_floor);
  610. if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE ||
  611. m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REALLOCATE)
  612. {
  613. t_dispatch_node_statu_msg.mutable_running_pack_info()->CopyFrom(m_park_table_msg);
  614. }
  615. else if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP ||
  616. m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REVOCATION )
  617. {
  618. t_dispatch_node_statu_msg.mutable_running_pick_info()->CopyFrom(m_pick_table_msg);
  619. }
  620. else
  621. {
  622. }
  623. t_dispatch_node_statu_msg.set_unit_id(m_dispatch_manager_id);
  624. t_dispatch_node_statu_msg.set_plc_heartbeat(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_plc_heartbeat);
  625. t_dispatch_node_statu_msg.set_plc_status_info(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_plc_status_info);
  626. t_dispatch_node_statu_msg.set_plc_carrier_status(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_plc_carrier_status);
  627. // t_dispatch_node_statu_msg.set_plc_carrier_status(3);
  628. t_dispatch_node_statu_msg.set_plc_inlet_1_status(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_plc_status_info & 0x10);
  629. t_dispatch_node_statu_msg.set_plc_inlet_2_status(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_plc_status_info & 0x20);
  630. t_dispatch_node_statu_msg.set_plc_outlet_3_status(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_plc_status_info & 0x40);
  631. t_dispatch_node_statu_msg.set_plc_outlet_4_status(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_plc_status_info & 0x80);
  632. for (int i = 0; i < 4; ++i)
  633. {
  634. dispatch_plc_passway_status * p_dispatch_plc_passway_status = t_dispatch_node_statu_msg.mutable_dispatch_plc_passway_status_vector()->Add();
  635. p_dispatch_plc_passway_status->set_car_height(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[i].m_car_height);
  636. p_dispatch_plc_passway_status->set_outside_door_status(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[i].m_outside_door_status);
  637. p_dispatch_plc_passway_status->set_inside_door_status(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[i].m_inside_door_status);
  638. p_dispatch_plc_passway_status->set_comb_body_status(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[i].m_comb_body_status);
  639. p_dispatch_plc_passway_status->set_sensor_1(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[i].m_sensor_1);
  640. p_dispatch_plc_passway_status->set_sensor_2(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[i].m_sensor_2);
  641. p_dispatch_plc_passway_status->set_turnplate_angle_min(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[i].m_turnplate_angle_min);
  642. p_dispatch_plc_passway_status->set_turnplate_angle_max(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_passway_vector[i].m_turnplate_angle_max);
  643. p_dispatch_plc_passway_status->set_plc_passway_enable(m_dispatch_plc.m_dispatch_plc_status_from_plc_to_manager.m_plc_status_info & (0x10<<i) );
  644. // p_dispatch_plc_passway_status->set_plc_passway_enable(1 );
  645. }
  646. std::string t_msg = t_dispatch_node_statu_msg.DebugString();
  647. // std::cout << " huli test :::: " << " +++++++++++++++++++++++++++++++++++++++++++ = " << 11233 << std::endl;
  648. // std::cout << " huli test t_dispatch_node_statu_msg:::: " << " t_msg.size() = " << t_msg.size() << std::endl;
  649. // std::cout << " huli test t_dispatch_node_statu_msg:::: " << " t_msg = " << t_msg << std::endl;
  650. // std::cout << " huli test :::: " << " ----------------------------------------- = " << 44556 << std::endl;
  651. System_communication::get_instance_references().encapsulate_status_msg(t_msg);
  652. return Error_code::SUCCESS;
  653. }
  654. //在流程的map 里面释放指定的流程
  655. Error_manager Dispatch_manager::release_dispatch_process(std::string command_key)
  656. {
  657. std::unique_lock<std::mutex> t_lock(m_lock);
  658. return Error_code::SUCCESS;
  659. }
  660. //调度模块 ///地面雷达的状态消息(地面雷达->null)
  661. //Error_manager Dispatch_manager::execute_for_ground_status_msg(message::Ground_status_msg ground_status_msg)
  662. //{
  663. // int t_inlet_id = ground_status_msg.mutable_id_struct()->terminal_id() %2;
  664. // return m_dispatch_ground_lidar[t_inlet_id].execute_for_ground_status_msg(ground_status_msg);
  665. //
  666. // return Error_code::SUCCESS;
  667. //}
  668. //调度模块 ///单片机的状态消息
  669. //Error_manager Dispatch_manager::execute_for_singlechip_data_msg(message::Singlechip_data singlechip_data_msg, bool validity)
  670. //{
  671. // if ( singlechip_data_msg.has_terminalid() && singlechip_data_msg.has_dispatchdirection() )
  672. // {
  673. // if ( singlechip_data_msg.terminalid() >=0 &&
  674. // singlechip_data_msg.terminalid() <=5 &&
  675. // singlechip_data_msg.dispatchdirection() >=1 &&
  676. // singlechip_data_msg.dispatchdirection() <=2 )
  677. // {
  678. // int t_temp = (singlechip_data_msg.terminalid() %2) + (singlechip_data_msg.dispatchdirection()-1)*2;
  679. // return m_dispatch_singlechip[t_temp].execute_for_singlechip_data_msg(singlechip_data_msg, validity);
  680. // }
  681. // }
  682. //
  683. // return Error_code::SUCCESS;
  684. //}
  685. //获取空闲的出口,返回0表示没有空闲出口, 返回1~6表示对应的出口
  686. int Dispatch_manager::get_outlet_for_ready()
  687. {
  688. // std::cout << " huli test 111:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  689. #ifdef CHUTIAN_PROJECT_PROJECT
  690. //A单元优先分配A2, B和C正常分配
  691. if ( m_dispatch_manager_id == 0 )
  692. {
  693. //检查出口, 并删除取车完成 //判断2个出口是否空闲
  694. if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  695. {
  696. std::cout << " huli test 444:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  697. if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+2) == Error_code::SUCCESS )
  698. {
  699. std::cout << " huli test 555:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  700. return m_dispatch_manager_id*2+2;
  701. }
  702. }
  703. else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
  704. {
  705. std::cout << " huli test 222:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  706. if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+1) == Error_code::SUCCESS )
  707. {
  708. std::cout << " huli test 333:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  709. return m_dispatch_manager_id*2+1;
  710. }
  711. }
  712. else
  713. {
  714. return 0;
  715. }
  716. }
  717. else
  718. {
  719. //检查出口, 并删除取车完成 //判断2个出口是否空闲
  720. if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
  721. {
  722. std::cout << " huli test 222:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  723. if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+1) == Error_code::SUCCESS )
  724. {
  725. std::cout << " huli test 333:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  726. return m_dispatch_manager_id*2+1;
  727. }
  728. }
  729. else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  730. {
  731. std::cout << " huli test 444:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  732. if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+2) == Error_code::SUCCESS )
  733. {
  734. std::cout << " huli test 555:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  735. return m_dispatch_manager_id*2+2;
  736. }
  737. }
  738. else
  739. {
  740. return 0;
  741. }
  742. }
  743. #endif //CHUTIAN_PROJECT_PROJECT
  744. #ifdef SHANGGUJIE_PROJECT_PROJECT
  745. return 3;
  746. #endif//SHANGGUJIE_PROJECT_PROJECT
  747. return 0;
  748. }
  749. Dispatch_manager::Dispatch_manager_status Dispatch_manager::get_dispatch_manager_status()
  750. {
  751. return m_dispatch_manager_status;
  752. }
  753. int Dispatch_manager::get_dispatch_manager_id()
  754. {
  755. return m_dispatch_manager_id;
  756. }
  757. void Dispatch_manager::set_dispatch_manager_id(int dispatch_manager_id)
  758. {
  759. m_dispatch_manager_id = dispatch_manager_id;
  760. }
  761. //资源分配
  762. //void Dispatch_manager::resource_allocation()
  763. //{
  764. // return;
  765. //
  766. // LOG(INFO) << " Dispatch_manager::resource_allocation() start " << this;
  767. // Error_manager t_error;
  768. //
  769. //
  770. // while (m_dispatch_manager_condition.is_alive())
  771. // {
  772. // m_dispatch_manager_condition.wait();
  773. // if ( m_dispatch_manager_condition.is_alive() )
  774. // {
  775. // std::this_thread::sleep_for(std::chrono::microseconds(1));
  776. // std::this_thread::sleep_for(std::chrono::milliseconds(100));
  777. //// std::this_thread::sleep_for(std::chrono::seconds(1));
  778. // std::this_thread::yield();
  779. // std::unique_lock<std::mutex> t_lock(m_lock);
  780. //
  781. // //排序和资源分配
  782. // if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  783. // {
  784. // //补全 id_struct
  785. // if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP)
  786. // {
  787. // //判断2个出口是否空闲, 在资源分配时,提前分配好出口终端号
  788. // if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
  789. // {
  790. // auto iter = m_dispatch_request_pickup_list.begin();
  791. // if ( iter != m_dispatch_request_pickup_list.end() )
  792. // {
  793. // iter->mutable_id_struct()->set_terminal_id(m_dispatch_manager_id*2);
  794. // m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  795. // m_dispatch_manager_status = E_DISPATCH_MANAGER_PICKUP;
  796. // m_dispatch_request_pickup_list.erase(iter);
  797. // m_pickup_updata_time = std::chrono::system_clock::now();
  798. // }
  799. // }
  800. // else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  801. // {
  802. // auto iter = m_dispatch_request_pickup_list.begin();
  803. // if ( iter != m_dispatch_request_pickup_list.end() )
  804. // {
  805. // iter->mutable_id_struct()->set_terminal_id(m_dispatch_manager_id*2+1);
  806. // m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  807. // m_dispatch_manager_status = E_DISPATCH_MANAGER_PICKUP;
  808. // m_dispatch_request_pickup_list.erase(iter);
  809. // m_pickup_updata_time = std::chrono::system_clock::now();
  810. // }
  811. // }
  812. // //else什么也不做,直接切换到存车就行
  813. //
  814. // m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  815. // }
  816. // else if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE )
  817. // {
  818. // auto iter = m_dispatch_request_store_list.begin();
  819. // if ( iter != m_dispatch_request_store_list.end() )
  820. // {
  821. // iter->mutable_id_struct()->set_unit_id(m_dispatch_manager_id );
  822. // m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  823. // m_dispatch_manager_status = E_DISPATCH_MANAGER_STORE;
  824. // m_dispatch_request_store_list.erase(iter);
  825. // m_store_updata_time = std::chrono::system_clock::now();
  826. // }
  827. // m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  828. // }
  829. // else
  830. // {
  831. // ///////
  832. // }
  833. // }
  834. // else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY)
  835. // {
  836. // m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  837. // }
  838. // else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_BUSY)
  839. // {
  840. // m_dispatch_manager_status = E_DISPATCH_MANAGER_BUSY;
  841. // }
  842. // else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT)
  843. // {
  844. // m_dispatch_manager_status = E_DISPATCH_MANAGER_DISCONNECT;
  845. // }
  846. // else
  847. // {
  848. // m_dispatch_manager_status = E_DISPATCH_MANAGER_FAULT;
  849. // }
  850. //
  851. //
  852. // //检查时间, 并删除存车完成
  853. // for (auto iter = m_dispatch_response_store_map.begin(); iter != m_dispatch_response_store_map.end();)
  854. // {
  855. // if ( std::chrono::system_clock::now() - iter->first > std::chrono::seconds(10) )
  856. // {
  857. // iter = m_dispatch_response_store_map.erase(iter);
  858. // m_store_updata_time = std::chrono::system_clock::now();
  859. // }
  860. // else
  861. // {
  862. // ++iter;
  863. // }
  864. // }
  865. //
  866. // //检查出口, 并删除取车完成 //判断2个出口是否空闲
  867. // if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
  868. // {
  869. // if ( m_dispatch_response_pickup_map.find(m_dispatch_manager_id*2) != m_dispatch_response_pickup_map.end() )
  870. // {
  871. // m_dispatch_response_pickup_map.erase(m_dispatch_manager_id*2);
  872. // m_pickup_updata_time = std::chrono::system_clock::now();
  873. // }
  874. // }
  875. // else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  876. // {
  877. // if ( m_dispatch_response_pickup_map.find(m_dispatch_manager_id*2+1) != m_dispatch_response_pickup_map.end() )
  878. // {
  879. // m_dispatch_response_pickup_map.erase(m_dispatch_manager_id*2+1);
  880. // m_pickup_updata_time = std::chrono::system_clock::now();
  881. // }
  882. // }
  883. //
  884. // }
  885. // }
  886. //
  887. // LOG(INFO) << " Dispatch_manager::resource_allocation() end "<< this;
  888. //
  889. // return;
  890. //}
  891. //
  892. ////新版流程控制
  893. //void Dispatch_manager::process_control()
  894. //{
  895. // LOG(INFO) << " Dispatch_manager::process_control() start " << this;
  896. // Error_manager t_error;
  897. //
  898. // while (m_dispatch_manager_condition.is_alive())
  899. // {
  900. // m_dispatch_manager_condition.wait();
  901. // if (m_dispatch_manager_condition.is_alive())
  902. // {
  903. // std::this_thread::sleep_for(std::chrono::microseconds(1));
  904. // std::this_thread::sleep_for(std::chrono::milliseconds(1));
  905. //// std::this_thread::sleep_for(std::chrono::seconds(1));
  906. // std::this_thread::yield();
  907. // std::unique_lock<std::mutex> t_lock(m_lock);
  908. //
  909. // static int t_count = 0;
  910. // if (t_count % 1000 == 0 )
  911. // {
  912. // std::cout << " huli test :::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  913. // std::cout << " huli test :::: " << " m_dispatch_manager_status = " << m_dispatch_manager_status << std::endl;
  914. // }
  915. // t_count++;
  916. //
  917. // switch ( m_dispatch_manager_status )
  918. // {
  919. // case E_DISPATCH_MANAGER_READY:
  920. // {
  921. // //等待 count_command_signal_1_port 的消息
  922. // if ( m_count_flag )
  923. // {
  924. // m_dispatch_manager_status = E_DISPATCH_MANAGER_COUNT_NEW;
  925. // }
  926. // if ( m_post_flag )
  927. // {
  928. // m_dispatch_manager_status = E_DISPATCH_MANAGER_POST_NEW;
  929. // }
  930. // break;
  931. // }
  932. // case E_DISPATCH_MANAGER_COUNT_NEW:
  933. // {
  934. // //目前不用解析 m_count_command_signal_msg ,只要收到 count_command_signal_1_port, 直接进行下一步
  935. // //发送调度任务申请
  936. // char t_buf[256] = {0};
  937. // sprintf(t_buf, "%d-%d", m_dispatch_manager_id+1, m_device_floor);
  938. // std::string t_string(t_buf);
  939. // System_communication::get_instance_references().encapsulate_task_msg(t_string, 0);
  940. // std::cout << " huli test :::: " << " m_request_command_msg = " << t_string << std::endl;
  941. // m_dispatch_manager_status = E_DISPATCH_MANAGER_REQUEST_NEW;
  942. // break;
  943. // }
  944. // case E_DISPATCH_MANAGER_REQUEST_NEW:
  945. // {
  946. // //等待 post_command_1_port 的消息
  947. // if ( m_post_flag )
  948. // {
  949. // m_dispatch_manager_status = E_DISPATCH_MANAGER_POST_NEW;
  950. // }
  951. // break;
  952. // }
  953. // case E_DISPATCH_MANAGER_POST_NEW:
  954. // {
  955. // //解析 m_post_command_msg
  956. // std::string t_string = m_post_command_msg.m_message_buf;
  957. // //检索字符串的内容,
  958. // if ( t_string.find("entrance_measure_info") != std::string::npos )
  959. // {
  960. // //entrance_measure_info 是 park_table 特有的
  961. // if(! google::protobuf::TextFormat::ParseFromString(t_string, &m_park_table_msg))
  962. // {
  963. // //答复错误
  964. // }
  965. // else
  966. // {
  967. //// LOG(INFO) << " m_park_table_msg = "<< m_park_table_msg.DebugString() << " --- " << this;
  968. // m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
  969. //
  970. // //根据雷达x坐标, 计算入口id
  971. // int t_terminal = 0;
  972. // if (m_park_table_msg.entrance_measure_info().cx()<0)
  973. // {
  974. // t_terminal = m_dispatch_manager_id*2+1;
  975. // }
  976. // else if(m_park_table_msg.entrance_measure_info().cx()>0)
  977. // {
  978. // t_terminal = m_dispatch_manager_id*2+2;
  979. // }
  980. // m_park_table_msg.set_terminal_id(t_terminal);
  981. // m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
  982. // m_device_floor = m_park_table_msg.allocated_space_info().floor();
  983. // m_dispatch_manager_status = E_DISPATCH_MANAGER_COMPLETED_NEW;
  984. // }
  985. // }
  986. // else
  987. // {
  988. // if ( get_outlet_for_ready() !=0 )
  989. // {
  990. // if(! google::protobuf::TextFormat::ParseFromString(t_string, &m_pick_table_msg))
  991. // {
  992. // //答复错误
  993. // }
  994. // else
  995. // {
  996. // m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
  997. // m_pick_table_msg.set_terminal_id(get_outlet_for_ready());
  998. // m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg, m_dispatch_process_type, m_car_type);
  999. // m_device_floor = 1;
  1000. // m_dispatch_manager_status = E_DISPATCH_MANAGER_COMPLETED_NEW;
  1001. // }
  1002. // }
  1003. // //else 没有空闲出口,就无限等待
  1004. // }
  1005. // break;
  1006. // }
  1007. // case E_DISPATCH_MANAGER_COMPLETED_NEW:
  1008. // {
  1009. // //等待dispatch_plc执行,然后ack里面切回ready
  1010. // break;
  1011. // }
  1012. // default:
  1013. // {
  1014. // std::cout << " huli test :::: " << " Dispatch_manager::process_control() error, m_dispatch_manager_status = "
  1015. // << m_dispatch_manager_status << std::endl;
  1016. // break;
  1017. // }
  1018. // }
  1019. // }
  1020. // }
  1021. // LOG(INFO) << " Dispatch_manager::process_control() end "<< this;
  1022. // return;
  1023. //}
  1024. #ifdef CHUTIAN_PROJECT_PROJECT
  1025. //新版流程控制
  1026. void Dispatch_manager::process_sql()
  1027. {
  1028. LOG(INFO) << " Dispatch_manager::process_sql() start " << this;
  1029. Error_manager t_error;
  1030. while (m_dispatch_manager_condition.is_alive())
  1031. {
  1032. m_dispatch_manager_condition.wait();
  1033. if (m_dispatch_manager_condition.is_alive())
  1034. {
  1035. std::this_thread::sleep_for(std::chrono::microseconds(1));
  1036. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  1037. // std::this_thread::sleep_for(std::chrono::seconds(1));
  1038. std::this_thread::yield();
  1039. std::unique_lock<std::mutex> t_lock(m_lock);
  1040. static auto t_update_time = std::chrono::system_clock::now();
  1041. if(std::chrono::system_clock::now() - t_update_time > std::chrono::seconds(1))
  1042. {
  1043. t_update_time = std::chrono::system_clock::now();
  1044. std::cout << " huli test :::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  1045. std::cout << " huli test :::: " << " m_dispatch_manager_status = " << m_dispatch_manager_status << std::endl;
  1046. std::cout << " huli test :::: " << " xxxxxxxxx get_outlet_for_ready() = " << get_outlet_for_ready() << std::endl;
  1047. }
  1048. switch ( m_dispatch_manager_status )
  1049. {
  1050. case E_DISPATCH_MANAGER_READY:
  1051. case E_DISPATCH_MANAGER_REQUEST:
  1052. {
  1053. //循环请求, 请求周期1秒
  1054. // std::this_thread::sleep_for(std::chrono::seconds(1));
  1055. if ( std::chrono::system_clock::now() - m_request_updata_time > std::chrono::seconds(1) )
  1056. {
  1057. m_request_updata_time = std::chrono::system_clock::now();
  1058. //调度开始前, 向数据库发送请求的相关操作, 输入 穿梭机所在的楼层, 调度id 0~2, 空闲出口id, 如果不是0,就表示有空闲的出口
  1059. t_error = m_dispatch_command.dispatch_request_to_sql(m_device_floor, m_dispatch_manager_id, get_outlet_for_ready());
  1060. //std::cout << " huli test :::: " << " sssssssssssssssssssssssssssssssssss dispatch_request_to_sql = " << t_error << std::endl;
  1061. if ( t_error == Error_code::SUCCESS )
  1062. {
  1063. //如果找到了合适的指令, 那么交给plc来执行
  1064. if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  1065. {
  1066. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
  1067. m_park_table_msg = m_dispatch_command.m_park_table_msg;
  1068. m_car_type = m_dispatch_command.m_car_type;
  1069. m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
  1070. m_device_floor = m_park_table_msg.allocated_space_info().floor();
  1071. }
  1072. else if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  1073. {
  1074. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
  1075. m_pick_table_msg = m_dispatch_command.m_pick_table_msg;
  1076. m_car_type = m_dispatch_command.m_car_type;
  1077. m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg, m_dispatch_process_type, m_car_type);
  1078. m_device_floor = 1;
  1079. }
  1080. //切换到工作状态
  1081. m_dispatch_manager_status = E_DISPATCH_MANAGER_WORKING;
  1082. }
  1083. else if(t_error != Error_code::NODATA)
  1084. {
  1085. LOG(INFO) << " dispatch_request_to_sql error = " << t_error << this;
  1086. }
  1087. //else NODATA 无限循环
  1088. }
  1089. //else 等待
  1090. break;
  1091. }
  1092. case E_DISPATCH_MANAGER_WORKING:
  1093. {
  1094. //等待dispatch_plc执行, 完成后切换到 E_DISPATCH_MANAGER_RESPONSE
  1095. if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::DISPATCH_PLC_READY ||
  1096. m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::DISPATCH_PLC_FAULT)
  1097. {
  1098. if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_MIN_CAR )
  1099. {
  1100. //切换到 重新分配
  1101. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1102. m_car_type = Common_data::Car_type::MIN_CAR;
  1103. }
  1104. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_MID_CAR )
  1105. {
  1106. //切换到 重新分配
  1107. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1108. m_car_type = Common_data::Car_type::MID_CAR;
  1109. }
  1110. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_BIG_CAR )
  1111. {
  1112. //切换到 重新分配
  1113. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1114. m_car_type = Common_data::Car_type::BIG_CAR;
  1115. }
  1116. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_HUGE_CAR )
  1117. {
  1118. //切换到 重新分配
  1119. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1120. m_car_type = Common_data::Car_type::HUGE_CAR;
  1121. }
  1122. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_FAULT_CAR )
  1123. {
  1124. //切换到 重新分配
  1125. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1126. m_car_type = Common_data::Car_type::FAULT_CAR;
  1127. }
  1128. else
  1129. {
  1130. //切换到答复状态
  1131. m_dispatch_manager_status = E_DISPATCH_MANAGER_RESPONSE;
  1132. }
  1133. }
  1134. //else 无限等待
  1135. break;
  1136. }
  1137. case E_DISPATCH_MANAGER_REALLOCATE:
  1138. {
  1139. //调度 , 向数据库 重新分配车位,
  1140. t_error = m_dispatch_command.dispatch_reallocate_to_sql(m_car_type, get_outlet_for_ready());
  1141. if ( t_error == Error_code::SUCCESS )
  1142. {
  1143. //如果找到了合适的车位, 那么交给plc来执行
  1144. if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  1145. {
  1146. m_dispatch_process_type = m_dispatch_command.m_dispatch_process_type;
  1147. m_park_table_msg = m_dispatch_command.m_park_table_msg;
  1148. m_car_type = m_dispatch_command.m_car_type;
  1149. m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
  1150. m_device_floor = m_park_table_msg.allocated_space_info().floor();
  1151. }
  1152. //没有找到合适的车位, 那么放到出口
  1153. else if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  1154. {
  1155. m_dispatch_process_type = m_dispatch_command.m_dispatch_process_type;
  1156. m_pick_table_msg = m_dispatch_command.m_pick_table_msg;
  1157. m_car_type = m_dispatch_command.m_car_type;
  1158. m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg,m_dispatch_process_type, m_car_type);
  1159. m_device_floor = 1;
  1160. }
  1161. //切换到工作状态
  1162. m_dispatch_manager_status = E_DISPATCH_MANAGER_WORKING;
  1163. }
  1164. else if(t_error != Error_code::NODATA)
  1165. {
  1166. LOG(INFO) << " dispatch_request_to_sql error = " << t_error << this;
  1167. }
  1168. //else NODATA 无限循环
  1169. break;
  1170. break;
  1171. }
  1172. case E_DISPATCH_MANAGER_RESPONSE:
  1173. {
  1174. //调度完成后, 向数据库发送答复的相关操作
  1175. t_error = m_dispatch_command.dispatch_response_to_sql(m_dispatch_plc.get_result());
  1176. if(t_error != Error_code::SUCCESS)
  1177. {
  1178. LOG(INFO) << " dispatch_response_to_sql error = " << t_error << this;
  1179. }
  1180. //切换到ready状态
  1181. m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  1182. break;
  1183. }
  1184. default:
  1185. {
  1186. std::cout << " huli test :::: " << " Dispatch_manager::process_control() error, m_dispatch_manager_status = "
  1187. << m_dispatch_manager_status << std::endl;
  1188. break;
  1189. }
  1190. }
  1191. }
  1192. }
  1193. LOG(INFO) << " Dispatch_manager::process_sql() end "<< this;
  1194. return;
  1195. }
  1196. #endif//CHUTIAN_PROJECT_PROJECT
  1197. #ifdef SHANGGUJIE_PROJECT_PROJECT
  1198. //新版流程控制
  1199. void Dispatch_manager::process_sql_shanggujie()
  1200. {
  1201. LOG(INFO) << " Dispatch_manager::process_sql() start " << this;
  1202. Error_manager t_error;
  1203. while (m_dispatch_manager_condition.is_alive())
  1204. {
  1205. m_dispatch_manager_condition.wait();
  1206. if (m_dispatch_manager_condition.is_alive())
  1207. {
  1208. std::this_thread::sleep_for(std::chrono::microseconds(1));
  1209. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  1210. // std::this_thread::sleep_for(std::chrono::seconds(1));
  1211. std::this_thread::yield();
  1212. std::unique_lock<std::mutex> t_lock(m_lock);
  1213. static auto t_update_time = std::chrono::system_clock::now();
  1214. if(std::chrono::system_clock::now() - t_update_time > std::chrono::seconds(1))
  1215. {
  1216. t_update_time = std::chrono::system_clock::now();
  1217. std::cout << " huli test :::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  1218. std::cout << " huli test :::: " << " m_dispatch_manager_status = " << m_dispatch_manager_status << std::endl;
  1219. std::cout << " huli test :::: " << " xxxxxxxxx get_outlet_for_ready() = " << get_outlet_for_ready() << std::endl;
  1220. }
  1221. //huli need
  1222. // continue;
  1223. switch ( m_dispatch_manager_status )
  1224. {
  1225. case E_DISPATCH_MANAGER_READY:
  1226. case E_DISPATCH_MANAGER_REQUEST:
  1227. {
  1228. //循环请求, 请求周期1秒
  1229. // std::this_thread::sleep_for(std::chrono::seconds(1));
  1230. if ( std::chrono::system_clock::now() - m_request_updata_time > std::chrono::seconds(1) )
  1231. {
  1232. m_request_updata_time = std::chrono::system_clock::now();
  1233. //调度开始前, 向数据库发送请求的相关操作, 输入 穿梭机所在的楼层, 调度id 0~2, 空闲出口id, 如果不是0,就表示有空闲的出口
  1234. t_error = m_dispatch_command.dispatch_request_to_sql(m_device_floor, m_dispatch_manager_id, get_outlet_for_ready());
  1235. // std::cout << " huli test :::: " << " sssssssssssssssssssssssssssssssssss dispatch_request_to_sql = " << t_error << std::endl;
  1236. if ( t_error == Error_code::SUCCESS )
  1237. {
  1238. //如果找到了合适的指令, 那么交给plc来执行
  1239. if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  1240. {
  1241. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
  1242. m_park_table_msg = m_dispatch_command.m_park_table_msg;
  1243. m_car_type = m_dispatch_command.m_car_type;
  1244. m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
  1245. m_device_floor = m_park_table_msg.allocated_space_info().floor();
  1246. }
  1247. else if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  1248. {
  1249. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
  1250. m_pick_table_msg = m_dispatch_command.m_pick_table_msg;
  1251. m_car_type = m_dispatch_command.m_car_type;
  1252. m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg, m_dispatch_process_type, m_car_type);
  1253. m_device_floor = 1;
  1254. }
  1255. //切换到工作状态
  1256. m_dispatch_manager_status = E_DISPATCH_MANAGER_WORKING;
  1257. }
  1258. else if(t_error != Error_code::NODATA)
  1259. {
  1260. LOG(INFO) << " dispatch_request_to_sql error = " << t_error << this;
  1261. }
  1262. //else NODATA 无限循环
  1263. }
  1264. //else 等待
  1265. break;
  1266. }
  1267. case E_DISPATCH_MANAGER_WORKING:
  1268. {
  1269. //等待dispatch_plc执行, 完成后切换到 E_DISPATCH_MANAGER_RESPONSE
  1270. if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::DISPATCH_PLC_READY ||
  1271. m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::DISPATCH_PLC_FAULT)
  1272. {
  1273. if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_MIN_CAR )
  1274. {
  1275. //切换到 重新分配
  1276. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1277. m_car_type = Common_data::Car_type::MIN_CAR;
  1278. }
  1279. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_MID_CAR )
  1280. {
  1281. //切换到 重新分配
  1282. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1283. m_car_type = Common_data::Car_type::MID_CAR;
  1284. }
  1285. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_BIG_CAR )
  1286. {
  1287. //切换到 重新分配
  1288. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1289. m_car_type = Common_data::Car_type::BIG_CAR;
  1290. }
  1291. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_HUGE_CAR )
  1292. {
  1293. //切换到 重新分配
  1294. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1295. m_car_type = Common_data::Car_type::HUGE_CAR;
  1296. }
  1297. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_FAULT_CAR )
  1298. {
  1299. //切换到 重新分配
  1300. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1301. m_car_type = Common_data::Car_type::FAULT_CAR;
  1302. }
  1303. else
  1304. {
  1305. //切换到答复状态
  1306. m_dispatch_manager_status = E_DISPATCH_MANAGER_RESPONSE;
  1307. }
  1308. }
  1309. //else 无限等待
  1310. break;
  1311. }
  1312. case E_DISPATCH_MANAGER_REALLOCATE:
  1313. {
  1314. //调度 , 向数据库 重新分配车位,
  1315. t_error = m_dispatch_command.dispatch_reallocate_to_sql(m_car_type, get_outlet_for_ready());
  1316. if ( t_error == Error_code::SUCCESS )
  1317. {
  1318. //如果找到了合适的车位, 那么交给plc来执行
  1319. if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  1320. {
  1321. m_dispatch_process_type = m_dispatch_command.m_dispatch_process_type;
  1322. m_park_table_msg = m_dispatch_command.m_park_table_msg;
  1323. m_car_type = m_dispatch_command.m_car_type;
  1324. m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
  1325. m_device_floor = m_park_table_msg.allocated_space_info().floor();
  1326. }
  1327. //没有找到合适的车位, 那么放到出口
  1328. else if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  1329. {
  1330. m_dispatch_process_type = m_dispatch_command.m_dispatch_process_type;
  1331. m_pick_table_msg = m_dispatch_command.m_pick_table_msg;
  1332. m_car_type = m_dispatch_command.m_car_type;
  1333. m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg,m_dispatch_process_type, m_car_type);
  1334. m_device_floor = 1;
  1335. }
  1336. //切换到工作状态
  1337. m_dispatch_manager_status = E_DISPATCH_MANAGER_WORKING;
  1338. }
  1339. else if(t_error != Error_code::NODATA)
  1340. {
  1341. LOG(INFO) << " dispatch_request_to_sql error = " << t_error << this;
  1342. }
  1343. //else NODATA 无限循环
  1344. break;
  1345. break;
  1346. }
  1347. case E_DISPATCH_MANAGER_RESPONSE:
  1348. {
  1349. //调度完成后, 向数据库发送答复的相关操作
  1350. t_error = m_dispatch_command.dispatch_response_to_sql(m_dispatch_plc.get_result());
  1351. if(t_error != Error_code::SUCCESS)
  1352. {
  1353. LOG(INFO) << " dispatch_response_to_sql error = " << t_error << this;
  1354. }
  1355. //切换到ready状态
  1356. m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  1357. break;
  1358. }
  1359. default:
  1360. {
  1361. std::cout << " huli test :::: " << " Dispatch_manager::process_control() error, m_dispatch_manager_status = "
  1362. << m_dispatch_manager_status << std::endl;
  1363. break;
  1364. }
  1365. }
  1366. }
  1367. }
  1368. LOG(INFO) << " Dispatch_manager::process_sql() end "<< this;
  1369. return;
  1370. }
  1371. #endif//SHANGGUJIE_PROJECT_PROJECT