dbthread.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "dbthread.h"
  2. #include "yt_unit_shm.h"
  3. extern YT_UNIT_SHM *ytShm;
  4. DBThread::DBThread(QObject *parent, QSqlDatabase *db, QSqlDatabase *db3) :
  5. QThread(parent)
  6. {
  7. Db = db;
  8. Db3 = db3;
  9. sqlList.clear();
  10. sqlList3.clear();
  11. }
  12. void DBThread::appendSQL(quint8 sqlid, QString sql)
  13. {
  14. if(sqlid==1)
  15. sqlList.append(sql);
  16. else if(sqlid==3)
  17. sqlList3.append(sql);
  18. }
  19. void DBThread::run()
  20. {
  21. ytShm->dogTimeList.dogTime[DB_THREAD].IDX = DB_THREAD;
  22. ytShm->dogTimeList.dogTime[DB_THREAD].WAITSEC = 30;
  23. ytShm->dogTimeList.dogTime[DB_THREAD].ENABLED = 0x01;
  24. emit log(QString("[%1] db thread start\r\n")
  25. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")));
  26. while(1)
  27. {
  28. ytShm->dogTimeList.dogTime[DB_THREAD].LASTFEED = QDateTime::currentDateTime().toTime_t();
  29. for(int comm=0; comm<4; comm++)
  30. {
  31. if(ytShm->commList.comm[comm].UNSAVED==0x01)
  32. {
  33. appendSQL(1,QString("update `yt_t_comm` set `CommType`=%1, `CommPath`='%2', Enabled=%3 where Id=%4")
  34. .arg(ytShm->commList.comm[comm].CommType)
  35. .arg(QString(ytShm->commList.comm[comm].PATH))
  36. .arg(ytShm->commList.comm[comm].ENABLED)
  37. .arg(ytShm->commList.comm[comm].IDX));
  38. ytShm->commList.comm[comm].UNSAVED = 0x00;
  39. }
  40. for(int addr=0; addr<256; addr++)
  41. {
  42. if(ytShm->sPointList.sPoint[comm][addr].UNSAVED==0x01)
  43. {
  44. 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")
  45. .arg(ytShm->sPointList.sPoint[comm][addr].ENABLED)
  46. .arg(QString(ytShm->sPointList.sPoint[comm][addr].NAME))
  47. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_TYPE)
  48. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_ALARM)
  49. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_VALUE)
  50. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_DLY)
  51. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_AD)
  52. .arg(ytShm->sPointList.sPoint[comm][addr].OFFSET)
  53. .arg(ytShm->sPointList.sPoint[comm][addr].OFFSET_FLAG)
  54. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_MAX)
  55. .arg(ytShm->sPointList.sPoint[comm][addr].PNO_MIN)
  56. .arg(ytShm->sPointList.sPoint[comm][addr].ALARM_H)
  57. .arg(ytShm->sPointList.sPoint[comm][addr].ALARM_L)
  58. .arg(ytShm->sPointList.sPoint[comm][addr].SUB_TYPE)
  59. .arg(ytShm->sPointList.sPoint[comm][addr].COMM_IDX)
  60. .arg(ytShm->sPointList.sPoint[comm][addr].BUS_ADD));
  61. ytShm->sPointList.sPoint[comm][addr].UNSAVED = 0x00;
  62. }
  63. }
  64. }
  65. if(sqlList.length()>0)
  66. {
  67. if(Db->transaction())
  68. {
  69. while(sqlList.length()>0)
  70. {
  71. emit dbg_log(QString("[%1] do sql: \"%2\"\r\n")
  72. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  73. .arg(sqlList.first()));
  74. Db->exec(sqlList.first());
  75. sqlList.removeFirst();
  76. usleep(10000);
  77. }
  78. Db->commit();
  79. }
  80. }
  81. else if(sqlList3.length()>0)
  82. {
  83. if(Db3->transaction())
  84. {
  85. while(sqlList3.length()>0)
  86. {
  87. emit dbg_log(QString("[%1] do sql: \"%2\"\r\n")
  88. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  89. .arg(sqlList3.first()));
  90. printf("%s\n",sqlList3.first().toUtf8().data());
  91. Db3->exec(sqlList3.first());
  92. sqlList3.removeFirst();
  93. usleep(10000);
  94. }
  95. Db3->commit();
  96. }
  97. }
  98. usleep(500000);
  99. }
  100. }