TThreadPollingF.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include "WindowMsgF.h"
  4. #include "AppGlobalF.h"
  5. #include "ITSUtilF.h"
  6. #include "CDSLinkF.h"
  7. #include "CDSIfscF.h"
  8. #include "CDSRoadF.h"
  9. #include "CDSRepeatCongestF.h"
  10. #include "CDSIncidentF.h"
  11. #include "CDSProcessF.h"
  12. #include "CDSFacilityF.h"
  13. #include "CDSVilgFrcsF.h"
  14. #include "CDSDatabaseF.h"
  15. #include "ITS_OPLibF.h"
  16. #ifdef USE_UTIS
  17. #include "CDSUtisF.h"
  18. #endif
  19. #pragma hdrstop
  20. #include "TThreadPollingF.h"
  21. #pragma warning(disable:8004)
  22. #pragma package(smart_init)
  23. //---------------------------------------------------------------------------
  24. // Important: Methods and properties of objects in VCL can only be
  25. // used in a method called using Synchronize, for example:
  26. //
  27. // Synchronize(&UpdateCaption);
  28. //
  29. // where UpdateCaption could look like:
  30. //
  31. // void __fastcall TThreadPolling::UpdateCaption()
  32. // {
  33. // Form1->Caption = "Updated in a thread";
  34. // }
  35. //---------------------------------------------------------------------------
  36. __fastcall TThreadPolling::TThreadPolling(bool CreateSuspended)
  37. : TThread(CreateSuspended)
  38. {
  39. ::CoInitialize(NULL);
  40. try
  41. {
  42. FreeOnTerminate = true;
  43. Priority = tpNormal;//tpIdle;//tpNormal
  44. g_AppCfg.thr.bRunning = true;
  45. m_lMainWinHandle = (HWND)g_AppCfg.lMainWinHandle;
  46. m_sDbConnString = "";
  47. if (g_AppCfg.itsdb.sConnectStr == "")
  48. {
  49. m_sDbConnString += "Provider=" + g_AppCfg.itsdb.sProvider;
  50. m_sDbConnString += ";Password=" + g_AppCfg.itsdb.sPassword;
  51. m_sDbConnString += ";Persist Security Info=True";
  52. m_sDbConnString += ";User ID=" + g_AppCfg.itsdb.sUserName;
  53. m_sDbConnString += ";Data Source=" + g_AppCfg.itsdb.sServerName;
  54. }
  55. else
  56. {
  57. m_sDbConnString = g_AppCfg.itsdb.sConnectStr;
  58. }
  59. m_pConnection = NULL;
  60. m_pConnection = new TADOConnection(NULL);
  61. }
  62. catch(...)
  63. {
  64. Terminate();
  65. }
  66. }
  67. //---------------------------------------------------------------------------
  68. __fastcall TThreadPolling::~TThreadPolling()
  69. {
  70. if (m_pConnection)
  71. {
  72. if (m_pConnection->Connected == true)
  73. {
  74. m_pConnection->Close();
  75. }
  76. delete m_pConnection;
  77. m_pConnection = NULL;
  78. }
  79. // ::CoUninitialize();
  80. }
  81. //---------------------------------------------------------------------------
  82. /*
  83. * 데이터베이스 연결
  84. * arguments
  85. *
  86. * return
  87. *
  88. */
  89. bool __fastcall TThreadPolling::DbConnect()
  90. {
  91. ::CoInitialize(NULL);
  92. try
  93. {
  94. m_pConnection->ConnectionString = m_sDbConnString;
  95. m_pConnection->KeepConnection = true;
  96. m_pConnection->LoginPrompt = false;
  97. m_pConnection->Open();
  98. }
  99. catch(EDatabaseError &E)
  100. {
  101. return false;
  102. }
  103. catch (Exception &e)
  104. {
  105. return false;
  106. }
  107. catch (...)
  108. {
  109. return false;
  110. }
  111. return true;
  112. }
  113. //---------------------------------------------------------------------------
  114. void __fastcall TThreadPolling::Execute()
  115. {
  116. ::CoInitialize(NULL);
  117. NameThreadForDebugging("PollingThread");
  118. MSG Msg;
  119. int nRes;
  120. while (!Terminated)
  121. {
  122. if (DbConnect())
  123. {
  124. break;
  125. }
  126. if (g_AppCfg.bAppClose)
  127. {
  128. return;
  129. }
  130. Sleep(1000);
  131. }
  132. while(!Terminated)
  133. {
  134. if (GetMessage(&Msg, NULL, 0, 0) == 0) {
  135. Terminate();
  136. }
  137. else {
  138. nRes = 0;
  139. try
  140. {
  141. if (Msg.message == WM_QUIT)
  142. {
  143. nRes = -999;
  144. break;
  145. }
  146. if (Msg.message != WM_THREAD) continue;
  147. if (!IsDbOpen())
  148. {
  149. DbConnect();
  150. }
  151. m_lMainWinHandle = (HWND)g_AppCfg.lMainWinHandle;
  152. switch (Msg.wParam)
  153. {
  154. case WP_PING:
  155. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_PING, WP_PING);
  156. break;
  157. case WP_PARAM_TRAFFIC:
  158. if (m_pConnection->Connected == false)
  159. {
  160. if (DbConnect() == false)
  161. {
  162. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, Msg.lParam);
  163. }
  164. }
  165. if (IsDbOpen())
  166. {
  167. switch(Msg.lParam)
  168. {
  169. case LP_MSG_TRAFFIC: SelTrafficAllData(Msg.lParam); break;
  170. case LP_MSG_FACILITY_STTS: SelFacilityStatusData(Msg.lParam); break;
  171. case LP_MSG_PROCESS_STTS: SelProcessStatusData(Msg.lParam); break;
  172. case LP_MSG_INCIDENT: SelIncidentData(Msg.lParam); break;
  173. case LP_MSG_DATABASE_STTS: SelDatabaseSttsData(Msg.lParam); break;
  174. case LP_MSG_05: SelTrafficUtisRunData(Msg.lParam); break;
  175. case LP_MSG_14: SelFacilityData(Msg.lParam); break;
  176. case LP_MSG_09: SelProcessData(Msg.lParam); break;
  177. case LP_MSG_16: SelBlackBoxEventData(Msg.lParam); break;
  178. case LP_MSG_WEATHER: SelVilgFrcsData(Msg.lParam); break;
  179. }
  180. }
  181. break;
  182. }
  183. }
  184. catch(...)
  185. {
  186. //nRes = -999;
  187. }
  188. }
  189. if (nRes == -999) break;
  190. }
  191. // ::CoUninitialize();
  192. }
  193. //---------------------------------------------------------------------------
  194. bool __fastcall TThreadPolling::SelTrafficAllData(LPARAM LParam)
  195. {
  196. bool bRes = false;
  197. //링크소통정보(전체)
  198. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  199. bRes = ItsLinkManager->LoadTraffic(m_pConnection);
  200. bRes = ItsIfscManager->LoadTraffic(m_pConnection);
  201. bRes = ItsRoadManager->LoadTraffic(m_pConnection);
  202. bRes = ItsRepeatCongestManager->LoadFromDb(m_pConnection);
  203. //bRes = ItsLinkManager->LoadClctSystStts(m_pConnection);
  204. if (bRes == true)
  205. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  206. else
  207. {
  208. m_pConnection->Close();
  209. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  210. }
  211. return bRes;
  212. }
  213. //---------------------------------------------------------------------------
  214. bool __fastcall TThreadPolling::SelTrafficUtisRunData(LPARAM LParam)
  215. {
  216. bool bRes = false;
  217. //UTIS 운영상태
  218. #ifdef USE_UTIS
  219. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  220. bRes = ItsUtisManager->LoadUtisStatusFromDb(m_pConnection);
  221. if (bRes == true)
  222. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  223. else
  224. {
  225. m_pConnection->Close();
  226. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  227. }
  228. #else
  229. bRes = true;
  230. #endif
  231. return bRes;
  232. }
  233. //---------------------------------------------------------------------------
  234. bool __fastcall TThreadPolling::SelDatabaseSttsData(LPARAM LParam)
  235. {
  236. bool bRes = false;
  237. //데이터베이스 상태
  238. bRes = ItsDatabaseManager->LoadFromDb(m_pConnection);
  239. if (bRes == true)
  240. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  241. else
  242. {
  243. m_pConnection->Close();
  244. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  245. }
  246. return bRes;
  247. }
  248. //---------------------------------------------------------------------------
  249. bool __fastcall TThreadPolling::SelFacilityStatusData(LPARAM LParam)
  250. {
  251. bool bRes = false;
  252. //시설물 상태정보
  253. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  254. bRes = ItsFacilityManager->LoadFacilityStatusFromDb(m_pConnection);
  255. if (bRes == true)
  256. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  257. else
  258. {
  259. m_pConnection->Close();
  260. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  261. }
  262. return bRes;
  263. }
  264. //---------------------------------------------------------------------------
  265. bool __fastcall TThreadPolling::SelProcessStatusData(LPARAM LParam)
  266. {
  267. bool bRes = false;
  268. //프로세스 상태정보
  269. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  270. bRes = ItsProcessManager->LoadProcessStatusFromDb(m_pConnection);
  271. if (bRes == true)
  272. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  273. else
  274. {
  275. m_pConnection->Close();
  276. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  277. }
  278. return bRes;
  279. }
  280. //---------------------------------------------------------------------------
  281. bool __fastcall TThreadPolling::SelIncidentData(LPARAM LParam)
  282. {
  283. bool bRes = false;
  284. //돌발발생정보
  285. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  286. bRes = ItsIncidentManager->LoadFromDb(m_pConnection);
  287. if (bRes == false)
  288. {
  289. m_pConnection->Close();
  290. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  291. return false;
  292. }
  293. #if 0
  294. bRes = ItsIncidentManager->LoadFromAutoIncident(m_pConnection);
  295. #endif
  296. if (bRes == true)
  297. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  298. else
  299. {
  300. m_pConnection->Close();
  301. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  302. }
  303. return bRes;
  304. }
  305. //---------------------------------------------------------------------------
  306. bool __fastcall TThreadPolling::SelFacilityData(LPARAM LParam)
  307. {
  308. bool bRes = false;
  309. //시설물정보 재로딩
  310. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  311. bRes = ItsFacilityManager->LoadFromDb(m_pConnection);
  312. if (bRes == false)
  313. {
  314. m_pConnection->Close();
  315. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  316. return false;
  317. }
  318. bRes = ItsFacilityManager->LoadFacilityStatusFromDb(m_pConnection);
  319. if (bRes == true)
  320. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  321. else
  322. {
  323. m_pConnection->Close();
  324. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  325. }
  326. return bRes;
  327. }
  328. //---------------------------------------------------------------------------
  329. bool __fastcall TThreadPolling::SelProcessData(LPARAM LParam)
  330. {
  331. bool bRes = false;
  332. //프로세스정보 재로딩
  333. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  334. bRes = ItsProcessManager->LoadFromDb(m_pConnection);
  335. if (bRes == false)
  336. {
  337. m_pConnection->Close();
  338. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  339. return false;
  340. }
  341. bRes = ItsProcessManager->LoadProcessStatusFromDb(m_pConnection);
  342. if (bRes == true)
  343. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  344. else
  345. {
  346. m_pConnection->Close();
  347. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  348. }
  349. return bRes;
  350. }
  351. //---------------------------------------------------------------------------
  352. bool __fastcall TThreadPolling::SelBlackBoxEventData(LPARAM LParam)
  353. {
  354. bool bRes = false;
  355. #if 0
  356. //블랙박스이벤트 재로딩
  357. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  358. bRes = ItsEventManager->LoadFromDb(m_pConnection);
  359. if (bRes == true)
  360. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  361. else
  362. {
  363. m_pConnection->Close();
  364. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  365. }
  366. #endif
  367. return bRes;
  368. }
  369. //---------------------------------------------------------------------------
  370. bool __fastcall TThreadPolling::SelVilgFrcsData(LPARAM LParam)
  371. {
  372. bool bRes = true;
  373. #if defined(USE_VILG) || defined(USE_ATMP)
  374. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  375. #ifdef USE_VILG
  376. //기상정보 조회
  377. bRes = ItsVilgFrcsManager->LoadVilgFrcsInfo(m_pConnection);
  378. #endif
  379. #ifdef USE_ATMP
  380. //기상정보 조회
  381. bRes = ItsVilgFrcsManager->LoadAtmpPltnInfo(m_pConnection);
  382. #endif
  383. if (bRes == true)
  384. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  385. else
  386. {
  387. m_pConnection->Close();
  388. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  389. }
  390. #endif
  391. return bRes;
  392. }
  393. //---------------------------------------------------------------------------