123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- /*
- * @Description: 模拟主程序发送车位分配请求,并监听状态信息
- * @Author: yct
- * @Date: 2020-07-08 15:44:43
- * @LastEditTime: 2020-07-22 18:15:55
- * @LastEditors: yct
- */
- #include <iostream>
- #include <nnxx/message>
- #include <nnxx/socket.h>
- #include <nnxx/bus.h>
- #include "unistd.h"
- #include "parkspace_allocation_message.pb.h"
- #include <thread>
- std::string send_allocation_request(int n)
- {
- // 发送车位分配请求
- message::Error_manager error_code;
- message::Base_info base_info;
- message::Car_info car_info;
- message::Parkspace_allocation_request_msg request;
- base_info.set_msg_type(message::Message_type::eParkspace_allocation_request_msg);
- base_info.set_sender(message::eMain);
- base_info.set_receiver(message::eParkspace);
- base_info.set_timeout_ms(2000);
- car_info.set_car_height(1.0);
- car_info.set_car_width(1.0);
- car_info.set_license("鄂A12345");
- request.mutable_base_info()->CopyFrom(base_info);
- request.set_command_id(n);
- request.set_terminal_id(1);
- request.mutable_car_info()->CopyFrom(car_info);
- // socket.send(request.SerializeAsString());
- return request.SerializeAsString();
- }
- std::string send_search_requests(int n, std::string license="鄂A12345")
- {
- // 发送车位查询请求
- message::Error_manager error_code;
- message::Base_info base_info;
- message::Car_info car_info;
- message::Parkspace_search_request_msg request;
- base_info.set_msg_type(message::Message_type::eParkspace_search_request_msg);
- base_info.set_sender(message::eMain);
- base_info.set_receiver(message::eParkspace);
- base_info.set_timeout_ms(10);
- car_info.set_car_height(2.0);
- car_info.set_car_width(2.0);
- car_info.set_license(license);
- request.mutable_base_info()->CopyFrom(base_info);
- request.set_command_id(n);
- request.mutable_car_info()->CopyFrom(car_info);
- // socket.send(request.SerializeAsString());
- return request.SerializeAsString();
- }
- std::string send_release_request(int n)
- {
- // 发送车位释放请求
- message::Error_manager error_code;
- message::Base_info base_info;
- message::Car_info car_info;
- message::Parkspace_info space_info;
- message::Parkspace_release_request_msg request;
- base_info.set_msg_type(message::Message_type::eParkspace_release_request_msg);
- base_info.set_sender(message::eMain);
- base_info.set_receiver(message::eParkspace);
- base_info.set_timeout_ms(2000);
- car_info.set_car_height(1.5);
- car_info.set_car_width(1.85);
- car_info.set_license("鄂A12345");
- space_info.set_parkspace_id(1);
- space_info.set_index(1);
- space_info.set_direction(message::Direction::eForward);
- space_info.set_floor(33);
- space_info.set_width(1.5);
- space_info.set_height(1.5);
- space_info.set_parkspace_status(message::Parkspace_status::eParkspace_occupied);
- space_info.mutable_car_info()->CopyFrom(car_info);
- request.mutable_base_info()->CopyFrom(base_info);
- request.set_command_id(n);
- request.mutable_release_space_info()->CopyFrom(space_info);
- // socket.send(request.SerializeAsString());
- return request.SerializeAsString();
- }
- std::string send_confirm_request(int n)
- {
- // 发送车位释放请求
- message::Error_manager error_code;
- message::Base_info base_info;
- message::Car_info car_info;
- message::Parkspace_info space_info;
- message::Parkspace_confirm_alloc_request_msg request;
- base_info.set_msg_type(message::Message_type::eParkspace_confirm_alloc_request_msg);
- base_info.set_sender(message::eMain);
- base_info.set_receiver(message::eParkspace);
- base_info.set_timeout_ms(2000);
- car_info.set_car_height(1.5);
- car_info.set_car_width(1.85);
- car_info.set_license("鄂A12345");
- space_info.set_parkspace_id(1);
- space_info.set_index(1);
- space_info.set_direction(message::Direction::eForward);
- space_info.set_floor(33);
- space_info.set_width(1.5);
- space_info.set_height(1.5);
- space_info.set_parkspace_status(message::Parkspace_status::eParkspace_occupied);
- space_info.mutable_car_info()->CopyFrom(car_info);
- request.mutable_base_info()->CopyFrom(base_info);
- request.set_command_id(n);
- request.mutable_confirm_space_info()->CopyFrom(space_info);
- // socket.send(request.SerializeAsString());
- return request.SerializeAsString();
- }
- int main()
- {
- nnxx::socket socket{nnxx::SP, nnxx::BUS};
- // socket.bind("tcp://127.0.0.1:7000");
- socket.connect("tcp://127.0.0.1:7001");
- int n = 0;
- message::Base_msg base_msg;
- usleep(1000 * 3000);
- std::string alloc_req = send_allocation_request(1);
- socket.send(alloc_req);
- usleep(1000 * 500);
- // alloc_req = send_allocation_request(2);
- // socket.send(alloc_req);
- // alloc_req = send_allocation_request(3);
- // socket.send(alloc_req);
- // alloc_req = send_allocation_request(4);
- // socket.send(alloc_req);
- // alloc_req = send_allocation_request(5);
- // socket.send(alloc_req);
- std::string confirm_req = send_confirm_request(1);
- socket.send(confirm_req);
- usleep(1000 * 500);
-
- std::string search_req = send_search_requests(1);
- socket.send(search_req);
- usleep(1000 * 500);
- std::string release_req = send_release_request(1);
- socket.send(release_req);
- usleep(1000 * 500);
- // usleep(1000*200);
- // search_req = send_search_requests(3);
- // socket.send(search_req);
- // release_req = send_release_request(3);
- // socket.send(release_req);
- while (1)
- {
- // std::string alloc_req = send_allocation_request(n);
- // std::string search_req = send_search_requests(n);
- // std::string alloc_req = send_allocation_request(n);
- // socket.send(alloc_req);
- // usleep(1000*2000);
- // std::string search_req = send_search_requests(n);
- // socket.send(search_req);
- // // usleep(1000*2000);
- // std::string release_req = send_release_request(n);
- // socket.send(release_req);
- // usleep(1000*2000);
- for (size_t i = 0; i < 5; i++)
- {
- // 接收消息
- std::string t_receive_string = socket.recv<std::string>(1);
- if (t_receive_string.length() > 0)
- {
- bool result = base_msg.ParseFromString(t_receive_string);
- // 接收并打印车位状态信息
- std::cout << "========================" << result << "============================" << std::endl;
- std::cout << "cycle " << n << std::endl;
- if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_allocation_status_msg)
- {
- message::Parkspace_allocation_status_msg parkspace_status;
- std::cout << "----------------- 车位状态 -----------------" << std::endl;
- parkspace_status.ParseFromString(t_receive_string);
- std::cout << parkspace_status.DebugString() << std::endl;
- // continue;
- }
- else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_allocation_response_msg)
- {
- message::Parkspace_allocation_response_msg response;
- response.ParseFromString(t_receive_string);
- std::cout << "----------------- 分配车位结果 -----------------" << std::endl;
- std::cout << response.DebugString() << std::endl;
- }
- else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_search_response_msg)
- {
- message::Parkspace_search_response_msg response;
- response.ParseFromString(t_receive_string);
- std::cout << "----------------- 查询车辆位置结果 -----------------" << std::endl;
- std::cout << response.DebugString() << std::endl;
- }
- else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_release_response_msg)
- {
- message::Parkspace_release_response_msg response;
- response.ParseFromString(t_receive_string);
- std::cout << "----------------- 释放车位结果 -----------------" << std::endl;
- std::cout << response.DebugString() << std::endl;
- }
- else if (base_msg.base_info().msg_type() == message::Message_type::eParkspace_confirm_alloc_response_msg)
- {
- message::Parkspace_confirm_alloc_response_msg response;
- response.ParseFromString(t_receive_string);
- std::cout << "----------------- 确认占用车位结果 -----------------" << std::endl;
- std::cout << response.DebugString() << std::endl;
- }
- }
- }
- // std::this_thread::yield();
- n++;
- usleep(1000 * 1000);
- }
- }
|