TThreadPollingF.cpp 8.0 KB

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