lightingcore.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "lightingcore.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. #include "../agVideoProcess/boxdeviceshm.h"
  4. AGBoxShm *agBoxShm;
  5. BoxDeviceShm *boxDeviceShm;
  6. void LightingCore::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 LightingCore::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. LightingCore::LightingCore(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,&LightingCore::onConnected);
  54. m_client->setUsername(username);
  55. m_client->setPassword(password.toLatin1());
  56. m_client->setCleanSession(true);
  57. m_client->connectToHost();
  58. clientthread = new ClientThread(this);
  59. connect(clientthread,&ClientThread::mqttData,this,&LightingCore::mqtt_data);
  60. connect(clientthread,&ClientThread::dataListLog,this,&LightingCore::dataLog);
  61. connect(clientthread,&ClientThread::responseMsg,this,&LightingCore::response_msg);
  62. tcpserver = new TcpServer(this);
  63. connect(tcpserver,&TcpServer::control_data,this,&LightingCore::control_data);
  64. connect(tcpserver,&TcpServer::tcp_log,this,&LightingCore::dataLog);
  65. timer = new QTimer(this);
  66. connect(timer,&QTimer::timeout,this,&LightingCore::time_out);
  67. timer->start(60000);
  68. }
  69. LightingCore::~LightingCore()
  70. {
  71. logthread->stop();
  72. clientthread->stop();
  73. }
  74. void LightingCore::start()
  75. {
  76. clientthread->start();
  77. tcpserver->start();
  78. time_out();
  79. }
  80. void LightingCore::control_data(QString deviceId,int switchStatus)
  81. {
  82. clientthread->controlData(deviceId,switchStatus);
  83. }
  84. void LightingCore::time_out()
  85. {
  86. agBoxShm->processStatus[6].t_time = QDateTime::currentDateTime().toTime_t();
  87. clientthread->deviceStatus();
  88. for(int i=0;i<1024;i++){
  89. if((boxDeviceShm->device[i].Enabled == 0x01)&&(boxDeviceShm->device[i].device_type == 510)){
  90. uint curTime = QDateTime::currentDateTime().toTime_t();
  91. if((curTime - boxDeviceShm->device[i].lastTime)>120){
  92. boxDeviceShm->device[i].lastTime = curTime;
  93. 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-lg\"}")
  94. .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));
  95. }
  96. }
  97. }
  98. }
  99. void LightingCore::response_msg(QString msg)
  100. {
  101. tcpserver->response_msg(msg);
  102. }
  103. void LightingCore::onConnected()
  104. {
  105. logthread->appendData(QString("mqtt onConnected"));
  106. }
  107. void LightingCore::dataLog(QString log)
  108. {
  109. logthread->appendData(log);
  110. }
  111. void LightingCore::mqtt_data(QString mqtt_msg)
  112. {
  113. printf("test6666 [%s]\n",mqtt_msg.toUtf8().data());
  114. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  115. m_client->connectToHost();
  116. }
  117. m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",mqtt_msg.toUtf8()));
  118. if(mqttIdx > 9999){
  119. mqttIdx = 1;
  120. }
  121. }