videocore.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "videocore.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. #include "boxdeviceshm.h"
  4. AGBoxShm *agBoxShm;
  5. BoxDeviceShm *boxDeviceShm;
  6. void VideoCore::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. int nrow = 0;
  18. sql.clear();
  19. sql = QString("select device_code,device_id,device_type,company_code,device_gateway from yt_t_device");
  20. qry = db.exec(sql);
  21. while (qry.next()) {
  22. if(nrow > 1023)
  23. break;
  24. boxDeviceShm->device[nrow].Enabled = 0x01;
  25. sprintf(boxDeviceShm->device[nrow].device_code,"%s",qry.value(0).toString().toUtf8().data());
  26. sprintf(boxDeviceShm->device[nrow].device_id,"%s",qry.value(1).toString().toUtf8().data());
  27. boxDeviceShm->device[nrow].device_type = qry.value(2).toInt();
  28. sprintf(boxDeviceShm->device[nrow].company_code,"%s",qry.value(3).toString().toUtf8().data());
  29. sprintf(boxDeviceShm->device[nrow].device_gateway,"%s",qry.value(4).toString().toUtf8().data());
  30. nrow++;
  31. }
  32. for(int i=nrow;i<1024;i++){
  33. boxDeviceShm->device[i].Enabled = 0x00;
  34. }
  35. nrow = 0;
  36. qry.clear();
  37. sql.clear();
  38. sql = QString("select device_type,attribute_name,attribute_code from yt_t_attribute");
  39. qry = db.exec(sql);
  40. while (qry.next()) {
  41. if(nrow > 299)
  42. break;
  43. boxDeviceShm->procuctattrbute[nrow].Enabled = 0x01;
  44. boxDeviceShm->procuctattrbute[nrow].device_type = qry.value(0).toInt();
  45. sprintf(boxDeviceShm->procuctattrbute[nrow].attribute_name,"%s",qry.value(1).toString().toUtf8().data());
  46. sprintf(boxDeviceShm->procuctattrbute[nrow].attribute_code,"%s",qry.value(2).toString().toUtf8().data());
  47. nrow++;
  48. }
  49. for(int i=nrow;i<300;i++){
  50. boxDeviceShm->procuctattrbute[i].Enabled = 0x00;
  51. }
  52. }
  53. bool VideoCore::shm_load(){
  54. key_t key;
  55. int shmid;
  56. if((key=ftok(SHM_PATH,static_cast<int>(SHM_PORT)))==-1){
  57. return false;
  58. }
  59. if((shmid=shmget(key,sizeof(AGBoxShm),IPC_CREAT|0666))==-1){
  60. return false;
  61. }
  62. agBoxShm = static_cast<AGBoxShm *>(shmat(shmid,nullptr,0));
  63. if((key=ftok(VIDEO_PATH,static_cast<int>(VIDEO_PORT)))==-1){
  64. return false;
  65. }
  66. if((shmid=shmget(key,sizeof(BoxDeviceShm),IPC_CREAT|0666))==-1){
  67. return false;
  68. }
  69. boxDeviceShm = static_cast<BoxDeviceShm *>(shmat(shmid,nullptr,0));
  70. shm_init();
  71. return true;
  72. }
  73. VideoCore::VideoCore(QObject *parent) : QObject(parent)
  74. {
  75. logthread = new LogThread(this);
  76. logthread->start();
  77. db = QSqlDatabase::addDatabase("QSQLITE","conf_db");
  78. db.setDatabaseName(QString("/opt/db/yt_conf.db"));
  79. if(!db.open()){
  80. logthread->appendData(QString("[agvideo] open yt_conf.db failed"));
  81. }
  82. if(shm_load()){
  83. logthread->appendData(QString("[agvideo] shm load success"));
  84. }
  85. mqttIdx = 1;
  86. m_client = new QMQTT::Client(QHostAddress(ip),static_cast<quint16>(port.toInt()),this);
  87. connect(m_client,&QMQTT::Client::connected,this,&VideoCore::onConnected);
  88. m_client->setUsername(username);
  89. m_client->setPassword(password.toLatin1());
  90. m_client->setCleanSession(true);
  91. m_client->connectToHost();
  92. httpthread = new HttpThread(this);
  93. connect(httpthread,&HttpThread::dataLog,this,&VideoCore::dataLog);
  94. connect(httpthread,&HttpThread::mqttData,this,&VideoCore::mqtt_data);
  95. tcpserver = new TcpServer(this);
  96. connect(tcpserver,&TcpServer::tcp_log,this,&VideoCore::dataLog);
  97. tcpserver1 = new TcpServer1(this);
  98. connect(tcpserver1,&TcpServer1::tcp_log,this,&VideoCore::dataLog);
  99. }
  100. VideoCore::~VideoCore()
  101. {
  102. logthread->stop();
  103. httpthread->stop();
  104. }
  105. void VideoCore::start()
  106. {
  107. tcpserver->start();
  108. tcpserver1->start();
  109. }
  110. void VideoCore::onConnected()
  111. {
  112. logthread->appendData(QString("mqtt onConnected"));
  113. }
  114. void VideoCore::dataLog(QString log)
  115. {
  116. logthread->appendData(log);
  117. }
  118. void VideoCore::mqtt_data(QString mqtt_msg)
  119. {
  120. printf("test1111 [%s]\n",mqtt_msg.toUtf8().data());
  121. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  122. m_client->connectToHost();
  123. }
  124. m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",mqtt_msg.toUtf8()));
  125. if(mqttIdx > 9999){
  126. mqttIdx = 1;
  127. }
  128. }