parkspace_client.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * @Description: 模拟主程序发送车位分配请求,并监听状态信息
  3. * @Author: yct
  4. * @Date: 2020-07-08 15:44:43
  5. * @LastEditTime: 2020-07-22 18:15:55
  6. * @LastEditors: yct
  7. */
  8. #include <iostream>
  9. #include <nnxx/message>
  10. #include <nnxx/socket.h>
  11. #include <nnxx/bus.h>
  12. #include "unistd.h"
  13. #include "parkspace_allocation_message.pb.h"
  14. #include <thread>
  15. std::string send_allocation_request(int n)
  16. {
  17. // 发送车位分配请求
  18. message::Error_manager error_code;
  19. message::Base_info base_info;
  20. message::Car_info car_info;
  21. message::Parkspace_allocation_request_msg request;
  22. base_info.set_msg_type(message::Message_type::eParkspace_allocation_request_msg);
  23. base_info.set_sender(message::eMain);
  24. base_info.set_receiver(message::eParkspace);
  25. base_info.set_timeout_ms(2000);
  26. car_info.set_car_height(1.0);
  27. car_info.set_car_width(1.0);
  28. car_info.set_license("鄂A12345");
  29. request.mutable_base_info()->CopyFrom(base_info);
  30. request.set_command_id(n);
  31. request.set_terminal_id(1);
  32. request.mutable_car_info()->CopyFrom(car_info);
  33. // socket.send(request.SerializeAsString());
  34. return request.SerializeAsString();
  35. }
  36. std::string send_search_requests(int n, std::string license="鄂A12345")
  37. {
  38. // 发送车位查询请求
  39. message::Error_manager error_code;
  40. message::Base_info base_info;
  41. message::Car_info car_info;
  42. message::Parkspace_search_request_msg request;
  43. base_info.set_msg_type(message::Message_type::eParkspace_search_request_msg);
  44. base_info.set_sender(message::eMain);
  45. base_info.set_receiver(message::eParkspace);
  46. base_info.set_timeout_ms(10);
  47. car_info.set_car_height(2.0);
  48. car_info.set_car_width(2.0);
  49. car_info.set_license(license);
  50. request.mutable_base_info()->CopyFrom(base_info);
  51. request.set_command_id(n);
  52. request.mutable_car_info()->CopyFrom(car_info);
  53. // socket.send(request.SerializeAsString());
  54. return request.SerializeAsString();
  55. }
  56. std::string send_release_request(int n)
  57. {
  58. // 发送车位释放请求
  59. message::Error_manager error_code;
  60. message::Base_info base_info;
  61. message::Car_info car_info;
  62. message::Parkspace_info space_info;
  63. message::Parkspace_release_request_msg request;
  64. base_info.set_msg_type(message::Message_type::eParkspace_release_request_msg);
  65. base_info.set_sender(message::eMain);
  66. base_info.set_receiver(message::eParkspace);
  67. base_info.set_timeout_ms(2000);
  68. car_info.set_car_height(1.5);
  69. car_info.set_car_width(1.85);
  70. car_info.set_license("鄂A12345");
  71. space_info.set_parkspace_id(1);
  72. space_info.set_index(1);
  73. space_info.set_direction(message::Direction::eForward);
  74. space_info.set_floor(33);
  75. space_info.set_width(1.5);
  76. space_info.set_height(1.5);
  77. space_info.set_parkspace_status(message::Parkspace_status::eParkspace_occupied);
  78. space_info.mutable_car_info()->CopyFrom(car_info);
  79. request.mutable_base_info()->CopyFrom(base_info);
  80. request.set_command_id(n);
  81. request.mutable_release_space_info()->CopyFrom(space_info);
  82. // socket.send(request.SerializeAsString());
  83. return request.SerializeAsString();
  84. }
  85. std::string send_confirm_request(int n)
  86. {
  87. // 发送车位释放请求
  88. message::Error_manager error_code;
  89. message::Base_info base_info;
  90. message::Car_info car_info;
  91. message::Parkspace_info space_info;
  92. message::Parkspace_confirm_alloc_request_msg request;
  93. base_info.set_msg_type(message::Message_type::eParkspace_confirm_alloc_request_msg);
  94. base_info.set_sender(message::eMain);
  95. base_info.set_receiver(message::eParkspace);
  96. base_info.set_timeout_ms(2000);
  97. car_info.set_car_height(1.5);
  98. car_info.set_car_width(1.85);
  99. car_info.set_license("鄂A12345");
  100. space_info.set_parkspace_id(1);
  101. space_info.set_index(1);
  102. space_info.set_direction(message::Direction::eForward);
  103. space_info.set_floor(33);
  104. space_info.set_width(1.5);
  105. space_info.set_height(1.5);
  106. space_info.set_parkspace_status(message::Parkspace_status::eParkspace_occupied);
  107. space_info.mutable_car_info()->CopyFrom(car_info);
  108. request.mutable_base_info()->CopyFrom(base_info);
  109. request.set_command_id(n);
  110. request.mutable_confirm_space_info()->CopyFrom(space_info);
  111. // socket.send(request.SerializeAsString());
  112. return request.SerializeAsString();
  113. }
  114. int main()
  115. {
  116. nnxx::socket socket{nnxx::SP, nnxx::BUS};
  117. // socket.bind("tcp://127.0.0.1:7000");
  118. socket.connect("tcp://127.0.0.1:7001");
  119. int n = 0;
  120. message::Base_msg base_msg;
  121. usleep(1000 * 3000);
  122. std::string alloc_req = send_allocation_request(1);
  123. socket.send(alloc_req);
  124. usleep(1000 * 500);
  125. // alloc_req = send_allocation_request(2);
  126. // socket.send(alloc_req);
  127. // alloc_req = send_allocation_request(3);
  128. // socket.send(alloc_req);
  129. // alloc_req = send_allocation_request(4);
  130. // socket.send(alloc_req);
  131. // alloc_req = send_allocation_request(5);
  132. // socket.send(alloc_req);
  133. std::string confirm_req = send_confirm_request(1);
  134. socket.send(confirm_req);
  135. usleep(1000 * 500);
  136. std::string search_req = send_search_requests(1);
  137. socket.send(search_req);
  138. usleep(1000 * 500);
  139. std::string release_req = send_release_request(1);
  140. socket.send(release_req);
  141. usleep(1000 * 500);
  142. // usleep(1000*200);
  143. // search_req = send_search_requests(3);
  144. // socket.send(search_req);
  145. // release_req = send_release_request(3);
  146. // socket.send(release_req);
  147. while (1)
  148. {
  149. // std::string alloc_req = send_allocation_request(n);
  150. // std::string search_req = send_search_requests(n);
  151. // std::string alloc_req = send_allocation_request(n);
  152. // socket.send(alloc_req);
  153. // usleep(1000*2000);
  154. // std::string search_req = send_search_requests(n);
  155. // socket.send(search_req);
  156. // // usleep(1000*2000);
  157. // std::string release_req = send_release_request(n);
  158. // socket.send(release_req);
  159. // usleep(1000*2000);
  160. for (size_t i = 0; i < 5; i++)
  161. {
  162. // 接收消息
  163. std::string t_receive_string = socket.recv<std::string>(1);
  164. if (t_receive_string.length() > 0)
  165. {
  166. bool result = base_msg.ParseFromString(t_receive_string);
  167. // 接收并打印车位状态信息
  168. std::cout << "========================" << result << "============================" << std::endl;
  169. std::cout << "cycle " << n << std::endl;
  170. if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_allocation_status_msg)
  171. {
  172. message::Parkspace_allocation_status_msg parkspace_status;
  173. std::cout << "----------------- 车位状态 -----------------" << std::endl;
  174. parkspace_status.ParseFromString(t_receive_string);
  175. std::cout << parkspace_status.DebugString() << std::endl;
  176. // continue;
  177. }
  178. else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_allocation_response_msg)
  179. {
  180. message::Parkspace_allocation_response_msg response;
  181. response.ParseFromString(t_receive_string);
  182. std::cout << "----------------- 分配车位结果 -----------------" << std::endl;
  183. std::cout << response.DebugString() << std::endl;
  184. }
  185. else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_search_response_msg)
  186. {
  187. message::Parkspace_search_response_msg response;
  188. response.ParseFromString(t_receive_string);
  189. std::cout << "----------------- 查询车辆位置结果 -----------------" << std::endl;
  190. std::cout << response.DebugString() << std::endl;
  191. }
  192. else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_release_response_msg)
  193. {
  194. message::Parkspace_release_response_msg response;
  195. response.ParseFromString(t_receive_string);
  196. std::cout << "----------------- 释放车位结果 -----------------" << std::endl;
  197. std::cout << response.DebugString() << std::endl;
  198. }
  199. else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_confirm_alloc_response_msg)
  200. {
  201. message::Parkspace_confirm_alloc_response_msg response;
  202. response.ParseFromString(t_receive_string);
  203. std::cout << "----------------- 确认占用车位结果 -----------------" << std::endl;
  204. std::cout << response.DebugString() << std::endl;
  205. }
  206. }
  207. }
  208. // std::this_thread::yield();
  209. n++;
  210. usleep(1000 * 1000);
  211. }
  212. }