dispatch_singlechip.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. //调度单片机 初始化, 单片机id, 0或者1, 单片机方向, 1是入口, 2是出口
  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. //
  52. //// std::unique_lock<std::mutex> t_lock2(m_lock);
  53. //// m_singlechip_data_msg = singlechip_data_msg;
  54. //// m_validity = validity;
  55. //// m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
  56. //// m_singlechip_data_msg_updata_flag = true;
  57. //
  58. // return Error_code::SUCCESS;
  59. //}
  60. //调度单片机 执行状态消息
  61. Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg_new(in_mcpu_statu &t_in_mcpu_statu, bool validity)
  62. {
  63. std::unique_lock<std::mutex> t_lock2(m_lock);
  64. m_in_mcpu_statu = t_in_mcpu_statu;
  65. m_validity = validity;
  66. m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
  67. m_singlechip_data_msg_updata_flag = true;
  68. return Error_code::SUCCESS;
  69. }
  70. //调度单片机 执行状态消息
  71. Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg_new(out_mcpu_statu &t_out_mcpu_statu, bool validity)
  72. {
  73. std::unique_lock<std::mutex> t_lock2(m_lock);
  74. m_out_mcpu_statu = t_out_mcpu_statu;
  75. m_validity = validity;
  76. m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
  77. m_singlechip_data_msg_updata_flag = true;
  78. return Error_code::SUCCESS;
  79. }
  80. Dispatch_singlechip::Dispatch_singlechip_status Dispatch_singlechip::get_dispatch_singlechip_status()
  81. {
  82. return m_dispatch_singlechip_status;
  83. }
  84. //判断出口是否空闲
  85. bool Dispatch_singlechip::is_outlet_ready()
  86. {
  87. std::unique_lock<std::mutex> t_lock(m_lock);
  88. // outside_safety 0无效, 1无车, 2有车
  89. if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY &&
  90. // m_singlechip_data_msg.insideexistenceflag() == 0)
  91. m_out_mcpu_statu.outside_safety() == 1)
  92. {
  93. return true;
  94. }
  95. else
  96. {
  97. return false;
  98. }
  99. }
  100. //执行外界任务的执行函数
  101. void Dispatch_singlechip::execute_thread_fun()
  102. {
  103. LOG(INFO) << " Dispatch_singlechip::execute_thread_fun() start " << this;
  104. Error_manager t_error;
  105. while (m_execute_condition.is_alive())
  106. {
  107. m_execute_condition.wait();
  108. if (m_execute_condition.is_alive())
  109. {
  110. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  111. // std::this_thread::sleep_for(std::chrono::seconds(1));
  112. std::this_thread::yield();
  113. // std::cout << " huli test :::: " << "uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu m_singlechip_data_msg_updata_flag = " << m_singlechip_data_msg_updata_flag << std::endl;
  114. if (m_singlechip_data_msg_updata_flag)
  115. {
  116. // std::cout << " huli test :::: " << "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj m_singlechip_data_msg_updata_flag = " << m_singlechip_data_msg_updata_flag << std::endl;
  117. // std::cout << " huli test :::: " << " m_singlechip_data_msg = " << m_singlechip_data_msg.DebugString() << std::endl;
  118. std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
  119. std::unique_lock<std::mutex> t_lock2(m_lock);
  120. //将nnxx的protobuf 转化为 snap7的DB块, 把店面雷达数据转发给plc
  121. // int t_temp = (m_singlechip_data_msg.terminalid() % 2) + (m_singlechip_data_msg.dispatchdirection()-1)*2;
  122. int t_temp = (m_singlechip_direction-1)*2 + m_singlechip_id;
  123. Dispatch_communication::Singlechip_response_from_manager_to_plc *p_response = &Dispatch_communication::get_instance_references().m_singlechip_response_from_manager_to_plc[t_temp];
  124. Dispatch_communication::Singlechip_request_from_plc_to_manager *p_request = &Dispatch_communication::get_instance_references().m_singlechip_request_from_plc_to_manager[t_temp];
  125. if (p_request->m_request_communication_mode != 2)
  126. {
  127. p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
  128. // int heartbeat = p_response->m_response_heartbeat;
  129. // std::cout << " huli test :::: " << " mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmp_response->m_response_heartbeat = " << heartbeat << std::endl;
  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. //
  147. // p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
  148. // p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
  149. // p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
  150. // p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
  151. // p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
  152. // p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
  153. // p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
  154. // p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
  155. // p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
  156. // p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
  157. // p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
  158. // p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
  159. // p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
  160. // p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
  161. // p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
  162. // p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();
  163. if ( m_singlechip_direction ==1 )
  164. {
  165. p_response->m_outside_door_status = m_in_mcpu_statu.door_statu()-1;
  166. p_response->m_over_border_status = m_in_mcpu_statu.back_io()-1;
  167. p_response->m_inside_existence_flag = m_in_mcpu_statu.is_occupy()-1;
  168. p_response->m_car_height_status_current = m_in_mcpu_statu.heighth()-1;
  169. }
  170. else if(m_singlechip_direction ==2)
  171. {
  172. p_response->m_outside_door_status = m_out_mcpu_statu.door_statu()-1;
  173. p_response->m_inside_existence_flag = m_out_mcpu_statu.outside_safety()-1;
  174. }
  175. }
  176. else if (p_response->m_response_refresh_command == p_request->m_request_refresh_command)
  177. {
  178. p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
  179. p_response->m_response_communication_mode = p_request->m_request_communication_mode;
  180. p_response->m_response_refresh_command = p_request->m_request_refresh_command;
  181. }
  182. else
  183. {
  184. p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
  185. p_response->m_response_communication_mode = p_request->m_request_communication_mode;
  186. p_response->m_response_refresh_command = p_request->m_request_refresh_command;
  187. p_response->m_response_data_validity = m_validity;
  188. // p_response->m_process_status = m_singlechip_data_msg.processstatus();
  189. // p_response->m_process_control = m_singlechip_data_msg.processcontrol();
  190. // p_response->m_over_border_status = m_singlechip_data_msg.overborderstatus();
  191. // p_response->m_car_height_status_current = m_singlechip_data_msg.carheightstatuscurrent();
  192. // p_response->m_car_height_status_passing = m_singlechip_data_msg.carheightstatuspassing();
  193. // p_response->m_outside_door_status = m_singlechip_data_msg.outsidedoorstatus();
  194. // p_response->m_outside_door_control = m_singlechip_data_msg.outsidedoorcontrol();
  195. // p_response->m_dispatch_finish_flag = m_singlechip_data_msg.dispatchfinishflag();
  196. // p_response->m_inside_existence_flag = m_singlechip_data_msg.insideexistenceflag();
  197. // p_response->m_outside_existence_flag = m_singlechip_data_msg.outsideexistenceflag();
  198. // p_response->m_reset_flag = m_singlechip_data_msg.resetflag();
  199. // p_response->m_stop_flag = m_singlechip_data_msg.stopflag();
  200. // p_response->m_error_code = 0;
  201. //
  202. // p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
  203. // p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
  204. // p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
  205. // p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
  206. // p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
  207. // p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
  208. // p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
  209. // p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
  210. // p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
  211. // p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
  212. // p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
  213. // p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
  214. // p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
  215. // p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
  216. // p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
  217. // p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();
  218. if ( m_singlechip_direction ==1 )
  219. {
  220. p_response->m_outside_door_status = m_in_mcpu_statu.door_statu()-1;
  221. p_response->m_over_border_status = m_in_mcpu_statu.back_io()-1;
  222. p_response->m_inside_existence_flag = m_in_mcpu_statu.is_occupy()-1;
  223. p_response->m_car_height_status_current = m_in_mcpu_statu.heighth()-1;
  224. }
  225. else if(m_singlechip_direction ==2)
  226. {
  227. p_response->m_outside_door_status = m_out_mcpu_statu.door_statu()-1;
  228. p_response->m_inside_existence_flag = m_out_mcpu_statu.outside_safety()-1;
  229. }
  230. }
  231. m_singlechip_data_msg_updata_flag = false;
  232. }
  233. //判断超时
  234. {
  235. std::unique_lock<std::mutex> t_lock3(m_lock);
  236. if (std::chrono::system_clock::now() - m_singlechip_data_msg_updata_time > std::chrono::seconds(5))
  237. {
  238. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_DISCONNECT;
  239. }
  240. else
  241. {
  242. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_READY;
  243. }
  244. }
  245. }
  246. }
  247. }