TThreadPollingF.cpp 8.2 KB

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