dispatch_manager.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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_network_message_new(Network_message* p_msg)
  225. {
  226. std::unique_lock<std::mutex> t_lock(m_lock);
  227. //根据自定义的socket_id, 分发给4个单片机模块.
  228. if ( 0 <= p_msg->m_socket_id && p_msg->m_socket_id < 4 )
  229. {
  230. return m_dispatch_singlechip[p_msg->m_socket_id].execute_for_singlechip_data_msg_new(p_msg->m_message_buf);
  231. }
  232. return Error_code::SUCCESS;
  233. }
  234. //对外的接口函数,负责接受并处理任务单,
  235. //Error_manager Dispatch_manager::execute_task(Dispatch_manager::Dispatch_motion_direction dispatch_motion_direction)
  236. //{
  237. // return Error_code::SUCCESS;
  238. //// std::this_thread::sleep_for(std::chrono::seconds(rand()%3+3));
  239. //
  240. // if ( dispatch_motion_direction == E_PICKUP_CAR )
  241. // {
  242. // return Error_code::SUCCESS;
  243. // }
  244. //
  245. //// return Error_code::SUCCESS;
  246. //
  247. //// srand(0);
  248. // unsigned int t_probability = rand();
  249. //
  250. // if ( t_probability%100 >=20 )
  251. // {
  252. // return Error_code::SUCCESS;
  253. // }
  254. // else
  255. // {
  256. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  257. // " Dispatch_manager::execute_task() error ");
  258. // }
  259. //}
  260. //检查能否执行消息指令
  261. Error_manager Dispatch_manager::check_execute_msg(Rabbitmq_message* p_msg)
  262. {
  263. return Error_code::SUCCESS;
  264. /*
  265. Error_manager t_error = Dispatch_manager::get_instance_references().check_status();
  266. if ( t_error == Error_code::SUCCESS )
  267. {
  268. return Error_code::SUCCESS;
  269. }
  270. else if (t_error.get_error_level() == NEGLIGIBLE_ERROR)//一级故障,轻微故障,
  271. {
  272. std::cout << "Dispatch_manager _is_busy , " << std::endl;
  273. //返回繁忙之后, 通信模块1秒后再次调用check
  274. return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  275. }
  276. switch ( p_msg->get_message_type() )
  277. {
  278. case Communication_message::Message_type::eDispatch_request_msg:
  279. {
  280. return Error_code::SUCCESS;
  281. break;
  282. }
  283. case Communication_message::Message_type::eDispatch_plan_response_msg:
  284. {
  285. return Error_code::SUCCESS;
  286. break;
  287. }
  288. case Communication_message::Message_type::eDispatch_control_request_msg:
  289. {
  290. return Error_code::SUCCESS;
  291. break;
  292. }
  293. default :
  294. {
  295. //无效的消息,
  296. return Error_manager(Error_code::INVALID_MESSAGE, Error_level::NEGLIGIBLE_ERROR,
  297. " INVALID_MESSAGE error ");
  298. break;
  299. }
  300. }
  301. */
  302. return Error_code::SUCCESS;
  303. }
  304. //检查状态
  305. Error_manager Dispatch_manager::check_status()
  306. {
  307. if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_READY )
  308. {
  309. return Error_code::SUCCESS;
  310. }
  311. else if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_STORE || m_dispatch_manager_status == E_DISPATCH_MANAGER_PICKUP )
  312. {
  313. return Error_code::SUCCESS;
  314. //无论存取, 状态都是返回正常,运行接受下一个任务
  315. // return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_BUSY, Error_level::NEGLIGIBLE_ERROR,
  316. // " Dispatch_manager::check_status() error ");
  317. }
  318. else
  319. {
  320. return Error_manager(Error_code::DISPATCH_MANAGER_STATUS_ERROR, Error_level::MINOR_ERROR,
  321. " Dispatch_manager::check_status() error ");
  322. }
  323. return Error_code::SUCCESS;
  324. }
  325. //调度模块 //执行搬运请求(主控->调度管理)
  326. //Error_manager Dispatch_manager::execute_for_dispatch_request_msg(message::Dispatch_request_msg& dispatch_request_msg)
  327. //{
  328. // LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_request_msg--- "<< dispatch_request_msg.DebugString() << " "<< this;
  329. // LOG(INFO) << " dispatch_request_msg->m_command_key = "<<dispatch_request_msg.command_key()<< " "<< this;
  330. //
  331. // Error_manager t_error;
  332. //
  333. // std::unique_lock<std::mutex> t_lock(m_lock);
  334. // if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  335. // {
  336. // m_dispatch_request_store_list.push_back(dispatch_request_msg);
  337. // m_store_updata_time = std::chrono::system_clock::now();
  338. // }
  339. // else
  340. // {
  341. // m_dispatch_request_pickup_list.push_back(dispatch_request_msg);
  342. // m_pickup_updata_time = std::chrono::system_clock::now();
  343. // }
  344. //
  345. //
  346. //
  347. // //need, 目前直接转发给调度plc执行, 后续要存队列,然后增加调度逻辑.
  348. //
  349. //// m_dispatch_plc.execute_for_dispatch_request_msg(dispatch_request_msg);
  350. //
  351. //
  352. //
  353. // return Error_code::SUCCESS;
  354. //}
  355. ////调度模块 答复数据异常
  356. //Error_manager Dispatch_manager::send_dispatch_response_msg_with_error(message::Dispatch_request_msg &dispatch_request_msg, Error_manager error)
  357. //{
  358. // message::Dispatch_response_msg t_dispatch_response_msg;
  359. // t_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  360. // t_dispatch_response_msg.mutable_base_info()->set_timeout_ms(dispatch_request_msg.base_info().timeout_ms());
  361. // t_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  362. // t_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  363. // t_dispatch_response_msg.set_command_key(dispatch_request_msg.command_key());
  364. //
  365. // t_dispatch_response_msg.mutable_error_manager()->set_error_code(error.get_error_code());
  366. // t_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)error.get_error_level());
  367. // t_dispatch_response_msg.mutable_error_manager()->set_error_description(error.get_error_description());
  368. //
  369. //// System_communication::get_instance_references().encapsulate_msg(t_dispatch_response_msg.SerializeAsString());
  370. // System_communication::get_instance_references().encapsulate_task_msg(t_dispatch_response_msg.SerializeAsString());
  371. // return Error_code::SUCCESS;
  372. //}
  373. //
  374. ////调度模块 //调度总规划的答复(调度算法->调度管理)
  375. //Error_manager Dispatch_manager::execute_for_dispatch_plan_response_msg(message::Dispatch_plan_response_msg &dispatch_plan_response_msg)
  376. //{
  377. // LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_plan_response_msg--- "<< this;
  378. // LOG(INFO) << " dispatch_plan_response_msg->m_command_key = "<<dispatch_plan_response_msg.command_key()<< " "<< this;
  379. //
  380. // Error_manager t_error;
  381. //
  382. // return Error_code::SUCCESS;
  383. //}
  384. ////调度模块 //调度控制的任务请求(调度算法->调度管理)
  385. //Error_manager Dispatch_manager::execute_for_dispatch_control_request_msg(message::Dispatch_control_request_msg &dispatch_control_request_msg)
  386. //{
  387. // LOG(INFO) << " ---Dispatch_manager::execute_for_dispatch_control_request_msg--- "<< this;
  388. // LOG(INFO) << " dispatch_control_request_msg->m_command_key = "<<dispatch_control_request_msg.command_key()<< " "<< this;
  389. //
  390. // Error_manager t_error;
  391. //
  392. // return Error_code::SUCCESS;
  393. //}
  394. //定时发送 调度管理的状态
  395. Error_manager Dispatch_manager::encapsulate_send_dispatch_manager_status()
  396. {
  397. // Error_manager t_error;
  398. //
  399. // int t_dispatch_manager_id = get_dispatch_manager_id();
  400. //
  401. // std::string t_msg1;
  402. // //创建一条 调度管理总管理的状态 旧版
  403. // message::Dispatch_terminal_status_msg t_dispatch_terminal_status_msg;
  404. // t_dispatch_terminal_status_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_status_msg);
  405. // t_dispatch_terminal_status_msg.mutable_base_info()->set_timeout_ms(5000);
  406. // t_dispatch_terminal_status_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  407. // t_dispatch_terminal_status_msg.mutable_base_info()->set_receiver(message::Communicator::eEmpty);
  408. //
  409. // t_dispatch_terminal_status_msg.mutable_id_struct()->set_unit_id(t_dispatch_manager_id);
  410. // t_dispatch_terminal_status_msg.set_terminal_status(message::Terminal_status(m_dispatch_manager_status));
  411. // t_dispatch_terminal_status_msg.set_passageway_direction(message::Passageway_direction::E_BILATERAL);
  412. //
  413. //
  414. // //E_DISPATCH_POCESS_OVER
  415. // {
  416. // std::unique_lock<std::mutex> t_lock(m_lock);
  417. // for (auto iter = m_dispatch_response_store_map.begin(); iter != m_dispatch_response_store_map.end(); ++iter)
  418. // {
  419. // message::Dispatch_process_information * tp_dispatch_process_information =
  420. // t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
  421. // tp_dispatch_process_information->set_car_numberplate(iter->second.parkspace_info_ex(0).car_info().car_numberplate());
  422. // tp_dispatch_process_information->set_command_key(iter->second.command_key());
  423. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->second.dispatch_motion_direction());
  424. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->second.id_struct());
  425. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->second.locate_information());
  426. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->second.parkspace_info_ex());
  427. // tp_dispatch_process_information->set_car_type(iter->second.car_type());
  428. //
  429. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_OVER);
  430. // }
  431. // for (auto iter = m_dispatch_response_pickup_map.begin(); iter != m_dispatch_response_pickup_map.end(); ++iter)
  432. // {
  433. // message::Dispatch_process_information * tp_dispatch_process_information =
  434. // t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
  435. // tp_dispatch_process_information->set_car_numberplate(iter->second.parkspace_info_ex(0).car_info().car_numberplate());
  436. // tp_dispatch_process_information->set_command_key(iter->second.command_key());
  437. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->second.dispatch_motion_direction());
  438. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->second.id_struct());
  439. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->second.locate_information());
  440. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->second.parkspace_info_ex());
  441. // tp_dispatch_process_information->set_car_type(iter->second.car_type());
  442. //
  443. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_OVER);
  444. // }
  445. // }
  446. //
  447. //
  448. // //E_DISPATCH_POCESS_WORKING
  449. // {
  450. // if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST ||
  451. // m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_WORKING||
  452. // m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_BUSY)
  453. // {
  454. // message::Dispatch_request_msg t_dispatch_request_msg = m_dispatch_plc.get_dispatch_request_msg();
  455. // if ( t_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  456. // {
  457. // message::Dispatch_process_information * tp_dispatch_process_information =
  458. // t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
  459. // tp_dispatch_process_information->set_car_numberplate(t_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate());
  460. // tp_dispatch_process_information->set_command_key(t_dispatch_request_msg.command_key());
  461. // tp_dispatch_process_information->set_dispatch_motion_direction(t_dispatch_request_msg.dispatch_motion_direction());
  462. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(t_dispatch_request_msg.id_struct());
  463. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(t_dispatch_request_msg.locate_information());
  464. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(t_dispatch_request_msg.parkspace_info_ex());
  465. // tp_dispatch_process_information->set_car_type(t_dispatch_request_msg.car_type());
  466. //
  467. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WORKING);
  468. // tp_dispatch_process_information->set_working_total_time(m_dispatch_plc.get_response_working_total_time());
  469. // tp_dispatch_process_information->set_working_remaining_time(m_dispatch_plc.get_response_working_remaining_time());
  470. // }
  471. // else
  472. // {
  473. // message::Dispatch_process_information * tp_dispatch_process_information =
  474. // t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
  475. // tp_dispatch_process_information->set_car_numberplate(t_dispatch_request_msg.parkspace_info_ex(0).car_info().car_numberplate());
  476. // tp_dispatch_process_information->set_command_key(t_dispatch_request_msg.command_key());
  477. // tp_dispatch_process_information->set_dispatch_motion_direction(t_dispatch_request_msg.dispatch_motion_direction());
  478. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(t_dispatch_request_msg.id_struct());
  479. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(t_dispatch_request_msg.locate_information());
  480. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(t_dispatch_request_msg.parkspace_info_ex());
  481. // tp_dispatch_process_information->set_car_type(t_dispatch_request_msg.car_type());
  482. //
  483. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WORKING);
  484. // tp_dispatch_process_information->set_working_total_time(m_dispatch_plc.get_response_working_total_time());
  485. // tp_dispatch_process_information->set_working_remaining_time(m_dispatch_plc.get_response_working_remaining_time());
  486. // }
  487. // }
  488. // }
  489. //
  490. // //E_DISPATCH_POCESS_WAIT
  491. // {
  492. // std::unique_lock<std::mutex> t_lock(m_lock);
  493. // int t_store_wait_number = 1;
  494. // for (auto iter = m_dispatch_request_store_list.begin(); iter != m_dispatch_request_store_list.end(); ++iter)
  495. // {
  496. // message::Dispatch_process_information * tp_dispatch_process_information =
  497. // t_dispatch_terminal_status_msg.mutable_dispatch_store_process_information_vector()->Add();
  498. // tp_dispatch_process_information->set_car_numberplate(iter->parkspace_info_ex(0).car_info().car_numberplate());
  499. // tp_dispatch_process_information->set_command_key(iter->command_key());
  500. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->dispatch_motion_direction());
  501. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->id_struct());
  502. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->locate_information());
  503. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->parkspace_info_ex());
  504. // tp_dispatch_process_information->set_car_type(iter->car_type());
  505. //
  506. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WAIT);
  507. // tp_dispatch_process_information->set_wait_number(t_store_wait_number);
  508. // t_store_wait_number++;
  509. // }
  510. // int t_pickup_wait_number = 1;
  511. // for (auto iter = m_dispatch_request_pickup_list.begin(); iter != m_dispatch_request_pickup_list.end(); ++iter)
  512. // {
  513. // message::Dispatch_process_information * tp_dispatch_process_information =
  514. // t_dispatch_terminal_status_msg.mutable_dispatch_pickup_process_information_vector()->Add();
  515. // tp_dispatch_process_information->set_car_numberplate(iter->parkspace_info_ex(0).car_info().car_numberplate());
  516. // tp_dispatch_process_information->set_command_key(iter->command_key());
  517. // tp_dispatch_process_information->set_dispatch_motion_direction(iter->dispatch_motion_direction());
  518. // tp_dispatch_process_information->mutable_id_struct()->CopyFrom(iter->id_struct());
  519. // tp_dispatch_process_information->mutable_locate_information()->CopyFrom(iter->locate_information());
  520. // tp_dispatch_process_information->mutable_parkspace_info_ex()->CopyFrom(iter->parkspace_info_ex());
  521. // tp_dispatch_process_information->set_car_type(iter->car_type());
  522. //
  523. // tp_dispatch_process_information->set_dispatch_process_status(message::Dispatch_process_status::E_DISPATCH_POCESS_WAIT);
  524. // tp_dispatch_process_information->set_wait_number(t_pickup_wait_number);
  525. // t_pickup_wait_number++;
  526. // }
  527. // }
  528. //
  529. // std::string t_store_updata_time = Time_tool::get_instance_references().get_time_string_millisecond(m_store_updata_time);
  530. // std::string t_pickup_updata_time = Time_tool::get_instance_references().get_time_string_millisecond(m_pickup_updata_time);
  531. //
  532. // t_dispatch_terminal_status_msg.set_dispatch_store_process_information_updata_time(t_store_updata_time);
  533. // t_dispatch_terminal_status_msg.set_dispatch_pickup_process_information_updata_time(t_pickup_updata_time);
  534. //
  535. //
  536. // t_msg1 = t_dispatch_terminal_status_msg.SerializeAsString();
  537. // System_communication::get_instance_references().encapsulate_status_msg(t_msg1);
  538. //
  539. return Error_code::SUCCESS;
  540. }
  541. //定时发送 调度管理的状态
  542. Error_manager Dispatch_manager::encapsulate_send_dispatch_manager_status_new()
  543. {
  544. std::unique_lock<std::mutex> t_lock(m_lock);
  545. dispatch_node_statu t_dispatch_node_statu_msg;
  546. if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_READY )
  547. {
  548. t_dispatch_node_statu_msg.set_statu(eIdle);
  549. // t_dispatch_node_statu_msg.set_statu(eBusy);
  550. }
  551. else if ( m_dispatch_manager_status == E_DISPATCH_MANAGER_UNKNOW ||
  552. m_dispatch_manager_status == E_DISPATCH_MANAGER_DISCONNECT ||
  553. m_dispatch_manager_status == E_DISPATCH_MANAGER_FAULT )
  554. {
  555. t_dispatch_node_statu_msg.set_statu(eFault);
  556. }
  557. else
  558. {
  559. t_dispatch_node_statu_msg.set_statu(eBusy);
  560. }
  561. t_dispatch_node_statu_msg.set_idle_stop_floor(m_device_floor);
  562. if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE ||
  563. m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REALLOCATE)
  564. {
  565. t_dispatch_node_statu_msg.mutable_running_pack_info()->CopyFrom(m_park_table_msg);
  566. }
  567. else if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP ||
  568. m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_REVOCATION )
  569. {
  570. t_dispatch_node_statu_msg.mutable_running_pick_info()->CopyFrom(m_pick_table_msg);
  571. }
  572. else
  573. {
  574. }
  575. t_dispatch_node_statu_msg.set_unit_id(m_dispatch_manager_id+1);
  576. t_dispatch_node_statu_msg.set_plc_heartbeat(m_dispatch_plc.m_plc_heartbeat);
  577. t_dispatch_node_statu_msg.set_plc_status_info(m_dispatch_plc.m_plc_status_info);
  578. dispatch_region_info * p_dispatch_region_info_1 = t_dispatch_node_statu_msg.mutable_dispatch_region_info_vector()->Add();
  579. p_dispatch_region_info_1->set_terminal_id(m_dispatch_manager_id*2+1);
  580. p_dispatch_region_info_1->set_turnplate_angle_min(m_dispatch_plc.m_turnplate_angle_min1);
  581. p_dispatch_region_info_1->set_turnplate_angle_max(m_dispatch_plc.m_turnplate_angle_max1);
  582. dispatch_region_info * p_dispatch_region_info_2 = t_dispatch_node_statu_msg.mutable_dispatch_region_info_vector()->Add();
  583. p_dispatch_region_info_2->set_terminal_id(m_dispatch_manager_id*2+2);
  584. p_dispatch_region_info_2->set_turnplate_angle_min(m_dispatch_plc.m_turnplate_angle_min2);
  585. p_dispatch_region_info_2->set_turnplate_angle_max(m_dispatch_plc.m_turnplate_angle_max2);
  586. std::string t_msg = t_dispatch_node_statu_msg.DebugString();
  587. // std::cout << " huli test t_dispatch_node_statu_msg:::: " << " t_msg.size() = " << t_msg.size() << std::endl;
  588. // std::cout << " huli test t_dispatch_node_statu_msg:::: " << " t_msg = " << t_msg << std::endl;
  589. System_communication::get_instance_references().encapsulate_status_msg(t_msg);
  590. return Error_code::SUCCESS;
  591. }
  592. //在流程的map 里面释放指定的流程
  593. Error_manager Dispatch_manager::release_dispatch_process(std::string command_key)
  594. {
  595. std::unique_lock<std::mutex> t_lock(m_lock);
  596. return Error_code::SUCCESS;
  597. }
  598. //调度模块 ///地面雷达的状态消息(地面雷达->null)
  599. //Error_manager Dispatch_manager::execute_for_ground_status_msg(message::Ground_status_msg ground_status_msg)
  600. //{
  601. // int t_inlet_id = ground_status_msg.mutable_id_struct()->terminal_id() %2;
  602. // return m_dispatch_ground_lidar[t_inlet_id].execute_for_ground_status_msg(ground_status_msg);
  603. //
  604. // return Error_code::SUCCESS;
  605. //}
  606. //调度模块 ///单片机的状态消息
  607. //Error_manager Dispatch_manager::execute_for_singlechip_data_msg(message::Singlechip_data singlechip_data_msg, bool validity)
  608. //{
  609. // if ( singlechip_data_msg.has_terminalid() && singlechip_data_msg.has_dispatchdirection() )
  610. // {
  611. // if ( singlechip_data_msg.terminalid() >=0 &&
  612. // singlechip_data_msg.terminalid() <=5 &&
  613. // singlechip_data_msg.dispatchdirection() >=1 &&
  614. // singlechip_data_msg.dispatchdirection() <=2 )
  615. // {
  616. // int t_temp = (singlechip_data_msg.terminalid() %2) + (singlechip_data_msg.dispatchdirection()-1)*2;
  617. // return m_dispatch_singlechip[t_temp].execute_for_singlechip_data_msg(singlechip_data_msg, validity);
  618. // }
  619. // }
  620. //
  621. // return Error_code::SUCCESS;
  622. //}
  623. //获取空闲的出口,返回0表示没有空闲出口, 返回1~6表示对应的出口
  624. int Dispatch_manager::get_outlet_for_ready()
  625. {
  626. std::cout << " huli test 111:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  627. //A单元优先分配A2, B和C正常分配
  628. if ( m_dispatch_manager_id == 0 )
  629. {
  630. //检查出口, 并删除取车完成 //判断2个出口是否空闲
  631. if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  632. {
  633. std::cout << " huli test 444:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  634. if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+2) == Error_code::SUCCESS )
  635. {
  636. std::cout << " huli test 555:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  637. return m_dispatch_manager_id*2+2;
  638. }
  639. }
  640. else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
  641. {
  642. std::cout << " huli test 222:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  643. if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+1) == Error_code::SUCCESS )
  644. {
  645. std::cout << " huli test 333:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  646. return m_dispatch_manager_id*2+1;
  647. }
  648. }
  649. else
  650. {
  651. return 0;
  652. }
  653. }
  654. else
  655. {
  656. //检查出口, 并删除取车完成 //判断2个出口是否空闲
  657. if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
  658. {
  659. std::cout << " huli test 222:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  660. if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+1) == Error_code::SUCCESS )
  661. {
  662. std::cout << " huli test 333:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  663. return m_dispatch_manager_id*2+1;
  664. }
  665. }
  666. else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  667. {
  668. std::cout << " huli test 444:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  669. if ( m_dispatch_command.check_export_id_is_ready(m_dispatch_manager_id*2+2) == Error_code::SUCCESS )
  670. {
  671. std::cout << " huli test 555:::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  672. return m_dispatch_manager_id*2+2;
  673. }
  674. }
  675. else
  676. {
  677. return 0;
  678. }
  679. }
  680. return 0;
  681. }
  682. Dispatch_manager::Dispatch_manager_status Dispatch_manager::get_dispatch_manager_status()
  683. {
  684. return m_dispatch_manager_status;
  685. }
  686. int Dispatch_manager::get_dispatch_manager_id()
  687. {
  688. return m_dispatch_manager_id;
  689. }
  690. void Dispatch_manager::set_dispatch_manager_id(int dispatch_manager_id)
  691. {
  692. m_dispatch_manager_id = dispatch_manager_id;
  693. }
  694. //资源分配
  695. //void Dispatch_manager::resource_allocation()
  696. //{
  697. // return;
  698. //
  699. // LOG(INFO) << " Dispatch_manager::resource_allocation() start " << this;
  700. // Error_manager t_error;
  701. //
  702. //
  703. // while (m_dispatch_manager_condition.is_alive())
  704. // {
  705. // m_dispatch_manager_condition.wait();
  706. // if ( m_dispatch_manager_condition.is_alive() )
  707. // {
  708. // std::this_thread::sleep_for(std::chrono::microseconds(1));
  709. // std::this_thread::sleep_for(std::chrono::milliseconds(100));
  710. //// std::this_thread::sleep_for(std::chrono::seconds(1));
  711. // std::this_thread::yield();
  712. // std::unique_lock<std::mutex> t_lock(m_lock);
  713. //
  714. // //排序和资源分配
  715. // if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  716. // {
  717. // //补全 id_struct
  718. // if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP)
  719. // {
  720. // //判断2个出口是否空闲, 在资源分配时,提前分配好出口终端号
  721. // if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
  722. // {
  723. // auto iter = m_dispatch_request_pickup_list.begin();
  724. // if ( iter != m_dispatch_request_pickup_list.end() )
  725. // {
  726. // iter->mutable_id_struct()->set_terminal_id(m_dispatch_manager_id*2);
  727. // m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  728. // m_dispatch_manager_status = E_DISPATCH_MANAGER_PICKUP;
  729. // m_dispatch_request_pickup_list.erase(iter);
  730. // m_pickup_updata_time = std::chrono::system_clock::now();
  731. // }
  732. // }
  733. // else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  734. // {
  735. // auto iter = m_dispatch_request_pickup_list.begin();
  736. // if ( iter != m_dispatch_request_pickup_list.end() )
  737. // {
  738. // iter->mutable_id_struct()->set_terminal_id(m_dispatch_manager_id*2+1);
  739. // m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  740. // m_dispatch_manager_status = E_DISPATCH_MANAGER_PICKUP;
  741. // m_dispatch_request_pickup_list.erase(iter);
  742. // m_pickup_updata_time = std::chrono::system_clock::now();
  743. // }
  744. // }
  745. // //else什么也不做,直接切换到存车就行
  746. //
  747. // m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  748. // }
  749. // else if ( m_dispatch_motion_direction_next == Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE )
  750. // {
  751. // auto iter = m_dispatch_request_store_list.begin();
  752. // if ( iter != m_dispatch_request_store_list.end() )
  753. // {
  754. // iter->mutable_id_struct()->set_unit_id(m_dispatch_manager_id );
  755. // m_dispatch_plc.execute_for_dispatch_request_msg(*iter);
  756. // m_dispatch_manager_status = E_DISPATCH_MANAGER_STORE;
  757. // m_dispatch_request_store_list.erase(iter);
  758. // m_store_updata_time = std::chrono::system_clock::now();
  759. // }
  760. // m_dispatch_motion_direction_next = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  761. // }
  762. // else
  763. // {
  764. // ///////
  765. // }
  766. // }
  767. // else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY)
  768. // {
  769. // m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  770. // }
  771. // else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_BUSY)
  772. // {
  773. // m_dispatch_manager_status = E_DISPATCH_MANAGER_BUSY;
  774. // }
  775. // else if(m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT)
  776. // {
  777. // m_dispatch_manager_status = E_DISPATCH_MANAGER_DISCONNECT;
  778. // }
  779. // else
  780. // {
  781. // m_dispatch_manager_status = E_DISPATCH_MANAGER_FAULT;
  782. // }
  783. //
  784. //
  785. // //检查时间, 并删除存车完成
  786. // for (auto iter = m_dispatch_response_store_map.begin(); iter != m_dispatch_response_store_map.end();)
  787. // {
  788. // if ( std::chrono::system_clock::now() - iter->first > std::chrono::seconds(10) )
  789. // {
  790. // iter = m_dispatch_response_store_map.erase(iter);
  791. // m_store_updata_time = std::chrono::system_clock::now();
  792. // }
  793. // else
  794. // {
  795. // ++iter;
  796. // }
  797. // }
  798. //
  799. // //检查出口, 并删除取车完成 //判断2个出口是否空闲
  800. // if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[2].is_outlet_ready() )
  801. // {
  802. // if ( m_dispatch_response_pickup_map.find(m_dispatch_manager_id*2) != m_dispatch_response_pickup_map.end() )
  803. // {
  804. // m_dispatch_response_pickup_map.erase(m_dispatch_manager_id*2);
  805. // m_pickup_updata_time = std::chrono::system_clock::now();
  806. // }
  807. // }
  808. // else if ( Dispatch_manager::get_instance_references().m_dispatch_singlechip[3].is_outlet_ready() )
  809. // {
  810. // if ( m_dispatch_response_pickup_map.find(m_dispatch_manager_id*2+1) != m_dispatch_response_pickup_map.end() )
  811. // {
  812. // m_dispatch_response_pickup_map.erase(m_dispatch_manager_id*2+1);
  813. // m_pickup_updata_time = std::chrono::system_clock::now();
  814. // }
  815. // }
  816. //
  817. // }
  818. // }
  819. //
  820. // LOG(INFO) << " Dispatch_manager::resource_allocation() end "<< this;
  821. //
  822. // return;
  823. //}
  824. //
  825. ////新版流程控制
  826. //void Dispatch_manager::process_control()
  827. //{
  828. // LOG(INFO) << " Dispatch_manager::process_control() start " << this;
  829. // Error_manager t_error;
  830. //
  831. // while (m_dispatch_manager_condition.is_alive())
  832. // {
  833. // m_dispatch_manager_condition.wait();
  834. // if (m_dispatch_manager_condition.is_alive())
  835. // {
  836. // std::this_thread::sleep_for(std::chrono::microseconds(1));
  837. // std::this_thread::sleep_for(std::chrono::milliseconds(1));
  838. //// std::this_thread::sleep_for(std::chrono::seconds(1));
  839. // std::this_thread::yield();
  840. // std::unique_lock<std::mutex> t_lock(m_lock);
  841. //
  842. // static int t_count = 0;
  843. // if (t_count % 1000 == 0 )
  844. // {
  845. // std::cout << " huli test :::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  846. // std::cout << " huli test :::: " << " m_dispatch_manager_status = " << m_dispatch_manager_status << std::endl;
  847. // }
  848. // t_count++;
  849. //
  850. // switch ( m_dispatch_manager_status )
  851. // {
  852. // case E_DISPATCH_MANAGER_READY:
  853. // {
  854. // //等待 count_command_signal_1_port 的消息
  855. // if ( m_count_flag )
  856. // {
  857. // m_dispatch_manager_status = E_DISPATCH_MANAGER_COUNT_NEW;
  858. // }
  859. // if ( m_post_flag )
  860. // {
  861. // m_dispatch_manager_status = E_DISPATCH_MANAGER_POST_NEW;
  862. // }
  863. // break;
  864. // }
  865. // case E_DISPATCH_MANAGER_COUNT_NEW:
  866. // {
  867. // //目前不用解析 m_count_command_signal_msg ,只要收到 count_command_signal_1_port, 直接进行下一步
  868. // //发送调度任务申请
  869. // char t_buf[256] = {0};
  870. // sprintf(t_buf, "%d-%d", m_dispatch_manager_id+1, m_device_floor);
  871. // std::string t_string(t_buf);
  872. // System_communication::get_instance_references().encapsulate_task_msg(t_string, 0);
  873. // std::cout << " huli test :::: " << " m_request_command_msg = " << t_string << std::endl;
  874. // m_dispatch_manager_status = E_DISPATCH_MANAGER_REQUEST_NEW;
  875. // break;
  876. // }
  877. // case E_DISPATCH_MANAGER_REQUEST_NEW:
  878. // {
  879. // //等待 post_command_1_port 的消息
  880. // if ( m_post_flag )
  881. // {
  882. // m_dispatch_manager_status = E_DISPATCH_MANAGER_POST_NEW;
  883. // }
  884. // break;
  885. // }
  886. // case E_DISPATCH_MANAGER_POST_NEW:
  887. // {
  888. // //解析 m_post_command_msg
  889. // std::string t_string = m_post_command_msg.m_message_buf;
  890. // //检索字符串的内容,
  891. // if ( t_string.find("entrance_measure_info") != std::string::npos )
  892. // {
  893. // //entrance_measure_info 是 park_table 特有的
  894. // if(! google::protobuf::TextFormat::ParseFromString(t_string, &m_park_table_msg))
  895. // {
  896. // //答复错误
  897. // }
  898. // else
  899. // {
  900. //// LOG(INFO) << " m_park_table_msg = "<< m_park_table_msg.DebugString() << " --- " << this;
  901. // m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
  902. //
  903. // //根据雷达x坐标, 计算入口id
  904. // int t_terminal = 0;
  905. // if (m_park_table_msg.entrance_measure_info().cx()<0)
  906. // {
  907. // t_terminal = m_dispatch_manager_id*2+1;
  908. // }
  909. // else if(m_park_table_msg.entrance_measure_info().cx()>0)
  910. // {
  911. // t_terminal = m_dispatch_manager_id*2+2;
  912. // }
  913. // m_park_table_msg.set_terminal_id(t_terminal);
  914. // m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
  915. // m_device_floor = m_park_table_msg.allocated_space_info().floor();
  916. // m_dispatch_manager_status = E_DISPATCH_MANAGER_COMPLETED_NEW;
  917. // }
  918. // }
  919. // else
  920. // {
  921. // if ( get_outlet_for_ready() !=0 )
  922. // {
  923. // if(! google::protobuf::TextFormat::ParseFromString(t_string, &m_pick_table_msg))
  924. // {
  925. // //答复错误
  926. // }
  927. // else
  928. // {
  929. // m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
  930. // m_pick_table_msg.set_terminal_id(get_outlet_for_ready());
  931. // m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg, m_dispatch_process_type, m_car_type);
  932. // m_device_floor = 1;
  933. // m_dispatch_manager_status = E_DISPATCH_MANAGER_COMPLETED_NEW;
  934. // }
  935. // }
  936. // //else 没有空闲出口,就无限等待
  937. // }
  938. // break;
  939. // }
  940. // case E_DISPATCH_MANAGER_COMPLETED_NEW:
  941. // {
  942. // //等待dispatch_plc执行,然后ack里面切回ready
  943. // break;
  944. // }
  945. // default:
  946. // {
  947. // std::cout << " huli test :::: " << " Dispatch_manager::process_control() error, m_dispatch_manager_status = "
  948. // << m_dispatch_manager_status << std::endl;
  949. // break;
  950. // }
  951. // }
  952. // }
  953. // }
  954. // LOG(INFO) << " Dispatch_manager::process_control() end "<< this;
  955. // return;
  956. //}
  957. //新版流程控制
  958. void Dispatch_manager::process_sql()
  959. {
  960. LOG(INFO) << " Dispatch_manager::process_sql() start " << this;
  961. Error_manager t_error;
  962. while (m_dispatch_manager_condition.is_alive())
  963. {
  964. m_dispatch_manager_condition.wait();
  965. if (m_dispatch_manager_condition.is_alive())
  966. {
  967. std::this_thread::sleep_for(std::chrono::microseconds(1));
  968. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  969. // std::this_thread::sleep_for(std::chrono::seconds(1));
  970. std::this_thread::yield();
  971. std::unique_lock<std::mutex> t_lock(m_lock);
  972. static auto t_update_time = std::chrono::system_clock::now();
  973. if(std::chrono::system_clock::now() - t_update_time > std::chrono::seconds(1))
  974. {
  975. t_update_time = std::chrono::system_clock::now();
  976. std::cout << " huli test :::: " << " m_dispatch_manager_id = " << m_dispatch_manager_id << std::endl;
  977. std::cout << " huli test :::: " << " m_dispatch_manager_status = " << m_dispatch_manager_status << std::endl;
  978. std::cout << " huli test :::: " << " xxxxxxxxx get_outlet_for_ready() = " << get_outlet_for_ready() << std::endl;
  979. }
  980. switch ( m_dispatch_manager_status )
  981. {
  982. case E_DISPATCH_MANAGER_READY:
  983. case E_DISPATCH_MANAGER_REQUEST:
  984. {
  985. //循环请求, 请求周期1秒
  986. // std::this_thread::sleep_for(std::chrono::seconds(1));
  987. if ( std::chrono::system_clock::now() - m_request_updata_time > std::chrono::seconds(1) )
  988. {
  989. m_request_updata_time = std::chrono::system_clock::now();
  990. //调度开始前, 向数据库发送请求的相关操作, 输入 穿梭机所在的楼层, 调度id 0~2, 空闲出口id, 如果不是0,就表示有空闲的出口
  991. t_error = m_dispatch_command.dispatch_request_to_sql(m_device_floor, m_dispatch_manager_id, get_outlet_for_ready());
  992. //std::cout << " huli test :::: " << " sssssssssssssssssssssssssssssssssss dispatch_request_to_sql = " << t_error << std::endl;
  993. if ( t_error == Error_code::SUCCESS )
  994. {
  995. //如果找到了合适的指令, 那么交给plc来执行
  996. if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  997. {
  998. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
  999. m_park_table_msg = m_dispatch_command.m_park_table_msg;
  1000. m_car_type = m_dispatch_command.m_car_type;
  1001. m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
  1002. m_device_floor = m_park_table_msg.allocated_space_info().floor();
  1003. }
  1004. else if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  1005. {
  1006. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
  1007. m_pick_table_msg = m_dispatch_command.m_pick_table_msg;
  1008. m_car_type = m_dispatch_command.m_car_type;
  1009. m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg, m_dispatch_process_type, m_car_type);
  1010. m_device_floor = 1;
  1011. }
  1012. //切换到工作状态
  1013. m_dispatch_manager_status = E_DISPATCH_MANAGER_WORKING;
  1014. }
  1015. else if(t_error != Error_code::NODATA)
  1016. {
  1017. LOG(INFO) << " dispatch_request_to_sql error = " << t_error << this;
  1018. }
  1019. //else NODATA 无限循环
  1020. }
  1021. //else 等待
  1022. break;
  1023. }
  1024. case E_DISPATCH_MANAGER_WORKING:
  1025. {
  1026. //等待dispatch_plc执行, 完成后切换到 E_DISPATCH_MANAGER_RESPONSE
  1027. if ( m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::DISPATCH_PLC_READY ||
  1028. m_dispatch_plc.get_dispatch_plc_status() == Dispatch_plc::DISPATCH_PLC_FAULT)
  1029. {
  1030. if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_MIN_CAR )
  1031. {
  1032. //切换到 重新分配
  1033. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1034. m_car_type = Common_data::Car_type::MIN_CAR;
  1035. }
  1036. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_MID_CAR )
  1037. {
  1038. //切换到 重新分配
  1039. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1040. m_car_type = Common_data::Car_type::MID_CAR;
  1041. }
  1042. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_BIG_CAR )
  1043. {
  1044. //切换到 重新分配
  1045. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1046. m_car_type = Common_data::Car_type::BIG_CAR;
  1047. }
  1048. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_HUGE_CAR )
  1049. {
  1050. //切换到 重新分配
  1051. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1052. m_car_type = Common_data::Car_type::HUGE_CAR;
  1053. }
  1054. else if ( m_dispatch_plc.get_result() == DISPATCH_PLC_REALLOCATE_FAULT_CAR )
  1055. {
  1056. //切换到 重新分配
  1057. m_dispatch_manager_status = E_DISPATCH_MANAGER_REALLOCATE;
  1058. m_car_type = Common_data::Car_type::FAULT_CAR;
  1059. }
  1060. else
  1061. {
  1062. //切换到答复状态
  1063. m_dispatch_manager_status = E_DISPATCH_MANAGER_RESPONSE;
  1064. }
  1065. }
  1066. //else 无限等待
  1067. break;
  1068. }
  1069. case E_DISPATCH_MANAGER_REALLOCATE:
  1070. {
  1071. //调度 , 向数据库 重新分配车位,
  1072. t_error = m_dispatch_command.dispatch_reallocate_to_sql(m_car_type, get_outlet_for_ready());
  1073. if ( t_error == Error_code::SUCCESS )
  1074. {
  1075. //如果找到了合适的车位, 那么交给plc来执行
  1076. if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  1077. {
  1078. m_dispatch_process_type = m_dispatch_command.m_dispatch_process_type;
  1079. m_park_table_msg = m_dispatch_command.m_park_table_msg;
  1080. m_car_type = m_dispatch_command.m_car_type;
  1081. m_dispatch_plc.execute_for_dispatch_request_msg(m_park_table_msg, m_dispatch_process_type, m_car_type);
  1082. m_device_floor = m_park_table_msg.allocated_space_info().floor();
  1083. }
  1084. //没有找到合适的车位, 那么放到出口
  1085. else if ( m_dispatch_command.m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  1086. {
  1087. m_dispatch_process_type = m_dispatch_command.m_dispatch_process_type;
  1088. m_pick_table_msg = m_dispatch_command.m_pick_table_msg;
  1089. m_car_type = m_dispatch_command.m_car_type;
  1090. m_dispatch_plc.execute_for_dispatch_request_msg(m_pick_table_msg,m_dispatch_process_type, m_car_type);
  1091. m_device_floor = 1;
  1092. }
  1093. //切换到工作状态
  1094. m_dispatch_manager_status = E_DISPATCH_MANAGER_WORKING;
  1095. }
  1096. else if(t_error != Error_code::NODATA)
  1097. {
  1098. LOG(INFO) << " dispatch_request_to_sql error = " << t_error << this;
  1099. }
  1100. //else NODATA 无限循环
  1101. break;
  1102. break;
  1103. }
  1104. case E_DISPATCH_MANAGER_RESPONSE:
  1105. {
  1106. //调度完成后, 向数据库发送答复的相关操作
  1107. t_error = m_dispatch_command.dispatch_response_to_sql(m_dispatch_plc.get_result());
  1108. if(t_error != Error_code::SUCCESS)
  1109. {
  1110. LOG(INFO) << " dispatch_response_to_sql error = " << t_error << this;
  1111. }
  1112. //切换到ready状态
  1113. m_dispatch_manager_status = E_DISPATCH_MANAGER_READY;
  1114. break;
  1115. }
  1116. default:
  1117. {
  1118. std::cout << " huli test :::: " << " Dispatch_manager::process_control() error, m_dispatch_manager_status = "
  1119. << m_dispatch_manager_status << std::endl;
  1120. break;
  1121. }
  1122. }
  1123. }
  1124. }
  1125. LOG(INFO) << " Dispatch_manager::process_sql() end "<< this;
  1126. return;
  1127. }