#include "TDAgent.h"
#include "TDengine.h"

TDengine objTD;
TDAgent::TDAgent()
	: QObject()
{
}

TDAgent::~TDAgent()
{
}

void TDAgent::SubCB(const std::string topic, const std::string msg)
{
    emit sigEvent(topic, msg);
}

bool TDAgent::Connect(tagSetup ts)
{
    objTD.Setup(ts);

    return true;
}

bool TDAgent::Subscribe(std::list<std::string> topics)
{
    bool ret = true;
    std::list<std::string>::iterator itr;
    for (itr = topics.begin(); itr != topics.end(); ++itr)
    {
        objTD.subscribe(itr->c_str(), this);
    }

    objTD.start();
    return ret;
}

void TDAgent::Publish(std::string szKey,QVariant v)
{
    std::string szContent;
    switch( v.type() )
    {
    case QMetaType::Bool:
        szContent = v.toBool() ? "true":"false";
        break;
    case QMetaType::Int:
        szContent = std::to_string(v.toInt());
        break;
    case QMetaType::UInt:
        szContent = std::to_string(v.toUInt());
        break;
    case QMetaType::LongLong:
        szContent = std::to_string(v.toLongLong());
        break;
    case QMetaType::ULongLong:
        szContent = std::to_string(v.toULongLong());
        break;
    case QMetaType::Double:
        szContent = std::to_string(v.toDouble());
        break;
    case QMetaType::QString:
        szContent = v.toString().toLocal8Bit().toStdString();
        break;
    }

//    objRedis.publish(szKey.c_str(), szContent.c_str()); //把数据发送到对应主题
}
