#include "tool/error_code.hpp"
#include "rabbitmq/rabbitmq_communication.h"
#include "grpc/streamServer.h"
#include "mqtt/mqtt_communication.h"
#include "proto/communication.pb.h"

class CommunicationManager {
public:
    static CommunicationManager *iter() {
        static CommunicationManager *instance = nullptr;
        if (instance == nullptr) {
            instance = new CommunicationManager();
        }
        return instance;
    }
    ~CommunicationManager() = default;

    Error_manager Init(const CommunicationManagerConfig &config);
protected:
    void run();

    MeasureStatu getMeasureData(DetectManager::DetectResult &detect_measure_info);

    Error_manager sendGrpcData(const MeasureStatu &measure_statu, DetectManager::DetectResult &measure_result);

    Error_manager sendMqttData(const MeasureStatu &measure_statu, DetectManager::DetectResult &measure_result);

    Error_manager sendRabbitmqData(const MeasureStatu &measure_statu, DetectManager::DetectResult &measure_result);

    Range_status outOfRangeDetection(const MeasureStatu &measure_statu, DetectManager::DetectResult &measure_result, bool isPlc);

    Error_manager addOffset(DetectManager::DetectResult &measure_result);

    Error_manager removeOffset(DetectManager::DetectResult &measure_result);

private:
    StreamRpcServer *m_grpc_server = nullptr;

    Thread_condition *condition = nullptr;
    std::thread *run_t = nullptr;
    CommunicationManagerConfig m_config;
    int last_range_statu = 0;
    int heart = 0;
};


