#include "mqtt_config.hpp"
#include "mqtt_client_wrapper.hpp"
#include <iostream>
#include <thread>

std::string tcp_server_ip = "192.168.4.244";

// 声明4个启动函数
void start_mqtt_config_client(const std::string &, int);
void start_uplink_data_client(const std::string &, int);
void start_uplink_notify_client(const std::string &, int);
void start_broadcast_client(const std::string &, int);

int main()
{
    Logger::set_log_to_file("mqtt_client.log");
    LOG_INFO("MQTT client starting...");

    try
    {
        g_mqtt_config = g_mqtt_config.load_from_file("config.json");
    }
    catch (const std::exception &e)
    {
        LOG_ERROR(std::string("[Fatal] ") + e.what());
        return 1;
    }

    // 启动4个TCP客户端线程
    // start_mqtt_config_client(tcp_server_ip, g_mqtt_config.ports.config_port);
    // start_uplink_data_client(tcp_server_ip, g_mqtt_config.ports.uplink_1_port);
    // start_uplink_notify_client(tcp_server_ip, g_mqtt_config.ports.uplink_2_port);
    start_broadcast_client(tcp_server_ip, g_mqtt_config.ports.downlink_port);
    // 启动 MQTT 客户端线程
    std::thread mqtt_thread(mqtt_client_thread_func);
    mqtt_thread.detach();
    // 阻塞
    while (true)
        std::this_thread::sleep_for(std::chrono::seconds(60));

    return 0;
}
