TThreadPollingF.cpp 12 KB

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