TThreadPollingF.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include "WindowMsgF.h"
  4. #include "AppGlobalF.h"
  5. #include "ITSUtilF.h"
  6. #include "CDSProcessF.h"
  7. #include "CDSCctvCtlrF.h"
  8. #include "CDSIncidentF.h"
  9. #pragma hdrstop
  10. #include "TThreadPollingF.h"
  11. #pragma package(smart_init)
  12. //---------------------------------------------------------------------------
  13. // Important: Methods and properties of objects in VCL can only be
  14. // used in a method called using Synchronize, for example:
  15. //
  16. // Synchronize(&UpdateCaption);
  17. //
  18. // where UpdateCaption could look like:
  19. //
  20. // void __fastcall TThreadPolling::UpdateCaption()
  21. // {
  22. // Form1->Caption = "Updated in a thread";
  23. // }
  24. //---------------------------------------------------------------------------
  25. __fastcall TThreadPolling::TThreadPolling(bool CreateSuspended)
  26. : TThread(CreateSuspended)
  27. {
  28. ::CoInitialize(NULL);
  29. try
  30. {
  31. //if (g_AppCfg.bDebug) ITSUtil_Trace("Start");
  32. FreeOnTerminate = true;
  33. Priority = tpNormal;//tpIdle;//tpNormal
  34. g_AppCfg.thr.bRunning = true;
  35. m_lMainWinHandle = (HWND)g_AppCfg.lMainWinHandle;
  36. m_sDbConnString = "";
  37. m_sDbConnString += "Provider=" + g_AppCfg.itsdb.sProvider;
  38. m_sDbConnString += ";Password=" + g_AppCfg.itsdb.sPassword;
  39. m_sDbConnString += ";Persist Security Info=True";
  40. m_sDbConnString += ";User ID=" + g_AppCfg.itsdb.sUserName;
  41. m_sDbConnString += ";Data Source=" + g_AppCfg.itsdb.sServerName;
  42. m_pConnection = NULL;
  43. m_pConnection = new TADOConnection(NULL);
  44. }
  45. catch(...)
  46. {
  47. Terminate();
  48. }
  49. }
  50. //---------------------------------------------------------------------------
  51. __fastcall TThreadPolling::~TThreadPolling()
  52. {
  53. if (m_pConnection)
  54. {
  55. if (m_pConnection->Connected == true)
  56. {
  57. m_pConnection->Close();
  58. }
  59. delete m_pConnection;
  60. m_pConnection = NULL;
  61. }
  62. // ::CoUninitialize();
  63. }
  64. //---------------------------------------------------------------------------
  65. /*
  66. * 데이터베이스 연결
  67. * arguments
  68. *
  69. * return
  70. *
  71. */
  72. bool __fastcall TThreadPolling::DbConnect()
  73. {
  74. ::CoInitialize(NULL);
  75. try
  76. {
  77. m_pConnection->ConnectionString = m_sDbConnString;
  78. m_pConnection->KeepConnection = true;
  79. m_pConnection->LoginPrompt = false;
  80. m_pConnection->Open();
  81. }
  82. catch(EDatabaseError &E)
  83. {
  84. return false;
  85. }
  86. catch (Exception &e)
  87. {
  88. return false;
  89. }
  90. catch (...)
  91. {
  92. return false;
  93. }
  94. return true;
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall TThreadPolling::Execute()
  98. {
  99. ::CoInitialize(NULL);
  100. NameThreadForDebugging("PollingThread");
  101. MSG Msg;
  102. int nRes;
  103. //if (g_AppCfg.bDebug) ITSUtil_Trace("Execute");
  104. while (!Terminated)
  105. {
  106. if (DbConnect())
  107. {
  108. break;
  109. }
  110. if (g_AppCfg.bAppClose)
  111. {
  112. return;
  113. }
  114. Sleep(1000);
  115. }
  116. while(!Terminated)
  117. {
  118. if (GetMessage(&Msg, NULL, 0, 0) == 0) {
  119. Terminate();
  120. }
  121. else {
  122. nRes = 0;
  123. try
  124. {
  125. if (Msg.message == WM_QUIT)
  126. {
  127. nRes = -999;
  128. break;
  129. }
  130. if (Msg.message != WM_THREAD) continue;
  131. if (!IsDbOpen())
  132. {
  133. DbConnect();
  134. }
  135. m_lMainWinHandle = (HWND)g_AppCfg.lMainWinHandle;
  136. switch (Msg.wParam)
  137. {
  138. case WP_PING:
  139. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_PING, WP_PING);
  140. break;
  141. case WP_PARAM_TRAFFIC:
  142. switch(Msg.lParam)
  143. {
  144. case LP_MSG_00: SelProcessStatusData(Msg.lParam); break;
  145. case LP_MSG_01: SelCctvStatusData(Msg.lParam); break;
  146. case LP_MSG_05: SelIncidentData(Msg.lParam); break;
  147. }
  148. break;
  149. }
  150. }
  151. catch(...)
  152. {
  153. //nRes = -999;
  154. }
  155. }
  156. if (nRes == -999) break;
  157. }
  158. // ::CoUninitialize();
  159. }
  160. //---------------------------------------------------------------------------
  161. bool __fastcall TThreadPolling::SelCctvStatusData(LPARAM LParam)
  162. {
  163. bool bRes = false;
  164. //시설물(VMS) 상태정보
  165. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  166. bRes = CctvCtlrManager->LoadStatusFromDb(m_pConnection);
  167. if (bRes)
  168. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  169. else
  170. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  171. return bRes;
  172. }
  173. //---------------------------------------------------------------------------
  174. bool __fastcall TThreadPolling::SelProcessStatusData(LPARAM LParam)
  175. {
  176. bool bRes = false;
  177. //프로세스 상태정보
  178. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  179. bRes = ItsProcessManager->LoadProcessStatusFromDb(m_pConnection);
  180. if (bRes)
  181. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  182. else
  183. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  184. return bRes;
  185. }
  186. //---------------------------------------------------------------------------
  187. bool __fastcall TThreadPolling::SelIncidentData(LPARAM LParam)
  188. {
  189. bool bRes = false;
  190. if (ItsIncidentManager)
  191. {
  192. //돌발발생정보
  193. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  194. bRes = ItsIncidentManager->LoadFromDb(m_pConnection);
  195. //bRes = ItsIncidentManager->LoadFromAutoIncident(m_pConnection);
  196. }
  197. else
  198. {
  199. bRes = true;
  200. }
  201. if (bRes)
  202. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  203. else
  204. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  205. return bRes;
  206. }
  207. //---------------------------------------------------------------------------