#include <filesystem> #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" namespace fs = std::filesystem; int main() { //启动TCP服务端 std::thread service_thread([] { boost::asio::io_service io; TcpService::getInstance().start(Config::TCP_SERVICE_LOCALE_PORT); io.run(); }); // 启动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 << "\033[33m" "MessageHandler [MethaneSerialPort]" << e.what() << "\033[0m" << std::endl; } }); // 拉起第三方脚本 std::thread script_thread([] { fs::current_path(Config::SLAM_SCRIPT_PATH); for (const auto &entry: fs::directory_iterator(fs::current_path())) { if (entry.is_regular_file() && entry.path().extension() == ".sh") { const auto script_path = entry.path(); std::ostringstream cmd_ss; cmd_ss << "./" + script_path.filename().string() << " > " << Config::SLAM_LOG_FILE << " 2>&1"; const std::string command = cmd_ss.str(); std::system(command.c_str()); break; } } }); // 等待所有线程完成 service_thread.join(); client_thread.join(); gas_serial_port_thread.join(); script_thread.join(); return 0; }