pickup_terminal.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Created by zx on 2020/7/16.
  3. //
  4. #include "pickup_terminal.h"
  5. #include "Terminal_communication.h"
  6. pickup_terminal::pickup_terminal(int terminal_id)
  7. {
  8. m_terminal_id=terminal_id;
  9. m_thread_safe_queue= nullptr;
  10. }
  11. Error_manager pickup_terminal::init(threadsafe_queue<message::Car_info>* queue)
  12. {
  13. m_thread_safe_queue=queue;
  14. m_exit_cond.reset(false,false,false);
  15. m_pthread=new std::thread(pickup_thread,this);
  16. return SUCCESS;
  17. }
  18. pickup_terminal::~pickup_terminal()
  19. {
  20. m_exit_cond.set_pass_ever(true);
  21. if(m_pthread)
  22. {
  23. if(m_pthread->joinable())
  24. m_pthread->join();
  25. delete m_pthread;
  26. m_pthread=nullptr;
  27. }
  28. }
  29. Error_manager pickup_terminal::pickup(message::Car_info& car_info)
  30. {
  31. message::Pickup_command_request_msg request;
  32. message::Base_info base_info;
  33. base_info.set_msg_type(message::ePickup_command_request_msg);
  34. base_info.set_sender(message::eTerminor);
  35. base_info.set_receiver(message::eMain);
  36. request.mutable_base_info()->CopyFrom(base_info);
  37. request.mutable_car_info()->CopyFrom(car_info);
  38. request.set_terminal_id(m_terminal_id);
  39. //发送停车请求
  40. Error_manager code;
  41. message::Pickup_command_response_msg response;
  42. code=Terminal_communication::get_instance_pointer()->pickup_request(request,response);
  43. if(code!=SUCCESS)
  44. {
  45. std::cout<<"取车请求失败:"<<code.to_string()<<std::endl;
  46. return ERROR;
  47. }
  48. //等待停车完成
  49. bool last_signal=false;
  50. while(m_exit_cond.wait_for_millisecond(200)==false)
  51. {
  52. message::Picking_process_statu_msg msg;
  53. code = Terminal_communication::get_instance_pointer()->get_picking_statu(car_info.license(), msg);
  54. if (code != SUCCESS) {
  55. if(last_signal==true)
  56. {
  57. return SUCCESS;
  58. }
  59. }
  60. else
  61. {
  62. last_signal=true;
  63. }
  64. }
  65. return FAILED;
  66. }
  67. void pickup_terminal::pickup_thread(pickup_terminal* picker)
  68. {
  69. while(picker->m_exit_cond.wait_for_millisecond(200)==false)
  70. {
  71. message::Car_info car_info;
  72. if(picker->m_thread_safe_queue== nullptr)
  73. continue;
  74. if(picker->m_thread_safe_queue->try_pop(car_info)==true)
  75. {
  76. /*LOG(WARNING)<<"取车终端:"<<picker->m_terminal_id<<",车辆:"<<car_info.license().c_str()
  77. <<",开始取车,剩余out:"<<picker->m_thread_safe_queue->size();*/
  78. Error_manager code=picker->pickup(car_info);
  79. }
  80. }
  81. }