diff --git a/CMakeLists.txt b/CMakeLists.txt index 229ed37..e233d79 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,5 +17,7 @@ src/slam_wrapper.cpp src/robotic_arm_serial_port.cpp src/methane_serial_port.cpp + src/utils.cpp + src/utils.hpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 229ed37..e233d79 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,5 +17,7 @@ src/slam_wrapper.cpp src/robotic_arm_serial_port.cpp src/methane_serial_port.cpp + src/utils.cpp + src/utils.hpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index 4b183d3..8a15d63 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -5,6 +5,7 @@ #include "methane_serial_port.hpp" #include "tcp_service.hpp" #include "constant_config.hpp" +#include "utils.hpp" #include #include @@ -67,8 +68,12 @@ const auto w = buffer[5]; const auto result = x * (1 << 24) + y * (1 << 16) + z * (1 << 8) + w; TcpService::getInstance().update_gas_value(result); - //TODO 上传服务器 - // upload_to_server_async(result); + // 上传服务器 + if (Utils::is_network_reachable(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::HTTP_PORT)) { + upload_to_server_async(result); + } else { + std::cout << "网络异常,上传失败" << std::endl; + } } MethaneSerialPort::MethaneSerialPort( diff --git a/CMakeLists.txt b/CMakeLists.txt index 229ed37..e233d79 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,5 +17,7 @@ src/slam_wrapper.cpp src/robotic_arm_serial_port.cpp src/methane_serial_port.cpp + src/utils.cpp + src/utils.hpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index 4b183d3..8a15d63 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -5,6 +5,7 @@ #include "methane_serial_port.hpp" #include "tcp_service.hpp" #include "constant_config.hpp" +#include "utils.hpp" #include #include @@ -67,8 +68,12 @@ const auto w = buffer[5]; const auto result = x * (1 << 24) + y * (1 << 16) + z * (1 << 8) + w; TcpService::getInstance().update_gas_value(result); - //TODO 上传服务器 - // upload_to_server_async(result); + // 上传服务器 + if (Utils::is_network_reachable(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::HTTP_PORT)) { + upload_to_server_async(result); + } else { + std::cout << "网络异常,上传失败" << std::endl; + } } MethaneSerialPort::MethaneSerialPort( diff --git a/src/slam_wrapper.cpp b/src/slam_wrapper.cpp index da1ae6e..7e01df9 100644 --- a/src/slam_wrapper.cpp +++ b/src/slam_wrapper.cpp @@ -5,6 +5,7 @@ #include "slam_wrapper.hpp" #include "tcp_service.hpp" #include "constant_config.hpp" +#include "utils.hpp" #include #include @@ -336,8 +337,12 @@ send_msg.floor_index_().push_back(0); // Floor number,just give a fixed value of 0. send_msg.pcdmap_index_().push_back(0); // PCD Map number,just give a fixed value of 0. pubQtCommand->Write(send_msg); - //TODO 上传服务器 - // upload_pcd_file(); + //上传服务器 + if (Utils::is_network_reachable(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::HTTP_PORT)) { + upload_pcd_file(); + } else { + std::cout << "网络异常,上传失败" << std::endl; + } } void SlamWrapper::start_relocation() { diff --git a/CMakeLists.txt b/CMakeLists.txt index 229ed37..e233d79 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,5 +17,7 @@ src/slam_wrapper.cpp src/robotic_arm_serial_port.cpp src/methane_serial_port.cpp + src/utils.cpp + src/utils.hpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index 4b183d3..8a15d63 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -5,6 +5,7 @@ #include "methane_serial_port.hpp" #include "tcp_service.hpp" #include "constant_config.hpp" +#include "utils.hpp" #include #include @@ -67,8 +68,12 @@ const auto w = buffer[5]; const auto result = x * (1 << 24) + y * (1 << 16) + z * (1 << 8) + w; TcpService::getInstance().update_gas_value(result); - //TODO 上传服务器 - // upload_to_server_async(result); + // 上传服务器 + if (Utils::is_network_reachable(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::HTTP_PORT)) { + upload_to_server_async(result); + } else { + std::cout << "网络异常,上传失败" << std::endl; + } } MethaneSerialPort::MethaneSerialPort( diff --git a/src/slam_wrapper.cpp b/src/slam_wrapper.cpp index da1ae6e..7e01df9 100644 --- a/src/slam_wrapper.cpp +++ b/src/slam_wrapper.cpp @@ -5,6 +5,7 @@ #include "slam_wrapper.hpp" #include "tcp_service.hpp" #include "constant_config.hpp" +#include "utils.hpp" #include #include @@ -336,8 +337,12 @@ send_msg.floor_index_().push_back(0); // Floor number,just give a fixed value of 0. send_msg.pcdmap_index_().push_back(0); // PCD Map number,just give a fixed value of 0. pubQtCommand->Write(send_msg); - //TODO 上传服务器 - // upload_pcd_file(); + //上传服务器 + if (Utils::is_network_reachable(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::HTTP_PORT)) { + upload_pcd_file(); + } else { + std::cout << "网络异常,上传失败" << std::endl; + } } void SlamWrapper::start_relocation() { diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..b795f3c --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,21 @@ +// +// Created by pengx on 2025/7/11. +// + +#include "utils.hpp" + +bool Utils::is_network_reachable(const std::string &host, const std::string &service) { + try { + boost::asio::io_context io; + boost::asio::ip::tcp::resolver resolver(io); + boost::asio::ip::tcp::socket socket(io); + + const auto endpoints = resolver.resolve(host, service); + boost::asio::connect(socket, endpoints); + + return true; + } catch (std::exception &e) { + std::cerr << "Network error: " << e.what() << std::endl; + return false; + } +} diff --git a/CMakeLists.txt b/CMakeLists.txt index 229ed37..e233d79 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,5 +17,7 @@ src/slam_wrapper.cpp src/robotic_arm_serial_port.cpp src/methane_serial_port.cpp + src/utils.cpp + src/utils.hpp ) target_link_libraries(message_handler unitree_sdk2) \ No newline at end of file diff --git a/src/methane_serial_port.cpp b/src/methane_serial_port.cpp index 4b183d3..8a15d63 100644 --- a/src/methane_serial_port.cpp +++ b/src/methane_serial_port.cpp @@ -5,6 +5,7 @@ #include "methane_serial_port.hpp" #include "tcp_service.hpp" #include "constant_config.hpp" +#include "utils.hpp" #include #include @@ -67,8 +68,12 @@ const auto w = buffer[5]; const auto result = x * (1 << 24) + y * (1 << 16) + z * (1 << 8) + w; TcpService::getInstance().update_gas_value(result); - //TODO 上传服务器 - // upload_to_server_async(result); + // 上传服务器 + if (Utils::is_network_reachable(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::HTTP_PORT)) { + upload_to_server_async(result); + } else { + std::cout << "网络异常,上传失败" << std::endl; + } } MethaneSerialPort::MethaneSerialPort( diff --git a/src/slam_wrapper.cpp b/src/slam_wrapper.cpp index da1ae6e..7e01df9 100644 --- a/src/slam_wrapper.cpp +++ b/src/slam_wrapper.cpp @@ -5,6 +5,7 @@ #include "slam_wrapper.hpp" #include "tcp_service.hpp" #include "constant_config.hpp" +#include "utils.hpp" #include #include @@ -336,8 +337,12 @@ send_msg.floor_index_().push_back(0); // Floor number,just give a fixed value of 0. send_msg.pcdmap_index_().push_back(0); // PCD Map number,just give a fixed value of 0. pubQtCommand->Write(send_msg); - //TODO 上传服务器 - // upload_pcd_file(); + //上传服务器 + if (Utils::is_network_reachable(ConstantConfig::SERVICE_ADDRESS, ConstantConfig::HTTP_PORT)) { + upload_pcd_file(); + } else { + std::cout << "网络异常,上传失败" << std::endl; + } } void SlamWrapper::start_relocation() { diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..b795f3c --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,21 @@ +// +// Created by pengx on 2025/7/11. +// + +#include "utils.hpp" + +bool Utils::is_network_reachable(const std::string &host, const std::string &service) { + try { + boost::asio::io_context io; + boost::asio::ip::tcp::resolver resolver(io); + boost::asio::ip::tcp::socket socket(io); + + const auto endpoints = resolver.resolve(host, service); + boost::asio::connect(socket, endpoints); + + return true; + } catch (std::exception &e) { + std::cerr << "Network error: " << e.what() << std::endl; + return false; + } +} diff --git a/src/utils.hpp b/src/utils.hpp new file mode 100644 index 0000000..bb20489 --- /dev/null +++ b/src/utils.hpp @@ -0,0 +1,16 @@ +// +// Created by pengx on 2025/7/11. +// + +#ifndef UTILS_HPP +#define UTILS_HPP + +#include +#include + +class Utils { +public: + static bool is_network_reachable(const std::string &host, const std::string &service); +}; + +#endif //UTILS_HPP