Terminal_communication.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. Terminal_communication::Terminal_communication(){}
  8. Terminal_communication::~Terminal_communication(){}
  9. //重载函数
  10. Error_manager Terminal_communication::encapsulate_msg(Communication_message* message)
  11. {
  12. Error_manager code;
  13. switch (message->get_message_type())
  14. {
  15. case Communication_message::eStore_command_request_msg:
  16. {
  17. message::Store_command_request_msg request;
  18. request.ParseFromString(message->get_message_buf());
  19. //清空记录
  20. m_store_response_msg=message::Store_command_response_msg();
  21. //发送请求
  22. code= Communication_socket_base::encapsulate_msg(message);
  23. break;
  24. }
  25. case Communication_message::ePickup_command_request_msg:
  26. {
  27. message::Pickup_command_request_msg request;
  28. request.ParseFromString(message->get_message_buf());
  29. //清空记录
  30. m_pickup_response_msg=message::Pickup_command_response_msg();
  31. //发送请求
  32. code= Communication_socket_base::encapsulate_msg(message);
  33. break;
  34. }
  35. default:
  36. code= Error_manager(ERROR,NEGLIGIBLE_ERROR,"terminal message table is not exist");
  37. }
  38. return code;
  39. }
  40. Error_manager Terminal_communication::execute_msg(Communication_message* p_msg)
  41. {
  42. if(p_msg->get_message_type()==Communication_message::eStore_command_response_msg)
  43. {
  44. message::Store_command_response_msg response;
  45. if(false==response.ParseFromString(p_msg->get_message_buf()))
  46. {
  47. return Error_manager(ERROR,CRITICAL_ERROR,"停车指令反馈信息解析错误");
  48. }
  49. message::Base_info base_info=response.base_info();
  50. if(base_info.sender()==message::eMain && base_info.receiver()==message::eTerminor)
  51. {
  52. message::Error_manager error_code=response.code();
  53. if(error_code.error_code()==0)
  54. {
  55. m_store_response_msg=response;
  56. return SUCCESS;
  57. }
  58. }
  59. }
  60. if(p_msg->get_message_type()==Communication_message::ePickup_command_response_msg)
  61. {
  62. message::Pickup_command_response_msg response;
  63. if(false==response.ParseFromString(p_msg->get_message_buf()))
  64. {
  65. return Error_manager(ERROR,CRITICAL_ERROR,"停车指令反馈信息解析错误");
  66. }
  67. message::Base_info base_info=response.base_info();
  68. if(base_info.sender()==message::eMain && base_info.receiver()==message::eTerminor)
  69. {
  70. message::Error_manager error_code=response.code();
  71. if(error_code.error_code()==0)
  72. {
  73. m_pickup_response_msg=response;
  74. return SUCCESS;
  75. }
  76. }
  77. }
  78. if(p_msg->get_message_type()==Communication_message::eEntrance_statu_msg)
  79. {
  80. message::Entrance_statu_msg msg;
  81. if(msg.ParseFromString(p_msg->get_message_buf())==false)
  82. {
  83. std::cout<<" ERROR 解析Entrance_statu_msg 失败"<<std::endl;
  84. }
  85. message::Base_info base_info=msg.base_info();
  86. if(base_info.sender()==message::eMain)
  87. {
  88. m_entrance_status=msg;
  89. }
  90. }
  91. return SUCCESS;
  92. return Error_manager(FAILED,MINOR_ERROR,"terminal communication 未知消息");
  93. }
  94. /*
  95. * 检测消息是否可被处理
  96. */
  97. Error_manager Terminal_communication::check_msg(Communication_message* p_msg)
  98. {
  99. return SUCCESS;
  100. }
  101. /*
  102. * 心跳发送函数,重载
  103. */
  104. Error_manager Terminal_communication::encapsulate_send_data()
  105. {
  106. return SUCCESS;
  107. }
  108. //检查消息是否可以被解析, 需要重载
  109. Error_manager Terminal_communication::check_executer(Communication_message* p_msg)
  110. {
  111. return SUCCESS;
  112. }
  113. /*
  114. * 设置终端id
  115. */
  116. Error_manager Terminal_communication::set_terminal_id(int id)
  117. {
  118. if(id<0 || id>5)
  119. return Error_manager(ERROR,CRITICAL_ERROR,"终端设置id不在范围");
  120. m_terminal_id=id;
  121. return SUCCESS;
  122. }
  123. Error_manager Terminal_communication::send_pickup_task(message::Parkspace_search_request_msg& msg)
  124. {
  125. if(msg.has_base_info()==false)
  126. return FAILED;
  127. if(msg.base_info().sender()!=message::eTerminor ||msg.base_info().msg_type()!=message::eParkspace_search_request_msg)
  128. return FAILED;
  129. Communication_message message;
  130. message.reset(msg.base_info(),msg.SerializeAsString());
  131. return Communication_socket_base::encapsulate_msg(&message);
  132. }
  133. /*
  134. * 发送停车指令请求
  135. */
  136. Error_manager Terminal_communication::store_request(message::Store_command_request_msg& request,message::Store_command_response_msg& response)
  137. {
  138. if(request.has_locate_information()==false ||
  139. request.has_car_info()==false)
  140. {
  141. return Error_manager(ERROR,CRITICAL_ERROR,"停车指令请求消息缺少必要信息");
  142. }
  143. Error_manager code;
  144. Communication_message message;
  145. message.reset(request.base_info(),request.SerializeAsString());
  146. int timeout=1000;
  147. code=encapsulate_msg(&message);
  148. if(code!=SUCCESS)
  149. return code;
  150. //循环查询请求是否被处理
  151. auto start_time=std::chrono::system_clock::now();
  152. double time=0;
  153. do {
  154. //查询到记录
  155. ///查询是否存在,并且删除该记录,
  156. //判断是否接收到回应,若回应信息被赋值则证明有回应
  157. if (m_store_response_msg.has_base_info() && m_store_response_msg.has_code()) {
  158. message::Base_info response_base = m_store_response_msg.base_info();
  159. //检查类型是否匹配
  160. if (response_base.msg_type() != message::eStore_command_response_msg) {
  161. return Error_manager(ERROR, CRITICAL_ERROR,
  162. "停车指令反馈消息 response msg type error");
  163. }
  164. //检查基本信息是否匹配
  165. if (response_base.sender() != message::eMain ||
  166. response_base.receiver() != message::eTerminor ) {
  167. return Error_manager(PARKSPACE_RELEASE_RESPONSE_INFO_ERROR, MAJOR_ERROR,
  168. "parkspace store response msg info error");
  169. }
  170. response=m_store_response_msg;
  171. m_store_response_msg=message::Store_command_response_msg();
  172. return SUCCESS;
  173. }
  174. auto end_time = std::chrono::system_clock::now();
  175. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  176. time = 1000.0 * double(duration.count()) * std::chrono::microseconds::period::num /
  177. std::chrono::microseconds::period::den;
  178. std::this_thread::yield();
  179. usleep(1000);
  180. }while(time<double(timeout));
  181. //超时,删除记录,返回错误
  182. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"parkspace store request timeout");
  183. }
  184. /*
  185. * 发送取车指令请求
  186. */
  187. Error_manager Terminal_communication::pickup_request(message::Pickup_command_request_msg& request,message::Pickup_command_response_msg& response)
  188. {
  189. if(request.has_car_info()==false)
  190. {
  191. return Error_manager(ERROR,CRITICAL_ERROR,"取车指令请求消息缺少车辆信息");
  192. }
  193. Error_manager code;
  194. Communication_message message;
  195. message.reset(request.base_info(),request.SerializeAsString());
  196. int timeout=1000;
  197. code=encapsulate_msg(&message);
  198. if(code!=SUCCESS)
  199. return code;
  200. // m_statu=eTerminal_picking;
  201. //循环查询请求是否被处理
  202. auto start_time=std::chrono::system_clock::now();
  203. double time=0;
  204. do {
  205. //查询到记录
  206. ///查询是否存在,并且删除该记录,
  207. //判断是否接收到回应,若回应信息被赋值则证明有回应
  208. if (m_pickup_response_msg.has_base_info() && m_pickup_response_msg.has_code())
  209. {
  210. message::Base_info response_base = m_pickup_response_msg.base_info();
  211. //检查类型是否匹配
  212. if (response_base.msg_type() != message::ePickup_command_response_msg) {
  213. return Error_manager(ERROR, CRITICAL_ERROR,
  214. "停车指令反馈消息 response msg type error");
  215. }
  216. //检查基本信息是否匹配
  217. if (response_base.sender() != message::eMain ||
  218. response_base.receiver() != message::eTerminor ) {
  219. return Error_manager(PARKSPACE_RELEASE_RESPONSE_INFO_ERROR, MAJOR_ERROR,
  220. "parkspace release response msg info error");
  221. }
  222. response.CopyFrom(m_pickup_response_msg);
  223. m_pickup_response_msg.clear_base_info();
  224. //m_pickup_response_msg=message::Pickup_command_response_msg();
  225. return SUCCESS;
  226. }
  227. auto end_time = std::chrono::system_clock::now();
  228. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
  229. time = 1000.0 * double(duration.count()) * std::chrono::microseconds::period::num /
  230. std::chrono::microseconds::period::den;
  231. std::this_thread::yield();
  232. usleep(1000);
  233. }while(time<double(timeout));
  234. //超时,删除记录,返回错误
  235. return Error_manager(RESPONSE_TIMEOUT,MINOR_ERROR,"parkspace release request timeout");
  236. }
  237. /*
  238. * 检查入口完成状态
  239. */
  240. Error_manager Terminal_communication::check_store_entrance_statu(int terminal_id,message::Entrance_statu& statu)
  241. {
  242. std::lock_guard<std::mutex> lk(m_statu_lock);
  243. auto it=m_entrance_status.mutable_store_msg_map()->find(terminal_id);
  244. if(it==m_entrance_status.mutable_store_msg_map()->end())
  245. {
  246. return ERROR;
  247. }
  248. else
  249. {
  250. statu=it->second;
  251. return SUCCESS;
  252. }
  253. }
  254. /*
  255. * 检查出口完成状态
  256. */
  257. Error_manager Terminal_communication::check_pickup_entrance_statu(int terminal_id,message::Entrance_statu& statu)
  258. {
  259. std::lock_guard<std::mutex> lk(m_statu_lock);
  260. auto it=m_entrance_status.mutable_pickup_msg_map()->find(terminal_id);
  261. if(it==m_entrance_status.mutable_pickup_msg_map()->end())
  262. {
  263. return ERROR;
  264. }
  265. else
  266. {
  267. statu=it->second;
  268. return SUCCESS;
  269. }
  270. }