12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #include "mqttalarmdatathread.h"
- #include "../DataWriteDog/writeshm.h"
- MqttAlarmDataThread::MqttAlarmDataThread(QObject *parent) : QThread(parent)
- {
- timer = new QTimer(this);
- connect(timer,&QTimer::timeout,this,&MqttAlarmDataThread::time_out);
- }
- void MqttAlarmDataThread::run()
- {
- }
- void MqttAlarmDataThread::time_out()
- {
- dataWriteShm->processStatus[1].t_time = QDateTime::currentDateTime().toTime_t();
- if((m_client->connectionState()==QMQTT::STATE_DISCONNECTED)||(m_client->connectionState()==QMQTT::STATE_INIT)){
- m_client->connectToHost();
- }
- }
- void MqttAlarmDataThread::mqtt_conf(QString ip,QString port,QString username,QString password)
- {
- mqtt_ip = ip;
- mqtt_port = port;
- mqtt_username = username;
- mqtt_passwd = password;
- m_client = new QMQTT::Client(QHostAddress(mqtt_ip),static_cast<quint16>(mqtt_port.toInt()),this);
- m_client->setUsername(mqtt_username);
- m_client->setPassword(mqtt_passwd.toLatin1());
- m_client->setCleanSession(true);
- connect(m_client,&QMQTT::Client::connected,this,&MqttAlarmDataThread::onConnected);
- connect(m_client,&QMQTT::Client::received,this,&MqttAlarmDataThread::onReceived);
- m_client->connectToHost();
- timer->start(1000);
- }
- void MqttAlarmDataThread::onConnected()
- {
- emit mq_log(QString("[%1] WriteProcess MqttAlarmDataThread onConnected").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")));
- m_client->subscribe("alarm-collector",0);
- }
- void MqttAlarmDataThread::onReceived(const QMQTT::Message &message)
- {
- QString topic = message.topic();
- QByteArray data = message.payload();
- emit sendMqttData(topic,data);
- }
|