TThreadPollingF.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include "WindowMsgF.h"
  4. #include "AppGlobalF.h"
  5. #include "ITSUtilF.h"
  6. #include "CDSTrafficF.h"
  7. #include "CDSLinkF.h"
  8. #include "CDSIfscF.h"
  9. #include "CDSRoadF.h"
  10. #include "CDSRepeatCongestF.h"
  11. #include "CDSIncidentF.h"
  12. #include "CDSProcessF.h"
  13. #include "CDSFacilityF.h"
  14. #include "CDSVilgFrcsF.h"
  15. #include "CDSDatabaseF.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. if (g_AppCfg.bDebug) ITSUtil_Trace("Start");
  43. FreeOnTerminate = true;
  44. Priority = tpNormal;//tpIdle;//tpNormal
  45. g_AppCfg.thr.bRunning = true;
  46. m_lMainWinHandle = (HWND)g_AppCfg.lMainWinHandle;
  47. m_sDbConnString = "";
  48. m_sDbConnString += "Provider=" + g_AppCfg.itsdb.sProvider;
  49. m_sDbConnString += ";Password=" + g_AppCfg.itsdb.sPassword;
  50. m_sDbConnString += ";Persist Security Info=True";
  51. m_sDbConnString += ";User ID=" + g_AppCfg.itsdb.sUserName;
  52. m_sDbConnString += ";Data Source=" + g_AppCfg.itsdb.sServerName;
  53. m_pConnection = NULL;
  54. m_pConnection = new TADOConnection(NULL);
  55. }
  56. catch(...)
  57. {
  58. Terminate();
  59. }
  60. }
  61. //---------------------------------------------------------------------------
  62. __fastcall TThreadPolling::~TThreadPolling()
  63. {
  64. if (m_pConnection)
  65. {
  66. if (m_pConnection->Connected == true)
  67. {
  68. m_pConnection->Close();
  69. }
  70. delete m_pConnection;
  71. m_pConnection = NULL;
  72. }
  73. // ::CoUninitialize();
  74. }
  75. //---------------------------------------------------------------------------
  76. /*
  77. * 데이터베이스 연결
  78. * arguments
  79. *
  80. * return
  81. *
  82. */
  83. bool __fastcall TThreadPolling::DbConnect()
  84. {
  85. ::CoInitialize(NULL);
  86. try
  87. {
  88. m_pConnection->ConnectionString = m_sDbConnString;
  89. m_pConnection->KeepConnection = true;
  90. m_pConnection->LoginPrompt = false;
  91. m_pConnection->Open();
  92. }
  93. catch(EDatabaseError &E)
  94. {
  95. return false;
  96. }
  97. catch (Exception &e)
  98. {
  99. return false;
  100. }
  101. catch (...)
  102. {
  103. return false;
  104. }
  105. return true;
  106. }
  107. //---------------------------------------------------------------------------
  108. void __fastcall TThreadPolling::Execute()
  109. {
  110. ::CoInitialize(NULL);
  111. NameThreadForDebugging("PollingThread");
  112. MSG Msg;
  113. int nRes;
  114. if (g_AppCfg.bDebug) ITSUtil_Trace("Execute");
  115. while (!Terminated)
  116. {
  117. if (DbConnect())
  118. {
  119. break;
  120. }
  121. if (g_AppCfg.bAppClose)
  122. {
  123. return;
  124. }
  125. Sleep(1000);
  126. }
  127. while(!Terminated)
  128. {
  129. if (GetMessage(&Msg, NULL, 0, 0) == 0) {
  130. Terminate();
  131. }
  132. else {
  133. nRes = 0;
  134. try
  135. {
  136. if (Msg.message == WM_QUIT)
  137. {
  138. nRes = -999;
  139. break;
  140. }
  141. #if 0
  142. if (Msg.wParam == WM_QUIT)
  143. {
  144. nRes = -999;
  145. break;
  146. }
  147. #endif
  148. if (Msg.message != WM_THREAD) continue;
  149. if (!IsDbOpen())
  150. {
  151. DbConnect();
  152. }
  153. m_lMainWinHandle = (HWND)g_AppCfg.lMainWinHandle;
  154. switch (Msg.wParam)
  155. {
  156. case WP_PING:
  157. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_PING, WP_PING);
  158. break;
  159. case WP_PARAM_TRAFFIC:
  160. if (m_pConnection->Connected == false)
  161. {
  162. if (DbConnect() == false)
  163. {
  164. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, Msg.lParam);
  165. }
  166. }
  167. if (IsDbOpen())
  168. {
  169. switch(Msg.lParam)
  170. {
  171. case LP_MSG_20: SelTrafficData(Msg.lParam); break; //요거는 사용하지 않음......
  172. case LP_MSG_00: SelTrafficAllData(Msg.lParam); break;
  173. case LP_MSG_01: SelTrafficLinkData(Msg.lParam); break;
  174. case LP_MSG_02: SelTrafficServiceLinkData(Msg.lParam); break;
  175. case LP_MSG_03: SelTrafficRoadData(Msg.lParam); break;
  176. case LP_MSG_04: SelTrafficRrCongestData(Msg.lParam); break;
  177. case LP_MSG_05: SelTrafficUtisRunData(Msg.lParam); break;
  178. case LP_MSG_11: SelFacilityStatusData(Msg.lParam); break;
  179. case LP_MSG_12: SelProcessStatusData(Msg.lParam); break;
  180. case LP_MSG_13: SelIncidentData(Msg.lParam); break;
  181. case LP_MSG_14: SelFacilityData(Msg.lParam); break;
  182. case LP_MSG_09: SelProcessData(Msg.lParam); break;
  183. case LP_MSG_16: SelBlackBoxEventData(Msg.lParam); break;
  184. case LP_MSG_17: SelVilgFrcsData(Msg.lParam); break;
  185. }
  186. }
  187. break;
  188. }
  189. }
  190. catch(...)
  191. {
  192. //nRes = -999;
  193. }
  194. }
  195. if (nRes == -999) break;
  196. }
  197. // ::CoUninitialize();
  198. }
  199. //---------------------------------------------------------------------------
  200. bool __fastcall TThreadPolling::SelTrafficData(LPARAM LParam)
  201. {
  202. bool bRes = false;
  203. bRes = SelTrafficLinkData(LP_MSG_01);
  204. if (bRes == false) return false;
  205. bRes = SelTrafficServiceLinkData(LP_MSG_02);
  206. if (bRes == false) return false;
  207. bRes = SelTrafficRoadData(LP_MSG_03);
  208. if (bRes == false) return false;
  209. bRes = SelTrafficRrCongestData(LP_MSG_04);
  210. if (bRes == false) return false;
  211. bRes = SelTrafficUtisRunData(LP_MSG_05);
  212. return bRes;
  213. }
  214. //---------------------------------------------------------------------------
  215. bool __fastcall TThreadPolling::SelTrafficAllData(LPARAM LParam)
  216. {
  217. bool bRes = false;
  218. //링크소통정보(전체)
  219. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  220. bRes = ItsTrafficManager->LoadFromDb(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. return bRes;
  229. }
  230. //---------------------------------------------------------------------------
  231. bool __fastcall TThreadPolling::SelTrafficLinkData(LPARAM LParam)
  232. {
  233. bool bRes = false;
  234. //레벨1 소통정보
  235. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  236. bRes = ItsLinkManager->LoadTraffic(m_pConnection);
  237. if (bRes == true)
  238. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  239. else
  240. {
  241. m_pConnection->Close();
  242. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  243. }
  244. return bRes;
  245. }
  246. //---------------------------------------------------------------------------
  247. bool __fastcall TThreadPolling::SelTrafficServiceLinkData(LPARAM LParam)
  248. {
  249. bool bRes = false;
  250. //서비스링크 소통정보(레벨2)
  251. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  252. bRes = ItsIfscManager->LoadTraffic(m_pConnection);
  253. if (bRes == true)
  254. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  255. else
  256. {
  257. m_pConnection->Close();
  258. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  259. }
  260. return bRes;
  261. }
  262. //---------------------------------------------------------------------------
  263. bool __fastcall TThreadPolling::SelTrafficRoadData(LPARAM LParam)
  264. {
  265. bool bRes = false;
  266. //도로 소통정보(레벨3)
  267. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  268. bRes = ItsRoadManager->LoadTraffic(m_pConnection);
  269. if (bRes == true)
  270. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  271. else
  272. {
  273. m_pConnection->Close();
  274. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  275. }
  276. return bRes;
  277. }
  278. //---------------------------------------------------------------------------
  279. bool __fastcall TThreadPolling::SelTrafficRrCongestData(LPARAM LParam)
  280. {
  281. bool bRes = false;
  282. //반복정체구간
  283. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  284. bRes = ItsRepeatCongestManager->LoadFromDb(m_pConnection);
  285. //bRes = ItsRepeatCongestManager->LoadFromDbWthrCnfm(m_pConnection);
  286. if (bRes == true)
  287. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  288. else
  289. {
  290. m_pConnection->Close();
  291. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  292. }
  293. return bRes;
  294. }
  295. //---------------------------------------------------------------------------
  296. bool __fastcall TThreadPolling::SelTrafficUtisRunData(LPARAM LParam)
  297. {
  298. bool bRes = false;
  299. //UTIS 운영상태
  300. #ifdef USE_UTIS
  301. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  302. bRes = ItsUtisManager->LoadUtisStatusFromDb(m_pConnection);
  303. if (bRes == true)
  304. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  305. else
  306. {
  307. m_pConnection->Close();
  308. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  309. }
  310. #else
  311. bRes = true;
  312. bRes = ItsDatabaseManager->LoadFromDb(m_pConnection);
  313. if (bRes == true)
  314. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  315. else
  316. {
  317. m_pConnection->Close();
  318. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  319. }
  320. #endif
  321. return bRes;
  322. }
  323. //---------------------------------------------------------------------------
  324. bool __fastcall TThreadPolling::SelFacilityStatusData(LPARAM LParam)
  325. {
  326. bool bRes = false;
  327. //시설물 상태정보
  328. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  329. bRes = ItsFacilityManager->LoadFacilityStatusFromDb(m_pConnection);
  330. if (bRes == true)
  331. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  332. else
  333. {
  334. m_pConnection->Close();
  335. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  336. }
  337. return bRes;
  338. }
  339. //---------------------------------------------------------------------------
  340. bool __fastcall TThreadPolling::SelProcessStatusData(LPARAM LParam)
  341. {
  342. bool bRes = false;
  343. //프로세스 상태정보
  344. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  345. bRes = ItsProcessManager->LoadProcessStatusFromDb(m_pConnection);
  346. if (bRes == true)
  347. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  348. else
  349. {
  350. m_pConnection->Close();
  351. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  352. }
  353. return bRes;
  354. }
  355. //---------------------------------------------------------------------------
  356. bool __fastcall TThreadPolling::SelIncidentData(LPARAM LParam)
  357. {
  358. bool bRes = false;
  359. //돌발발생정보
  360. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  361. bRes = ItsIncidentManager->LoadFromDb(m_pConnection);
  362. if (bRes == false)
  363. {
  364. m_pConnection->Close();
  365. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  366. return false;
  367. }
  368. bRes = ItsIncidentManager->LoadFromAutoIncident(m_pConnection);
  369. if (bRes == true)
  370. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  371. else
  372. {
  373. m_pConnection->Close();
  374. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  375. }
  376. return bRes;
  377. }
  378. //---------------------------------------------------------------------------
  379. bool __fastcall TThreadPolling::SelFacilityData(LPARAM LParam)
  380. {
  381. bool bRes = false;
  382. //시설물정보 재로딩
  383. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  384. bRes = ItsFacilityManager->LoadFromDb(m_pConnection);
  385. if (bRes == false)
  386. {
  387. m_pConnection->Close();
  388. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  389. return false;
  390. }
  391. bRes = ItsFacilityManager->LoadFacilityStatusFromDb(m_pConnection);
  392. if (bRes == true)
  393. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  394. else
  395. {
  396. m_pConnection->Close();
  397. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  398. }
  399. return bRes;
  400. }
  401. //---------------------------------------------------------------------------
  402. bool __fastcall TThreadPolling::SelProcessData(LPARAM LParam)
  403. {
  404. bool bRes = false;
  405. //프로세스정보 재로딩
  406. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  407. bRes = ItsProcessManager->LoadFromDb(m_pConnection);
  408. if (bRes == false)
  409. {
  410. m_pConnection->Close();
  411. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  412. return false;
  413. }
  414. bRes = ItsProcessManager->LoadProcessStatusFromDb(m_pConnection);
  415. if (bRes == true)
  416. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  417. else
  418. {
  419. m_pConnection->Close();
  420. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  421. }
  422. return bRes;
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TThreadPolling::SelBlackBoxEventData(LPARAM LParam)
  426. {
  427. bool bRes = false;
  428. //블랙박스이벤트 재로딩
  429. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  430. bRes = ItsEventManager->LoadFromDb(m_pConnection);
  431. if (bRes == true)
  432. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  433. else
  434. {
  435. m_pConnection->Close();
  436. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  437. }
  438. return bRes;
  439. }
  440. //---------------------------------------------------------------------------
  441. bool __fastcall TThreadPolling::SelVilgFrcsData(LPARAM LParam)
  442. {
  443. bool bRes = false;
  444. //기상정보 조회
  445. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_START, LParam);
  446. bRes = ItsVilgFrcsManager->LoadVilgFrcsInfo(m_pConnection);
  447. if (bRes == true)
  448. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_OK, LParam);
  449. else
  450. {
  451. m_pConnection->Close();
  452. POST_MSG(m_lMainWinHandle, WM_THREAD, WP_DB_SELECT_ERROR, LParam);
  453. }
  454. return bRes;
  455. }
  456. //---------------------------------------------------------------------------