mqtteventdatathread.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "mqtteventdatathread.h"
  2. #include "../DataWriteDog/writeshm.h"
  3. MqttEventDataThread::MqttEventDataThread(QObject *parent) : QThread(parent)
  4. {
  5. timer = new QTimer(this);
  6. connect(timer,&QTimer::timeout,this,&MqttEventDataThread::time_out);
  7. }
  8. void MqttEventDataThread::run()
  9. {
  10. }
  11. void MqttEventDataThread::time_out()
  12. {
  13. dataWriteShm->processStatus[1].t_time = QDateTime::currentDateTime().toTime_t();
  14. if((m_client->connectionState()==QMQTT::STATE_DISCONNECTED)||(m_client->connectionState()==QMQTT::STATE_INIT)){
  15. m_client->connectToHost();
  16. }
  17. }
  18. void MqttEventDataThread::mqtt_conf(QString ip,QString port,QString username,QString password)
  19. {
  20. mqtt_ip = ip;
  21. mqtt_port = port;
  22. mqtt_username = username;
  23. mqtt_passwd = password;
  24. m_client = new QMQTT::Client(QHostAddress(mqtt_ip),static_cast<quint16>(mqtt_port.toInt()),this);
  25. m_client->setUsername(mqtt_username);
  26. m_client->setPassword(mqtt_passwd.toLatin1());
  27. m_client->setCleanSession(true);
  28. connect(m_client,&QMQTT::Client::connected,this,&MqttEventDataThread::onConnected);
  29. connect(m_client,&QMQTT::Client::received,this,&MqttEventDataThread::onReceived);
  30. m_client->connectToHost();
  31. timer->start(1000);
  32. }
  33. void MqttEventDataThread::onConnected()
  34. {
  35. emit mq_log(QString("[%1] WriteProcess MqttEventDataThread onConnected").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")));
  36. m_client->subscribe("event-collector",0);
  37. }
  38. void MqttEventDataThread::onReceived(const QMQTT::Message &message)
  39. {
  40. QString topic = message.topic();
  41. QByteArray data = message.payload();
  42. emit sendMqttData(topic,data);
  43. }