mqttsub.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "mqttsub.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. MqttSub::MqttSub(QObject *parent) : QThread(parent)
  4. {
  5. minute = 255;
  6. timer = new QTimer(this);
  7. connect(timer,&QTimer::timeout,this,&MqttSub::time_out);
  8. }
  9. void MqttSub::run()
  10. {
  11. }
  12. void MqttSub::time_out()
  13. {
  14. if((m_client->connectionState()==QMQTT::STATE_DISCONNECTED)||(m_client->connectionState()==QMQTT::STATE_INIT)){
  15. m_client->connectToHost();
  16. }
  17. }
  18. void MqttSub::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 = new QMQTT::Client(QHostAddress("172.17.35.51"),1883,this);
  28. m_client->setUsername("admin");
  29. m_client->setPassword("public");
  30. m_client->setCleanSession(true);
  31. connect(m_client,&QMQTT::Client::connected,this,&MqttSub::onConnected);
  32. connect(m_client,&QMQTT::Client::received,this,&MqttSub::onReceived);
  33. m_client->connectToHost();
  34. timer->start(1000);
  35. }
  36. void MqttSub::onConnected()
  37. {
  38. m_client->subscribe("/edge/bainfo/72B4884AE5CB4407B4D5D0BACB509D79/rtg",0);
  39. m_client->subscribe("/edge/bainfo/271ACE89CB504642950FC7B3C2505205/rtg",0);
  40. m_client->subscribe("/edge/bainfo/9849B7BE3D204EF984D851EC067BF9C1/rtg",0);
  41. m_client->subscribe("/edge/bainfo/CE1A787947914261B2ADAF5AA9FC1EBC/rtg",0);
  42. }
  43. void MqttSub::onReceived(const QMQTT::Message &message)
  44. {
  45. agBoxShm->processStatus[5].t_time=QDateTime::currentDateTime().toTime_t();
  46. minute = QDateTime::currentDateTime().time().minute();
  47. // if((minute==01)||(minute==02)){
  48. // QString topic = message.topic();
  49. // QByteArray data = message.payload();
  50. // emit sendMqttData(topic,data);
  51. // }
  52. QString topic = message.topic();
  53. QByteArray data = message.payload();
  54. emit sendMqttData(topic,data);
  55. }