store_terminal.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // Created by zx on 2020/7/16.
  3. //
  4. #include "store_terminal.h"
  5. #include "Terminal_communication.h"
  6. store_terminal::store_terminal(int terminal_id)
  7. {
  8. m_terminal_id=terminal_id;
  9. m_thread_safe_queue= nullptr;
  10. m_thread_safe_output_queue=nullptr;
  11. }
  12. store_terminal::~store_terminal()
  13. {
  14. m_exit_cond.set_pass_ever(true);
  15. if(m_pthread)
  16. {
  17. if(m_pthread->joinable())
  18. m_pthread->join();
  19. delete m_pthread;
  20. m_pthread=nullptr;
  21. }
  22. }
  23. Error_manager store_terminal::init(threadsafe_queue<message::Car_info>* input,
  24. threadsafe_queue<message::Car_info>* output)
  25. {
  26. m_thread_safe_queue=input;
  27. m_thread_safe_output_queue=output;
  28. m_exit_cond.reset(false,false,false);
  29. m_pthread=new std::thread(storing_thread,this);
  30. return SUCCESS;
  31. }
  32. Error_manager store_terminal::storing(message::Car_info& car_info)
  33. {
  34. message::Store_command_request_msg request;
  35. message::Base_info base_info;
  36. base_info.set_msg_type(message::eStore_command_request_msg);
  37. base_info.set_sender(message::eTerminor);
  38. base_info.set_receiver(message::eMain);
  39. request.mutable_base_info()->CopyFrom(base_info);
  40. request.mutable_car_info()->CopyFrom(car_info);
  41. message::Locate_information locate_info;
  42. locate_info.set_locate_x(0.9);
  43. locate_info.set_locate_y(2.25);
  44. locate_info.set_locate_angle(90.0);
  45. locate_info.set_locate_wheel_base(2.7);
  46. locate_info.set_locate_width(car_info.car_width());
  47. locate_info.set_locate_height(car_info.car_height());
  48. locate_info.set_locate_correct(true);
  49. request.mutable_locate_information()->CopyFrom(locate_info);
  50. request.set_terminal_id(m_terminal_id);
  51. //发送停车请求
  52. Error_manager code;
  53. message::Store_command_response_msg response;
  54. code=Terminal_communication::get_instance_pointer()->store_request(request,response);
  55. if(code!=SUCCESS)
  56. {
  57. std::cout<<" request_error: "<<code.to_string()<<std::endl;
  58. return ERROR;
  59. }
  60. //等待停车完成
  61. bool last_signal=false;
  62. while(m_exit_cond.wait_for_millisecond(50)==false)
  63. {
  64. message::Storing_process_statu_msg msg;
  65. code = Terminal_communication::get_instance_pointer()->get_storing_statu(car_info.license(), msg);
  66. if (code != SUCCESS) {
  67. if(last_signal==true)
  68. {
  69. if (m_thread_safe_output_queue)
  70. m_thread_safe_output_queue->push(car_info);
  71. return SUCCESS;
  72. }
  73. }
  74. else
  75. {
  76. last_signal=true;
  77. }
  78. }
  79. return FAILED;
  80. }
  81. void store_terminal::storing_thread(store_terminal* pStore)
  82. {
  83. while(pStore->m_exit_cond.wait_for_millisecond(200)==false)
  84. {
  85. message::Car_info car_info;
  86. if(pStore->m_thread_safe_queue== nullptr)
  87. continue;
  88. if(pStore->m_thread_safe_queue->try_pop(car_info)==true)
  89. {
  90. LOG(INFO)<<"车辆:"<<car_info.license().c_str()<<",停车开始---"<<pStore->m_terminal_id<<"---,剩余in:"
  91. <<pStore->m_thread_safe_queue->size();
  92. Error_manager code=pStore->storing(car_info);
  93. LOG_IF(INFO,code==SUCCESS)<<"车辆:"<<car_info.license().c_str()<<",停车结束---"<<pStore->m_terminal_id<<"----------,剩余in:"
  94. <<pStore->m_thread_safe_queue->size();
  95. LOG_IF(ERROR,code!=SUCCESS)<<"车辆:"<<car_info.license().c_str()<<",停车结束---"<<pStore->m_terminal_id<<"---------,剩余in:"
  96. <<pStore->m_thread_safe_queue->size();
  97. }
  98. }
  99. }