videocore.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "videocore.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. AGBoxShm *agBoxShm;
  4. void VideoCore::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 = 'video-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 = 'video-get-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 = 'video-preview-hk'");
  35. qry = db.exec(sql);
  36. while (qry.next()) {
  37. tcpserverurl = qry.value(0).toString();
  38. tcpserverkey = qry.value(1).toString();
  39. tcpserversignature = 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 = 'video-control-hk'");
  44. qry = db.exec(sql);
  45. while (qry.next()) {
  46. tcpserver1url = qry.value(0).toString();
  47. tcpserver1key = qry.value(1).toString();
  48. tcpserver1signature = qry.value(2).toString();
  49. }
  50. }
  51. bool VideoCore::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. VideoCore::VideoCore(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("[agvideo] open yt_conf.db failed"));
  72. }
  73. if(shm_load()){
  74. logthread->appendData(QString("[agvideo] shm load success"));
  75. }
  76. mqttIdx = 1;
  77. m_client = new QMQTT::Client(QHostAddress(ip),static_cast<quint16>(port.toInt()),this);
  78. connect(m_client,&QMQTT::Client::connected,this,&VideoCore::onConnected);
  79. m_client->setUsername(username);
  80. m_client->setPassword(password.toLatin1());
  81. m_client->setCleanSession(true);
  82. m_client->connectToHost();
  83. httpthread = new HttpThread(this);
  84. connect(httpthread,&HttpThread::dataLog,this,&VideoCore::dataLog);
  85. connect(httpthread,&HttpThread::mqttData,this,&VideoCore::mqtt_data);
  86. httpthread->app_info(httpurl,httpkey,httpsignature);
  87. tcpserver = new TcpServer(this);
  88. connect(tcpserver,&TcpServer::tcp_log,this,&VideoCore::dataLog);
  89. tcpserver->app_info(tcpserverurl,tcpserverkey,tcpserversignature);
  90. tcpserver1 = new TcpServer1(this);
  91. connect(tcpserver1,&TcpServer1::tcp_log,this,&VideoCore::dataLog);
  92. tcpserver1->app_info(tcpserver1url,tcpserver1key,tcpserver1signature);
  93. devinfoserver = new DevInfoTcpServer(this);
  94. connect(devinfoserver,&DevInfoTcpServer::tcp_log,this,&VideoCore::dataLog);
  95. connect(devinfoserver,&DevInfoTcpServer::append_sql,this,&VideoCore::appendSql);
  96. devinfoserver->app_info(devinfourl,devinfokey,devinfosignature);
  97. dbthread = new DatabaseThread(this,&db);
  98. connect(dbthread,&DatabaseThread::dbdata_log,this,&VideoCore::dataLog);
  99. }
  100. VideoCore::~VideoCore()
  101. {
  102. logthread->stop();
  103. httpthread->stop();
  104. dbthread->stop();
  105. }
  106. void VideoCore::start()
  107. {
  108. tcpserver->start();
  109. tcpserver1->start();
  110. devinfoserver->start();
  111. dbthread->start();
  112. }
  113. void VideoCore::onConnected()
  114. {
  115. logthread->appendData(QString("mqtt onConnected"));
  116. }
  117. void VideoCore::dataLog(QString log)
  118. {
  119. logthread->appendData(log);
  120. }
  121. void VideoCore::mqtt_data(QString mqtt_msg)
  122. {
  123. printf("test1111 [%s]\n",mqtt_msg.toUtf8().data());
  124. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  125. m_client->connectToHost();
  126. }
  127. m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",mqtt_msg.toUtf8()));
  128. if(mqttIdx > 9999){
  129. mqttIdx = 1;
  130. }
  131. }
  132. void VideoCore::appendSql(QString sql)
  133. {
  134. dbthread->appendSql(sql);
  135. shm_init();
  136. }