mqttpub.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "mqttpub.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. MqttPub::MqttPub(QObject *parent) : QThread(parent)
  4. {
  5. keep = false;
  6. mqttDataList.clear();
  7. }
  8. void MqttPub::stop()
  9. {
  10. keep = false;
  11. }
  12. void MqttPub::run()
  13. {
  14. keep = true;
  15. while (keep) {
  16. if(mqttDataList.length()>0){
  17. while (mqttDataList.length()>0) {
  18. MqttData md = mqttDataList.first();
  19. QString topic = md.topic;
  20. QByteArray data = md.data;
  21. QJsonParseError parseErr;
  22. QJsonDocument doc = QJsonDocument::fromJson(data,&parseErr);
  23. printf("environment parseErr.error %d\n",parseErr.error);
  24. if(parseErr.error==QJsonParseError::NoError){
  25. QString nodeInfo = "";
  26. QString deviceId = "";
  27. QString companyCode = "";
  28. QString deviceType = "";
  29. QJsonObject obj_doc = doc.object();
  30. QString sn = obj_doc.value("sn").toString();
  31. int ts = obj_doc.value("ts").toInt();
  32. QDateTime datetime = QDateTime::fromTime_t(ts);
  33. int minute = datetime.time().minute();
  34. printf("topic:%s, datetime: %s, minute: %d\n",topic.toUtf8().data(),datetime.toString("yyyy-MM-dd HH:mm:ss").toUtf8().data(),minute);
  35. QJsonValue dev_value = obj_doc.value("devs");
  36. if(dev_value.isArray() && (minute == 00)){
  37. QJsonArray list_array = dev_value.toArray();
  38. for(int i=0;i<list_array.size();i++){
  39. QJsonObject obj_dev = list_array.at(i).toObject();
  40. QString deviceCode = sn + "-" + obj_dev.value("dev").toString();
  41. printf("deviceCode: %s\n",deviceCode.toUtf8().data());
  42. for(int i=0;i<1024;i++){
  43. if(agBoxShm->device[i].Enabled == 0x01){
  44. if(deviceCode.compare(QString(agBoxShm->device[i].device_code))==0){
  45. agBoxShm->device[i].lastTime = QDateTime::currentDateTime().toTime_t();
  46. deviceId = QString(agBoxShm->device[i].device_id);
  47. companyCode = QString(agBoxShm->device[i].product_code);
  48. deviceType = QString::number(agBoxShm->device[i].device_type);
  49. agBoxShm->device->lastTime = QDateTime::currentDateTime().toTime_t();
  50. break;
  51. }
  52. }
  53. }
  54. QJsonValue d_value = obj_dev.value("d");
  55. if(d_value.isArray()){
  56. QJsonArray d_array = d_value.toArray();
  57. if(d_array.size()==8){
  58. for(int i=0;i<d_array.size();i++){
  59. QJsonObject list_object = d_array.at(i).toObject();
  60. QString name = list_object.value("m").toString();
  61. double value = list_object.value("v").toDouble();
  62. for(int i=0;i<300;i++){
  63. if(name.compare(QString(agBoxShm->procuctattrbute[i].attribute_name))==0){
  64. nodeInfo.append(QString("\"%1\":%2,").arg(QString(agBoxShm->procuctattrbute[i].attribute_code)).arg(value));
  65. break;
  66. }
  67. }
  68. }
  69. nodeInfo.append(QString("\"device_status\":1"));
  70. emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-ev\"}")
  71. .arg(deviceId).arg(deviceCode).arg(companyCode).arg(ts).arg(nodeInfo).arg(deviceType));
  72. }
  73. }
  74. }
  75. }
  76. }
  77. mqttDataList.removeFirst();
  78. usleep(1000);
  79. }
  80. }
  81. usleep(50000);
  82. }
  83. }
  84. void MqttPub::devMessage(MqttData dev)
  85. {
  86. mqttDataList.append(dev);
  87. }