jzmqttpub.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "jzmqttpub.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. JZMqttPub::JZMqttPub(QObject *parent) : QThread(parent)
  4. {
  5. keep = false;
  6. mqttDataList.clear();
  7. }
  8. void JZMqttPub::stop()
  9. {
  10. keep = false;
  11. }
  12. void JZMqttPub::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("jz device parseErr.error %d\n",parseErr.error);
  24. if(parseErr.error==QJsonParseError::NoError){
  25. QString nodeInfo = "";
  26. QString deviceId = "";
  27. QString productCode = "";
  28. QString deviceType = "";
  29. QJsonObject obj_doc = doc.object();
  30. int ts = obj_doc.value("ts").toInt();
  31. QDateTime datatime = QDateTime::fromTime_t(ts);
  32. int minute = datatime.time().minute();
  33. QJsonValue dev_value = obj_doc.value("devs");
  34. if(dev_value.isArray() && (minute == 00)){
  35. int ts = QDateTime::fromString(datatime.toString("yyyy-MM-dd HH")+":00:00","yyyy-MM-dd HH:mm:ss").toTime_t();
  36. QJsonArray list_array = dev_value.toArray();
  37. for(int i=0;i<list_array.size();i++){
  38. QJsonObject obj_dev = list_array.at(i).toObject();
  39. QString deviceCode = obj_dev.value("dev").toString();
  40. printf("deviceCode: %s\n",deviceCode.toUtf8().data());
  41. for(int j=0;j<1024;j++){
  42. if(agBoxShm->device[j].Enabled == 0x01){
  43. if(deviceCode.compare(QString(agBoxShm->device[j].device_code))==0){
  44. deviceId = QString(agBoxShm->device[j].device_id);
  45. productCode = QString(agBoxShm->device[j].product_code);
  46. deviceType = QString::number(agBoxShm->device[j].device_type);
  47. break;
  48. }
  49. }
  50. }
  51. QJsonValue d_value = obj_dev.value("d");
  52. if(d_value.isArray()){
  53. QJsonArray d_array = d_value.toArray();
  54. if(d_array.size() > 0){
  55. nodeInfo.append(QString("\"on_line\":1"));
  56. emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-xf\"}")
  57. .arg(deviceId).arg(deviceCode).arg(productCode).arg(ts).arg(nodeInfo).arg(deviceType));
  58. }
  59. }
  60. }
  61. }
  62. }
  63. mqttDataList.removeFirst();
  64. usleep(1000);
  65. }
  66. }
  67. usleep(50000);
  68. }
  69. }
  70. void JZMqttPub::devMessage(MqttData dev)
  71. {
  72. mqttDataList.append(dev);
  73. }