Terminal_communication.cpp 13 KB

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