mqttalarmdatathread.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "mqttalarmdatathread.h"
  2. #include "../DataWriteDog/writeshm.h"
  3. MqttAlarmDataThread::MqttAlarmDataThread(QObject *parent) : QThread(parent)
  4. {
  5. timer = new QTimer(this);
  6. connect(timer,&QTimer::timeout,this,&MqttAlarmDataThread::time_out);
  7. }
  8. void MqttAlarmDataThread::run()
  9. {
  10. }
  11. void MqttAlarmDataThread::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 MqttAlarmDataThread::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,&MqttAlarmDataThread::onConnected);
  29. connect(m_client,&QMQTT::Client::received,this,&MqttAlarmDataThread::onReceived);
  30. m_client->connectToHost();
  31. timer->start(1000);
  32. }
  33. void MqttAlarmDataThread::onConnected()
  34. {
  35. emit mq_log(QString("[%1] WriteProcess MqttAlarmDataThread onConnected").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")));
  36. m_client->subscribe("alarm-collector",0);
  37. }
  38. void MqttAlarmDataThread::onReceived(const QMQTT::Message &message)
  39. {
  40. QString topic = message.topic();
  41. QByteArray data = message.payload();
  42. emit sendMqttData(topic,data);
  43. }