Terminal_communication.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // Created by zx on 2020/7/13.
  3. //
  4. #include "Terminal_communication.h"
  5. #include "process_message.pb.h"
  6. #include <condition_variable>
  7. #include <central_control_message.pb.h>
  8. Terminal_communication::Terminal_communication()
  9. :m_update_msg_map_thread(nullptr)
  10. {
  11. if(m_update_msg_map_thread== nullptr)
  12. {
  13. m_publish_exit_condition.reset(false, false, false);
  14. m_update_msg_map_thread=new std::thread(thread_update_map_function,this);
  15. }
  16. }
  17. Terminal_communication::~Terminal_communication(){}
  18. //重载函数
  19. Error_manager Terminal_communication::encapsulate_msg(Communication_message* message)
  20. {
  21. Error_manager code;
  22. switch (message->get_message_type())
  23. {
  24. case Communication_message::eStore_command_request_msg:
  25. {
  26. message::Store_command_request_msg request;
  27. request.ParseFromString(message->get_message_buf());
  28. //发送请求
  29. code= Communication_socket_base::encapsulate_msg(message);
  30. break;
  31. }
  32. case Communication_message::ePickup_command_request_msg:
  33. {
  34. message::Pickup_command_request_msg request;
  35. request.ParseFromString(message->get_message_buf());
  36. //发送请求
  37. code= Communication_socket_base::encapsulate_msg(message);
  38. break;
  39. }
  40. default:
  41. code= Error_manager(ERROR,NEGLIGIBLE_ERROR,"terminal message table is not exist");
  42. }
  43. return code;
  44. }
  45. Error_manager Terminal_communication::execute_msg(Communication_message* p_msg)
  46. {
  47. if(p_msg->get_message_type()==Communication_message::eStore_command_response_msg)
  48. {
  49. message::Store_command_response_msg response;
  50. if(false==response.ParseFromString(p_msg->get_message_buf()))
  51. {
  52. return Error_manager(ERROR,CRITICAL_ERROR,"停车指令反馈信息解析错误");
  53. }
  54. message::Base_info base_info=response.base_info();
  55. if(base_info.sender()==message::eMain && base_info.receiver()==message::eTerminor)
  56. {
  57. message::Error_manager error_code=response.code();
  58. m_store_response_msg_table[response.license()]=response;
  59. return SUCCESS;
  60. }
  61. }
  62. if(p_msg->get_message_type()==Communication_message::ePickup_command_response_msg)
  63. {
  64. message::Pickup_command_response_msg response;
  65. if(false==response.ParseFromString(p_msg->get_message_buf()))
  66. {
  67. return Error_manager(ERROR,CRITICAL_ERROR,"停车指令反馈信息解析错误");
  68. }
  69. LOG(INFO)<<" 取车反馈:"<<response.license();
  70. message::Base_info base_info=response.base_info();
  71. if(base_info.sender()==message::eMain && base_info.receiver()==message::eTerminor)
  72. {
  73. message::Error_manager error_code=response.code();
  74. m_pickup_response_msg_table[response.license()]=response;
  75. return SUCCESS;
  76. }
  77. }
  78. if(p_msg->get_message_type()==Communication_message::eStoring_process_statu_msg)
  79. {
  80. message::Storing_process_statu_msg msg;
  81. if(msg.ParseFromString(p_msg->get_message_buf())==false)
  82. {
  83. return Error_manager(ERROR,CRITICAL_ERROR,"停车流程状态信息解析错误");
  84. }
  85. message::Base_info base_info=msg.base_info();
  86. if(base_info.sender()==message::eMain)
  87. {
  88. if(m_storing_process_statu_map.find(msg.license())==false)
  89. {
  90. m_storing_license_queue.push(msg.license());
  91. }
  92. m_storing_process_statu_map[msg.license()]=msg;
  93. std::chrono::system_clock::time_point time_point=std::chrono::system_clock::now();
  94. m_storing_statu_process_time_point_map[msg.license()]=time_point;
  95. }
  96. }
  97. if(p_msg->get_message_type()==Communication_message::ePicking_process_statu_msg)
  98. {
  99. message::Picking_process_statu_msg msg;
  100. if(msg.ParseFromString(p_msg->get_message_buf())==false)
  101. {
  102. return Error_manager(ERROR,CRITICAL_ERROR,"取车流程状态信息解析错误");
  103. }
  104. message::Base_info base_info=msg.base_info();
  105. if(base_info.sender()==message::eMain)
  106. {
  107. if(m_picking_statu_process_map.find(msg.license())==false)
  108. {
  109. m_picking_license_queue.push(msg.license());
  110. }
  111. m_picking_statu_process_map[msg.license()]=msg;
  112. std::chrono::system_clock::time_point time_point=std::chrono::system_clock::now();
  113. m_picking_statu_process_time_point_map[msg.license()]=time_point;
  114. }
  115. }
  116. if(p_msg->get_message_type()==Communication_message::eCentral_controller_statu_msg)
  117. {
  118. message::Central_controller_statu_msg msg;
  119. if(msg.ParseFromString(p_msg->get_message_buf())==false)
  120. {
  121. return Error_manager(ERROR,CRITICAL_ERROR,"中控状态信息解析错误");
  122. }
  123. message::Base_info base_info=msg.base_info();
  124. if(base_info.sender()==message::eMain)
  125. {
  126. }
  127. }
  128. return SUCCESS;
  129. }
  130. /*
  131. * 检测消息是否可被处理
  132. */
  133. Error_manager Terminal_communication::check_msg(Communication_message* p_msg)
  134. {
  135. return SUCCESS;
  136. }
  137. /*
  138. * 心跳发送函数,重载
  139. */
  140. Error_manager Terminal_communication::encapsulate_send_data()
  141. {
  142. return SUCCESS;
  143. }
  144. //检查消息是否可以被解析, 需要重载
  145. Error_manager Terminal_communication::check_executer(Communication_message* p_msg)
  146. {
  147. return SUCCESS;
  148. }
  149. /*
  150. * 发送停车指令请求
  151. */
  152. Error_manager Terminal_communication::store_request(message::Store_command_request_msg& request,message::Store_command_response_msg& result)
  153. {
  154. if(request.has_locate_information()==false ||
  155. request.has_car_info()==false)
  156. {
  157. return Error_manager(ERROR,CRITICAL_ERROR,"停车指令请求消息缺少必要信息");
  158. }
  159. Error_manager code;
  160. Communication_message message;
  161. message.reset(request.base_info(),request.SerializeAsString());
  162. int timeout=1000*5;
  163. code=encapsulate_msg(&message);
  164. if(code!=SUCCESS)
  165. return code;
  166. //循环查询请求是否被处理
  167. auto start_time=std::chrono::system_clock::now();
  168. double time=0;
  169. do {
  170. ///查询是否存在,并且删除该记录,
  171. message::Store_command_response_msg response;
  172. if(m_store_response_msg_table.find(request.car_info().license(),response))
  173. {
  174. //判断是否接收到回应,若回应信息被赋值则证明有回应
  175. if (response.has_base_info() && response.has_code())
  176. {
  177. message::Base_info response_base = response.base_info();
  178. //检查类型是否匹配
  179. if (response_base.msg_type() != message::eStore_command_response_msg) {
  180. return Error_manager(ERROR, CRITICAL_ERROR,
  181. "停车指令反馈消息 response msg type error");
  182. }
  183. //检查基本信息是否匹配
  184. if (response_base.sender() != message::eMain ||
  185. response_base.receiver() != message::eTerminor ) {
  186. return Error_manager(PARKSPACE_RELEASE_RESPONSE_INFO_ERROR, MAJOR_ERROR,
  187. "parkspace store response msg info error");
  188. }
  189. result = response;
  190. m_store_response_msg_table.erase(request.car_info().license());
  191. if(response.has_code())
  192. {
  193. if(response.code().error_code()==0)
  194. return SUCCESS;
  195. }
  196. return Error_manager(FAILED,MINOR_ERROR,"车位查询返回错误码");
  197. }
  198. }
  199. auto end_time=std::chrono::system_clock::now();
  200. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  201. time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  202. if(time>double(timeout))
  203. {
  204. m_store_response_msg_table.erase(request.car_info().license());
  205. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"storing terminal request timeout");
  206. }
  207. usleep(1000);
  208. }while(1);
  209. }
  210. /*
  211. * 发送取车指令请求
  212. */
  213. Error_manager Terminal_communication::pickup_request(message::Pickup_command_request_msg& request,message::Pickup_command_response_msg& result)
  214. {
  215. if(request.has_car_info()==false)
  216. {
  217. return Error_manager(ERROR,CRITICAL_ERROR,"取车指令请求消息缺少车辆信息");
  218. }
  219. Error_manager code;
  220. Communication_message message;
  221. message.reset(request.base_info(),request.SerializeAsString());
  222. int timeout=1000*3;
  223. code=encapsulate_msg(&message);
  224. if(code!=SUCCESS)
  225. return code;
  226. // m_statu=eTerminal_picking;
  227. //循环查询请求是否被处理
  228. auto start_time=std::chrono::system_clock::now();
  229. double time=0;
  230. do {
  231. ///查询是否存在,并且删除该记录,
  232. message::Pickup_command_response_msg response;
  233. if(m_pickup_response_msg_table.find(request.car_info().license(),response))
  234. {
  235. //判断是否接收到回应,若回应信息被赋值则证明有回应
  236. if (response.has_base_info() && response.has_code())
  237. {
  238. message::Base_info response_base = response.base_info();
  239. //检查类型是否匹配
  240. if (response_base.msg_type() != message::ePickup_command_response_msg) {
  241. return Error_manager(ERROR, CRITICAL_ERROR,
  242. "停车指令反馈消息 response msg type error");
  243. }
  244. //检查基本信息是否匹配
  245. if (response_base.sender() != message::eMain ||
  246. response_base.receiver() != message::eTerminor ) {
  247. return Error_manager(PARKSPACE_RELEASE_RESPONSE_INFO_ERROR, MAJOR_ERROR,
  248. "parkspace release response msg info error");
  249. }
  250. result = response;
  251. m_pickup_response_msg_table.erase(request.car_info().license());
  252. if(response.has_code())
  253. {
  254. if(response.code().error_code()==0)
  255. return SUCCESS;
  256. }
  257. return Error_manager(FAILED,MINOR_ERROR,"车位查询返回错误码");
  258. }
  259. }
  260. auto end_time=std::chrono::system_clock::now();
  261. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  262. time=1000.0*double(duration.count()) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den;
  263. if(time>double(timeout))
  264. {
  265. m_pickup_response_msg_table.erase(request.car_info().license());
  266. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"picking terminal request timeout");
  267. }
  268. usleep(1000);
  269. }while(1);
  270. }
  271. /*
  272. * 获取停车流程状态
  273. */
  274. Error_manager Terminal_communication::get_storing_statu(std::string car_license,message::Storing_process_statu_msg& msg)
  275. {
  276. if(m_storing_process_statu_map.find(car_license,msg)) {
  277. return SUCCESS;
  278. }
  279. return FAILED;
  280. }
  281. /*
  282. * 获取取车流程状态
  283. */
  284. Error_manager Terminal_communication::get_picking_statu(std::string car_license,message::Picking_process_statu_msg& msg)
  285. {
  286. if(m_picking_statu_process_map.find(car_license,msg))
  287. return SUCCESS;
  288. return FAILED;
  289. }
  290. void Terminal_communication::update_map()
  291. {
  292. while(false==m_publish_exit_condition.wait_for_ex(std::chrono::milliseconds(200)))
  293. {
  294. std::string license;
  295. if (m_storing_license_queue.try_pop(license))
  296. {
  297. std::chrono::system_clock::time_point t1 = std::chrono::system_clock::now();
  298. std::chrono::system_clock::time_point start = m_storing_statu_process_time_point_map[license];
  299. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t1 - start);
  300. double time = 1000.0 * double(duration.count()) * std::chrono::microseconds::period::num /
  301. std::chrono::microseconds::period::den;
  302. if (time > 300)
  303. {
  304. m_storing_statu_process_time_point_map.erase(license);
  305. m_storing_process_statu_map.erase(license);
  306. }
  307. else
  308. {
  309. m_storing_license_queue.push(license);
  310. }
  311. }
  312. if (m_picking_license_queue.try_pop(license))
  313. {
  314. std::chrono::system_clock::time_point t1 = std::chrono::system_clock::now();
  315. std::chrono::system_clock::time_point start = m_picking_statu_process_time_point_map[license];
  316. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t1 - start);
  317. double time = 1000.0 * double(duration.count()) * std::chrono::microseconds::period::num /
  318. std::chrono::microseconds::period::den;
  319. if (time > 300)
  320. {
  321. m_picking_statu_process_time_point_map.erase(license);
  322. m_picking_statu_process_map.erase(license);
  323. }
  324. else
  325. {
  326. m_picking_license_queue.push(license);
  327. }
  328. }
  329. }
  330. }
  331. void Terminal_communication::thread_update_map_function(Terminal_communication *terminal) {
  332. terminal->update_map();
  333. }