4b6beba143c65ad6cf17091e4a1b56706d1567cb.svn-base 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 where `CommId`=%14 and `BusAdd`=%15")
  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].COMM_IDX)
  72. .arg(ytShm->sPointList.sPoint[comm][addr].BUS_ADD));
  73. ytShm->sPointList.sPoint[comm][addr].UNSAVED = 0x00;
  74. }
  75. if(ytShm->spStatusList.spStatus[comm][addr].UNSAVED==0x01)
  76. {
  77. 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")
  78. .arg(ytShm->spStatusList.spStatus[comm][addr].REALTIME_VALUE)
  79. .arg(ytShm->spStatusList.spStatus[comm][addr].REALTIME_TIMESTAMP)
  80. .arg(ytShm->spStatusList.spStatus[comm][addr].ALARM_STATUS)
  81. .arg(ytShm->spStatusList.spStatus[comm][addr].ALARM_TIMESTAMP)
  82. .arg(ytShm->spStatusList.spStatus[comm][addr].ALARM_SENT?1:0)
  83. .arg(ytShm->spStatusList.spStatus[comm][addr].MAX_VALUE)
  84. .arg(ytShm->spStatusList.spStatus[comm][addr].MAX_TIMESTAMP)
  85. .arg(ytShm->spStatusList.spStatus[comm][addr].MIN_VALUE)
  86. .arg(ytShm->spStatusList.spStatus[comm][addr].MIN_TIMESTAMP)
  87. .arg(ytShm->spStatusList.spStatus[comm][addr].TOTAL_VALUE)
  88. .arg(ytShm->spStatusList.spStatus[comm][addr].AVE_VALUE)
  89. .arg(ytShm->spStatusList.spStatus[comm][addr].COUNT)
  90. .arg(comm).arg(addr));
  91. ytShm->spStatusList.spStatus[comm][addr].UNSAVED = 0x00;
  92. }
  93. }
  94. }
  95. if(sqlList.length()>0)
  96. {
  97. if(Db->transaction())
  98. {
  99. while(sqlList.length()>0)
  100. {
  101. emit dbg_log(QString("[%1] do sql: \"%2\"\r\n")
  102. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  103. .arg(sqlList.first()));
  104. Db->exec(sqlList.first());
  105. sqlList.removeFirst();
  106. usleep(10000);
  107. }
  108. Db->commit();
  109. }
  110. }
  111. else if(sqlList2.length()>0)
  112. {
  113. if(Db2->transaction())
  114. {
  115. while(sqlList2.length()>0)
  116. {
  117. emit dbg_log(QString("[%1] do sql: \"%2\"\r\n")
  118. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  119. .arg(sqlList2.first()));
  120. Db2->exec(sqlList2.first());
  121. sqlList2.removeFirst();
  122. usleep(10000);
  123. }
  124. Db2->commit();
  125. }
  126. }
  127. else if(sqlList3.length()>0)
  128. {
  129. if(Db3->transaction())
  130. {
  131. while(sqlList3.length()>0)
  132. {
  133. emit dbg_log(QString("[%1] do sql: \"%2\"\r\n")
  134. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  135. .arg(sqlList3.first()));
  136. Db3->exec(sqlList3.first());
  137. sqlList3.removeFirst();
  138. usleep(10000);
  139. }
  140. Db3->commit();
  141. }
  142. }
  143. usleep(500000);
  144. }
  145. }