dispatch_plc.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //
  2. // Created by huli on 2021/8/3.
  3. //
  4. #include "dispatch_plc.h"
  5. #include "../system/system_communication.h"
  6. Dispatch_plc::Dispatch_plc()
  7. {
  8. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_UNKNOW;
  9. m_plc_id = 0;
  10. mp_execute_thread = NULL;
  11. }
  12. Dispatch_plc::~Dispatch_plc()
  13. {
  14. dispatch_plc_uninit();
  15. }
  16. //调度plc 初始化
  17. Error_manager Dispatch_plc::dispatch_plc_init(int plc_id)
  18. {
  19. m_plc_id = plc_id;
  20. m_status_updata_time = std::chrono::system_clock::now();
  21. m_last_heartbeat = 0;
  22. // 线程默认开启
  23. m_execute_condition.reset(false, true, false);
  24. mp_execute_thread = new std::thread(&Dispatch_plc::execute_thread_fun, this);
  25. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
  26. return Error_code::SUCCESS;
  27. }
  28. //调度plc 反初始化
  29. Error_manager Dispatch_plc::dispatch_plc_uninit()
  30. {
  31. if (mp_execute_thread)
  32. {
  33. m_execute_condition.kill_all();
  34. }
  35. if (mp_execute_thread)
  36. {
  37. mp_execute_thread->join();
  38. delete mp_execute_thread;
  39. mp_execute_thread = NULL;
  40. }
  41. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_UNKNOW;
  42. return Error_code::SUCCESS;
  43. }
  44. //调度plc 执行请求
  45. Error_manager Dispatch_plc::execute_for_dispatch_request_msg(message::Dispatch_request_msg& dispatch_request_msg)
  46. {
  47. if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY )
  48. {
  49. std::unique_lock<std::mutex> t_lock(m_lock);
  50. //设定超时时间, 默认比任务指令里面的时间少10秒,
  51. if ( dispatch_request_msg.base_info().has_timeout_ms() )
  52. {
  53. m_timeout_ms = dispatch_request_msg.base_info().timeout_ms() - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  54. }
  55. else
  56. {
  57. m_timeout_ms = DISPATCH_PROCESS_TIMEOUT_MS - DISPATCH_PROCESS_ATTENUATION_TIMEOUT_MS;
  58. }
  59. m_command_key = dispatch_request_msg.command_key();
  60. m_start_time = std::chrono::system_clock::now();
  61. m_dispatch_request_msg = dispatch_request_msg;
  62. if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  63. {
  64. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_STORE;
  65. }
  66. else if ( dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  67. {
  68. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_PICKUP;
  69. }
  70. else
  71. {
  72. return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  73. " dispatch_request_msg.dispatch_motion_direction() PARAMRTER ERROR ");
  74. }
  75. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  76. m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_REQUEST;
  77. }
  78. else
  79. {
  80. return Error_manager(Error_code::DISPATCH_PLC_STATUS_ERROR, Error_level::MINOR_ERROR,
  81. " m_dispatch_plc_status error ");
  82. }
  83. return Error_code::SUCCESS;
  84. }
  85. Dispatch_plc::Dispatch_plc_status Dispatch_plc::get_dispatch_plc_status()
  86. {
  87. return m_dispatch_plc_status;
  88. }
  89. //执行外界任务的执行函数
  90. void Dispatch_plc::execute_thread_fun()
  91. {
  92. LOG(INFO) << " Dispatch_plc::execute_thread_fun() start " << this;
  93. Error_manager t_error;
  94. while (m_execute_condition.is_alive())
  95. {
  96. m_execute_condition.wait();
  97. if (m_execute_condition.is_alive())
  98. {
  99. std::this_thread::sleep_for(std::chrono::microseconds(1));
  100. std::this_thread::sleep_for(std::chrono::seconds(1));
  101. std::this_thread::yield();
  102. // std::unique_lock<std::mutex> t_lock(m_lock);
  103. std::cout << " huli test :::: " << " m_plc_id = " << m_plc_id << std::endl;
  104. std::cout << " huli test :::: " << " m_dispatch_plc_status = " << m_dispatch_plc_status << std::endl;
  105. switch ((Dispatch_plc_status) m_dispatch_plc_status)
  106. {
  107. case DISPATCH_PLC_READY:
  108. {
  109. if ( m_dispatch_process_type == Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW )
  110. {
  111. update_dispatch_plc_communication();
  112. }
  113. else
  114. {
  115. m_dispatch_plc_status = DISPATCH_PLC_REQUEST;
  116. }
  117. break;
  118. }
  119. case DISPATCH_PLC_REQUEST://给plc发送请求
  120. {
  121. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  122. {
  123. //超时直接报错
  124. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  125. " DISPATCH_PLC_TIME_OUT fun error ");
  126. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  127. }
  128. else
  129. {
  130. //封装 给plc的调度请求
  131. m_result = encapsulate_dispatch_request_to_plc();
  132. if ( m_result == Error_code::SUCCESS )
  133. {
  134. m_dispatch_plc_status = DISPATCH_PLC_WORKING;
  135. }
  136. else
  137. {
  138. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  139. }
  140. }
  141. break;
  142. }
  143. case DISPATCH_PLC_WORKING:
  144. {
  145. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::milliseconds(m_timeout_ms) )
  146. {
  147. //超时直接报错
  148. m_result = Error_manager(Error_code::DISPATCH_PLC_TIME_OUT, Error_level::MINOR_ERROR,
  149. " DISPATCH_PLC_TIME_OUT fun error ");
  150. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  151. }
  152. else
  153. {
  154. update_dispatch_plc_communication();
  155. //检查plc答复反馈
  156. m_result = check_response_from_plc();
  157. if ( m_result == Error_code::SUCCESS )
  158. {
  159. m_dispatch_plc_status = DISPATCH_PLC_RESPONSE;
  160. }
  161. else if ( m_result != Error_code::NODATA )
  162. {
  163. m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  164. }
  165. //else 原地等待
  166. }
  167. break;
  168. }
  169. case DISPATCH_PLC_RESPONSE:
  170. {
  171. //发送调度答复信息给主控
  172. send_dispatch_response_to_main_control();
  173. //无论前面的流程是否正确,都要给主控答复, 反馈里面填充错误码即可.
  174. if ( m_result == Error_code::SUCCESS )
  175. {
  176. //流程正常结束, 恢复到待机状态.
  177. m_dispatch_plc_status = DISPATCH_PLC_READY;
  178. m_dispatch_process_type = Common_data::Dispatch_process_type::DISPATCH_PROCESS_TYPE_UNKNOW;
  179. m_result.error_manager_clear_all();
  180. }
  181. else
  182. {
  183. //流程异常结束, 进入到故障状态.
  184. m_dispatch_plc_status = DISPATCH_PLC_FAULT;
  185. }
  186. break;
  187. }
  188. case DISPATCH_PLC_FAULT:
  189. {
  190. //不在接受新的任务,但是保持基本的通信正常
  191. update_dispatch_plc_communication();
  192. break;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. //封装 给plc的调度请求
  199. Error_manager Dispatch_plc::encapsulate_dispatch_request_to_plc()
  200. {
  201. //把m_dispatch_request_msg的信息传给本地的数据缓存.
  202. std::unique_lock<std::mutex> t_lock(m_lock);
  203. m_request_key = m_dispatch_request_msg.command_key();
  204. if ( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_STORE_CAR )
  205. {
  206. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_STORE;
  207. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_INLET;
  208. }
  209. else if( m_dispatch_request_msg.dispatch_motion_direction() == message::Dispatch_motion_direction::E_PICKUP_CAR )
  210. {
  211. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_PICKUP;
  212. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_OUTLET;
  213. }
  214. else
  215. {
  216. m_request_dispatch_motion_direction = Common_data::Dispatch_motion_direction::DISPATCH_MOTION_DIRECTION_UNKNOWN;
  217. m_request_passageway_direction = Common_data::Passageway_direction::PASSAGEWAY_DIRECTION_UNKNOWN;
  218. }
  219. m_request_passageway_id = m_dispatch_request_msg.terminal_id();
  220. if ( m_dispatch_request_msg.locate_information().locate_correct() )
  221. {
  222. m_request_car_center_x = m_dispatch_request_msg.locate_information().locate_x();
  223. m_request_car_center_y = m_dispatch_request_msg.locate_information().locate_y();
  224. m_request_car_angle = m_dispatch_request_msg.locate_information().locate_angle();
  225. m_request_car_front_theta = m_dispatch_request_msg.locate_information().locate_front_theta();
  226. m_request_car_length = m_dispatch_request_msg.locate_information().locate_length();
  227. m_request_car_width = m_dispatch_request_msg.locate_information().locate_width();
  228. m_request_car_height = m_dispatch_request_msg.locate_information().locate_height();
  229. m_request_car_wheel_base = m_dispatch_request_msg.locate_information().locate_wheel_base();
  230. m_request_car_wheel_width = m_dispatch_request_msg.locate_information().locate_wheel_width();
  231. }
  232. if ( m_dispatch_request_msg.parkspace_info_ex_size() ==1 )
  233. {
  234. m_request_parkingspace_index_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_index_id();
  235. m_request_parkingspace_unit_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_unit_id();
  236. m_request_parkingspace_floor_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_floor_id();
  237. m_request_parkingspace_room_id = m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_room_id();
  238. //车位朝向, 小号朝前朝南, 大号朝后朝北
  239. m_request_parkingspace_direction = (Common_data::Parkingspace_direction)m_dispatch_request_msg.parkspace_info_ex(0).parkingspace_direction();
  240. m_request_car_license = m_dispatch_request_msg.parkspace_info_ex(0).car_info().license();
  241. m_request_car_type = (Common_data::Car_type)m_dispatch_request_msg.parkspace_info_ex(0).car_type();
  242. }
  243. else
  244. {
  245. return Error_manager(Error_code::DISPATCH_PLC_REQUEST_ERROR, Error_level::MINOR_ERROR,
  246. " m_dispatch_request_msg.parkspace_info_ex_size() !=1 error ");
  247. }
  248. return Error_code::SUCCESS;
  249. }
  250. //更新plc通信
  251. Error_manager Dispatch_plc::update_dispatch_plc_communication()
  252. {
  253. std::unique_lock<std::mutex> t_lock(m_lock);
  254. std::unique_lock<std::mutex> t_lock1(Dispatch_communication::get_instance_references().m_data_lock);
  255. /*
  256. //请求消息, 调度->plc
  257. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  258. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  259. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  260. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  261. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  262. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  263. m_request_key = "123456789";
  264. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  265. static unsigned char index = 0;
  266. index++;
  267. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = index;
  268. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = 02;
  269. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = 03;
  270. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = 04;
  271. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = 05;
  272. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = 6;
  273. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = 7;
  274. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = 8;
  275. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = 11;
  276. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = 12;
  277. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = 13;
  278. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = 14;
  279. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = 15;
  280. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = 16;
  281. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = 17;
  282. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = 18;
  283. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = 19;
  284. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  285. int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
  286. m_request_car_license = "ABCDEF";
  287. memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
  288. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = 33;
  289. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = 44;
  290. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = 55;
  291. */
  292. //请求消息, 调度->plc
  293. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_data * tp_dispatch_request_from_manager_to_plc_for_data =
  294. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_data;
  295. Dispatch_communication::Dispatch_request_from_manager_to_plc_for_key * m_dispatch_request_from_manager_to_plc_for_key =
  296. & Dispatch_communication::get_instance_references().m_dispatch_request_from_manager_to_plc_for_key;
  297. memset(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, 0, 50);
  298. int t_size1 = m_request_key.size()<=50 ? m_request_key.size() : 50 ;
  299. memcpy(m_dispatch_request_from_manager_to_plc_for_key->m_request_key, m_request_key.c_str(), t_size1);
  300. tp_dispatch_request_from_manager_to_plc_for_data->m_request_dispatch_motion_direction = m_request_dispatch_motion_direction;
  301. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_id = m_request_passageway_id;
  302. tp_dispatch_request_from_manager_to_plc_for_data->m_request_passageway_direction = m_request_passageway_direction;
  303. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_index_id = m_request_parkingspace_index_id;
  304. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_unit_id = m_request_parkingspace_unit_id;
  305. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_floor_id = m_request_parkingspace_floor_id;
  306. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_room_id = m_request_parkingspace_room_id;
  307. tp_dispatch_request_from_manager_to_plc_for_data->m_request_parkingspace_direction = m_request_parkingspace_direction;
  308. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_x = m_request_car_center_x;
  309. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_center_y = m_request_car_center_y;
  310. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_angle = m_request_car_angle;
  311. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_front_theta = m_request_car_front_theta;
  312. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_length = m_request_car_length;
  313. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_width = m_request_car_width;
  314. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_height = m_request_car_height;
  315. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base = m_request_car_wheel_base;
  316. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_width = m_request_car_wheel_width;
  317. memset(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, 0, 20);
  318. int t_size2 = m_request_key.size()<=20 ? m_request_key.size() : 20 ;
  319. memcpy(tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_license, m_request_car_license.c_str(), t_size2);
  320. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_type = m_request_car_type;
  321. tp_dispatch_request_from_manager_to_plc_for_data->m_request_anticollision_lidar_flag = m_request_anticollision_lidar_flag;
  322. tp_dispatch_request_from_manager_to_plc_for_data->m_request_car_wheel_base_exact_value = m_request_car_wheel_base_exact_value;
  323. //答复消息, plc->调度
  324. Dispatch_communication::Dispatch_response_from_plc_to_manager * tp_dispatch_response_from_plc_to_manager =
  325. & Dispatch_communication::get_instance_references().m_dispatch_response_from_plc_to_manager;
  326. m_response_key = (char*) tp_dispatch_response_from_plc_to_manager->m_response_key;
  327. m_response_status = (Dispatch_plc::Response_status)tp_dispatch_response_from_plc_to_manager->m_response_status;
  328. m_response_dispatch_motion_direction = (Common_data::Dispatch_motion_direction)tp_dispatch_response_from_plc_to_manager->m_response_dispatch_motion_direction;
  329. m_response_passageway_id = tp_dispatch_response_from_plc_to_manager->m_response_passageway_id;
  330. m_response_passageway_direction = (Common_data::Passageway_direction)tp_dispatch_response_from_plc_to_manager->m_response_passageway_direction;
  331. m_response_parkingspace_index_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_index_id;
  332. m_response_parkingspace_unit_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_unit_id;
  333. m_response_parkingspace_floor_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_floor_id;
  334. m_response_parkingspace_room_id = tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_room_id;
  335. m_response_parkingspace_direction = (Common_data::Parkingspace_direction)tp_dispatch_response_from_plc_to_manager->m_response_parkingspace_direction;
  336. m_response_car_center_x = tp_dispatch_response_from_plc_to_manager->m_response_car_center_x;
  337. m_response_car_center_y = tp_dispatch_response_from_plc_to_manager->m_response_car_center_y;
  338. m_response_car_angle = tp_dispatch_response_from_plc_to_manager->m_response_car_angle;
  339. m_response_car_front_theta = tp_dispatch_response_from_plc_to_manager->m_response_car_front_theta;
  340. m_response_car_length = tp_dispatch_response_from_plc_to_manager->m_response_car_length;
  341. m_response_car_width = tp_dispatch_response_from_plc_to_manager->m_response_car_width;
  342. m_response_car_height = tp_dispatch_response_from_plc_to_manager->m_response_car_height;
  343. m_response_car_wheel_base = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base;
  344. m_response_car_wheel_width = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_width;
  345. m_response_car_license = (char*) tp_dispatch_response_from_plc_to_manager->m_response_car_license;
  346. m_response_car_type = (Common_data::Car_type) tp_dispatch_response_from_plc_to_manager->m_response_car_type;
  347. m_response_anticollision_lidar_flag = (Common_data::Anticollision_lidar_flag)tp_dispatch_response_from_plc_to_manager->m_response_anticollision_lidar_flag;
  348. m_response_car_wheel_base_exact_value = tp_dispatch_response_from_plc_to_manager->m_response_car_wheel_base_exact_value;
  349. /*
  350. std::cout << " huli test :::: " << " ------------------------------------------------------ = " << std::endl;
  351. std::cout << " huli test :::: " << " m_response_key = " << m_response_key << std::endl;
  352. std::cout << " huli test :::: " << " m_response_status = " << (int)m_response_status << std::endl;
  353. std::cout << " huli test :::: " << " m_response_dispatch_motion_direction = " << (int)m_response_dispatch_motion_direction << std::endl;
  354. std::cout << " huli test :::: " << " m_response_passageway_id = " << m_response_passageway_id << std::endl;
  355. std::cout << " huli test :::: " << " m_response_passageway_direction = " << (int)m_response_passageway_direction << std::endl;
  356. std::cout << " huli test :::: " << " m_response_parkingspace_index_id = " << m_response_parkingspace_index_id << std::endl;
  357. std::cout << " huli test :::: " << " m_response_parkingspace_unit_id = " << m_response_parkingspace_unit_id << std::endl;
  358. std::cout << " huli test :::: " << " m_response_parkingspace_floor_id = " << m_response_parkingspace_floor_id << std::endl;
  359. std::cout << " huli test :::: " << " m_response_parkingspace_room_id = " << m_response_parkingspace_room_id << std::endl;
  360. std::cout << " huli test :::: " << " m_response_parkingspace_direction = " << (int)m_response_parkingspace_direction << std::endl;
  361. std::cout << " huli test :::: " << " m_response_car_center_x = " << m_response_car_center_x << std::endl;
  362. std::cout << " huli test :::: " << " m_response_car_center_y = " << m_response_car_center_y << std::endl;
  363. std::cout << " huli test :::: " << " m_response_car_license = " << m_response_car_license << std::endl;
  364. */
  365. //状态消息, plc->调度
  366. Dispatch_communication::Dispatch_plc_status_from_plc_to_manager * tp_dispatch_plc_status_from_plc_to_manager =
  367. & Dispatch_communication::get_instance_references().m_dispatch_plc_status_from_plc_to_manager;
  368. //need
  369. // //通过心跳帧来判断通信是否正常
  370. // if ( m_last_heartbeat != tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat )
  371. // {
  372. // m_last_heartbeat = tp_dispatch_plc_status_from_plc_to_manager->m_heartbeat;
  373. // m_status_updata_time = std::chrono::system_clock::now();
  374. //
  375. // if ( m_dispatch_plc_status == Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT )
  376. // {
  377. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_READY;
  378. // }
  379. // }
  380. // else if(std::chrono::system_clock::now() - m_status_updata_time > std::chrono::milliseconds(COMMUNICATION_OVER_TIME_MS))
  381. // {
  382. // m_dispatch_plc_status = Dispatch_plc::Dispatch_plc_status::DISPATCH_PLC_DISCONNECT;
  383. // }
  384. // //else 继续等待,直到消息刷新或者超时.
  385. return Error_code::SUCCESS;
  386. }
  387. //检查plc答复反馈
  388. Error_manager Dispatch_plc::check_response_from_plc()
  389. {
  390. std::unique_lock<std::mutex> t_lock(m_lock);
  391. //need, 调试代码, 延时10s, 直接返回成功
  392. if ( std::chrono::system_clock::now() - m_start_time > std::chrono::seconds(10) )
  393. {
  394. return Error_code::SUCCESS;
  395. }
  396. //返回没有收到数据
  397. else
  398. {
  399. return Error_code::NODATA;
  400. }
  401. //检查唯一码
  402. if ( m_response_key == m_request_key )
  403. {
  404. //如果故障,则添加错误码
  405. if ( m_response_status != RESPONS_WORKING )
  406. {
  407. //添加错误码
  408. Error_manager t_error(DISPATCH_PLC_RESPONS_ERROR, MINOR_ERROR, " Dispatch_plc::check_response_from_plc() m_respons_status is error");
  409. return t_error;
  410. }
  411. return Error_code::SUCCESS;
  412. }
  413. //返回没有收到数据
  414. else
  415. {
  416. return Error_code::NODATA;
  417. }
  418. return Error_code::SUCCESS;
  419. }
  420. //发送调度答复信息给主控
  421. Error_manager Dispatch_plc::send_dispatch_response_to_main_control()
  422. {
  423. std::unique_lock<std::mutex> t_lock(m_lock);
  424. m_dispatch_response_msg.mutable_base_info()->set_msg_type(message::Message_type::eDispatch_response_msg);
  425. m_dispatch_response_msg.mutable_base_info()->set_timeout_ms(m_dispatch_request_msg.base_info().timeout_ms());
  426. m_dispatch_response_msg.mutable_base_info()->set_sender(message::Communicator::eDispatch_manager);
  427. m_dispatch_response_msg.mutable_base_info()->set_receiver(message::Communicator::eMain);
  428. m_dispatch_response_msg.set_command_key(m_dispatch_request_msg.command_key());
  429. m_dispatch_response_msg.set_dispatch_motion_direction(m_dispatch_request_msg.dispatch_motion_direction());
  430. m_dispatch_response_msg.set_terminal_id(m_dispatch_request_msg.terminal_id());
  431. m_dispatch_response_msg.mutable_locate_information()->CopyFrom(m_dispatch_request_msg.locate_information());
  432. m_dispatch_response_msg.mutable_parkspace_info_ex()->CopyFrom(m_dispatch_request_msg.parkspace_info_ex());
  433. m_dispatch_response_msg.set_car_type(m_dispatch_request_msg.car_type());
  434. if ( m_dispatch_response_msg.dispatch_motion_direction() == message::E_STORE_CAR )
  435. {
  436. for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  437. {
  438. if ( m_result == Error_code::SUCCESS )
  439. {
  440. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  441. }
  442. else
  443. {
  444. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  445. }
  446. }
  447. }
  448. else if( m_dispatch_response_msg.dispatch_motion_direction() == message::E_PICKUP_CAR )
  449. {
  450. for (int i = 0; i < m_dispatch_response_msg.parkspace_info_ex_size(); ++i)
  451. {
  452. if ( m_result == Error_code::SUCCESS )
  453. {
  454. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_empty);
  455. }
  456. else
  457. {
  458. m_dispatch_response_msg.mutable_parkspace_info_ex(i)->set_parkspace_status_target(message::Parkspace_status::eParkspace_occupied);
  459. }
  460. }
  461. }
  462. m_dispatch_response_msg.mutable_error_manager()->set_error_code(m_result.get_error_code());
  463. m_dispatch_response_msg.mutable_error_manager()->set_error_level((message::Error_level)m_result.get_error_level());
  464. m_dispatch_response_msg.mutable_error_manager()->set_error_description(m_result.get_error_description());
  465. std::cout << " huli test :::: " << " m_dispatch_response_msg = " << m_dispatch_response_msg.DebugString()<< std::endl;
  466. std::string t_msg = m_dispatch_response_msg.SerializeAsString();
  467. System_communication::get_instance_references().encapsulate_msg(t_msg);
  468. return Error_code::SUCCESS;
  469. }