#include <fstream> #include <iostream> #include <thread> #include <boost/asio.hpp> #include <boost/format.hpp> #include "tcp_service.hpp" #include "tcp_client.hpp" #include "methane_serial_port.hpp" #include "config.hpp" int main() { //启动TCP服务端 std::thread service_thread([] { try { boost::asio::io_service io; TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); io.run(); } catch (const boost::wrapexcept<boost::system::system_error> &e) { std::cerr << "[WARN] Boost exception: " << e.what() << std::endl; } }); // 启动TCP客户端 std::thread client_thread([] { boost::asio::io_service io; TcpClient client; client.connect(Config::REMOTE_SERVICE_IP, Config::TCP_SERVICE_REMOTE_PORT); io.run(); }); //启动甲烷数据查询串口线程 std::thread gas_serial_port_thread([] { try { boost::asio::io_service io; //打开串口 MethaneSerialPort sp(io, Config::GAS_PORT, Config::BAUD_RATE); //甲烷串口 sp.read_from_port(); io.run(); } catch (std::exception &e) { std::cerr << e.what() << std::endl; } }); // 拉起第三方脚本 std::thread script_thread([] { const auto cmd = ( boost::format("setsid sh -c '%1%' > %2% 2>&1 &") % Config::SLAM_SCRIPT_FILE % Config::SLAM_LOG_FILE ).str(); std::system(cmd.c_str()); // 等几秒钟,扫一遍日志确认启动成功 std::this_thread::sleep_for(std::chrono::seconds(2)); std::ifstream log(Config::SLAM_LOG_FILE); std::string line; bool ok = false; while (std::getline(log, line)) { // 确认启动是否成功 if (line.find("process started") != std::string::npos) { ok = true; break; } } if (ok) { std::cout << "Unitree Slam service init success" << std::endl; } else { std::cerr << "[WARN] Unitree Slam service may not be ready yet" << std::endl; } }); // 等待所有线程完成 service_thread.join(); client_thread.join(); gas_serial_port_thread.join(); script_thread.join(); return 0; }