DbmThreadF.cpp 8.5 KB

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