core.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "core.h"
  2. #include "../AGBoxDog/boxshm.h"
  3. AGBoxShm *agBoxShm;
  4. void Core::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. }
  15. bool Core::shm_load(){
  16. key_t key;
  17. int shmid;
  18. if((key=ftok(SHM_PATH,static_cast<int>(SHM_PORT)))==-1){
  19. return false;
  20. }
  21. if((shmid=shmget(key,sizeof(AGBoxShm),IPC_CREAT|0666))==-1){
  22. return false;
  23. }
  24. agBoxShm = static_cast<AGBoxShm *>(shmat(shmid,nullptr,0));
  25. shm_init();
  26. return true;
  27. }
  28. Core::Core(QObject *parent) : QObject(parent)
  29. {
  30. logthread = new LogThread(this);
  31. logthread->start();
  32. db = QSqlDatabase::addDatabase("QSQLITE","conf_db");
  33. db.setDatabaseName(QString("/opt/db/yt_conf.db"));
  34. if(!db.open()){
  35. logthread->appendData(QString("[agMqttProcess] open yt_conf.db failed"));
  36. }
  37. if(shm_load()){
  38. logthread->appendData(QString("[agMqttProcess] shm load success"));
  39. }
  40. mqttIdx = 1;
  41. mqttIdx1 = 1;
  42. mqttIdx2 = 1;
  43. m_client = new QMQTT::Client(QHostAddress(ip),static_cast<quint16>(port.toInt()),this);
  44. connect(m_client,&QMQTT::Client::connected,this,&Core::onConnected);
  45. m_client->setUsername(username);
  46. m_client->setPassword(password.toLatin1());
  47. m_client->setCleanSession(true);
  48. m_client->connectToHost();
  49. jzmqttsub = new JZMqttSub(this);
  50. connect(jzmqttsub,&JZMqttSub::sendMqttData,this,&Core::receiveDevData);
  51. jzmqttpub = new JZMqttPub(this);
  52. connect(jzmqttpub,&JZMqttPub::mqttData,this,&Core::mqtt_data);
  53. connect(jzmqttpub,&JZMqttPub::dataListLog,this,&Core::dataLog);
  54. httpthread = new HttpThread(this);
  55. connect(httpthread,&HttpThread::dataLog,this,&Core::dataLog);
  56. connect(httpthread,&HttpThread::mqttData,this,&Core::mqtt_data1);
  57. ytmqttsub = new YTMqttSub(this);
  58. connect(ytmqttsub,&YTMqttSub::sendMqttData,this,&Core::receiveYtDevData);
  59. ytmqttpub = new YTMqttPub(this);
  60. connect(ytmqttpub,&YTMqttPub::mqttData,this,&Core::mqtt_data2);
  61. connect(ytmqttpub,&YTMqttPub::mqttLog,this,&Core::dataLog);
  62. timer = new QTimer(this);
  63. connect(timer,&QTimer::timeout,this,&Core::time_out);
  64. timer->start(1000);
  65. }
  66. Core::~Core()
  67. {
  68. logthread->stop();
  69. jzmqttpub->stop();
  70. }
  71. void Core::start()
  72. {
  73. jzmqttsub->start();
  74. jzmqttsub->mqtt_conf(ip,port,username,password);
  75. jzmqttpub->start();
  76. ytmqttsub->start();
  77. ytmqttsub->mqtt_conf(ip,port,username,password);
  78. ytmqttpub->start();
  79. }
  80. void Core::time_out()
  81. {
  82. agBoxShm->processStatus[1].t_time=QDateTime::currentDateTime().toTime_t();
  83. }
  84. void Core::onConnected()
  85. {
  86. logthread->appendData(QString("mqtt onConnected"));
  87. }
  88. void Core::dataLog(QString log)
  89. {
  90. logthread->appendData(log);
  91. }
  92. void Core::mqtt_data(QString mqtt_msg)
  93. {
  94. printf("test1111 [%s]\n",mqtt_msg.toUtf8().data());
  95. logthread->appendData(QString("[%1] jz mqtt %2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(mqtt_msg));
  96. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  97. m_client->connectToHost();
  98. }
  99. m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",mqtt_msg.toUtf8()));
  100. if(mqttIdx > 9999){
  101. mqttIdx = 1;
  102. }
  103. }
  104. void Core::mqtt_data1(QString mqtt_msg)
  105. {
  106. printf("test2222 [%s]\n",mqtt_msg.toUtf8().data());
  107. logthread->appendData(QString("[%1] fj&ali http %2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(mqtt_msg));
  108. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  109. m_client->connectToHost();
  110. }
  111. m_client->publish(QMQTT::Message(mqttIdx1++,"data-collector",mqtt_msg.toUtf8()));
  112. if(mqttIdx1 > 9999){
  113. mqttIdx1 = 1;
  114. }
  115. }
  116. void Core::mqtt_data2(QString mqtt_msg)
  117. {
  118. printf("test3333 [%s]\n",mqtt_msg.toUtf8().data());
  119. logthread->appendData(QString("[%1] yt mqtt %2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(mqtt_msg));
  120. if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
  121. m_client->connectToHost();
  122. }
  123. m_client->publish(QMQTT::Message(mqttIdx2++,"data-collector",mqtt_msg.toUtf8()));
  124. if(mqttIdx2 > 9999){
  125. mqttIdx2 = 1;
  126. }
  127. }
  128. void Core::receiveDevData(QString topic,QByteArray data)
  129. {
  130. jzmqttpub->devMessage(MqttData(topic,data));
  131. }
  132. void Core::receiveYtDevData(QString topic,QByteArray data)
  133. {
  134. ytmqttpub->devMessage(MqttData(topic,data));
  135. }