123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- #include "mqttpub.h"
- #include "../AGBoxDog/boxshm.h"
- MqttPub::MqttPub(QObject *parent) : QThread(parent)
- {
- keep = false;
- mqttDataList.clear();
- }
- void MqttPub::stop()
- {
- keep = false;
- }
- void MqttPub::run()
- {
- keep = true;
- while (keep) {
- if(mqttDataList.length()>0){
- while (mqttDataList.length()>0) {
- MqttData md = mqttDataList.first();
- QString topic = md.topic;
- QByteArray data = md.data;
- QJsonParseError parseErr;
- QJsonDocument doc = QJsonDocument::fromJson(data,&parseErr);
- printf("environment parseErr.error %d\n",parseErr.error);
- if(parseErr.error==QJsonParseError::NoError){
- QString nodeInfo = "";
- QString deviceId = "";
- QString companyCode = "";
- QString deviceType = "";
- QJsonObject obj_doc = doc.object();
- QString sn = obj_doc.value("sn").toString();
- int ts = obj_doc.value("ts").toInt();
- QDateTime datetime = QDateTime::fromTime_t(ts);
- int minute = datetime.time().minute();
- printf("topic:%s, datetime: %s, minute: %d\n",topic.toUtf8().data(),datetime.toString("yyyy-MM-dd HH:mm:ss").toUtf8().data(),minute);
- QJsonValue dev_value = obj_doc.value("devs");
- if(dev_value.isArray() && (minute == 00)){
- QJsonArray list_array = dev_value.toArray();
- for(int i=0;i<list_array.size();i++){
- QJsonObject obj_dev = list_array.at(i).toObject();
- QString deviceCode = sn + "-" + obj_dev.value("dev").toString();
- printf("deviceCode: %s\n",deviceCode.toUtf8().data());
- for(int i=0;i<1024;i++){
- if(agBoxShm->device[i].Enabled == 0x01){
- if(deviceCode.compare(QString(agBoxShm->device[i].device_code))==0){
- agBoxShm->device[i].lastTime = QDateTime::currentDateTime().toTime_t();
- deviceId = QString(agBoxShm->device[i].device_id);
- companyCode = QString(agBoxShm->device[i].product_code);
- deviceType = QString::number(agBoxShm->device[i].device_type);
- agBoxShm->device->lastTime = QDateTime::currentDateTime().toTime_t();
- break;
- }
- }
- }
- QJsonValue d_value = obj_dev.value("d");
- if(d_value.isArray()){
- QJsonArray d_array = d_value.toArray();
- if(d_array.size()==8){
- for(int i=0;i<d_array.size();i++){
- QJsonObject list_object = d_array.at(i).toObject();
- QString name = list_object.value("m").toString();
- double value = list_object.value("v").toDouble();
- for(int i=0;i<300;i++){
- if(name.compare(QString(agBoxShm->procuctattrbute[i].attribute_name))==0){
- nodeInfo.append(QString("\"%1\":%2,").arg(QString(agBoxShm->procuctattrbute[i].attribute_code)).arg(value));
- break;
- }
- }
- }
- nodeInfo.append(QString("\"device_status\":1"));
- emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-ev\"}")
- .arg(deviceId).arg(deviceCode).arg(companyCode).arg(ts).arg(nodeInfo).arg(deviceType));
- }
- }
- }
- }
- }
- mqttDataList.removeFirst();
- usleep(1000);
- }
- }
- usleep(50000);
- }
- }
- void MqttPub::devMessage(MqttData dev)
- {
- mqttDataList.append(dev);
- }
|