dealmqttalarmthread.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "dealmqttalarmthread.h"
  2. DealMqttAlarmThread::DealMqttAlarmThread(QObject *parent) : QThread(parent)
  3. {
  4. keep = false;
  5. mqttDataList.clear();
  6. }
  7. void DealMqttAlarmThread::stop()
  8. {
  9. keep = false;
  10. }
  11. void DealMqttAlarmThread::appendMqttData(MqttAlarmData mqttData)
  12. {
  13. mqttDataList.append(mqttData);
  14. }
  15. void DealMqttAlarmThread::run()
  16. {
  17. keep = true;
  18. while (keep) {
  19. if(mqttDataList.length()>0){
  20. while (mqttDataList.length()>0) {
  21. MqttAlarmData md = mqttDataList.first();
  22. QString topic = md.topic;
  23. QByteArray data = md.data;
  24. QJsonParseError parseErr;
  25. QJsonDocument doc = QJsonDocument::fromJson(data,&parseErr);
  26. printf("mqttDataList.length() %d, data write alarm parseErr.error %d\n",mqttDataList.length(),parseErr.error);
  27. if(parseErr.error==QJsonParseError::NoError){
  28. QJsonObject obj = doc.object();
  29. QString deviceId = obj.value("device_id").toString();
  30. QString deviceCode = obj.value("device_code").toString();
  31. QString productCode = obj.value("product_id").toString();
  32. int timeStamp = obj.value("timestamp").toInt();
  33. QString dataTime = QDateTime::fromTime_t(timeStamp).toString("yyyy-MM-dd HH:mm:ss");
  34. QString deviceType = obj.value("device_type").toString();
  35. QStringList spit = deviceType.split("-");
  36. QString devType = "";
  37. QString devBrief = "";
  38. if(spit.length()>1){
  39. devType = spit.at(0);
  40. devBrief = spit.at(1);
  41. }
  42. // if((devType.length()==3)&&(devType.startsWith("5"))){
  43. QString attribute_name = "";
  44. QString attribute_value = "";
  45. QJsonValue tag_value = obj.value("tags");
  46. QJsonObject tag_obj = tag_value.toObject();
  47. QVariantMap fd_tag = tag_obj.toVariantMap();
  48. QVariantMap::Iterator iter_tag;
  49. for(iter_tag=fd_tag.begin();iter_tag!=fd_tag.end();iter_tag++){
  50. attribute_name.append(QString("%1").arg(iter_tag.key())).append(",");
  51. attribute_value.append(QString("\'%1\'").arg(iter_tag.value().toString())).append(",");
  52. }
  53. QJsonValue metric_value = obj.value("metrics");
  54. QJsonObject metric_obj = metric_value.toObject();
  55. QVariantMap fd = metric_obj.toVariantMap();
  56. QVariantMap::Iterator iter;
  57. for(iter=fd.begin();iter!=fd.end();iter++){
  58. attribute_name.append(QString("%1").arg(iter.key())).append(",");
  59. attribute_value.append(QString("%1").arg(iter.value().toInt())).append(",");
  60. }
  61. attribute_name = attribute_name.left(attribute_name.length()-1);
  62. attribute_value = attribute_value.left(attribute_value.length()-1);
  63. QString sql = QString("insert into alarm_%1 (%2) values(%3);").arg(devBrief).arg(attribute_name).arg(attribute_value);
  64. emit dmqSql(sql);
  65. // }
  66. }
  67. mqttDataList.removeFirst();
  68. usleep(1000);
  69. }
  70. }
  71. usleep(50000);
  72. }
  73. }