command_manager.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // Created by zx on 2020/7/14.
  3. //
  4. #include "command_manager.h"
  5. #include "StoreProcessTask.h"
  6. #include "PickupProcessTask.h"
  7. #include "system_communicator.h"
  8. Command_manager::Command_manager()
  9. :m_publish_statu_thread(nullptr)
  10. ,m_thread_queue_process(nullptr)
  11. {
  12. }
  13. Command_manager::~Command_manager()
  14. {
  15. //等待线程池完成
  16. if(m_thread_queue_process!=nullptr) {
  17. m_thread_queue_process->WaitForFinish();
  18. m_thread_queue_process->Stop();
  19. }
  20. //退出线程
  21. m_publish_exit_condition.set_pass_ever(true);
  22. if(m_publish_statu_thread!= nullptr)
  23. {
  24. if(m_publish_statu_thread->joinable())
  25. {
  26. m_publish_statu_thread->join();
  27. }
  28. delete m_publish_statu_thread;
  29. m_publish_statu_thread=nullptr;
  30. }
  31. }
  32. Error_manager Command_manager::init()
  33. {
  34. //创建线程池
  35. if(m_thread_queue_process== nullptr)
  36. {
  37. m_thread_queue_process=tq::TQFactory::CreateDefaultQueue();
  38. m_thread_queue_process->Start(12);
  39. }
  40. m_publish_exit_condition.reset(false, false, false);
  41. ///创建状态发布线程
  42. if(m_publish_statu_thread== nullptr)
  43. {
  44. m_publish_statu_thread=new std::thread(publish_thread_func,this);
  45. }
  46. return SUCCESS;
  47. }
  48. /*
  49. * 执行停车请求
  50. */
  51. Error_manager Command_manager::execute_store_command(message::Store_command_request_msg& request,message::Store_command_response_msg& response)
  52. {
  53. if(m_publish_statu_thread==nullptr)
  54. {
  55. return Error_manager(ERROR,CRITICAL_ERROR,"线程池未初始化,bug");
  56. }
  57. if(request.base_info().msg_type()==message::eStore_command_request_msg
  58. &&request.base_info().receiver()==message::eMain
  59. &&request.base_info().sender()==message::eTerminor)
  60. {
  61. if(request.has_locate_information())
  62. {
  63. message::Locate_information locate_info=request.locate_information();
  64. if(locate_info.has_locate_correct())
  65. {
  66. if(locate_info.locate_correct()==true)
  67. {
  68. if(locate_info.has_locate_width()&&locate_info.has_locate_height()
  69. &&locate_info.has_locate_x()&&locate_info.has_locate_y()
  70. &&locate_info.has_locate_angle()&&locate_info.has_locate_wheel_base())
  71. {
  72. message::Base_info base_info;
  73. base_info.set_msg_type(message::eStore_command_response_msg);
  74. base_info.set_sender(message::eMain);
  75. base_info.set_receiver(message::eTerminor);
  76. response.mutable_base_info()->CopyFrom(base_info);
  77. response.set_terminal_id(request.terminal_id());
  78. message::Error_manager error_msg;
  79. error_msg.set_error_code(0);
  80. StoreProcessTask* task=new StoreProcessTask();
  81. int command_id=request.terminal_id()+10000;
  82. //初始化流程
  83. task->init_task(command_id,request.terminal_id(),locate_info,request.car_info());
  84. //获取车位
  85. Error_manager code=task->alloc_space();
  86. if(code==SUCCESS)
  87. {
  88. //更新状态
  89. Command_manager::get_instance_pointer()->updata_store_entrance_statu(request.terminal_id(),message::eWorking);
  90. m_thread_queue_process->AddTask(task);
  91. response.mutable_code()->CopyFrom(error_msg);
  92. return SUCCESS;
  93. }
  94. error_msg.set_error_code(code.get_error_code());
  95. error_msg.set_error_description(code.to_string());
  96. response.mutable_code()->CopyFrom(error_msg);
  97. LOG(ERROR)<<"创建停车流程失败(车位分配失败),终端:"<<request.terminal_id()<<
  98. "车牌:"<<request.car_info().license()<<" "<<code.to_string();
  99. return code;
  100. }
  101. }
  102. }
  103. }
  104. return Error_manager(FAILED,MINOR_ERROR,"创建停车流程失败......");
  105. }
  106. else
  107. {
  108. return Error_manager(INVALID_MESSAGE,MAJOR_ERROR,"停车请求基本信息错误");
  109. }
  110. }
  111. /*
  112. * 执行取车请求
  113. */
  114. Error_manager Command_manager::execute_pickup_command(message::Pickup_command_request_msg& request,message::Pickup_command_response_msg& response)
  115. {
  116. if(m_publish_statu_thread==nullptr)
  117. {
  118. return Error_manager(ERROR,CRITICAL_ERROR,"线程池未初始化,bug");
  119. }
  120. if(request.base_info().msg_type()==message::ePickup_command_request_msg
  121. &&request.base_info().receiver()==message::eMain
  122. &&request.base_info().sender()==message::eTerminor
  123. &&request.has_car_info()) {
  124. message::Base_info baseInfo;
  125. baseInfo.set_msg_type(message::ePickup_command_response_msg);
  126. baseInfo.set_sender(message::eMain);
  127. baseInfo.set_receiver(message::eTerminor);
  128. response.mutable_base_info()->CopyFrom(baseInfo);
  129. response.set_terminal_id(request.terminal_id());
  130. Error_manager code;
  131. PickupProcessTask *task = new PickupProcessTask();
  132. int command_id = request.terminal_id()+20000;
  133. //初始化流程
  134. task->init_task(command_id, request.terminal_id(), request.car_info());
  135. /////查询车位
  136. code=task->search_space();
  137. message::Error_manager error_msg;
  138. error_msg.set_error_code(0);
  139. if(code==SUCCESS)
  140. {
  141. //更新状态
  142. Command_manager::get_instance_pointer()->updata_pickup_entrance_statu(request.terminal_id(),message::eWorking);
  143. m_thread_queue_process->AddTask(task);
  144. response.mutable_code()->CopyFrom(error_msg);
  145. return SUCCESS;
  146. }
  147. error_msg.set_error_code(code.get_error_code());
  148. error_msg.set_error_description(code.to_string());
  149. response.mutable_code()->CopyFrom(error_msg);
  150. LOG(ERROR)<<"创建取车流程失败(车位查询失败),终端:"<<request.terminal_id()<<
  151. "车牌:"<<request.car_info().license()<<" "<<code.to_string();
  152. return code;
  153. }
  154. else
  155. {
  156. return Error_manager(INVALID_MESSAGE,MAJOR_ERROR,"停车请求信息错误");
  157. }
  158. }
  159. /*
  160. * 更新状态
  161. */
  162. void Command_manager::updata_store_entrance_statu(int command_id,message::Entrance_statu msg)
  163. {
  164. if(command_id<0 || command_id>100)
  165. return;
  166. ///
  167. std::lock_guard<std::mutex> lk(m_entrance_msg_lock);
  168. auto it=m_entrance_status.mutable_store_msg_map()->find(command_id);
  169. if(it==m_entrance_status.mutable_store_msg_map()->end())
  170. {
  171. m_entrance_status.mutable_store_msg_map()->insert(google::protobuf::MapPair<int,message::Entrance_statu >(command_id,msg));
  172. }
  173. else
  174. {
  175. it->second=msg;
  176. }
  177. //立刻发送
  178. System_communicator::get_instance_pointer()->send_entrance_statu(m_entrance_status);
  179. }
  180. void Command_manager::updata_pickup_entrance_statu(int command_id,message::Entrance_statu statu)
  181. {
  182. if(command_id<0 || command_id>100)
  183. return;
  184. ///
  185. std::lock_guard<std::mutex> lk(m_entrance_msg_lock);
  186. auto it=m_entrance_status.mutable_pickup_msg_map()->find(command_id);
  187. if(it==m_entrance_status.mutable_pickup_msg_map()->end())
  188. {
  189. m_entrance_status.mutable_pickup_msg_map()->insert(google::protobuf::MapPair<int,message::Entrance_statu>(command_id,statu));
  190. }
  191. else
  192. {
  193. it->second=statu;
  194. }
  195. System_communicator::get_instance_pointer()->send_entrance_statu(m_entrance_status);
  196. }
  197. /*
  198. * 发布状态线程
  199. */
  200. void Command_manager::publish_thread_func(Command_manager* p_commander)
  201. {
  202. if(p_commander)
  203. {
  204. p_commander->publish_status();
  205. }
  206. }
  207. void Command_manager::publish_status()
  208. {
  209. //未收到退出信号
  210. while(false==m_publish_exit_condition.wait_for_ex(std::chrono::milliseconds(100)))
  211. {
  212. /*
  213. * 通过communicator 发布状态
  214. */
  215. if(System_communicator::get_instance_pointer())
  216. {
  217. if(m_entrance_status.has_base_info()==false)
  218. {
  219. message::Base_info baseInfo;
  220. baseInfo.set_msg_type(message::eEntrance_statu_msg);
  221. baseInfo.set_sender(message::eMain);
  222. baseInfo.set_receiver(message::eEmpty); //所有对象都可接收
  223. m_entrance_status.mutable_base_info()->CopyFrom(baseInfo);
  224. }
  225. System_communicator::get_instance_pointer()->send_entrance_statu(m_entrance_status);
  226. }
  227. }
  228. }