dispatch_singlechip.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // Created by huli on 2021/11/18.
  3. //
  4. #include "dispatch_singlechip.h"
  5. Dispatch_singlechip::Dispatch_singlechip()
  6. {
  7. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_UNKNOW;
  8. m_plc_id = 0;
  9. m_singlechip_id = 0;
  10. m_singlechip_data_msg_updata_flag = false;
  11. m_validity = false;
  12. }
  13. Dispatch_singlechip::~Dispatch_singlechip()
  14. {
  15. dispatch_singlechip_uninit();
  16. }
  17. //调度单片机 初始化
  18. Error_manager Dispatch_singlechip::dispatch_singlechip_init(int plc_id, int singlechip_id, int singlechip_direction)
  19. {
  20. m_plc_id = plc_id;
  21. m_singlechip_id = singlechip_id;
  22. m_singlechip_direction = singlechip_direction;
  23. mp_execute_thread = nullptr ;
  24. m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
  25. m_singlechip_data_msg_updata_flag = false;
  26. // 线程默认开启
  27. m_execute_condition.reset(false, true, false);
  28. mp_execute_thread = new std::thread(&Dispatch_singlechip::execute_thread_fun, this);
  29. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_READY;
  30. return Error_code::SUCCESS;
  31. }
  32. //调度单片机 反初始化
  33. Error_manager Dispatch_singlechip::dispatch_singlechip_uninit()
  34. {
  35. if (mp_execute_thread)
  36. {
  37. m_execute_condition.kill_all();
  38. }
  39. if (mp_execute_thread)
  40. {
  41. mp_execute_thread->join();
  42. delete mp_execute_thread;
  43. mp_execute_thread = NULL;
  44. }
  45. m_dispatch_singlechip_status = Dispatch_singlechip::DISPATCH_SINGLECHIP_UNKNOW;
  46. return Error_code::SUCCESS;
  47. }
  48. //调度单片机 执行状态消息
  49. Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg(message::Singlechip_data &singlechip_data_msg, bool validity)
  50. {
  51. std::unique_lock<std::mutex> t_lock2(m_lock);
  52. m_singlechip_data_msg = singlechip_data_msg;
  53. m_validity = validity;
  54. m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
  55. m_singlechip_data_msg_updata_flag = true;
  56. return Error_code::SUCCESS;
  57. }
  58. Dispatch_singlechip::Dispatch_singlechip_status Dispatch_singlechip::get_dispatch_singlechip_status()
  59. {
  60. return m_dispatch_singlechip_status;
  61. }
  62. //判断出口是否空闲
  63. bool Dispatch_singlechip::is_outlet_ready()
  64. {
  65. if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY &&
  66. m_singlechip_data_msg.insideexistenceflag() == 0)
  67. {
  68. return true;
  69. }
  70. else
  71. {
  72. return false;
  73. }
  74. }
  75. //执行外界任务的执行函数
  76. void Dispatch_singlechip::execute_thread_fun()
  77. {
  78. LOG(INFO) << " Dispatch_singlechip::execute_thread_fun() start " << this;
  79. Error_manager t_error;
  80. while (m_execute_condition.is_alive())
  81. {
  82. m_execute_condition.wait();
  83. if (m_execute_condition.is_alive())
  84. {
  85. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  86. // std::this_thread::sleep_for(std::chrono::seconds(1));
  87. std::this_thread::yield();
  88. if (m_singlechip_data_msg_updata_flag)
  89. {
  90. // std::cout << " huli test :::: " << " m_singlechip_data_msg = " << m_singlechip_data_msg.DebugString() << std::endl;
  91. std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
  92. std::unique_lock<std::mutex> t_lock2(m_lock);
  93. //将nnxx的protobuf 转化为 snap7的DB块, 把店面雷达数据转发给plc
  94. int t_temp = (m_singlechip_data_msg.terminalid() % 2) + (m_singlechip_data_msg.dispatchdirection()-1)*2;
  95. Dispatch_communication::Singlechip_response_from_manager_to_plc *p_response = &Dispatch_communication::get_instance_references().m_singlechip_response_from_manager_to_plc[t_temp];
  96. Dispatch_communication::Singlechip_request_from_plc_to_manager *p_request = &Dispatch_communication::get_instance_references().m_singlechip_request_from_plc_to_manager[t_temp];
  97. if (p_request->m_request_communication_mode != 2)
  98. {
  99. p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
  100. p_response->m_response_communication_mode = p_request->m_request_communication_mode;
  101. p_response->m_response_refresh_command = p_request->m_request_refresh_command;
  102. p_response->m_response_data_validity = m_validity;
  103. p_response->m_process_status = m_singlechip_data_msg.processstatus();
  104. p_response->m_process_control = m_singlechip_data_msg.processcontrol();
  105. p_response->m_over_border_status = m_singlechip_data_msg.overborderstatus();
  106. p_response->m_car_height_status_current = m_singlechip_data_msg.carheightstatuscurrent();
  107. p_response->m_car_height_status_passing = m_singlechip_data_msg.carheightstatuspassing();
  108. p_response->m_outside_door_status = m_singlechip_data_msg.outsidedoorstatus();
  109. p_response->m_outside_door_control = m_singlechip_data_msg.outsidedoorcontrol();
  110. p_response->m_dispatch_finish_flag = m_singlechip_data_msg.dispatchfinishflag();
  111. p_response->m_inside_existence_flag = m_singlechip_data_msg.insideexistenceflag();
  112. p_response->m_outside_existence_flag = m_singlechip_data_msg.outsideexistenceflag();
  113. p_response->m_reset_flag = m_singlechip_data_msg.resetflag();
  114. p_response->m_stop_flag = m_singlechip_data_msg.stopflag();
  115. p_response->m_error_code = 0;
  116. p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
  117. p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
  118. p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
  119. p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
  120. p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
  121. p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
  122. p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
  123. p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
  124. p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
  125. p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
  126. p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
  127. p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
  128. p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
  129. p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
  130. p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
  131. p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();
  132. }
  133. else if (p_response->m_response_refresh_command == p_request->m_request_refresh_command)
  134. {
  135. p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
  136. p_response->m_response_communication_mode = p_request->m_request_communication_mode;
  137. p_response->m_response_refresh_command = p_request->m_request_refresh_command;
  138. }
  139. else
  140. {
  141. p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
  142. p_response->m_response_communication_mode = p_request->m_request_communication_mode;
  143. p_response->m_response_refresh_command = p_request->m_request_refresh_command;
  144. p_response->m_response_data_validity = m_validity;
  145. p_response->m_process_status = m_singlechip_data_msg.processstatus();
  146. p_response->m_process_control = m_singlechip_data_msg.processcontrol();
  147. p_response->m_over_border_status = m_singlechip_data_msg.overborderstatus();
  148. p_response->m_car_height_status_current = m_singlechip_data_msg.carheightstatuscurrent();
  149. p_response->m_car_height_status_passing = m_singlechip_data_msg.carheightstatuspassing();
  150. p_response->m_outside_door_status = m_singlechip_data_msg.outsidedoorstatus();
  151. p_response->m_outside_door_control = m_singlechip_data_msg.outsidedoorcontrol();
  152. p_response->m_dispatch_finish_flag = m_singlechip_data_msg.dispatchfinishflag();
  153. p_response->m_inside_existence_flag = m_singlechip_data_msg.insideexistenceflag();
  154. p_response->m_outside_existence_flag = m_singlechip_data_msg.outsideexistenceflag();
  155. p_response->m_reset_flag = m_singlechip_data_msg.resetflag();
  156. p_response->m_stop_flag = m_singlechip_data_msg.stopflag();
  157. p_response->m_error_code = 0;
  158. p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
  159. p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
  160. p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
  161. p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
  162. p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
  163. p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
  164. p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
  165. p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
  166. p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
  167. p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
  168. p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
  169. p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
  170. p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
  171. p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
  172. p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
  173. p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();
  174. }
  175. m_singlechip_data_msg_updata_flag = false;
  176. }
  177. //判断超时
  178. {
  179. std::unique_lock<std::mutex> t_lock3(m_lock);
  180. if (std::chrono::system_clock::now() - m_singlechip_data_msg_updata_time > std::chrono::seconds(5))
  181. {
  182. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_DISCONNECT;
  183. }
  184. else
  185. {
  186. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_READY;
  187. }
  188. }
  189. }
  190. }
  191. }