TDengineClient.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "TDengineClient.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. int32_t TDengineClient::msgProcess(TAOS_RES* msg)
  12. {
  13. char buf[1024];
  14. int32_t rows = 0;
  15. const char* topicName = tmq_get_topic_name(msg);
  16. const char* dbName = tmq_get_db_name(msg);
  17. int32_t vgroupId = tmq_get_vgroup_id(msg);
  18. //qDebug() << __FILE__ << __LINE__ << "db: " << dbName;
  19. //qDebug() << __FILE__ << __LINE__ << "topic: " << topicName;
  20. //qDebug() << __FILE__ << __LINE__ << "vgroup id:" << vgroupId;
  21. while (1)
  22. {
  23. TAOS_ROW row = taos_fetch_row(msg);
  24. if (row == NULL)
  25. break;
  26. TAOS_FIELD* fields = taos_fetch_fields(msg);
  27. int32_t numOfFields = taos_field_count(msg);
  28. // int32_t* length = taos_fetch_lengths(msg);
  29. // int32_t precision = taos_result_precision(msg);
  30. rows++;
  31. taos_print_row(buf, row, fields, numOfFields);
  32. //if( g_pSubCB != nullptr )
  33. {
  34. std::string topic = topicName;
  35. std::string content = buf;
  36. callback((char*)topic.c_str(),(char*)content.c_str(), usrData);
  37. }
  38. //qDebug() << __FILE__ << __LINE__ << "row content: " << buf;
  39. }
  40. return rows;
  41. }
  42. //构建消费者
  43. tmq_t* TDengineClient::buildConsumer()
  44. {
  45. tmq_conf_res_t code;
  46. tmq_conf_t* conf = tmq_conf_new();
  47. code = tmq_conf_set(conf, "td.connect.ip", host.toStdString().c_str());
  48. if (TMQ_CONF_OK != code) {
  49. tmq_conf_destroy(conf);
  50. return NULL;
  51. }
  52. //code = tmq_conf_set(conf, "enable.auto.commit", "true");
  53. code = tmq_conf_set(conf, "enable.auto.commit", "false"); //禁用提交回调
  54. if (TMQ_CONF_OK != code) {
  55. tmq_conf_destroy(conf);
  56. return NULL;
  57. }
  58. // code = tmq_conf_set(conf, "auto.commit.interval.ms", "1000");
  59. // if (TMQ_CONF_OK != code) {
  60. // tmq_conf_destroy(conf);
  61. // return NULL;
  62. // }
  63. code = tmq_conf_set(conf, "group.id", "cgrpName");
  64. if (TMQ_CONF_OK != code) {
  65. tmq_conf_destroy(conf);
  66. return NULL;
  67. }
  68. code = tmq_conf_set(conf, "client.id", "user defined name");
  69. if (TMQ_CONF_OK != code) {
  70. tmq_conf_destroy(conf);
  71. return NULL;
  72. }
  73. code = tmq_conf_set(conf, "td.connect.user", user.toStdString().c_str());
  74. if (TMQ_CONF_OK != code) {
  75. tmq_conf_destroy(conf);
  76. return NULL;
  77. }
  78. code = tmq_conf_set(conf, "td.connect.pass", password.toStdString().c_str());
  79. if (TMQ_CONF_OK != code) {
  80. tmq_conf_destroy(conf);
  81. return NULL;
  82. }
  83. //v3.2以后默认为最新的latest
  84. // code = tmq_conf_set(conf, "auto.offset.reset", "latest");
  85. // if (TMQ_CONF_OK != code) {
  86. // tmq_conf_destroy(conf);
  87. // return NULL;
  88. // }
  89. //tmq_conf_set_auto_commit_cb(conf, tmq_commit_cb_print, NULL); //禁用提交回调
  90. tmq_t* tmq = tmq_consumer_new(conf, NULL, 0);
  91. //qDebug()<< __FILE__ << __LINE__ << "tmq" << tmq;
  92. tmq_conf_destroy(conf);
  93. return tmq;
  94. }
  95. //构建主题
  96. tmq_list_t* TDengineClient::buildTopicList()
  97. {
  98. tmq_list_t* tmqList = tmq_list_new();
  99. std::list<std::string>::iterator itr;
  100. for( itr = topicList.begin(); itr != topicList.end(); ++itr )
  101. {
  102. std::string szTopic = *itr;
  103. int32_t code = tmq_list_append(tmqList, szTopic.c_str());
  104. if (code) {
  105. return NULL;
  106. }
  107. }
  108. return tmqList;
  109. }
  110. //轮询主题
  111. void TDengineClient::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. TDengineClient::TDengineClient(QObject *parent)
  148. : QObject(parent)
  149. {
  150. pConn = nullptr;
  151. //g_pSubCB = nullptr;
  152. topicList.clear();
  153. }
  154. TDengineClient::~TDengineClient()
  155. {
  156. taos_close(pConn);
  157. if(future.isRunning()){
  158. future.waitForFinished();
  159. }
  160. }
  161. void TDengineClient::exec(QString sql)
  162. {
  163. if (pConn)
  164. {
  165. TAOS_RES* pRes = taos_query(pConn, sql.toStdString().c_str());
  166. if (taos_errno(pRes) != 0) {
  167. qDebug() << __FILE__ << __LINE__ << "failed to insert into tab, reason:" << taos_errstr(pRes);
  168. qDebug()<< __FILE__ << __LINE__ << "err sql" << sql;
  169. }
  170. else
  171. {
  172. //qDebug()<< "exec ok";
  173. }
  174. taos_free_result(pRes);
  175. }
  176. }
  177. void TDengineClient::subscribe(QString ch, std::function<void(const char* topic, const char* data, void*usr)> fn , void* usrdata)
  178. {
  179. callback = fn;
  180. usrData = usrdata;
  181. topicList.push_back(ch.toLocal8Bit().toStdString());
  182. }
  183. void TDengineClient::psubscribe(QString ch, std::function<void(const char* topic, const char* data, void*usr)> fn, void*usrdata)
  184. {
  185. callback = fn;
  186. usrData = usrdata;
  187. topicList.push_back(ch.toLocal8Bit().toStdString());
  188. }
  189. void TDengineClient::Setup(const char* _host, const char* _user, const char* _passwd, uint16_t _port)
  190. {
  191. // host = ts.addr.c_str();
  192. // user = ts.user.c_str();
  193. // dbName = ts.db.c_str();
  194. // password = ts.password.c_str();
  195. // port = ts.port;
  196. host = _host;
  197. user = _user;
  198. password = _passwd;
  199. port = _port;
  200. }
  201. void TDengineClient::start()
  202. {
  203. pConn = taos_connect(host.toStdString().c_str(),
  204. user.toStdString().c_str(),
  205. password.toStdString().c_str(),
  206. dbName.toStdString().c_str(),
  207. port);
  208. if (pConn == NULL) {
  209. qDebug()<< __FILE__ << __LINE__ << "td conn err.";
  210. return;
  211. }
  212. //开启一个线程轮询订阅的主题
  213. future = QtConcurrent::run(this, &TDengineClient::topicLoop);
  214. }