TDengineClient.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #include <QVariant>
  12. int32_t TDengineClient::msgProcess(TAOS_RES* msg)
  13. {
  14. char buf[1024];
  15. int32_t rows = 0;
  16. const char* topicName = tmq_get_topic_name(msg);
  17. const char* dbName = tmq_get_db_name(msg);
  18. int32_t vgroupId = tmq_get_vgroup_id(msg);
  19. //qDebug() << __FILE__ << __LINE__ << "db: " << dbName;
  20. //qDebug() << __FILE__ << __LINE__ << "topic: " << topicName;
  21. //qDebug() << __FILE__ << __LINE__ << "vgroup id:" << vgroupId;
  22. while (1)
  23. {
  24. TAOS_ROW row = taos_fetch_row(msg);
  25. if (row == NULL)
  26. break;
  27. TAOS_FIELD* fields = taos_fetch_fields(msg);
  28. int32_t numOfFields = taos_field_count(msg);
  29. rows++;
  30. taos_print_row(buf, row, fields, numOfFields);
  31. QJsonObject jsonObjects;
  32. for(int k = 0;k < numOfFields;k++)
  33. {
  34. QJsonObject jsonVal;
  35. QString fieldName = fields[k].name;
  36. if(fields[k].type == TSDB_DATA_TYPE_TIMESTAMP)
  37. {
  38. qint64 value = *((int64_t *)row[k]);
  39. jsonVal.insert("value",value);
  40. jsonVal.insert("type", QMetaType::Type::QDateTime);
  41. }
  42. else if(fields[k].type == TSDB_DATA_TYPE_VARCHAR)
  43. {
  44. QString value((char *)row[k]);
  45. jsonVal.insert("value",value);
  46. jsonVal.insert("type", QMetaType::Type::QString);
  47. }
  48. else if(fields[k].type == TSDB_DATA_TYPE_DOUBLE)
  49. {
  50. double value = *(double *)(row[k]);
  51. jsonVal.insert("value",value);
  52. jsonVal.insert("type", QMetaType::Type::Double);
  53. }
  54. else if(fields[k].type == TSDB_DATA_TYPE_FLOAT)
  55. {
  56. float value = *(float *)(row[k]);
  57. jsonVal.insert("value",value);
  58. jsonVal.insert("type", QMetaType::Type::Float);
  59. }
  60. else if(fields[k].type == TSDB_DATA_TYPE_INT)
  61. {
  62. int value = *(int *)(row[k]);
  63. jsonVal.insert("value",value);
  64. jsonVal.insert("type", QMetaType::Type::Int);
  65. }
  66. else if(fields[k].type == TSDB_DATA_TYPE_BIGINT)
  67. {
  68. qint64 value = *(int64_t *)(row[k]);
  69. jsonVal.insert("value",value);
  70. jsonVal.insert("type", QMetaType::Type::LongLong);
  71. }
  72. jsonObjects.insert(fieldName, jsonVal);
  73. }
  74. std::string content = QJsonDocument(jsonObjects).toJson(QJsonDocument::Compact).toStdString();
  75. //if( g_pSubCB != nullptr )
  76. {
  77. std::string topic = topicName;
  78. //std::string content = buf;
  79. callback((char*)topic.c_str(),(char*)content.c_str(), usrData);
  80. }
  81. //qDebug() << __FILE__ << __LINE__ << "row content: " << buf;
  82. }
  83. return rows;
  84. }
  85. //构建消费者
  86. tmq_t* TDengineClient::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* TDengineClient::buildTopicList()
  140. {
  141. tmq_list_t* tmqList = tmq_list_new();
  142. std::set<std::string>::iterator itr;
  143. for( itr = topicList.begin(); itr != topicList.end(); ++itr )
  144. {
  145. std::string szTopic = *itr;
  146. int32_t code = tmq_list_append(tmqList, szTopic.c_str());
  147. if (code) {
  148. return NULL;
  149. }
  150. }
  151. return tmqList;
  152. }
  153. //轮询主题
  154. void TDengineClient::topicLoop()
  155. {
  156. int32_t code;
  157. tmq_t* tmq = buildConsumer();
  158. if (NULL == tmq) {
  159. qDebug()<< __FILE__ << __LINE__ << "err" << stderr;
  160. return;
  161. }
  162. tmq_list_t* topic_list = buildTopicList();
  163. code = tmq_subscribe(tmq, topic_list);
  164. if ( code ) {
  165. qDebug() << __FILE__ << __LINE__ << "Failed to tmq_subscribe(): " << tmq_err2str(code);
  166. }
  167. tmq_list_destroy(topic_list);
  168. int32_t totalRows = 0;
  169. int32_t msgCnt = 0;
  170. int32_t timeout = 100; //多个主题共用1个线程轮询,超时时间太长会影响其它主题实时性
  171. while (true) {
  172. TAOS_RES* tmqmsg = tmq_consumer_poll(tmq, timeout);
  173. if (tmqmsg) {
  174. msgCnt++;
  175. totalRows += msgProcess(tmqmsg);
  176. taos_free_result(tmqmsg);
  177. } else {
  178. //超时
  179. //break;
  180. }
  181. }
  182. code = tmq_consumer_close(tmq);
  183. if (code) {
  184. fprintf(stderr, "%% Failed to close consumer: %s\n", tmq_err2str(code));
  185. } else {
  186. fprintf(stderr, "%% Consumer closed\n");
  187. }
  188. qDebug()<< __FILE__ << __LINE__ << "stderr" << stderr;
  189. }
  190. TDengineClient::TDengineClient(QObject *parent)
  191. : QObject(parent)
  192. {
  193. pConn = nullptr;
  194. //g_pSubCB = nullptr;
  195. topicList.clear();
  196. }
  197. TDengineClient::~TDengineClient()
  198. {
  199. taos_close(pConn);
  200. if(future.isRunning()){
  201. future.waitForFinished();
  202. }
  203. }
  204. void TDengineClient::exec(QString sql)
  205. {
  206. if (pConn)
  207. {
  208. TAOS_RES* pRes = taos_query(pConn, sql.toStdString().c_str());
  209. if (taos_errno(pRes) != 0) {
  210. qDebug() << __FILE__ << __LINE__ << "failed to insert into tab, reason:" << taos_errstr(pRes);
  211. qDebug()<< __FILE__ << __LINE__ << "err sql" << sql;
  212. }
  213. else
  214. {
  215. //qDebug()<< "exec ok";
  216. }
  217. taos_free_result(pRes);
  218. }
  219. }
  220. void TDengineClient::subscribe(QString ch,const std::function<void(const char* topic, const char* data, void*usr)>& fn , void* usrdata)
  221. {
  222. callback = fn;
  223. usrData = usrdata;
  224. topicList.insert(ch.toLocal8Bit().toStdString());
  225. }
  226. void TDengineClient::subscribe(std::set<std::string>topics,const std::function<void(const char* topic, const char* data, void*usr)>& fn , void* usrdata)
  227. {
  228. callback = fn;
  229. usrData = usrdata;
  230. topicList.merge(topics);
  231. }
  232. void TDengineClient::psubscribe(QString ch,const std::function<void(const char* topic, const char* data, void*usr)>& fn, void*usrdata)
  233. {
  234. callback = fn;
  235. usrData = usrdata;
  236. topicList.insert(ch.toLocal8Bit().toStdString());
  237. }
  238. void TDengineClient::Setup(const char* _host, const char* _user, const char* _passwd, uint16_t _port)
  239. {
  240. // host = ts.addr.c_str();
  241. // user = ts.user.c_str();
  242. // dbName = ts.db.c_str();
  243. // password = ts.password.c_str();
  244. // port = ts.port;
  245. host = _host;
  246. user = _user;
  247. password = _passwd;
  248. port = _port;
  249. }
  250. void TDengineClient::start()
  251. {
  252. pConn = taos_connect(host.toStdString().c_str(),
  253. user.toStdString().c_str(),
  254. password.toStdString().c_str(),
  255. dbName.toStdString().c_str(),
  256. port);
  257. if (pConn == NULL) {
  258. qDebug()<< __FILE__ << __LINE__ << "td conn err.";
  259. return;
  260. }
  261. //开启一个线程轮询订阅的主题
  262. future = QtConcurrent::run(this, &TDengineClient::topicLoop);
  263. }