kunlang_tbox/src/main.cpp

168 lines
5.3 KiB
C++
Raw Normal View History

2025-12-11 09:08:35 +08:00
#include "main.h"
#include <unistd.h>
#include <csignal>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
// -----------------------------------------------------------------------------
// 命令行参数解析
// -----------------------------------------------------------------------------
struct CmdOptions
{
bool show_help = false;
bool show_version = false;
bool init_config = false;
std::string config_path;
};
CmdOptions parseArgs(int argc, char* argv[])
{
CmdOptions opts;
if (argc == 1)
{
// 默认情况:使用当前目录下的 config.ini
opts.config_path = "./config.ini";
return opts;
}
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if (arg == "-h" || arg == "--help")
{
if (opts.show_help || opts.show_version || !opts.config_path.empty())
{
std::cerr << "Error: duplicate or conflicting options.\n";
exit(1);
}
opts.show_help = true;
}
else if (arg == "-v" || arg == "--version")
{
if (opts.show_help || opts.show_version || !opts.config_path.empty())
{
std::cerr << "Error: duplicate or conflicting options.\n";
exit(1);
}
opts.show_version = true;
}
else if (arg == "-c" || arg == "--config")
{
if (!opts.config_path.empty() || opts.show_help || opts.show_version)
{
std::cerr << "Error: duplicate or conflicting options.\n";
exit(1);
}
if (i + 1 < argc) { opts.config_path = argv[++i]; }
else
{
std::cerr << "Error: missing config file path after " << arg << "\n";
exit(1);
}
}
else if (arg == "--init-config")
{
if (opts.show_help || opts.show_version || opts.init_config)
{
std::cerr << "Error: duplicate or conflicting options.\n";
exit(1);
}
opts.init_config = true;
}
else
{
std::cerr << "Unknown option: " << arg << "\n";
std::cerr << "Use -h to see available options.\n";
exit(1);
}
}
return opts;
}
// -----------------------------------------------------------------------------
// 日志初始化
// -----------------------------------------------------------------------------
Logger veh_rc_logger(common::get_executable_file_path("/log/veh_rc"));
Logger tbox_logger(common::get_executable_file_path("/log/tbox"));
Logger v2v_logger(common::get_executable_file_path("/log/v2v"));
// -----------------------------------------------------------------------------
// 主函数
// -----------------------------------------------------------------------------
int main(int argc, char* argv[])
{
CmdOptions opts = parseArgs(argc, argv);
if (opts.init_config)
{
std::string path = opts.config_path.empty() ? "./config.ini" : opts.config_path;
if (std::filesystem::exists(path))
{
std::cerr << "Config file already exists at " << path << "\n";
return 1;
}
ConfigManager::instance().load(path); // 使用 schema 默认值
ConfigManager::instance().save(); // 写出完整配置
std::cout << "Default config file created at:\n " << path << "\n";
std::cout << "Please review and edit it before running the app.\n";
return 0;
}
if (opts.show_help)
{
std::cout << "Usage: " << argv[0] << " [-v] [-h] [-c <config_path>]\n";
std::cout << "If no arguments are given, './config.ini' will be used by default.\n";
return 0;
}
if (opts.show_version)
{
std::cout << "Version: " << SOFTWARE_VERSION << "\n";
return 0;
}
if (!opts.config_path.empty())
{
if (!std::filesystem::exists(opts.config_path))
{
std::cerr << "Error: config file not found at " << opts.config_path << "\n";
return 1;
}
std::cout << "Using config file: " << opts.config_path << "\n";
ConfigManager::instance().load(opts.config_path);
}
// 启动线程
// init_can_bus_rc_ctrl("can0"); // 初始化远控 CAN 总线
// init_tcp_server_tbox_autodata("0.0.0.0", 50018); // 获取自驾数据
// init_tcp_client_vehicle_position("192.168.1.151", 3333); // 获取定位数据
// init_serial_at(ConfigManager::instance().getSerialDev(), ConfigManager::instance().getSerialBaudrate());
init_tcp_server_tbox_v2v("0.0.0.0", 10005); // 建立与域控间的V2V链路
init_mqtt_client_tbox_v2v(ConfigManager::instance().getMqttIp(), ConfigManager::instance().getMqttPort(),
ConfigManager::instance().getMqttUsername(),
ConfigManager::instance().getMqttPassword()); // 连接平台V2V MQTT服务器
// init_mqtt_client_veh_rc(ConfigManager::instance().getCockpitMqttIp(),
// ConfigManager::instance().getCockpitMqttPort()); // 连接台架的 MQTT 服务器
// 阻塞
while (true) { std::this_thread::sleep_for(std::chrono::seconds(5)); }
return 0;
}