TDengine.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "TDengine.h"
  2. #include "taos.h"
  3. #include <QtCore/QDebug>
  4. #include <QtConcurrent/QtConcurrent>
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include "taos.h"
  11. TAOS* pConn = NULL;
  12. EventSubInterface* g_pSubCB; // 订阅回调
  13. std::list<std::string> g_lstTopics;
  14. int32_t TDengine::msgProcess(TAOS_RES* msg)
  15. {
  16. char buf[1024];
  17. int32_t rows = 0;
  18. const char* topicName = tmq_get_topic_name(msg);
  19. const char* dbName = tmq_get_db_name(msg);
  20. int32_t vgroupId = tmq_get_vgroup_id(msg);
  21. qDebug() << __FILE__ << __LINE__ << "topic: " << topicName;
  22. qDebug() << __FILE__ << __LINE__ << "db: " << dbName;
  23. qDebug() << __FILE__ << __LINE__ << "vgroup id:" << vgroupId;
  24. while (1)
  25. {
  26. TAOS_ROW row = taos_fetch_row(msg);
  27. if (row == NULL)
  28. break;
  29. TAOS_FIELD* fields = taos_fetch_fields(msg);
  30. int32_t numOfFields = taos_field_count(msg);
  31. // int32_t* length = taos_fetch_lengths(msg);
  32. // int32_t precision = taos_result_precision(msg);
  33. rows++;
  34. taos_print_row(buf, row, fields, numOfFields);
  35. if( g_pSubCB != nullptr )
  36. {
  37. g_pSubCB->SubCB((char*)topicName,buf);
  38. }
  39. qDebug() << __FILE__ << __LINE__ << "row content: " << buf;
  40. }
  41. return rows;
  42. }
  43. //构建消费者
  44. tmq_t* TDengine::buildConsumer()
  45. {
  46. tmq_conf_res_t code;
  47. tmq_conf_t* conf = tmq_conf_new();
  48. code = tmq_conf_set(conf, "td.connect.ip", host.toStdString().c_str());
  49. if (TMQ_CONF_OK != code) {
  50. tmq_conf_destroy(conf);
  51. return NULL;
  52. }
  53. //code = tmq_conf_set(conf, "enable.auto.commit", "true");
  54. code = tmq_conf_set(conf, "enable.auto.commit", "false"); //禁用提交回调
  55. if (TMQ_CONF_OK != code) {
  56. tmq_conf_destroy(conf);
  57. return NULL;
  58. }
  59. // code = tmq_conf_set(conf, "auto.commit.interval.ms", "1000");
  60. // if (TMQ_CONF_OK != code) {
  61. // tmq_conf_destroy(conf);
  62. // return NULL;
  63. // }
  64. code = tmq_conf_set(conf, "group.id", "cgrpName");
  65. if (TMQ_CONF_OK != code) {
  66. tmq_conf_destroy(conf);
  67. return NULL;
  68. }
  69. code = tmq_conf_set(conf, "client.id", "user defined name");
  70. if (TMQ_CONF_OK != code) {
  71. tmq_conf_destroy(conf);
  72. return NULL;
  73. }
  74. code = tmq_conf_set(conf, "td.connect.user", user.toStdString().c_str());
  75. if (TMQ_CONF_OK != code) {
  76. tmq_conf_destroy(conf);
  77. return NULL;
  78. }
  79. code = tmq_conf_set(conf, "td.connect.pass", password.toStdString().c_str());
  80. if (TMQ_CONF_OK != code) {
  81. tmq_conf_destroy(conf);
  82. return NULL;
  83. }
  84. //v3.2以后默认为最新的latest
  85. // code = tmq_conf_set(conf, "auto.offset.reset", "latest");
  86. // if (TMQ_CONF_OK != code) {
  87. // tmq_conf_destroy(conf);
  88. // return NULL;
  89. // }
  90. //tmq_conf_set_auto_commit_cb(conf, tmq_commit_cb_print, NULL); //禁用提交回调
  91. tmq_t* tmq = tmq_consumer_new(conf, NULL, 0);
  92. qDebug()<< __FILE__ << __LINE__ << "tmq" << tmq;
  93. tmq_conf_destroy(conf);
  94. return tmq;
  95. }
  96. //构建主题
  97. tmq_list_t* TDengine::buildTopicList()
  98. {
  99. tmq_list_t* topicList = tmq_list_new();
  100. std::list<std::string>::iterator itr;
  101. for( itr = g_lstTopics.begin(); itr != g_lstTopics.end(); ++itr )
  102. {
  103. int32_t code = tmq_list_append(topicList, itr->c_str());
  104. if (code) {
  105. return NULL;
  106. }
  107. }
  108. return topicList;
  109. }
  110. //轮询主题
  111. void TDengine::topicLoop()
  112. {
  113. int32_t code;
  114. tmq_t* tmq = buildConsumer();
  115. if (NULL == tmq) {
  116. qDebug()<< __FILE__ << __LINE__ << "err" << stderr;
  117. return;
  118. }
  119. tmq_list_t* topic_list = buildTopicList();
  120. code = tmq_subscribe(tmq, topic_list);
  121. if ( code ) {
  122. qDebug() << __FILE__ << __LINE__ << "Failed to tmq_subscribe(): " << tmq_err2str(code);
  123. }
  124. tmq_list_destroy(topic_list);
  125. int32_t totalRows = 0;
  126. int32_t msgCnt = 0;
  127. int32_t timeout = 100; //多个主题共用1个线程轮询,超时时间太长会影响其它主题实时性
  128. while (true) {
  129. TAOS_RES* tmqmsg = tmq_consumer_poll(tmq, timeout);
  130. if (tmqmsg) {
  131. msgCnt++;
  132. totalRows += msgProcess(tmqmsg);
  133. taos_free_result(tmqmsg);
  134. } else {
  135. //超时
  136. //break;
  137. }
  138. }
  139. code = tmq_consumer_close(tmq);
  140. if (code) {
  141. fprintf(stderr, "%% Failed to close consumer: %s\n", tmq_err2str(code));
  142. } else {
  143. fprintf(stderr, "%% Consumer closed\n");
  144. }
  145. qDebug()<< __FILE__ << __LINE__ << "stderr" << stderr;
  146. }
  147. TDengine::TDengine(QObject *parent)
  148. : QObject(parent)
  149. {
  150. pConn = nullptr;
  151. g_pSubCB = nullptr;
  152. g_lstTopics.clear();
  153. }
  154. TDengine::~TDengine()
  155. {
  156. taos_close(pConn);
  157. }
  158. void TDengine::exec(QString sql)
  159. {
  160. if (pConn)
  161. {
  162. TAOS_RES* pRes = taos_query(pConn, sql.toStdString().c_str());
  163. if (taos_errno(pRes) != 0) {
  164. qDebug() << __FILE__ << __LINE__ << "failed to insert into tab, reason:" << taos_errstr(pRes);
  165. qDebug()<< __FILE__ << __LINE__ << "err sql" << sql;
  166. }
  167. else
  168. {
  169. //qDebug()<< "exec ok";
  170. }
  171. taos_free_result(pRes);
  172. }
  173. }
  174. void TDengine::subscribe(QString ch, EventSubInterface *fn)
  175. {
  176. g_pSubCB = fn;
  177. g_lstTopics.push_back(ch.toLocal8Bit().toStdString());
  178. }
  179. void TDengine::psubscribe(QString ch, EventSubInterface *fn)
  180. {
  181. g_pSubCB = fn;
  182. g_lstTopics.push_back(ch.toLocal8Bit().toStdString());
  183. }
  184. void TDengine::Setup(tagSetup ts)
  185. {
  186. host = ts.addr.c_str();
  187. user = ts.user.c_str();
  188. dbName = ts.db.c_str();
  189. password = ts.password.c_str();
  190. m_nPort = ts.port;
  191. }
  192. void TDengine::start()
  193. {
  194. pConn = taos_connect(host.toStdString().c_str(),
  195. user.toStdString().c_str(),
  196. password.toStdString().c_str(),
  197. dbName.toStdString().c_str(),
  198. m_nPort);
  199. if (pConn == NULL) {
  200. qDebug()<< __FILE__ << __LINE__ << "td conn err.";
  201. return;
  202. }
  203. //开启一个线程轮询订阅的主题
  204. QtConcurrent::run(this, &TDengine::topicLoop);
  205. }