dispatch_singlechip.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. //
  2. // Created by huli on 2021/11/18.
  3. //
  4. #include "dispatch_singlechip.h"
  5. #include "../system/system_communication.h"
  6. #include "../dispatch/dispatch_manager.h"
  7. Dispatch_singlechip::Dispatch_singlechip()
  8. {
  9. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_UNKNOW;
  10. m_plc_id = 0;
  11. m_singlechip_id = 0;
  12. m_singlechip_data_msg_updata_flag = false;
  13. m_validity = false;
  14. }
  15. Dispatch_singlechip::~Dispatch_singlechip()
  16. {
  17. dispatch_singlechip_uninit();
  18. }
  19. //调度单片机 初始化, 单片机id, 0或者1, 单片机方向, 1是入口, 2是出口
  20. Error_manager Dispatch_singlechip::dispatch_singlechip_init(int plc_id, int singlechip_id, int singlechip_direction)
  21. {
  22. m_plc_id = plc_id;
  23. m_singlechip_id = singlechip_id;
  24. m_singlechip_direction = singlechip_direction;
  25. mp_execute_thread = nullptr ;
  26. mp_updata_thread = nullptr ;
  27. m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
  28. m_singlechip_data_msg_updata_flag = false;
  29. // 线程默认开启
  30. m_execute_condition.reset(false, true, false);
  31. mp_execute_thread = new std::thread(&Dispatch_singlechip::execute_thread_fun, this);
  32. // 线程默认开启
  33. m_updata_condition.reset(false, true, false);
  34. mp_updata_thread = new std::thread(&Dispatch_singlechip::updata_thread_fun, this);
  35. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_READY;
  36. m_close_outsidedoor_flag = false;
  37. return Error_code::SUCCESS;
  38. }
  39. //调度单片机 反初始化
  40. Error_manager Dispatch_singlechip::dispatch_singlechip_uninit()
  41. {
  42. if (mp_execute_thread)
  43. {
  44. m_execute_condition.kill_all();
  45. }
  46. if (mp_execute_thread)
  47. {
  48. mp_execute_thread->join();
  49. delete mp_execute_thread;
  50. mp_execute_thread = NULL;
  51. }
  52. m_dispatch_singlechip_status = Dispatch_singlechip::DISPATCH_SINGLECHIP_UNKNOW;
  53. return Error_code::SUCCESS;
  54. }
  55. //调度单片机 执行状态消息
  56. //Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg(message::Singlechip_data &singlechip_data_msg, bool validity)
  57. //{
  58. //
  59. //// std::unique_lock<std::mutex> t_lock2(m_lock);
  60. //// m_singlechip_data_msg = singlechip_data_msg;
  61. //// m_validity = validity;
  62. //// m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
  63. //// m_singlechip_data_msg_updata_flag = true;
  64. //
  65. // return Error_code::SUCCESS;
  66. //}
  67. //调度单片机 执行状态消息
  68. Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg_new(in_mcpu_statu &t_in_mcpu_statu, bool validity)
  69. {
  70. std::unique_lock<std::mutex> t_lock2(m_lock);
  71. m_in_mcpu_statu = t_in_mcpu_statu;
  72. m_validity = validity;
  73. m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
  74. m_singlechip_data_msg_updata_flag = true;
  75. return Error_code::SUCCESS;
  76. }
  77. //调度单片机 执行状态消息
  78. Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg_new(out_mcpu_statu &t_out_mcpu_statu, bool validity)
  79. {
  80. std::unique_lock<std::mutex> t_lock2(m_lock);
  81. m_out_mcpu_statu = t_out_mcpu_statu;
  82. m_validity = validity;
  83. m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
  84. m_singlechip_data_msg_updata_flag = true;
  85. return Error_code::SUCCESS;
  86. }
  87. //单片机消息, 数据处理, 去头,去尾 //删除头部 @501, 删除尾部 $,
  88. Error_manager Dispatch_singlechip::data_processing(std::string & put_in, std::string & put_out)
  89. {
  90. int t_head_index = put_in.find("@");
  91. int t_tail_index = put_in.find('$');
  92. if ( t_head_index >=0 && t_tail_index > t_head_index )
  93. {
  94. //删除头部 @501, 删除尾部 $, 保留 {}
  95. put_out = put_in.substr(4, t_tail_index-t_head_index-4);
  96. }
  97. else
  98. {
  99. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  100. " fun error ");
  101. }
  102. return Error_code::SUCCESS;
  103. }
  104. //调度单片机 执行状态消息, 单片机通过tcp发过来的原始字符串
  105. Error_manager Dispatch_singlechip::execute_for_singlechip_data_msg_new(std::string singlechip_data_string)
  106. {
  107. Error_manager t_error;
  108. std::string t_singlechip_data_string_processing; //处理后的临时字符串
  109. //单片机消息, 数据处理, 去头,去尾 //删除头部 @501, 删除尾部 $,
  110. t_error = data_processing(singlechip_data_string, t_singlechip_data_string_processing);
  111. if ( t_error == Error_code::SUCCESS )
  112. {
  113. //json转protobuf
  114. message::Singlechip_data t_singlechip_data_message; //单片机的数据, proto格式
  115. auto result = google::protobuf::util::JsonStringToMessage(t_singlechip_data_string_processing,&t_singlechip_data_message);
  116. if ( result.error_code() == 0 )
  117. {
  118. m_singlechip_data_msg = t_singlechip_data_message;
  119. m_singlechip_data_msg_updata_time = std::chrono::system_clock::now();
  120. m_singlechip_data_msg_updata_flag = true;
  121. m_validity = true;
  122. }
  123. else
  124. {
  125. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  126. " fun error ");
  127. }
  128. }
  129. else
  130. {
  131. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  132. " fun error ");
  133. }
  134. return Error_code::SUCCESS;
  135. }
  136. Dispatch_singlechip::Dispatch_singlechip_status Dispatch_singlechip::get_dispatch_singlechip_status()
  137. {
  138. return m_dispatch_singlechip_status;
  139. }
  140. //判断出口是否空闲, 没有车就是空闲, 返回true
  141. bool Dispatch_singlechip::is_outlet_ready()
  142. {
  143. std::unique_lock<std::mutex> t_lock(m_lock);
  144. if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY &&
  145. // m_singlechip_data_msg.insideexistenceflag() == 0)
  146. m_out_mcpu_statu.outside_safety() == 1)// outside_safety 0无效, 1无车, 2有车
  147. {
  148. return true;
  149. }
  150. else
  151. {
  152. return false;
  153. }
  154. }
  155. //判断入口是否空闲, 没有车就是空闲, 返回true
  156. bool Dispatch_singlechip::is_inlet_ready()
  157. {
  158. std::unique_lock<std::mutex> t_lock(m_lock);
  159. if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY &&
  160. // m_singlechip_data_msg.insideexistenceflag() == 0)
  161. m_in_mcpu_statu.is_occupy() == 1)// 0无效, 1无车, 2有车
  162. {
  163. return true;
  164. }
  165. else
  166. {
  167. return false;
  168. }
  169. }
  170. //获取外门状态, 0表示未知, 1表示外门开到位, 2表示外门关到位, 3表示外门运行中 4表示外门故障
  171. message::Outside_door_status Dispatch_singlechip::get_outside_door_status()
  172. {
  173. std::unique_lock<std::mutex> t_lock(m_lock);
  174. if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY )
  175. {
  176. return m_singlechip_data_msg.outsidedoorstatus();
  177. }
  178. else
  179. {
  180. return message::Outside_door_status::OUTSIDE_DOOR_STATUS_UNKNOWN;
  181. }
  182. }
  183. //获取内部是否有车, 0表示无车, 1表示有车. -1表示未知
  184. int Dispatch_singlechip::get_inside_existence_flag()
  185. {
  186. std::unique_lock<std::mutex> t_lock(m_lock);
  187. if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY )
  188. {
  189. return m_singlechip_data_msg.insideexistenceflag();
  190. }
  191. else
  192. {
  193. return -1;
  194. }
  195. }
  196. //任务执行线程, 负责门控和数据库操作
  197. void Dispatch_singlechip::execute_thread_fun()
  198. {
  199. LOG(INFO) << " Dispatch_singlechip::execute_thread_fun() start " << this;
  200. Error_manager t_error;
  201. while (m_execute_condition.is_alive())
  202. {
  203. m_execute_condition.wait();
  204. if (m_execute_condition.is_alive())
  205. {
  206. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  207. // std::this_thread::sleep_for(std::chrono::seconds(1));
  208. //门控线程, 每1秒循环一次即可.
  209. static auto s_last_time = std::chrono::system_clock::now();
  210. if ( std::chrono::system_clock::now() - s_last_time >= std::chrono::seconds(1) )
  211. {
  212. if ( m_singlechip_direction == 1 )
  213. {
  214. inlet_outsidedoor_control();
  215. }
  216. else if ( m_singlechip_direction == 2 )
  217. {
  218. outlet_outsidedoor_control();
  219. }
  220. s_last_time = std::chrono::system_clock::now();
  221. }
  222. std::this_thread::yield();
  223. //...
  224. }
  225. }
  226. }
  227. //入口外门控制函数
  228. Error_manager Dispatch_singlechip::inlet_outsidedoor_control()
  229. {
  230. // return;
  231. Error_manager t_error;
  232. if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY )
  233. {
  234. int t_terminal_id = m_plc_id*2 + m_singlechip_id;
  235. int t_singlechip_index = (m_singlechip_direction-1)*2 + m_singlechip_id;
  236. std::string t_primary_key_now = "";
  237. t_error = Dispatch_manager::get_instance_references().m_dispatch_command.get_primary_key_for_store(t_terminal_id+1, t_primary_key_now);
  238. if ( t_error != Error_code::SUCCESS )
  239. {
  240. return t_error;
  241. }
  242. else if(t_primary_key_now != "")
  243. {
  244. int t_plc_outsidedoor_control = Dispatch_communication::get_instance_references().m_singlechip_request_from_plc_to_manager[t_singlechip_index].m_request_outsidedoor_control;
  245. if ( ( t_primary_key_now != m_primary_key_last && get_inside_existence_flag() == 1)
  246. || t_plc_outsidedoor_control == 0 )
  247. {
  248. m_close_outsidedoor_flag = true;
  249. m_primary_key_last = t_primary_key_now;
  250. }
  251. if ( m_close_outsidedoor_flag == true )
  252. {
  253. //有车就开门, 检查门的状态, 并发送关门指令
  254. if ( get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_OPEN ||
  255. get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_RUN )
  256. {
  257. //发送关门指令
  258. close_outside_door();
  259. }
  260. else if (get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_CLOSE)
  261. {
  262. m_close_outsidedoor_flag = false;
  263. }
  264. }
  265. }
  266. else
  267. {
  268. return Error_code::SUCCESS; ;
  269. }
  270. // //检查出口取车是否完成, 出口有取车完成指令就返回成功, 否则报错.
  271. // if ( Dispatch_manager::get_instance_references().m_dispatch_command.check_park_is_start(t_terminal_id+1) == Error_code::SUCCESS )
  272. // {
  273. // int t_outsidedoor_control = Dispatch_communication::get_instance_references().m_singlechip_request_from_plc_to_manager[t_singlechip_index].m_request_outsidedoor_control;
  274. //
  275. // //检测入口是否有车, //有车就开门, 检查门的状态, 并发送关门指令
  276. // if ( get_inside_existence_flag() == 1 || t_outsidedoor_control == 0 )
  277. // {
  278. // //有车就开门, 检查门的状态, 并发送关门指令
  279. // if ( get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_OPEN ||
  280. // get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_RUN )
  281. // {
  282. //
  283. // //发送关门指令
  284. // close_outside_door();
  285. // }
  286. // //else 等待汽车离开
  287. // }
  288. // //else 什么也不做
  289. // }
  290. // //else 什么也不做
  291. }
  292. return Error_code::SUCCESS;
  293. }
  294. //出口外门控制函数
  295. Error_manager Dispatch_singlechip::outlet_outsidedoor_control()
  296. {
  297. // return;
  298. if ( m_dispatch_singlechip_status == DISPATCH_SINGLECHIP_READY )
  299. {
  300. int t_terminal_id = m_plc_id*2 + m_singlechip_id;
  301. int t_singlechip_index = (m_singlechip_direction-1)*2 + m_singlechip_id;
  302. //检查出口取车是否完成, 出口有取车完成指令就返回成功, 否则报错.
  303. if ( Dispatch_manager::get_instance_references().m_dispatch_command.check_pickup_is_finish(t_terminal_id+1) == Error_code::SUCCESS )
  304. {
  305. //检测出口是否有车, 如果没有车, 直接清除数据库的指令
  306. if ( get_inside_existence_flag() == 0 )
  307. {
  308. //delete
  309. Dispatch_manager::get_instance_references().m_dispatch_command.delete_command_queue_for_export_id(t_terminal_id+1);
  310. }
  311. else if ( get_inside_existence_flag() == 1 )
  312. {
  313. //有车就开门, 检查门的状态, 并发送开门指令
  314. if ( get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_CLOSE ||
  315. get_outside_door_status() == message::Outside_door_status::OUTSIDE_DOOR_STATUS_RUN )
  316. {
  317. //发送开门指令
  318. open_outside_door();
  319. }
  320. //else 等待汽车离开
  321. }
  322. //else 什么也不做
  323. }
  324. //else 什么也不做
  325. }
  326. return Error_code::SUCCESS;;
  327. }
  328. //发送开门指令
  329. Error_manager Dispatch_singlechip::open_outside_door()
  330. {
  331. int t_terminal_id = m_plc_id*2 + m_singlechip_id;//terminal_id 0~5
  332. char buf[1024] = {0};
  333. //ProcessControl 为3表示半自动关门, 为4表示半自动开门
  334. sprintf(buf, "{\"TerminalID\": %d, \"DispatchDirection\": %d, \"OutPutDo\": {\"Do0\": 0, \"Do1\": 0, \"Do2\": 0,\"Do3\": 0,\"Do4\": 0, \"Do5\": 0, \"Do6\": 0, \"Do7\": 0}, \"ProcessControl\": 4}",
  335. t_terminal_id, m_singlechip_direction);
  336. //4个单片机分别对应tcp通道口的0~3
  337. int t_tcp_id = (m_singlechip_direction-1)*2 + m_singlechip_id;
  338. Dispatch_network::get_instance_references().encapsulate_msg(std::string(buf), t_tcp_id);
  339. return Error_code::SUCCESS;
  340. }
  341. //发送关门指令
  342. Error_manager Dispatch_singlechip::close_outside_door()
  343. {
  344. int t_terminal_id = m_plc_id*2 + m_singlechip_id; //terminal_id 0~5
  345. char buf[1024] = {0};
  346. //ProcessControl 为3表示半自动关门, 为4表示半自动开门
  347. sprintf(buf, "{\"TerminalID\": %d, \"DispatchDirection\": %d, \"OutPutDo\": {\"Do0\": 0, \"Do1\": 0, \"Do2\": 0,\"Do3\": 0,\"Do4\": 0, \"Do5\": 0, \"Do6\": 0, \"Do7\": 0}, \"ProcessControl\": 3}",
  348. t_terminal_id, m_singlechip_direction);
  349. //4个单片机分别对应tcp通道口的0~3
  350. int t_tcp_id = (m_singlechip_direction-1)*2 + m_singlechip_id;
  351. Dispatch_network::get_instance_references().encapsulate_msg(std::string(buf), t_tcp_id);
  352. return Error_code::SUCCESS;
  353. }
  354. //任务执行线程, 负责数据管理, 和通信转化, tcp rabbitmq snap7
  355. void Dispatch_singlechip::updata_thread_fun()
  356. {
  357. LOG(INFO) << " Dispatch_singlechip::updata_thread_fun() start " << this;
  358. Error_manager t_error;
  359. while (m_updata_condition.is_alive())
  360. {
  361. m_updata_condition.wait();
  362. if (m_updata_condition.is_alive())
  363. {
  364. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  365. // std::this_thread::sleep_for(std::chrono::seconds(1));
  366. std::this_thread::yield();
  367. //数据同步更新, tcp->rabbitmq, tcp->snap7
  368. updata_singlechip_data();
  369. }
  370. }
  371. }
  372. //数据同步更新, tcp->rabbitmq, tcp->snap7
  373. void Dispatch_singlechip::updata_singlechip_data()
  374. {
  375. //如果标志位是true, 就表示最新收到单片机消息, 那么同步数据消息
  376. if (m_singlechip_data_msg_updata_flag)
  377. {
  378. //将protobuf 转化为 snap7的DB块, 把店面雷达数据转发给plc
  379. //这个{}只是为了加锁
  380. {
  381. std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
  382. std::unique_lock<std::mutex> t_lock2(m_lock);
  383. //获取snap7的共享内存指针
  384. int t_temp = (m_singlechip_direction-1)*2 + m_singlechip_id;
  385. Dispatch_communication::Singlechip_response_from_manager_to_plc *p_response = &Dispatch_communication::get_instance_references().m_singlechip_response_from_manager_to_plc[t_temp];
  386. Dispatch_communication::Singlechip_request_from_plc_to_manager *p_request = &Dispatch_communication::get_instance_references().m_singlechip_request_from_plc_to_manager[t_temp];
  387. //m_request_communication_mode是刷新模式, 0=未知, 1=自动循环模式, 2=手动刷新模式
  388. if (p_request->m_request_communication_mode != 2)
  389. {
  390. p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
  391. p_response->m_response_communication_mode = p_request->m_request_communication_mode;
  392. p_response->m_response_refresh_command = p_request->m_request_refresh_command;
  393. p_response->m_response_data_validity = m_validity;
  394. p_response->m_process_status = m_singlechip_data_msg.processstatus();
  395. p_response->m_process_control = m_singlechip_data_msg.processcontrol();
  396. p_response->m_over_border_status = m_singlechip_data_msg.overborderstatus();
  397. p_response->m_car_height_status_current = m_singlechip_data_msg.carheightstatuscurrent();
  398. p_response->m_car_height_status_passing = m_singlechip_data_msg.carheightstatuspassing();
  399. p_response->m_outside_door_status = m_singlechip_data_msg.outsidedoorstatus();
  400. p_response->m_outside_door_control = m_singlechip_data_msg.outsidedoorcontrol();
  401. p_response->m_dispatch_finish_flag = m_singlechip_data_msg.dispatchfinishflag();
  402. p_response->m_inside_existence_flag = m_singlechip_data_msg.insideexistenceflag();
  403. p_response->m_outside_existence_flag = m_singlechip_data_msg.outsideexistenceflag();
  404. p_response->m_reset_flag = m_singlechip_data_msg.resetflag();
  405. p_response->m_stop_flag = m_singlechip_data_msg.stopflag();
  406. p_response->m_error_code = 0;
  407. p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
  408. p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
  409. p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
  410. p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
  411. p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
  412. p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
  413. p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
  414. p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
  415. p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
  416. p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
  417. p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
  418. p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
  419. p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
  420. p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
  421. p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
  422. p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();
  423. // if ( m_singlechip_direction ==1 )
  424. // {
  425. // p_response->m_outside_door_status = m_in_mcpu_statu.door_statu()-1;
  426. // p_response->m_over_border_status = m_in_mcpu_statu.back_io()-1;
  427. // p_response->m_inside_existence_flag = m_in_mcpu_statu.is_occupy()-1;
  428. // p_response->m_car_height_status_current = m_in_mcpu_statu.heighth()-1;
  429. // }
  430. // else if(m_singlechip_direction ==2)
  431. // {
  432. // p_response->m_outside_door_status = m_out_mcpu_statu.door_statu()-1;
  433. // p_response->m_inside_existence_flag = m_out_mcpu_statu.outside_safety()-1;
  434. // }
  435. }
  436. else if (p_response->m_response_refresh_command == p_request->m_request_refresh_command)
  437. {
  438. p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
  439. p_response->m_response_communication_mode = p_request->m_request_communication_mode;
  440. p_response->m_response_refresh_command = p_request->m_request_refresh_command;
  441. }
  442. else
  443. {
  444. p_response->m_response_heartbeat = 1+p_response->m_response_heartbeat;
  445. p_response->m_response_communication_mode = p_request->m_request_communication_mode;
  446. p_response->m_response_refresh_command = p_request->m_request_refresh_command;
  447. p_response->m_response_data_validity = m_validity;
  448. p_response->m_process_status = m_singlechip_data_msg.processstatus();
  449. p_response->m_process_control = m_singlechip_data_msg.processcontrol();
  450. p_response->m_over_border_status = m_singlechip_data_msg.overborderstatus();
  451. p_response->m_car_height_status_current = m_singlechip_data_msg.carheightstatuscurrent();
  452. p_response->m_car_height_status_passing = m_singlechip_data_msg.carheightstatuspassing();
  453. p_response->m_outside_door_status = m_singlechip_data_msg.outsidedoorstatus();
  454. p_response->m_outside_door_control = m_singlechip_data_msg.outsidedoorcontrol();
  455. p_response->m_dispatch_finish_flag = m_singlechip_data_msg.dispatchfinishflag();
  456. p_response->m_inside_existence_flag = m_singlechip_data_msg.insideexistenceflag();
  457. p_response->m_outside_existence_flag = m_singlechip_data_msg.outsideexistenceflag();
  458. p_response->m_reset_flag = m_singlechip_data_msg.resetflag();
  459. p_response->m_stop_flag = m_singlechip_data_msg.stopflag();
  460. p_response->m_error_code = 0;
  461. p_response->m_gpio_input[0] = m_singlechip_data_msg.inputdi().di0();
  462. p_response->m_gpio_input[1] = m_singlechip_data_msg.inputdi().di1();
  463. p_response->m_gpio_input[2] = m_singlechip_data_msg.inputdi().di2();
  464. p_response->m_gpio_input[3] = m_singlechip_data_msg.inputdi().di3();
  465. p_response->m_gpio_input[4] = m_singlechip_data_msg.inputdi().di4();
  466. p_response->m_gpio_input[5] = m_singlechip_data_msg.inputdi().di5();
  467. p_response->m_gpio_input[6] = m_singlechip_data_msg.inputdi().di6();
  468. p_response->m_gpio_input[7] = m_singlechip_data_msg.inputdi().di7();
  469. p_response->m_gpio_input[8] = m_singlechip_data_msg.inputdi().di8();
  470. p_response->m_gpio_input[9] = m_singlechip_data_msg.inputdi().di9();
  471. p_response->m_gpio_input[10] = m_singlechip_data_msg.inputdi().di10();
  472. p_response->m_gpio_input[11] = m_singlechip_data_msg.inputdi().di11();
  473. p_response->m_gpio_input[12] = m_singlechip_data_msg.inputdi().di12();
  474. p_response->m_gpio_input[13] = m_singlechip_data_msg.inputdi().di13();
  475. p_response->m_gpio_input[14] = m_singlechip_data_msg.inputdi().di14();
  476. p_response->m_gpio_input[15] = m_singlechip_data_msg.inputdi().di15();
  477. // if ( m_singlechip_direction ==1 )
  478. // {
  479. // p_response->m_outside_door_status = m_in_mcpu_statu.door_statu()-1;
  480. // p_response->m_over_border_status = m_in_mcpu_statu.back_io()-1;
  481. // p_response->m_inside_existence_flag = m_in_mcpu_statu.is_occupy()-1;
  482. // p_response->m_car_height_status_current = m_in_mcpu_statu.heighth()-1;
  483. // }
  484. // else if(m_singlechip_direction ==2)
  485. // {
  486. // p_response->m_outside_door_status = m_out_mcpu_statu.door_statu()-1;
  487. // p_response->m_inside_existence_flag = m_out_mcpu_statu.outside_safety()-1;
  488. // }
  489. }
  490. }
  491. //将protobuf 转化为 rabbitmq,
  492. //这个{}只是为了加锁
  493. {
  494. std::unique_lock<std::mutex> t_lock2(m_lock);
  495. //tcp的protobuf 转 rabbitmq的protobuf, 注意了, 所有数据+1
  496. if ( m_singlechip_direction ==1 )
  497. {
  498. m_in_mcpu_statu.set_door_statu(m_singlechip_data_msg.outsidedoorstatus()+1);
  499. m_in_mcpu_statu.set_is_occupy(m_singlechip_data_msg.insideexistenceflag()+1);
  500. m_in_mcpu_statu.set_heighth(m_singlechip_data_msg.carheightstatuscurrent()+1);
  501. //di1是后光电, 1表示光电联通无障碍, 0表示光电阻挡有障碍.
  502. if ( m_singlechip_data_msg.inputdi().di1() == 0 )
  503. {
  504. m_in_mcpu_statu.set_back_io(1);//后超界 0无效, 1后超界, 2正常
  505. }
  506. else if ( m_singlechip_data_msg.inputdi().di1() == 1 )
  507. {
  508. m_in_mcpu_statu.set_back_io(2);//后超界 0无效, 1后超界, 2正常
  509. }
  510. else
  511. {
  512. m_in_mcpu_statu.set_back_io(0);//后超界 0无效, 1后超界, 2正常
  513. }
  514. std::string t_msg = m_in_mcpu_statu.DebugString();
  515. //使用的rabbitmq通道要+1, tcp0号口给调度状态了, 1~4分别对应4个单片机
  516. int t_rabbitmq_id = (m_singlechip_direction-1)*2 + m_singlechip_id +1; //,每个单元的4个单片机分别对应1~4
  517. System_communication::get_instance_references().encapsulate_status_msg(t_msg, t_rabbitmq_id);
  518. }
  519. else if(m_singlechip_direction ==2)
  520. {
  521. m_out_mcpu_statu.set_door_statu(m_singlechip_data_msg.outsidedoorstatus()+1);
  522. m_out_mcpu_statu.set_outside_safety(m_singlechip_data_msg.insideexistenceflag()+1);
  523. std::string t_msg = m_out_mcpu_statu.DebugString();
  524. int t_rabbitmq_id = (m_singlechip_direction-1)*2 + m_singlechip_id +1; //,每个单元的4个单片机分别对应1~4
  525. //使用的rabbitmq通道要+1, tcp0号口给调度状态了, 1~4分别对应4个单片机
  526. System_communication::get_instance_references().encapsulate_status_msg(t_msg, t_rabbitmq_id);
  527. }
  528. }
  529. //数据更新完成后, 将更新标志位写false,
  530. m_singlechip_data_msg_updata_flag = false;
  531. }
  532. //判断超时
  533. {
  534. std::unique_lock<std::mutex> t_lock3(m_lock);
  535. if (std::chrono::system_clock::now() - m_singlechip_data_msg_updata_time > std::chrono::seconds(5))
  536. {
  537. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_DISCONNECT;
  538. }
  539. else
  540. {
  541. m_dispatch_singlechip_status = DISPATCH_SINGLECHIP_READY;
  542. }
  543. }
  544. return;
  545. }