a836b9a120782005358c5ae7eef3718d27745acf.svn-base 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "dbthread.h"
  2. #include "yt_unit_shm.h"
  3. extern YT_UNIT_SHM *ytShm;
  4. DBThread::DBThread(QObject *parent, QSqlDatabase *db, QSqlDatabase *db2, QSqlDatabase *db3) :
  5. QThread(parent)
  6. {
  7. Db = db;
  8. Db2 = db2;
  9. Db3 = db3;
  10. sqlList.clear();
  11. sqlList2.clear();
  12. sqlList3.clear();
  13. }
  14. void DBThread::appendSQL(quint8 sqlid, QString sql)
  15. {
  16. if(sqlid==1)
  17. sqlList.append(sql);
  18. else if(sqlid==2)
  19. sqlList2.append(sql);
  20. else if(sqlid==3)
  21. sqlList3.append(sql);
  22. }
  23. void DBThread::run()
  24. {
  25. ytShm->dogTimeList.dogTime[DB_THREAD].IDX = DB_THREAD;
  26. ytShm->dogTimeList.dogTime[DB_THREAD].WAITSEC = 30;
  27. ytShm->dogTimeList.dogTime[DB_THREAD].ENABLED = 0x01;
  28. emit log(QString("[%1] db thread start\r\n")
  29. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")));
  30. while(1)
  31. {
  32. ytShm->dogTimeList.dogTime[DB_THREAD].LASTFEED = QDateTime::currentDateTime().toTime_t();
  33. for(int comm=0; comm<4; comm++)
  34. {
  35. if(ytShm->commList.comm[comm].UNSAVED==0x01)
  36. {
  37. appendSQL(1,QString("update `yt_t_comm` set `CommType`=%1, `CommPath`='%2', Enabled=%3 where Id=%4")
  38. .arg(ytShm->commList.comm[comm].CommType)
  39. .arg(QString(ytShm->commList.comm[comm].PATH))
  40. .arg(ytShm->commList.comm[comm].ENABLED)
  41. .arg(ytShm->commList.comm[comm].IDX));
  42. ytShm->commList.comm[comm].UNSAVED = 0x00;
  43. }
  44. if(ytShm->commStatusList.commStatus[comm].UNSAVED==0x01)
  45. {
  46. appendSQL(2,QString("update `yt_sta_comm` set `Status`=%1, Updatetime=%2, Sent=%3 where CommId=%4")
  47. .arg(ytShm->commStatusList.commStatus[comm].STATUS)
  48. .arg(ytShm->commStatusList.commStatus[comm].TIMESTAMP)
  49. .arg(ytShm->commStatusList.commStatus[comm].SENT?1:0)
  50. .arg(comm));
  51. ytShm->commStatusList.commStatus[comm].UNSAVED = 0x00;
  52. }
  53. for(int addr=0; addr<256; addr++)
  54. {
  55. if(ytShm->sPointList.sPoint[comm][addr].UNSAVED==0x01)
  56. {
  57. appendSQL(1,QString("update `yt_t_point` set `Enabled`=%1, `Name`='%2', `PnoType`=%3, `PnoAlarm`=%4, `PnoValue`=%5, `PnoDly`=%6, `PnoAd`=%7, `Offset`=%8, `OffsetFlag`=%9, `PnoMax`=%10, `PnoMin`=%11, `AlarmH`=%12, `AlarmL`=%13, `SubType`=%14 where `CommId`=%15 and `BusAdd`=%16")
  58. .arg(ytShm->sPointList.sPoint[comm][addr].ENABLED)
  59. .arg(QString(ytShm->sPointList.sPoint[comm][addr].NAME))
  60. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_TYPE)
  61. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_ALARM)
  62. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_VALUE)
  63. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_DLY)
  64. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_AD)
  65. .arg(ytShm->sPointList.sPoint[comm][addr].OFFSET)
  66. .arg(ytShm->sPointList.sPoint[comm][addr].OFFSET_FLAG)
  67. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_MAX)
  68. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_MIN)
  69. .arg(ytShm->sPointList.sPoint[comm][addr].ALARM_H)
  70. .arg(ytShm->sPointList.sPoint[comm][addr].ALARM_L)
  71. .arg(ytShm->sPointList.sPoint[comm][addr].SUB_TYPE)
  72. .arg(ytShm->sPointList.sPoint[comm][addr].COMM_IDX)
  73. .arg(ytShm->sPointList.sPoint[comm][addr].BUS_ADD));
  74. ytShm->sPointList.sPoint[comm][addr].UNSAVED = 0x00;
  75. }
  76. if(ytShm->spStatusList.spStatus[comm][addr].UNSAVED==0x01)
  77. {
  78. appendSQL(2,QString("update `yt_sta_pno` set `RealtimeValue`=%1, `RealtimeTimestamp`=%2, `AlarmStatus`=%3, `AlarmTimestamp`=%4, `AlarmSent`=%5, `MaxValue`=%6, `MaxTimestamp`=%7, `MinValue`=%8, `MinTimestamp`=%9, `TotalValue`=%10, `AveValue`=%11, `Count`=%12 where `CommId`=%13 and `BusAdd`=%14")
  79. .arg(ytShm->spStatusList.spStatus[comm][addr].REALTIME_VALUE)
  80. .arg(ytShm->spStatusList.spStatus[comm][addr].REALTIME_TIMESTAMP)
  81. .arg(ytShm->spStatusList.spStatus[comm][addr].ALARM_STATUS)
  82. .arg(ytShm->spStatusList.spStatus[comm][addr].ALARM_TIMESTAMP)
  83. .arg(ytShm->spStatusList.spStatus[comm][addr].ALARM_SENT)
  84. .arg(ytShm->spStatusList.spStatus[comm][addr].MAX_VALUE)
  85. .arg(ytShm->spStatusList.spStatus[comm][addr].MAX_TIMESTAMP)
  86. .arg(ytShm->spStatusList.spStatus[comm][addr].MIN_VALUE)
  87. .arg(ytShm->spStatusList.spStatus[comm][addr].MIN_TIMESTAMP)
  88. .arg(ytShm->spStatusList.spStatus[comm][addr].TOTAL_VALUE)
  89. .arg(ytShm->spStatusList.spStatus[comm][addr].AVE_VALUE)
  90. .arg(ytShm->spStatusList.spStatus[comm][addr].COUNT)
  91. .arg(comm).arg(addr));
  92. ytShm->spStatusList.spStatus[comm][addr].UNSAVED = 0x00;
  93. }
  94. }
  95. }
  96. if(sqlList.length()>0)
  97. {
  98. if(Db->transaction())
  99. {
  100. while(sqlList.length()>0)
  101. {
  102. emit dbg_log(QString("[%1] do sql: \"%2\"\r\n")
  103. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  104. .arg(sqlList.first()));
  105. Db->exec(sqlList.first());
  106. sqlList.removeFirst();
  107. usleep(10000);
  108. }
  109. Db->commit();
  110. }
  111. }
  112. else if(sqlList2.length()>0)
  113. {
  114. if(Db2->transaction())
  115. {
  116. while(sqlList2.length()>0)
  117. {
  118. emit dbg_log(QString("[%1] do sql: \"%2\"\r\n")
  119. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  120. .arg(sqlList2.first()));
  121. Db2->exec(sqlList2.first());
  122. sqlList2.removeFirst();
  123. usleep(10000);
  124. }
  125. Db2->commit();
  126. }
  127. }
  128. else if(sqlList3.length()>0)
  129. {
  130. if(Db3->transaction())
  131. {
  132. while(sqlList3.length()>0)
  133. {
  134. emit dbg_log(QString("[%1] do sql: \"%2\"\r\n")
  135. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  136. .arg(sqlList3.first()));
  137. printf("%s\n",sqlList3.first().toUtf8().data());
  138. Db3->exec(sqlList3.first());
  139. sqlList3.removeFirst();
  140. usleep(10000);
  141. }
  142. Db3->commit();
  143. }
  144. }
  145. usleep(500000);
  146. }
  147. }