DbmReadThreadF.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include "VMSCommLibF.h"
  4. #pragma hdrstop
  5. #include "DbmReadThreadF.h"
  6. #include "CDSProcessF.h"
  7. #include "CDSManagerF.h"
  8. #include "CDSImageF.h"
  9. #include "CDSFormF.h"
  10. #include "CDSIfscF.h"
  11. #pragma package(smart_init)
  12. TDbmReadThread *DbmReadThread = NULL;
  13. //---------------------------------------------------------------------------
  14. TDbmReadThread::TDbmReadThread()
  15. {
  16. FPnlState = NULL;
  17. FTmrDb = NULL;
  18. }
  19. //---------------------------------------------------------------------------
  20. TDbmReadThread::~TDbmReadThread()
  21. {
  22. }
  23. //---------------------------------------------------------------------------
  24. int __fastcall TDbmReadThread::CloseDb()
  25. {
  26. try
  27. {
  28. if (FDb)
  29. {
  30. FDb->Close();
  31. }
  32. }
  33. catch(Exception &e)
  34. {
  35. }
  36. return 0;
  37. }
  38. //---------------------------------------------------------------------------
  39. void __fastcall TDbmReadThread::updateLedColor(TColor AColor)
  40. {
  41. try {
  42. if (FPnlState) {
  43. PostMessage((HWND)g_AppCfg.lMainWinHandle, WM_PANEL_REFRESH, 1, (LPARAM)AColor);
  44. }
  45. } catch(Exception &e) {}
  46. }
  47. //---------------------------------------------------------------------------
  48. DWORD __fastcall TDbmReadThread::Process(LPVOID AParam)
  49. {
  50. IPC_DBM_MESSAGE *pDbm;
  51. DWORD dwJob;
  52. AnsiString sSysTime;
  53. bool isDbOk;
  54. CoInitialize(NULL);
  55. FDb = new TDMAdoDb(NULL);
  56. if (g_AppCfg.IsDbConnectPerJob == false)
  57. {
  58. FDb->Connect(g_AppCfg.db.sConnectStr);
  59. }
  60. UINT nMsg = WM_DB_THREAD;
  61. while(!IsTerm() && !g_AppCfg.bThrExit)
  62. {
  63. updateLedColor(clGreen);
  64. if (!g_dbmReadQ.PopBlocking(dwJob))
  65. {
  66. continue;
  67. }
  68. if (dwJob == Q_CLOSE)
  69. {
  70. break;
  71. }
  72. else
  73. if (dwJob == Q_TICK)
  74. {
  75. updateLedColor(clRed);
  76. continue;
  77. }
  78. updateLedColor(clLime);
  79. pDbm = (IPC_DBM_MESSAGE*)dwJob;
  80. int nRes = 0;
  81. try
  82. {
  83. if (g_AppCfg.IsDbConnectPerJob == true)
  84. {
  85. FDb->Connect(g_AppCfg.db.sConnectStr);
  86. }
  87. if (FDb->IsDbConnected() == false)
  88. {
  89. if (!FDb->Connect(g_AppCfg.db.sConnectStr))
  90. {
  91. nRes = -1;
  92. FDb->SetDbState(false, nMsg);
  93. MERROR("DBMR=============> Database connect failed...");
  94. }
  95. }
  96. AnsiString dbJobName = APP_GetDbJobName(pDbm->Command);
  97. DWORD starDbJobTick = GetTickCount();
  98. if (pDbm->Command != dbm_save_ctlr_stts) {
  99. MINFO("DBMR=============> DBMS JOB START: %s", dbJobName.c_str());
  100. }
  101. if (nRes == 0)
  102. {
  103. if (FTmrDb) {
  104. FTmrDb->Tag = (int)pDbm->Command;
  105. }
  106. SendMessage((HWND)g_AppCfg.lMainWinHandle, nMsg, dbm_job_start, pDbm->Command);
  107. switch(pDbm->Command)
  108. {
  109. case dbm_save_unit_stts:
  110. nRes = CDSProcessManager->SaveUnitStts(FDb->GetConnection());
  111. break;
  112. //case dbm_save_unit_stts_hs:
  113. // nRes = CDSProcessManager->SaveUnitSttsHs(FDb->GetConnection());
  114. // break;
  115. case dbm_save_ctlr_stts:
  116. nRes = CDSCtlrManager->SaveCtlrStts(FDb->GetConnection(), pDbm->U.Data);
  117. break;
  118. case dbm_parma_res:
  119. nRes = CDSCtlrManager->SaveCtlrParam(FDb->GetConnection(), pDbm->U.Data);
  120. break;
  121. case dbm_save_ctlr_stts_all:
  122. nRes = CDSCtlrManager->SaveCtlrSttsAll(FDb->GetConnection());
  123. break;
  124. case dbm_load_onoff_time:
  125. nRes = CDSManager->LoadVmsOnOffTime(FDb->GetConnection());
  126. break;
  127. case dbm_load_download_job:
  128. nRes = CDSManager->LoadVmsDownLoadJob(FDb->GetConnection());
  129. break;
  130. case dbm_save_form_download:
  131. nRes = CDSManager->SaveVmsDownloadForm(FDb->GetConnection());
  132. break;
  133. case dbm_dnld_form_init:
  134. case dbm_dnld_form_cycle:
  135. case dbm_dnld_form_mode:
  136. /*
  137. * VMS 정보제공구간 교통정보를 생성한 후 VMS 메세지를 생성하는 경우
  138. */
  139. if (CDSVmsIfscTrafManager)
  140. {
  141. nRes = CDSVmsIfscTrafManager->CreateVmsIfscTraffic(FDb->GetConnection());
  142. }
  143. nRes = CDSManager->DownloadVmsForm(FDb->GetConnection(), pDbm->Command);
  144. break; // static_cycle, mode_change
  145. case dbm_provide_result:
  146. nRes = CDSManager->SaveProvideResult(FDb->GetConnection(), pDbm->U.Data);
  147. break;
  148. case dbm_master_symbol_image:
  149. case dbm_master_symbol_traffic:
  150. nRes = CDSImageManager->LoadVmsSymbolInfo(FDb->GetConnection());
  151. break;
  152. case dbm_master_form:
  153. nRes = CDSFormManager->LoadVmsFormInfo(FDb->GetConnection());
  154. break;
  155. case dbm_master_vms_ifsc:
  156. /*
  157. * VMS 정보제공구간 교통정보를 생성한 후 VMS 메세지를 생성하는 경우
  158. */
  159. if (CDSVmsIfscTrafManager)
  160. {
  161. nRes = CDSVmsIfscTrafManager->InitVmsIfscTraffic(FDb->GetConnection());
  162. }
  163. nRes = CDSIfscManager->LoadVmsIfscInfo(FDb->GetConnection());
  164. break;
  165. case dbm_check_db_alive:
  166. nRes = FDb->IsDbAlive(sSysTime);
  167. break;
  168. case dbm_initialize:
  169. nRes = app_initialize();
  170. break;
  171. default:
  172. MERROR("DBMR=============> Unknown DBMS Job Type: %d", pDbm->Command);
  173. break;
  174. }
  175. if (nRes < 0)
  176. {
  177. FDb->SetDbState(false, nMsg);
  178. FDb->Close();
  179. MERROR("DBMR=============> Database job error: %d", pDbm->Command);
  180. }
  181. else
  182. {
  183. FDb->SetDbState(true, nMsg);
  184. }
  185. }
  186. if (pDbm->Command != dbm_save_ctlr_stts) {
  187. MINFO("DBMR=============> DBMS JOB ..END: %s, %d, Elapsed: %u ms.", dbJobName.c_str(), nRes, GetTickCount() - starDbJobTick);
  188. }
  189. }
  190. __finally
  191. {
  192. if (g_AppCfg.IsDbConnectPerJob == true)
  193. {
  194. FDb->Close();
  195. }
  196. SendMessage((HWND)g_AppCfg.lMainWinHandle, nMsg, pDbm->Command, nRes);
  197. //SAFE_DELETE(pDbm);
  198. }
  199. }
  200. if (g_AppCfg.IsDbConnectPerJob == false)
  201. {
  202. FDb->Close();
  203. }
  204. SAFE_DELETE(FDb);
  205. Term();
  206. CoUninitialize();
  207. return 0;
  208. }
  209. //---------------------------------------------------------------------------
  210. int __fastcall TDbmReadThread::app_initialize()
  211. {
  212. #if 0
  213. MINFO("DCOM Process Information initialize...");
  214. if (DMManager->GetProcessInfo() != VERR_NONE)
  215. {
  216. MERROR("DCOM Process Information initialize failed. Program abnormal terminate.");
  217. return -1;
  218. }
  219. MINFO("DCOM VMS Information initialize...");
  220. if (DMManager->GeTCDSCtlrInfo() != VERR_NONE)
  221. {
  222. MERROR("DCOM VMS Information initialize failed. Program abnormal terminate.");
  223. return -2;
  224. }
  225. MINFO("DCOM VMS Scetion Information initialize...");
  226. if (DMManager->GetVmsIfscInfo() != VERR_NONE)
  227. {
  228. MERROR("DCOM VMS Scetion Information initialize failed. Program abnormal terminate.");
  229. return -3;
  230. }
  231. MINFO("DCOM VMS Form Information initialize...");
  232. if (DMManager->GetVmsImage() != VERR_NONE)
  233. {
  234. MERROR("DCOM VMS Form Information initialize failed. Program abnormal terminate.");
  235. return -4;
  236. }
  237. MINFO("DCOM VMS Form Information initialize...");
  238. if (DMManager->LoadVmsForm() != VERR_NONE)
  239. {
  240. MERROR("DCOM VMS Form Information initialize failed. Program abnormal terminate.");
  241. return -5;
  242. }
  243. #endif
  244. return 0;
  245. }
  246. //---------------------------------------------------------------------------