generatorcore.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "generatorcore.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. AGBoxShm *agBoxShm;
  4. void GeneratorCore::shm_init(){
  5. QSqlQuery qry;
  6. QString sql = QString("select mqtt_ip,mqtt_port,user_name,pass_word from yt_t_mqtt where item_name = 'data-agbox'");
  7. qry = db.exec(sql);
  8. while (qry.next()) {
  9. ip = qry.value(0).toString();
  10. port = qry.value(1).toString();
  11. username = qry.value(2).toString();
  12. password = qry.value(3).toString();
  13. }
  14. }
  15. bool GeneratorCore::shm_load(){
  16. key_t key;
  17. int shmid;
  18. if((key=ftok(SHM_PATH,static_cast<int>(SHM_PORT)))==-1){
  19. return false;
  20. }
  21. if((shmid=shmget(key,sizeof(AGBoxShm),IPC_CREAT|0666))==-1){
  22. return false;
  23. }
  24. agBoxShm = static_cast<AGBoxShm *>(shmat(shmid,nullptr,0));
  25. shm_init();
  26. return true;
  27. }
  28. GeneratorCore::GeneratorCore(QObject *parent) : QObject(parent)
  29. {
  30. logthread = new LogThread(this);
  31. logthread->start();
  32. db = QSqlDatabase::addDatabase("QSQLITE","conf_db");
  33. db.setDatabaseName(QString("/opt/db/yt_conf.db"));
  34. if(!db.open()){
  35. logthread->appendData(QString("[aggenerator] open yt_conf.db failed"));
  36. }
  37. if(shm_load()){
  38. logthread->appendData(QString("[aggenerator] shm load success"));
  39. }
  40. mqttIdx = 1;
  41. mqttAlarmIdx = 1;
  42. m_client = new QMQTT::Client(QHostAddress(ip),static_cast<quint16>(port.toInt()),this);
  43. connect(m_client,&QMQTT::Client::connected,this,&GeneratorCore::onConnected);
  44. m_client->setUsername(username);
  45. m_client->setPassword(password.toLatin1());
  46. m_client->setCleanSession(true);
  47. m_client->connectToHost();
  48. dbthread = new DatabaseThread(this,&db);
  49. connect(dbthread,&DatabaseThread::dbdata_log,this,&GeneratorCore::dataLog);
  50. dbthread->start();
  51. httpthread = new HttpThread(this);
  52. connect(httpthread,&HttpThread::dataLog,this,&GeneratorCore::dataLog);
  53. connect(httpthread,&HttpThread::mqttData,this,&GeneratorCore::mqtt_data);
  54. connect(httpthread,&HttpThread::mqttAlarm,this,&GeneratorCore::mqtt_alarm);
  55. devinfoserver = new DevInfoTcpServer(this);
  56. connect(devinfoserver,&DevInfoTcpServer::tcp_log,this,&GeneratorCore::dataLog);
  57. connect(devinfoserver,&DevInfoTcpServer::append_sql,this,&GeneratorCore::appendSql);
  58. }
  59. GeneratorCore::~GeneratorCore()
  60. {
  61. logthread->stop();
  62. dbthread->stop();
  63. httpthread->stop();
  64. }
  65. void GeneratorCore::start()
  66. {
  67. devinfoserver->start();
  68. }
  69. void GeneratorCore::onConnected()
  70. {
  71. logthread->appendData(QString("mqtt onConnected"));
  72. }
  73. void GeneratorCore::dataLog(QString log)
  74. {
  75. logthread->appendData(log);
  76. }
  77. void GeneratorCore::mqtt_data(QString mqtt_msg)
  78. {
  79. printf("test4444 [%s]\n",mqtt_msg.toUtf8().data());
  80. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  81. m_client->connectToHost();
  82. }
  83. m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",mqtt_msg.toUtf8()));
  84. if(mqttIdx > 9999){
  85. mqttIdx = 1;
  86. }
  87. }
  88. void GeneratorCore::mqtt_alarm(QString mqtt_msg)
  89. {
  90. printf("test4444 mqtt alarm [%s]\n",mqtt_msg.toUtf8().data());
  91. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  92. m_client->connectToHost();
  93. }
  94. m_client->publish(QMQTT::Message(mqttAlarmIdx++,"alarm-collector",mqtt_msg.toUtf8()));
  95. if(mqttAlarmIdx > 9999){
  96. mqttAlarmIdx = 1;
  97. }
  98. }
  99. void GeneratorCore::appendSql(QString sql)
  100. {
  101. dbthread->appendSql(sql);
  102. shm_init();
  103. }