parkspace_client.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #define CAR_LICENSE "鄂D12345"
  16. std::string send_allocation_request(int n)
  17. {
  18. // 发送车位分配请求
  19. message::Error_manager error_code;
  20. message::Base_info base_info;
  21. message::Car_info car_info;
  22. message::Parkspace_allocation_request_msg request;
  23. base_info.set_msg_type(message::Message_type::eParkspace_allocation_request_msg);
  24. base_info.set_sender(message::eMain);
  25. base_info.set_receiver(message::eParkspace);
  26. base_info.set_timeout_ms(2000);
  27. car_info.set_car_height(1.94);
  28. car_info.set_car_width(1.94);
  29. car_info.set_license(CAR_LICENSE);
  30. request.mutable_base_info()->CopyFrom(base_info);
  31. request.set_command_key(std::to_string(n));
  32. request.set_terminal_id(1);
  33. request.mutable_car_info()->CopyFrom(car_info);
  34. // socket.send(request.SerializeAsString());
  35. return request.SerializeAsString();
  36. }
  37. std::string send_search_requests(int n, std::string license=CAR_LICENSE)
  38. {
  39. // 发送车位查询请求
  40. message::Error_manager error_code;
  41. message::Base_info base_info;
  42. message::Car_info car_info;
  43. message::Parkspace_search_request_msg request;
  44. base_info.set_msg_type(message::Message_type::eParkspace_search_request_msg);
  45. base_info.set_sender(message::eMain);
  46. base_info.set_receiver(message::eParkspace);
  47. base_info.set_timeout_ms(10);
  48. car_info.set_car_height(2.0);
  49. car_info.set_car_width(2.0);
  50. car_info.set_license(license);
  51. request.mutable_base_info()->CopyFrom(base_info);
  52. request.set_command_key(std::to_string(n));
  53. request.mutable_car_info()->CopyFrom(car_info);
  54. // socket.send(request.SerializeAsString());
  55. return request.SerializeAsString();
  56. }
  57. std::string send_release_request(int n)
  58. {
  59. // 发送车位释放请求
  60. message::Error_manager error_code;
  61. message::Base_info base_info;
  62. message::Car_info car_info;
  63. message::Parkspace_info space_info;
  64. message::Parkspace_release_request_msg request;
  65. base_info.set_msg_type(message::Message_type::eParkspace_release_request_msg);
  66. base_info.set_sender(message::eMain);
  67. base_info.set_receiver(message::eParkspace);
  68. base_info.set_timeout_ms(2000);
  69. car_info.set_car_height(1.5);
  70. car_info.set_car_width(1.85);
  71. car_info.set_license(CAR_LICENSE);
  72. space_info.set_parkingspace_index_id(1);
  73. space_info.set_parkingspace_room_id(1);
  74. space_info.set_parkingspace_direction(message::Direction::eForward);
  75. space_info.set_parkingspace_floor_id(3);
  76. space_info.set_parkingspace_height(1.5);
  77. space_info.set_parkingspace_width(1.5);
  78. space_info.set_parkingspace_status(message::Parkspace_status::eParkspace_occupied);
  79. space_info.mutable_car_info()->CopyFrom(car_info);
  80. request.mutable_base_info()->CopyFrom(base_info);
  81. request.set_command_key(std::to_string(n));
  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(CAR_LICENSE);
  100. space_info.set_parkingspace_index_id(1);
  101. space_info.set_parkingspace_room_id(1);
  102. space_info.set_parkingspace_direction(message::Direction::eForward);
  103. space_info.set_parkingspace_floor_id(33);
  104. space_info.set_parkingspace_width(1.5);
  105. space_info.set_parkingspace_height(1.5);
  106. space_info.set_parkingspace_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_key(std::to_string(n));
  110. // socket.send(request.SerializeAsString());
  111. return request.SerializeAsString();
  112. }
  113. int main()
  114. {
  115. nnxx::socket socket{nnxx::SP, nnxx::BUS};
  116. // socket.bind("tcp://127.0.0.1:7000");
  117. // socket.connect("tcp://192.168.2.113:30008");
  118. // socket.connect("tcp://127.0.0.1:30005");
  119. socket.connect("tcp://192.168.2.113:30008");
  120. //
  121. int n = 0;
  122. message::Base_msg base_msg;
  123. usleep(1000 * 1000);
  124. std::string alloc_req = send_allocation_request(1);
  125. socket.send(alloc_req);
  126. usleep(1000 * 500);
  127. // std::string confirm_req = send_confirm_request(1);
  128. // socket.send(confirm_req);
  129. // usleep(1000 * 500);
  130. //
  131. // std::string search_req = send_search_requests(1);
  132. // socket.send(search_req);
  133. // usleep(1000 * 500);
  134. //
  135. // std::string release_req = send_release_request(1);
  136. // socket.send(release_req);
  137. // usleep(1000 * 500);
  138. // usleep(1000*200);
  139. // search_req = send_search_requests(3);
  140. // socket.send(search_req);
  141. // release_req = send_release_request(3);
  142. // socket.send(release_req);
  143. // while (1)
  144. // {
  145. // // std::string alloc_req = send_allocation_request(n);
  146. // // std::string search_req = send_search_requests(n);
  147. //
  148. // // std::string alloc_req = send_allocation_request(n);
  149. // // socket.send(alloc_req);
  150. // // usleep(1000*2000);
  151. // // std::string search_req = send_search_requests(n);
  152. // // socket.send(search_req);
  153. // // // usleep(1000*2000);
  154. // // std::string release_req = send_release_request(n);
  155. // // socket.send(release_req);
  156. // // usleep(1000*2000);
  157. //
  158. // for (size_t i = 0; i < 5; i++)
  159. // {
  160. // // 接收消息
  161. // std::string t_receive_string = socket.recv<std::string>(1);
  162. // if (t_receive_string.length() > 0)
  163. // {
  164. // bool result = base_msg.ParseFromString(t_receive_string);
  165. // // 接收并打印车位状态信息
  166. // std::cout << "========================" << result << "============================" << std::endl;
  167. // std::cout << "cycle " << n << std::endl;
  168. // if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_allocation_status_msg)
  169. // {
  170. // message::Parkspace_allocation_status_msg parkspace_status;
  171. // std::cout << "----------------- 车位状态 -----------------" << std::endl;
  172. // parkspace_status.ParseFromString(t_receive_string);
  173. // std::cout << parkspace_status.DebugString() << std::endl;
  174. // // continue;
  175. // }
  176. // else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_allocation_response_msg)
  177. // {
  178. // message::Parkspace_allocation_response_msg response;
  179. // response.ParseFromString(t_receive_string);
  180. // std::cout << "----------------- 分配车位结果 -----------------" << std::endl;
  181. // std::cout << response.DebugString() << std::endl;
  182. // }
  183. // else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_search_response_msg)
  184. // {
  185. // message::Parkspace_search_response_msg response;
  186. // response.ParseFromString(t_receive_string);
  187. // std::cout << "----------------- 查询车辆位置结果 -----------------" << std::endl;
  188. // std::cout << response.DebugString() << std::endl;
  189. // }
  190. // else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_release_response_msg)
  191. // {
  192. // message::Parkspace_release_response_msg response;
  193. // response.ParseFromString(t_receive_string);
  194. // std::cout << "----------------- 释放车位结果 -----------------" << std::endl;
  195. // std::cout << response.DebugString() << std::endl;
  196. // }
  197. // else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_confirm_alloc_response_msg)
  198. // {
  199. // message::Parkspace_confirm_alloc_response_msg response;
  200. // response.ParseFromString(t_receive_string);
  201. // std::cout << "----------------- 确认占用车位结果 -----------------" << std::endl;
  202. // std::cout << response.DebugString() << std::endl;
  203. // }
  204. // }
  205. // }
  206. //
  207. // // std::this_thread::yield();
  208. // n++;
  209. // usleep(1000 * 1000);
  210. // }
  211. }