#include "jzmqttsub.h"
#include "../AGBoxDog/boxshm.h"

JZMqttSub::JZMqttSub(QObject *parent) : QThread(parent)
{
    minute = 255;

    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&JZMqttSub::time_out);
}

void JZMqttSub::run()
{

}

void JZMqttSub::time_out()
{
    if((m_client->connectionState()==QMQTT::STATE_DISCONNECTED)||(m_client->connectionState()==QMQTT::STATE_INIT)){
        m_client->connectToHost();
    }

}

void JZMqttSub::mqtt_conf(QString ip,QString port,QString username,QString password)
{
    mqtt_ip = ip;
    mqtt_port = port;
    mqtt_username = username;
    mqtt_passwd = password;


//    m_client = new QMQTT::Client(QHostAddress(mqtt_ip),static_cast<quint16>(mqtt_port.toInt()),this);
//    m_client->setUsername(mqtt_username);
//    m_client->setPassword(mqtt_passwd.toLatin1());
    m_client = new QMQTT::Client(QHostAddress("10.108.34.2"),1883,this);
    m_client->setUsername("usky");
    m_client->setPassword("usky");
    m_client->setCleanSession(false);
    m_client->setAutoReconnect(true);
    m_client->setAutoReconnectInterval(60000);
    connect(m_client,&QMQTT::Client::connected,this,&JZMqttSub::onConnected);
    connect(m_client,&QMQTT::Client::received,this,&JZMqttSub::onReceived);
    m_client->connectToHost();


    timer->start(1000);
}

void JZMqttSub::onConnected()
{
    printf("xfjz onConnected\n");
    m_client->subscribe("/edge/baInfo/091267EB9CF743D8857330E22DF8EC2D/rtg",0);
    m_client->subscribe("/edge/baInfo/F3D0A204C7594361A308018307E8C349/rtg",0);
    m_client->subscribe("/edge/baInfo/3D25E5054A01457AA66466FEE1BCFA6E/rtg",0);
    m_client->subscribe("/edge/baInfo/340D46C186DB42C0BD2F1A99AC5E63F2/rtg",0);
    m_client->subscribe("/edge/baInfo/D9F69465CCE74E82BC38F61821D3E6BE/rtg",0);
    m_client->subscribe("/edge/baInfo/FB0CB2D69D214E8487BDA518C8726F56/rtg",0);
    m_client->subscribe("/edge/baInfo/207DE156F8844A92901449DDF0B4C76C/rtg",0);
    m_client->subscribe("/edge/baInfo/EC18DF7F35F14163B232E0B1EF55DDCE/rtg",0);
    m_client->subscribe("/edge/baInfo/8118A064BCA3458BAD020EC9DA12265D/rtg",0);
    m_client->subscribe("/edge/baInfo/67DF1D8A6471432591FC0ED10D3A7FC6/rtg",0);
    m_client->subscribe("/edge/baInfo/08D86E3BCA4D44B594AEF4150AB41F9B/rtg",0);
    m_client->subscribe("/edge/baInfo/5657148CD58C43159170E1960A72242F/rtg",0);
    m_client->subscribe("/edge/baInfo/21981D16C19842078E6E5DD326835B00/rtg",0);
    m_client->subscribe("/edge/baInfo/7C1CE5AEE4BF4706AFEA77BB2C35E57B/rtg",0);
    m_client->subscribe("/edge/baInfo/AA075EC348654E3690665AAD22C9FA75/rtg",0);
    m_client->subscribe("/edge/baInfo/162CDC71F7F74E7FA5CCED83EF3ECF05/rtg",0);

}

void JZMqttSub::onReceived(const QMQTT::Message &message)
{
    agBoxShm->processStatus[11].t_time=QDateTime::currentDateTime().toTime_t();

    QString topic = message.topic();
    QByteArray data = message.payload();

    emit sendMqttData(topic,data);

}








