databasethread.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #include "databasethread.h"
  2. //#include "waterMk01.h"
  3. #include "AlarmReport.h"
  4. #define HostName "47.98.201.187"
  5. #define HostPort 3306
  6. #define UserName "uskyIOT"
  7. #define PassWord "uskyIOT"
  8. #define DatabaseName "jdxf"
  9. #define sql_debug
  10. //extern MksensorList *mkList;
  11. DatabaseThread::DatabaseThread(QObject *parent) :
  12. QThread(parent)
  13. {
  14. c = NULL;
  15. keep = false;
  16. hour = 255;
  17. sqlList.clear();
  18. alarmRepList.clear();
  19. db = QSqlDatabase::addDatabase("QMYSQL","mksensor_db");
  20. db.setHostName(QString(HostName));
  21. db.setPort(HostPort);
  22. db.setUserName(QString(UserName));
  23. db.setPassword(QString(PassWord));
  24. db.setDatabaseName(QString(DatabaseName));
  25. db.open();
  26. mcdb = QSqlDatabase::addDatabase("QMYSQL","mksensor_sdb");
  27. mcdb.setHostName(QString("47.111.81.118"));
  28. mcdb.setPort(3306);
  29. mcdb.setUserName(QString("root"));
  30. mcdb.setPassword(QString("Yt2021"));
  31. mcdb.setDatabaseName(QString("jx_cover"));
  32. redisconnected=-1;
  33. // mcdb.open();
  34. }
  35. void DatabaseThread::appendSql(QString sql,int type)
  36. {
  37. if(type==1){
  38. sqlList.append(sql);
  39. }else if (type==2) {
  40. mcsqlList.append(sql);
  41. }
  42. }
  43. void DatabaseThread::appendAlarm(AlarmRep rep,int type)
  44. {
  45. if(type==1){
  46. alarmRepList.append(rep);
  47. }else if (type==2) {
  48. mcalarmRepList.append(rep);
  49. }
  50. }
  51. bool DatabaseThread::chktable(QString tbname)
  52. {
  53. QStringList tblist = db.tables();
  54. for(int i=0;i<tblist.size();i++){
  55. if(QString::compare(tblist.at(i),tbname)==0)
  56. return true;
  57. }
  58. return false;
  59. }
  60. void DatabaseThread::check_devicelist()
  61. {
  62. if(redisconnected==-1){
  63. redisconnected=redis_init();
  64. if(redisconnected==-1){
  65. printf("redis init failed\n");
  66. }
  67. redis_save(AUTH);
  68. redis_save(DBID);
  69. }
  70. printf("database thread check_devicelist()\n");
  71. uint nowTime = QDateTime::currentDateTime().toTime_t();
  72. hour=QTime::currentTime().hour();
  73. QString sql = QString("select owner_code, owner_name, unitinfo, owner_xh, company, dwtype from sp_owner where dwtype in (2,4,128) and owner_xh !=''");
  74. QSqlQuery qry = db.exec(sql);
  75. int nrow= 0;
  76. while(qry.next()){
  77. if(nrow>10239)
  78. break;
  79. // QString tbname = QString("sp_d%1").arg(qry.value(0).toString());
  80. // if(!chktable(tbname)){
  81. // db.exec(QString::fromUtf8("create table `%1` (`id` bigint(20) unsigned not null auto_increment primary key, `device_code` varchar(30) not null, `port` varchar(55) default null, `time` datetime default null, `status` varchar(50) default null, `address` varchar(50) default null, `ncmd` varchar(50) default null, `data1` varchar(50) default null, `data2` varchar(255) default null, `data3` varchar(255) default null, `data4` varchar(255) default null, `data5` varchar(255) default null )").arg(tbname));
  82. // db.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (null, '%1', %2, '状态', 1, '', '1970-01-01 08:00:00','')").arg(qry.value(0).toString()).arg(qry.value(5).toInt()));
  83. // db.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (null, '%1', %2, '剩余电量', 2, '', '1970-01-01 08:00:00','')").arg(qry.value(0).toString()).arg(qry.value(5).toInt()));
  84. // db.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (null, '%1', %2, '无线信号', 3, '', '1970-01-01 08:00:00','')").arg(qry.value(0).toString()).arg(qry.value(5).toInt()));
  85. // db.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (null, '%1', %2, '水压/水位', 4, '', '1970-01-01 08:00:00','')").arg(qry.value(0).toString()).arg(qry.value(5).toInt()));
  86. // }
  87. sql = QString("select true_status from sp_devices_status where deviceid='%1'")
  88. .arg(qry.value(0).toString());
  89. QSqlQuery qry2 = db.exec(sql);
  90. uint redistime = 0;
  91. //mkList->sensors[nrow].Enabled=0x01;
  92. if(qry2.next()){
  93. if(qry2.value(0).toUInt()==0){
  94. //mkList->sensors[nrow].LastCommtime = nowTime;
  95. redistime = nowTime;
  96. }
  97. }
  98. //sprintf(mkList->sensors[nrow].Device_Code,"%s",qry.value(0).toString().toUtf8().data());
  99. //sprintf(mkList->sensors[nrow].Device_Name,"%s",qry.value(1).toString().toUtf8().data());
  100. //sprintf(mkList->sensors[nrow].Device_Info,"%s",qry.value(2).toString().toUtf8().data());
  101. //sprintf(mkList->sensors[nrow].Device_Type,"%s",qry.value(5).toString().toUtf8().data());
  102. //sprintf(mkList->sensors[nrow].Company_Code,"%s",qry.value(4).toString().toUtf8().data());
  103. //printf("Device_Code:%s\n",QString(mkList->sensors[nrow].Device_Code).toUtf8().data());
  104. //printf("Device_Code:%s\n",QString(mkList->sensors[nrow].Company_Code).toUtf8().data());
  105. QString data = QString("hmset %1 row %2 devicecode %3 devicename %4 deviceinfo %5 devicetype %6 companycode %7 enabled %8 onlineflag %9 lastcommtime %10").arg(QString("%1%2").arg("sp_owner8-").arg(qry.value(0).toString().toUtf8().data())).arg(nrow).arg(qry.value(0).toString().toUtf8().data()).arg(qry.value(1).toString().toUtf8().data()).arg(qry.value(2).toString().toUtf8().data()).arg(qry.value(5).toString().toUtf8().data()).arg(qry.value(4).toString().toUtf8().data()).arg(QString("0x01")).arg(QString("0x00")).arg(redistime);
  106. redis_save(data.toUtf8());
  107. nrow++;
  108. }
  109. redis_save((QString("hmset tablelen sp_owner8 %1").arg(nrow)).toUtf8());
  110. redis_free();
  111. redisconnected = -1;
  112. // for(int i=nrow;i<10240;i++){
  113. // mkList->sensors[i].Enabled=0x00;
  114. // mkList->sensors[i].LastCommtime = 0;
  115. // }
  116. }
  117. void DatabaseThread::stop()
  118. {
  119. keep = false;
  120. }
  121. void DatabaseThread::run()
  122. {
  123. db.open();
  124. QString ip,port,name,passwd;
  125. QString mqttsql = QString("select ip, port, username, passwd from yt_dataprocessservice_mqtt where type = 'ytDP0008';");
  126. QSqlQuery mqttqry = db.exec(mqttsql);
  127. while(mqttqry.next())
  128. {
  129. ip = mqttqry.value(0).toString();
  130. port = mqttqry.value(1).toString();
  131. name = mqttqry.value(2).toString();
  132. passwd = mqttqry.value(3).toString();
  133. }
  134. emit mqttInfo(ip,port,name,passwd);
  135. mqttqry.clear();
  136. db.close();
  137. keep = true;
  138. printf("database thread start\n");
  139. while(keep){
  140. if(!db.isOpen()){
  141. db.open();
  142. }else{
  143. if(QTime::currentTime().hour()!=hour){
  144. check_devicelist();
  145. }
  146. if(alarmRepList.length()>0){
  147. while(alarmRepList.length()>0){
  148. AlarmRep rep = alarmRepList.first();
  149. QSqlQuery qry = db.exec(rep.Sql);
  150. #ifdef sql_debug
  151. emit SqlLog(QString("[ %1 sql] ").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz"))+rep.Sql);
  152. #endif
  153. quint64 lastid = qry.lastInsertId().toULongLong()&0xffffffffffffffff;
  154. if(lastid>0){
  155. emit AlarmReport(rep.DeviceId,lastid,rep.AlarmType,rep.AlarmTime);
  156. }
  157. alarmRepList.removeFirst();
  158. usleep(1000);
  159. }
  160. }
  161. if(sqlList.length()>0){
  162. db.exec(sqlList.first());
  163. #ifdef sql_debug
  164. emit SqlLog(QString("[ %1 sql ] ").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz"))+sqlList.first());
  165. #endif
  166. sqlList.removeFirst();
  167. }
  168. db.close();
  169. }
  170. usleep(10000);
  171. }
  172. if(db.isOpen())
  173. db.close();
  174. }
  175. int DatabaseThread::redis_init()
  176. {
  177. c = redisConnect(REDIS_HOST, REDIS_PORT);
  178. if (NULL == c || c->err) {
  179. if(c) {
  180. printf("Redis [%s:%d], Error:[%s]\n", REDIS_HOST, REDIS_PORT, c->errstr);
  181. redisFree(c);
  182. } else {
  183. printf("Redis [%s:%d] failure\n", REDIS_HOST, REDIS_PORT);
  184. }
  185. return -1;
  186. }
  187. return 0;
  188. }
  189. void DatabaseThread::redis_free()
  190. {
  191. if (c) {
  192. redisFree(c);
  193. }
  194. c = NULL;
  195. }
  196. int DatabaseThread::redis_save(const char *cmd)
  197. {
  198. int i = 0;
  199. redisReply *r = NULL;
  200. if (NULL == cmd) {
  201. return -1;
  202. }
  203. printf("%s, c %p\n", cmd,c);
  204. r = (redisReply *)redisCommand(c, cmd);
  205. if (NULL == r) {
  206. printf("Error[%d:%s]", c->err, c->errstr);
  207. return -1;
  208. }
  209. switch(r->type) {
  210. case REDIS_REPLY_STATUS:
  211. printf("type:%s, reply->len:%ld reply->str:%s\n", "REDIS_REPLY_STATUS", r->len, r->str);
  212. break;
  213. case REDIS_REPLY_ERROR:
  214. printf("type:%s, reply->len:%ld reply->str:%s\n", "REDIS_REPLY_ERROR", r->len, r->str);
  215. break;
  216. case REDIS_REPLY_INTEGER:
  217. printf("type:%s, reply->integer:%lld\n", "REDIS_REPLY_INTEGER", r->integer);
  218. break;
  219. case REDIS_REPLY_NIL:
  220. printf("type:%s, no data\n", "REDIS_REPLY_NIL");
  221. break;
  222. case REDIS_REPLY_STRING:
  223. printf("type:%s, reply->len:%ld reply->str:%s\n", "REDIS_REPLY_STRING", r->len, r->str);
  224. break;
  225. case REDIS_REPLY_ARRAY:
  226. printf("type:%s, reply->elements:%ld\n", "REDIS_REPLY_ARRAY", r->elements);
  227. for (i = 0; i < r->elements; i++) {
  228. printf("%d: %s\n", i, r->element[i]->str);
  229. }
  230. break;
  231. default:
  232. printf("unkonwn type:%d\n", r->type);
  233. break;
  234. }
  235. /*release reply and context */
  236. freeReplyObject(r);
  237. return 0;
  238. }
  239. QString DatabaseThread::redis_qstring(const char *cmd)
  240. {
  241. redisReply *r = NULL;
  242. if (NULL == cmd) {
  243. return "error";
  244. }
  245. printf("%s\n", cmd);
  246. r = (redisReply *)redisCommand(c, cmd);
  247. if (NULL == r) {
  248. printf("Error[%d:%s]", c->err, c->errstr);
  249. return "error";
  250. }
  251. switch(r->type) {
  252. case REDIS_REPLY_STATUS:
  253. printf("type:%s, reply->len:%ld reply->str:%s\n", "REDIS_REPLY_STATUS", r->len, r->str);
  254. break;
  255. case REDIS_REPLY_ERROR:
  256. printf("type:%s, reply->len:%ld reply->str:%s\n", "REDIS_REPLY_ERROR", r->len, r->str);
  257. break;
  258. case REDIS_REPLY_INTEGER:
  259. printf("type:%s, reply->integer:%lld\n", "REDIS_REPLY_INTEGER", r->integer);
  260. break;
  261. case REDIS_REPLY_NIL:
  262. printf("type:%s, no data\n", "REDIS_REPLY_NIL");
  263. break;
  264. case REDIS_REPLY_STRING:
  265. printf("type:%s, reply->len:%ld reply->str:%s\n", "REDIS_REPLY_STRING", r->len, r->str);
  266. break;
  267. case REDIS_REPLY_ARRAY:
  268. printf("type:%s, reply->elements:%ld\n", "REDIS_REPLY_ARRAY", r->elements);
  269. for (int i = 0; i < r->elements; i++) {
  270. printf("%d: %s\n", i, r->element[i]->str);
  271. }
  272. break;
  273. default:
  274. printf("unkonwn type:%d\n", r->type);
  275. break;
  276. }
  277. QString data = r->str;
  278. /*release reply and context */
  279. freeReplyObject(r);
  280. return data;
  281. }
  282. int DatabaseThread::redis_int(const char *cmd)
  283. {
  284. redisReply *r = NULL;
  285. if (NULL == cmd) {
  286. return -1;
  287. }
  288. printf("%s\n", cmd);
  289. r = (redisReply *)redisCommand(c, cmd);
  290. if (NULL == r) {
  291. printf("Error[%d:%s]", c->err, c->errstr);
  292. return -1;
  293. }
  294. switch(r->type) {
  295. case REDIS_REPLY_STATUS:
  296. printf("type:%s, reply->len:%ld reply->str:%s\n", "REDIS_REPLY_STATUS", r->len, r->str);
  297. break;
  298. case REDIS_REPLY_ERROR:
  299. printf("type:%s, reply->len:%ld reply->str:%s\n", "REDIS_REPLY_ERROR", r->len, r->str);
  300. break;
  301. case REDIS_REPLY_INTEGER:
  302. printf("type:%s, reply->integer:%lld\n", "REDIS_REPLY_INTEGER", r->integer);
  303. break;
  304. case REDIS_REPLY_NIL:
  305. printf("type:%s, no data\n", "REDIS_REPLY_NIL");
  306. break;
  307. case REDIS_REPLY_STRING:
  308. printf("type:%s, reply->len:%ld reply->str:%s\n", "REDIS_REPLY_STRING", r->len, r->str);
  309. break;
  310. case REDIS_REPLY_ARRAY:
  311. printf("type:%s, reply->elements:%ld\n", "REDIS_REPLY_ARRAY", r->elements);
  312. for (int i = 0; i < r->elements; i++) {
  313. printf("%d: %s\n", i, r->element[i]->str);
  314. }
  315. break;
  316. default:
  317. printf("unkonwn type:%d\n", r->type);
  318. break;
  319. }
  320. int data = r->integer;
  321. /*release reply and context */
  322. freeReplyObject(r);
  323. return data;
  324. }