jzmqttsub.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "jzmqttsub.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. JZMqttSub::JZMqttSub(QObject *parent) : QThread(parent)
  4. {
  5. minute = 255;
  6. timer = new QTimer(this);
  7. connect(timer,&QTimer::timeout,this,&JZMqttSub::time_out);
  8. }
  9. void JZMqttSub::run()
  10. {
  11. }
  12. void JZMqttSub::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 JZMqttSub::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("10.108.34.2"),1883,this);
  28. m_client->setUsername("usky");
  29. m_client->setPassword("usky");
  30. m_client->setCleanSession(false);
  31. m_client->setAutoReconnect(true);
  32. m_client->setAutoReconnectInterval(60000);
  33. connect(m_client,&QMQTT::Client::connected,this,&JZMqttSub::onConnected);
  34. connect(m_client,&QMQTT::Client::received,this,&JZMqttSub::onReceived);
  35. m_client->connectToHost();
  36. timer->start(1000);
  37. }
  38. void JZMqttSub::onConnected()
  39. {
  40. printf("xfjz onConnected\n");
  41. m_client->subscribe("/edge/baInfo/091267EB9CF743D8857330E22DF8EC2D/rtg",0);
  42. m_client->subscribe("/edge/baInfo/F3D0A204C7594361A308018307E8C349/rtg",0);
  43. m_client->subscribe("/edge/baInfo/3D25E5054A01457AA66466FEE1BCFA6E/rtg",0);
  44. m_client->subscribe("/edge/baInfo/340D46C186DB42C0BD2F1A99AC5E63F2/rtg",0);
  45. m_client->subscribe("/edge/baInfo/D9F69465CCE74E82BC38F61821D3E6BE/rtg",0);
  46. m_client->subscribe("/edge/baInfo/FB0CB2D69D214E8487BDA518C8726F56/rtg",0);
  47. m_client->subscribe("/edge/baInfo/207DE156F8844A92901449DDF0B4C76C/rtg",0);
  48. m_client->subscribe("/edge/baInfo/EC18DF7F35F14163B232E0B1EF55DDCE/rtg",0);
  49. m_client->subscribe("/edge/baInfo/8118A064BCA3458BAD020EC9DA12265D/rtg",0);
  50. m_client->subscribe("/edge/baInfo/67DF1D8A6471432591FC0ED10D3A7FC6/rtg",0);
  51. m_client->subscribe("/edge/baInfo/08D86E3BCA4D44B594AEF4150AB41F9B/rtg",0);
  52. m_client->subscribe("/edge/baInfo/5657148CD58C43159170E1960A72242F/rtg",0);
  53. m_client->subscribe("/edge/baInfo/21981D16C19842078E6E5DD326835B00/rtg",0);
  54. m_client->subscribe("/edge/baInfo/7C1CE5AEE4BF4706AFEA77BB2C35E57B/rtg",0);
  55. m_client->subscribe("/edge/baInfo/AA075EC348654E3690665AAD22C9FA75/rtg",0);
  56. m_client->subscribe("/edge/baInfo/162CDC71F7F74E7FA5CCED83EF3ECF05/rtg",0);
  57. }
  58. void JZMqttSub::onReceived(const QMQTT::Message &message)
  59. {
  60. agBoxShm->processStatus[11].t_time=QDateTime::currentDateTime().toTime_t();
  61. QString topic = message.topic();
  62. QByteArray data = message.payload();
  63. emit sendMqttData(topic,data);
  64. }