TDengine.cpp 6.5 KB

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