lightingcore.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "lightingcore.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. AGBoxShm *agBoxShm;
  4. void LightingCore::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. qry.clear();
  15. sql.clear();
  16. }
  17. bool LightingCore::shm_load(){
  18. key_t key;
  19. int shmid;
  20. if((key=ftok(SHM_PATH,static_cast<int>(SHM_PORT)))==-1){
  21. return false;
  22. }
  23. if((shmid=shmget(key,sizeof(AGBoxShm),IPC_CREAT|0666))==-1){
  24. return false;
  25. }
  26. agBoxShm = static_cast<AGBoxShm *>(shmat(shmid,nullptr,0));
  27. shm_init();
  28. return true;
  29. }
  30. LightingCore::LightingCore(QObject *parent) : QObject(parent)
  31. {
  32. logthread = new LogThread(this);
  33. logthread->start();
  34. db = QSqlDatabase::addDatabase("QSQLITE","conf_db");
  35. db.setDatabaseName(QString("/opt/db/yt_conf.db"));
  36. if(!db.open()){
  37. logthread->appendData(QString("[agenvironment] open yt_conf.db failed"));
  38. }
  39. if(shm_load()){
  40. logthread->appendData(QString("[agenvironment] shm load success"));
  41. }
  42. mqttIdx = 1;
  43. m_client = new QMQTT::Client(QHostAddress(ip),static_cast<quint16>(port.toInt()),this);
  44. connect(m_client,&QMQTT::Client::connected,this,&LightingCore::onConnected);
  45. m_client->setUsername(username);
  46. m_client->setPassword(password.toLatin1());
  47. m_client->setCleanSession(true);
  48. m_client->connectToHost();
  49. clientthread = new ClientThread(this);
  50. connect(clientthread,&ClientThread::mqttData,this,&LightingCore::mqtt_data);
  51. connect(clientthread,&ClientThread::dataListLog,this,&LightingCore::dataLog);
  52. connect(clientthread,&ClientThread::responseMsg,this,&LightingCore::response_msg);
  53. tcpserver = new TcpServer(this);
  54. connect(tcpserver,&TcpServer::control_data,this,&LightingCore::control_data);
  55. connect(tcpserver,&TcpServer::tcp_log,this,&LightingCore::dataLog);
  56. timer = new QTimer(this);
  57. connect(timer,&QTimer::timeout,this,&LightingCore::time_out);
  58. timer->start(60000);
  59. }
  60. LightingCore::~LightingCore()
  61. {
  62. logthread->stop();
  63. clientthread->stop();
  64. }
  65. void LightingCore::start()
  66. {
  67. clientthread->start();
  68. tcpserver->start();
  69. time_out();
  70. }
  71. void LightingCore::control_data(QString deviceId,int switchStatus)
  72. {
  73. clientthread->controlData(deviceId,switchStatus);
  74. }
  75. void LightingCore::time_out()
  76. {
  77. agBoxShm->processStatus[6].t_time = QDateTime::currentDateTime().toTime_t();
  78. clientthread->deviceStatus();
  79. for(int i=0;i<1024;i++){
  80. if((agBoxShm->device[i].Enabled == 0x01)&&(agBoxShm->device[i].device_type == 510)){
  81. uint curTime = QDateTime::currentDateTime().toTime_t();
  82. if((curTime - agBoxShm->device[i].lastTime)>120){
  83. agBoxShm->device[i].lastTime = curTime;
  84. 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\"}")
  85. .arg(agBoxShm->device[i].device_id).arg(agBoxShm->device[i].device_code).arg(agBoxShm->device[i].product_code).arg(curTime).arg(agBoxShm->device[i].device_type));
  86. }
  87. }
  88. }
  89. }
  90. void LightingCore::response_msg(QString msg)
  91. {
  92. tcpserver->response_msg(msg);
  93. }
  94. void LightingCore::onConnected()
  95. {
  96. logthread->appendData(QString("mqtt onConnected"));
  97. }
  98. void LightingCore::dataLog(QString log)
  99. {
  100. logthread->appendData(log);
  101. }
  102. void LightingCore::mqtt_data(QString mqtt_msg)
  103. {
  104. printf("test6666 [%s]\n",mqtt_msg.toUtf8().data());
  105. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  106. m_client->connectToHost();
  107. }
  108. m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",mqtt_msg.toUtf8()));
  109. if(mqttIdx > 9999){
  110. mqttIdx = 1;
  111. }
  112. }