environmentcore.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "environmentcore.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. #include "../agVideoProcess/boxdeviceshm.h"
  4. AGBoxShm *agBoxShm;
  5. BoxDeviceShm *boxDeviceShm;
  6. void EnvironmentCore::shm_init(){
  7. QSqlQuery qry;
  8. QString sql = QString("select mqtt_ip,mqtt_port,user_name,pass_word from yt_t_mqtt where item_name = 'data-agbox'");
  9. qry = db.exec(sql);
  10. while (qry.next()) {
  11. ip = qry.value(0).toString();
  12. port = qry.value(1).toString();
  13. username = qry.value(2).toString();
  14. password = qry.value(3).toString();
  15. }
  16. qry.clear();
  17. sql.clear();
  18. }
  19. bool EnvironmentCore::shm_load(){
  20. key_t key;
  21. int shmid;
  22. if((key=ftok(SHM_PATH,static_cast<int>(SHM_PORT)))==-1){
  23. return false;
  24. }
  25. if((shmid=shmget(key,sizeof(AGBoxShm),IPC_CREAT|0666))==-1){
  26. return false;
  27. }
  28. agBoxShm = static_cast<AGBoxShm *>(shmat(shmid,nullptr,0));
  29. if((key=ftok(VIDEO_PATH,static_cast<int>(VIDEO_PORT)))==-1){
  30. return false;
  31. }
  32. if((shmid=shmget(key,sizeof(BoxDeviceShm),IPC_CREAT|0666))==-1){
  33. return false;
  34. }
  35. boxDeviceShm = static_cast<BoxDeviceShm *>(shmat(shmid,nullptr,0));
  36. shm_init();
  37. return true;
  38. }
  39. EnvironmentCore::EnvironmentCore(QObject *parent) : QObject(parent)
  40. {
  41. logthread = new LogThread(this);
  42. logthread->start();
  43. db = QSqlDatabase::addDatabase("QSQLITE","conf_db");
  44. db.setDatabaseName(QString("/opt/db/yt_conf.db"));
  45. if(!db.open()){
  46. logthread->appendData(QString("[agenvironment] open yt_conf.db failed"));
  47. }
  48. if(shm_load()){
  49. logthread->appendData(QString("[agenvironment] shm load success"));
  50. }
  51. mqttIdx = 1;
  52. m_client = new QMQTT::Client(QHostAddress(ip),static_cast<quint16>(port.toInt()),this);
  53. connect(m_client,&QMQTT::Client::connected,this,&EnvironmentCore::onConnected);
  54. m_client->setUsername(username);
  55. m_client->setPassword(password.toLatin1());
  56. m_client->setCleanSession(true);
  57. m_client->connectToHost();
  58. mqttsub = new MqttSub(this);
  59. connect(mqttsub,&MqttSub::sendMqttData,this,&EnvironmentCore::receiveDevData);
  60. mqttpub = new MqttPub(this);
  61. connect(mqttpub,&MqttPub::mqttData,this,&EnvironmentCore::mqtt_data);
  62. connect(mqttpub,&MqttPub::dataListLog,this,&EnvironmentCore::dataLog);
  63. timer = new QTimer(this);
  64. connect(timer,&QTimer::timeout,this,&EnvironmentCore::time_out);
  65. timer->start(1000);
  66. }
  67. EnvironmentCore::~EnvironmentCore()
  68. {
  69. logthread->stop();
  70. mqttpub->stop();
  71. }
  72. void EnvironmentCore::start()
  73. {
  74. mqttsub->start();
  75. mqttsub->mqtt_conf(ip,port,username,password);
  76. mqttpub->start();
  77. }
  78. void EnvironmentCore::time_out()
  79. {
  80. agBoxShm->processStatus[5].t_time=QDateTime::currentDateTime().toTime_t();
  81. for(int i=0;i<1024;i++){
  82. if((boxDeviceShm->device[i].Enabled == 0x01)&&(boxDeviceShm->device[i].device_type == 509)){
  83. uint curTime = QDateTime::currentDateTime().toTime_t();
  84. if((curTime - boxDeviceShm->device[i].lastTime)>7200){
  85. boxDeviceShm->device[i].lastTime = QDateTime::currentDateTime().toTime_t();
  86. this->mqtt_data(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"device_status\":0},\"device_type\":\"%5-ev\"}")
  87. .arg(boxDeviceShm->device[i].device_id).arg(boxDeviceShm->device[i].device_code).arg(boxDeviceShm->device[i].company_code).arg(curTime).arg(boxDeviceShm->device[i].device_type));
  88. }
  89. }
  90. }
  91. }
  92. void EnvironmentCore::onConnected()
  93. {
  94. logthread->appendData(QString("mqtt onConnected"));
  95. }
  96. void EnvironmentCore::dataLog(QString log)
  97. {
  98. logthread->appendData(log);
  99. }
  100. void EnvironmentCore::mqtt_data(QString mqtt_msg)
  101. {
  102. printf("test5555 [%s]\n",mqtt_msg.toUtf8().data());
  103. logthread->appendData(QString("[%1] %2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(mqtt_msg));
  104. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  105. m_client->connectToHost();
  106. }
  107. m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",mqtt_msg.toUtf8()));
  108. if(mqttIdx > 9999){
  109. mqttIdx = 1;
  110. }
  111. }
  112. void EnvironmentCore::receiveDevData(QString topic,QByteArray data)
  113. {
  114. mqttpub->devMessage(MqttData(topic,data));
  115. }