#include "dealmqttalarmthread.h" DealMqttAlarmThread::DealMqttAlarmThread(QObject *parent) : QThread(parent) { keep = false; mqttDataList.clear(); } void DealMqttAlarmThread::stop() { keep = false; } void DealMqttAlarmThread::appendMqttData(MqttAlarmData mqttData) { mqttDataList.append(mqttData); } void DealMqttAlarmThread::run() { keep = true; while (keep) { if(mqttDataList.length()>0){ while (mqttDataList.length()>0) { MqttAlarmData md = mqttDataList.first(); QString topic = md.topic; QByteArray data = md.data; QJsonParseError parseErr; QJsonDocument doc = QJsonDocument::fromJson(data,&parseErr); printf("mqttDataList.length() %d, data write alarm parseErr.error %d\n",mqttDataList.length(),parseErr.error); if(parseErr.error==QJsonParseError::NoError){ QJsonObject obj = doc.object(); QString deviceId = obj.value("device_id").toString(); QString deviceCode = obj.value("device_code").toString(); QString productCode = obj.value("product_id").toString(); int timeStamp = obj.value("timestamp").toInt(); QString dataTime = QDateTime::fromTime_t(timeStamp).toString("yyyy-MM-dd HH:mm:ss"); QString deviceType = obj.value("device_type").toString(); QStringList spit = deviceType.split("-"); QString devType = ""; QString devBrief = ""; if(spit.length()>1){ devType = spit.at(0); devBrief = spit.at(1); } // if((devType.length()==3)&&(devType.startsWith("5"))){ QString attribute_name = ""; QString attribute_value = ""; QJsonValue tag_value = obj.value("tags"); QJsonObject tag_obj = tag_value.toObject(); QVariantMap fd_tag = tag_obj.toVariantMap(); QVariantMap::Iterator iter_tag; for(iter_tag=fd_tag.begin();iter_tag!=fd_tag.end();iter_tag++){ attribute_name.append(QString("%1").arg(iter_tag.key())).append(","); attribute_value.append(QString("\'%1\'").arg(iter_tag.value().toString())).append(","); } QJsonValue metric_value = obj.value("metrics"); QJsonObject metric_obj = metric_value.toObject(); QVariantMap fd = metric_obj.toVariantMap(); QVariantMap::Iterator iter; for(iter=fd.begin();iter!=fd.end();iter++){ attribute_name.append(QString("%1").arg(iter.key())).append(","); attribute_value.append(QString("%1").arg(iter.value().toInt())).append(","); } attribute_name = attribute_name.left(attribute_name.length()-1); attribute_value = attribute_value.left(attribute_value.length()-1); QString sql = QString("insert into alarm_%1 (%2) values(%3);").arg(devBrief).arg(attribute_name).arg(attribute_value); emit dmqSql(sql); // } } mqttDataList.removeFirst(); usleep(1000); } } usleep(50000); } }