TDengine.cpp 8.0 KB

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