dispatch_singlechip.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. void Dispatch_singlechip::execute_thread_fun()
  64. {
  65. LOG(INFO) << " Dispatch_singlechip::execute_thread_fun() start " << this;
  66. Error_manager t_error;
  67. while (m_execute_condition.is_alive())
  68. {
  69. m_execute_condition.wait();
  70. if (m_execute_condition.is_alive())
  71. {
  72. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  73. // std::this_thread::sleep_for(std::chrono::seconds(1));
  74. std::this_thread::yield();
  75. // std::cout << " huli test :::: " << " m_singlechip_data_msg = " << m_singlechip_data_msg.DebugString() << std::endl;
  76. if (m_singlechip_data_msg_updata_flag)
  77. {
  78. std::cout << " huli test :::: " << " m_singlechip_data_msg = " << m_singlechip_data_msg.DebugString() << std::endl;
  79. std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
  80. std::unique_lock<std::mutex> t_lock2(m_lock);
  81. //将nnxx的protobuf 转化为 snap7的DB块, 把店面雷达数据转发给plc
  82. int t_temp = (m_singlechip_data_msg.terminalid() % 2) + (m_singlechip_data_msg.dispatchdirection()-1)*2;
  83. Dispatch_communication::Singlechip_response_from_manager_to_plc *p_response = &Dispatch_communication::get_instance_references().m_singlechip_response_from_manager_to_plc[t_temp];
  84. Dispatch_communication::Singlechip_request_from_plc_to_manager *p_request = &Dispatch_communication::get_instance_references().m_singlechip_request_from_plc_to_manager[t_temp];
  85. if (p_request->m_request_communication_mode != 2)
  86. {
  87. p_response->m_response_heartbeat = 1-p_response->m_response_heartbeat;
  88. p_response->m_response_communication_mode = p_request->m_request_communication_mode;
  89. p_response->m_response_refresh_command = p_request->m_request_refresh_command;
  90. p_response->m_response_data_validity = m_validity;
  91. p_response->m_process_status = m_singlechip_data_msg.processstatus();
  92. p_response->m_process_control = m_singlechip_data_msg.processcontrol();
  93. p_response->m_over_border_status = m_singlechip_data_msg.overborderstatus();
  94. p_response->m_car_height_status_current = m_singlechip_data_msg.carheightstatuscurrent();
  95. p_response->m_car_height_status_passing = m_singlechip_data_msg.carheightstatuspassing();
  96. p_response->m_outside_door_status = m_singlechip_data_msg.outsidedoorstatus();
  97. p_response->m_outside_door_control = m_singlechip_data_msg.outsidedoorcontrol();
  98. p_response->m_dispatch_finish_flag = m_singlechip_data_msg.dispatchfinishflag();
  99. p_response->m_inside_existence_flag = m_singlechip_data_msg.insideexistenceflag();
  100. p_response->m_outside_existence_flag = m_singlechip_data_msg.outsideexistenceflag();
  101. p_response->m_reset_flag = m_singlechip_data_msg.resetflag();
  102. p_response->m_stop_flag = m_singlechip_data_msg.stopflag();
  103. p_response->m_error_code = 0;
  104. p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
  105. p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
  106. p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
  107. p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
  108. p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
  109. p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
  110. p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
  111. p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
  112. p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
  113. p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
  114. p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
  115. p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
  116. p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
  117. p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
  118. p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
  119. p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();
  120. }
  121. else if (p_response->m_response_refresh_command == p_request->m_request_refresh_command)
  122. {
  123. p_response->m_response_heartbeat = 1-p_response->m_response_heartbeat;
  124. p_response->m_response_communication_mode = p_request->m_request_communication_mode;
  125. p_response->m_response_refresh_command = p_request->m_request_refresh_command;
  126. }
  127. else
  128. {
  129. p_response->m_response_heartbeat = 1-p_response->m_response_heartbeat;
  130. p_response->m_response_communication_mode = p_request->m_request_communication_mode;
  131. p_response->m_response_refresh_command = p_request->m_request_refresh_command;
  132. p_response->m_response_data_validity = m_validity;
  133. p_response->m_process_status = m_singlechip_data_msg.processstatus();
  134. p_response->m_process_control = m_singlechip_data_msg.processcontrol();
  135. p_response->m_over_border_status = m_singlechip_data_msg.overborderstatus();
  136. p_response->m_car_height_status_current = m_singlechip_data_msg.carheightstatuscurrent();
  137. p_response->m_car_height_status_passing = m_singlechip_data_msg.carheightstatuspassing();
  138. p_response->m_outside_door_status = m_singlechip_data_msg.outsidedoorstatus();
  139. p_response->m_outside_door_control = m_singlechip_data_msg.outsidedoorcontrol();
  140. p_response->m_dispatch_finish_flag = m_singlechip_data_msg.dispatchfinishflag();
  141. p_response->m_inside_existence_flag = m_singlechip_data_msg.insideexistenceflag();
  142. p_response->m_outside_existence_flag = m_singlechip_data_msg.outsideexistenceflag();
  143. p_response->m_reset_flag = m_singlechip_data_msg.resetflag();
  144. p_response->m_stop_flag = m_singlechip_data_msg.stopflag();
  145. p_response->m_error_code = 0;
  146. p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
  147. p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
  148. p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
  149. p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
  150. p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
  151. p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
  152. p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
  153. p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
  154. p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
  155. p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
  156. p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
  157. p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
  158. p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
  159. p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
  160. p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
  161. p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();
  162. }
  163. m_singlechip_data_msg_updata_flag = false;
  164. }
  165. //判断超时
  166. {
  167. std::unique_lock<std::mutex> t_lock3(m_lock);
  168. if (std::chrono::system_clock::now() - m_singlechip_data_msg_updata_time > std::chrono::seconds(5))
  169. {
  170. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_DISCONNECT;
  171. }
  172. else
  173. {
  174. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_READY;
  175. }
  176. }
  177. }
  178. }
  179. }