ytmqttsub.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "ytmqttsub.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. YTMqttSub::YTMqttSub(QObject *parent) : QThread(parent)
  4. {
  5. timer = new QTimer(this);
  6. connect(timer,&QTimer::timeout,this,&YTMqttSub::time_out);
  7. }
  8. void YTMqttSub::run()
  9. {
  10. }
  11. void YTMqttSub::time_out()
  12. {
  13. if(m_client->connectionState() == QMQTT::STATE_INIT || m_client->connectionState() == QMQTT::STATE_DISCONNECTED){
  14. m_client->connectToHost();
  15. }
  16. }
  17. void YTMqttSub::mqtt_conf(QString ip, QString port, QString username, QString passwd)
  18. {
  19. // m_client = new QMQTT::Client(QHostAddress(ip),static_cast<quint16>(port.toInt()),this);
  20. // m_client->setUsername(username);
  21. // m_client->setPassword(passwd.toLatin1());
  22. m_client = new QMQTT::Client(QHostAddress("10.108.34.2"),1883,this);
  23. m_client->setUsername("usky");
  24. m_client->setPassword("usky");
  25. m_client->setCleanSession(true);
  26. connect(m_client,&QMQTT::Client::connected,this,&YTMqttSub::onConnected);
  27. connect(m_client,&QMQTT::Client::received,this,&YTMqttSub::onReceived);
  28. m_client->connectToHost();
  29. timer->start(1000);
  30. }
  31. void YTMqttSub::onConnected()
  32. {
  33. printf("xfyt mqtt onConnected\n");
  34. m_client->subscribe("/edge/baInfo/818DBB40CD2D4494936A75C76E89542C/rtg",0);
  35. m_client->subscribe("/edge/baInfo/B3E523F1D0E8402F818C30833C4F1C6F/rtg",0);
  36. m_client->subscribe("/edge/baInfo/E68D8A289878474D963D43F5FC624E34/rtg",0);
  37. m_client->subscribe("/edge/baInfo/3CC454CC33B74714BB7B8D8C83C72304/rtg",0);
  38. m_client->subscribe("/edge/baInfo/3932C85C53964902A0A426B03A4EBB42/rtg",0);
  39. m_client->subscribe("/edge/baInfo/ED2059D345D145DCBEB6B5E3B4CFCAD8/rtg",0);
  40. m_client->subscribe("/edge/baInfo/E4C82427D44D40E5B398002CA5A1EF42/rtg",0);
  41. m_client->subscribe("/edge/baInfo/0344B9CD3B6A468E87551B25BC679A96/rtg",0);
  42. m_client->subscribe("/edge/baInfo/2F0C601E76E9444997F5E72EAB86856F/rtg",0);
  43. m_client->subscribe("/edge/baInfo/3B6CEDD977434B89AE9029938D673BB3/rtg",0);
  44. }
  45. void YTMqttSub::onReceived(const QMQTT::Message &message)
  46. {
  47. agBoxShm->processStatus[11].t_time=QDateTime::currentDateTime().toTime_t();
  48. QString topic = message.topic();
  49. QByteArray data = message.payload();
  50. emit sendMqttData(topic,data);
  51. }