store_terminal.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. LOG(WARNING)<<" request_error: "<<code.to_string()<<std::endl;
  58. return ERROR;
  59. }
  60. if(response.code().error_code()!=0)
  61. {
  62. LOG(WARNING)<<" store request response code error: "<<response.code().error_description();
  63. return ERROR;
  64. }
  65. //等待停车完成
  66. bool last_signal=false;
  67. message::Storing_process_statu_msg last_msg;
  68. while(m_exit_cond.wait_for_millisecond(50)==false)
  69. {
  70. message::Storing_process_statu_msg msg;
  71. code = Terminal_communication::get_instance_pointer()->get_storing_statu(car_info.license(), msg);
  72. //未找到
  73. if (code != SUCCESS) {
  74. if(last_signal==true )
  75. {
  76. if(last_msg.completed()==true) {
  77. if (m_thread_safe_output_queue)
  78. m_thread_safe_output_queue->push(car_info);
  79. }
  80. return SUCCESS;
  81. }
  82. }
  83. else
  84. {
  85. last_msg=msg;
  86. last_signal=true;
  87. }
  88. }
  89. return FAILED;
  90. }
  91. void store_terminal::storing_thread(store_terminal* pStore)
  92. {
  93. while(pStore->m_exit_cond.wait_for_millisecond(1)==false)
  94. {
  95. message::Car_info car_info;
  96. if(pStore->m_thread_safe_queue== nullptr)
  97. continue;
  98. if(pStore->m_thread_safe_queue->try_pop(car_info)==true)
  99. {
  100. LOG(INFO)<<"车辆:"<<car_info.license().c_str()<<",停车开始---"<<pStore->m_terminal_id<<"---,剩余in:"
  101. <<pStore->m_thread_safe_queue->size();
  102. Error_manager code=pStore->storing(car_info);
  103. LOG_IF(INFO,code==SUCCESS)<<"车辆:"<<car_info.license().c_str()<<",停车结束---"<<pStore->m_terminal_id<<"----------,剩余in:"
  104. <<pStore->m_thread_safe_queue->size();
  105. LOG_IF(ERROR,code!=SUCCESS)<<"车辆:"<<car_info.license().c_str()<<",停车结束---"<<pStore->m_terminal_id<<"---------,剩余in:"
  106. <<pStore->m_thread_safe_queue->size();
  107. }
  108. }
  109. }