mqttsub.cpp 2.1 KB

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