guardcore.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "guardcore.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. AGBoxShm *agBoxShm;
  4. void GuardCore::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. sql.clear();
  15. qry.clear();
  16. sql = QString("select url,app_key,app_signature from yt_t_platforminfo where type = 'door-search-hk'");
  17. qry = db.exec(sql);
  18. while (qry.next()) {
  19. devinfourl = qry.value(0).toString();
  20. devinfokey = qry.value(1).toString();
  21. devinfosignature = qry.value(2).toString();
  22. }
  23. sql.clear();
  24. qry.clear();
  25. sql = QString("select url,app_key,app_signature from yt_t_platforminfo where type = 'door-event-hk'");
  26. qry = db.exec(sql);
  27. while (qry.next()) {
  28. httpurl = qry.value(0).toString();
  29. httpkey = qry.value(1).toString();
  30. httpsignature = qry.value(2).toString();
  31. }
  32. sql.clear();
  33. qry.clear();
  34. sql = QString("select url,app_key,app_signature from yt_t_platforminfo where type = 'door-state-hk'");
  35. qry = db.exec(sql);
  36. while (qry.next()) {
  37. httpurl1 = qry.value(0).toString();
  38. httpkey1 = qry.value(1).toString();
  39. httpsignature1 = qry.value(2).toString();
  40. }
  41. sql.clear();
  42. qry.clear();
  43. sql = QString("select url,app_key,app_signature from yt_t_platforminfo where type = 'door-control-hk'");
  44. qry = db.exec(sql);
  45. while (qry.next()) {
  46. tcpserverurl = qry.value(0).toString();
  47. tcpserverkey = qry.value(1).toString();
  48. tcpserversignature = qry.value(2).toString();
  49. }
  50. }
  51. bool GuardCore::shm_load(){
  52. key_t key;
  53. int shmid;
  54. if((key=ftok(SHM_PATH,static_cast<int>(SHM_PORT)))==-1){
  55. return false;
  56. }
  57. if((shmid=shmget(key,sizeof(AGBoxShm),IPC_CREAT|0666))==-1){
  58. return false;
  59. }
  60. agBoxShm = static_cast<AGBoxShm *>(shmat(shmid,nullptr,0));
  61. shm_init();
  62. return true;
  63. }
  64. GuardCore::GuardCore(QObject *parent) : QObject(parent)
  65. {
  66. logthread = new LogThread(this);
  67. logthread->start();
  68. db = QSqlDatabase::addDatabase("QSQLITE","conf_db");
  69. db.setDatabaseName(QString("/opt/db/yt_conf.db"));
  70. if(!db.open()){
  71. logthread->appendData(QString("[agguard] open yt_conf.db failed"));
  72. }
  73. if(shm_load()){
  74. logthread->appendData(QString("[agguard] shm load success"));
  75. }
  76. mqttIdx = 1;
  77. mqttEventIdx = 1;
  78. m_client = new QMQTT::Client(QHostAddress(ip),static_cast<quint16>(port.toInt()),this);
  79. connect(m_client,&QMQTT::Client::connected,this,&GuardCore::onConnected);
  80. m_client->setUsername(username);
  81. m_client->setPassword(password.toLatin1());
  82. m_client->setCleanSession(true);
  83. m_client->connectToHost();
  84. dbthread = new DatabaseThread(this,&db);
  85. connect(dbthread,&DatabaseThread::dbdata_log,this,&GuardCore::dataLog);
  86. httpthread = new HttpThread(this);
  87. connect(httpthread,&HttpThread::dataLog,this,&GuardCore::dataLog);
  88. connect(httpthread,&HttpThread::mqttData,this,&GuardCore::mqtt_data);
  89. connect(httpthread,&HttpThread::mqttEvent,this,&GuardCore::mqtt_event);
  90. httpthread->app_info(httpurl,httpkey,httpsignature,httpurl1,httpkey1,httpsignature);
  91. tcpserver = new TcpServer(this);
  92. connect(tcpserver,&TcpServer::tcp_log,this,&GuardCore::dataLog);
  93. tcpserver->app_info(tcpserverurl,tcpserverkey,tcpserversignature);
  94. devinfoserver = new DevInfoTcpServer(this);
  95. connect(devinfoserver,&DevInfoTcpServer::tcp_log,this,&GuardCore::dataLog);
  96. connect(devinfoserver,&DevInfoTcpServer::append_sql,this,&GuardCore::appendSql);
  97. devinfoserver->app_info(devinfourl,devinfokey,devinfosignature);
  98. }
  99. GuardCore::~GuardCore()
  100. {
  101. logthread->stop();
  102. dbthread->stop();
  103. httpthread->stop();
  104. }
  105. void GuardCore::start()
  106. {
  107. tcpserver->start();
  108. devinfoserver->start();
  109. dbthread->start();
  110. }
  111. void GuardCore::onConnected()
  112. {
  113. logthread->appendData(QString("mqtt onConnected"));
  114. }
  115. void GuardCore::dataLog(QString log)
  116. {
  117. logthread->appendData(log);
  118. }
  119. void GuardCore::mqtt_data(QString mqtt_msg)
  120. {
  121. printf("test2222 [%s]\n",mqtt_msg.toUtf8().data());
  122. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  123. m_client->connectToHost();
  124. }
  125. m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",mqtt_msg.toUtf8()));
  126. if(mqttIdx > 9999){
  127. mqttIdx = 1;
  128. }
  129. }
  130. void GuardCore::mqtt_event(QString mqtt_msg)
  131. {
  132. printf("test2222 mqtt event [%s]\n",mqtt_msg.toUtf8().data());
  133. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  134. m_client->connectToHost();
  135. }
  136. m_client->publish(QMQTT::Message(mqttEventIdx++,"event-collector",mqtt_msg.toUtf8()));
  137. if(mqttEventIdx > 9999){
  138. mqttEventIdx = 1;
  139. }
  140. }
  141. void GuardCore::appendSql(QString sql)
  142. {
  143. dbthread->appendSql(sql);
  144. shm_init();
  145. }