DbmThreadF.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. DWORD __fastcall TDbmThread::Process(LPVOID AParam)
  40. {
  41. IPC_DBM_MESSAGE *pDbm;
  42. DWORD dwJob;
  43. AnsiString sSysTime;
  44. bool isDbOk;
  45. CoInitialize(NULL);
  46. TPanel *pState = FPnlState;
  47. FDb = new TDMAdoDb(NULL);
  48. if (g_AppCfg.IsDbConnectPerJob == false)
  49. {
  50. FDb->Connect(g_AppCfg.db.sConnectStr);
  51. }
  52. while(!IsTerm() && !g_AppCfg.bThrExit)
  53. {
  54. try {
  55. if (pState) pState->Color = pState->Color == clLime ? clGreen : clRed;
  56. }catch(Exception &e) {}
  57. if (!g_dbmQ.PopBlocking(dwJob))
  58. {
  59. continue;
  60. }
  61. if (dwJob == Q_CLOSE)
  62. {
  63. break;
  64. }
  65. else
  66. if (dwJob == Q_TICK)
  67. {
  68. try {
  69. if (pState) pState->Color = pState->Color == clRed ? clGreen : clRed;
  70. }catch(Exception &e) {}
  71. continue;
  72. }
  73. if (pState) pState->Color = clLime;
  74. pDbm = (IPC_DBM_MESSAGE*)dwJob;
  75. int nRes = 0;
  76. try
  77. {
  78. if (g_AppCfg.IsDbConnectPerJob == true)
  79. {
  80. FDb->Connect(g_AppCfg.db.sConnectStr);
  81. }
  82. if (FDb->IsDbConnected() == false)
  83. {
  84. if (!FDb->Connect(g_AppCfg.db.sConnectStr))
  85. {
  86. nRes = -1;
  87. FDb->SetDbState(false);
  88. MERROR("Database connect failed...");
  89. }
  90. }
  91. if (nRes == 0)
  92. {
  93. if (FTmrDb) FTmrDb->Tag = (int)pDbm->Command;
  94. SendMessage((HWND)g_AppCfg.lMainWinHandle, WM_DB_THREAD, dbm_job_start, 0);
  95. switch(pDbm->Command)
  96. {
  97. case dbm_save_unit_stts:
  98. nRes = CDSProcessManager->SaveUnitStts(FDb->GetConnection());
  99. break;
  100. //case dbm_save_unit_stts_hs:
  101. // nRes = CDSProcessManager->SaveUnitSttsHs(FDb->GetConnection());
  102. // break;
  103. case dbm_save_ctlr_stts:
  104. nRes = CDSCtlrManager->SaveCtlrStts(FDb->GetConnection(), pDbm->U.Data);
  105. break;
  106. case dbm_parma_res:
  107. nRes = CDSCtlrManager->SaveCtlrParam(FDb->GetConnection(), pDbm->U.Data);
  108. break;
  109. case dbm_save_ctlr_stts_all:
  110. nRes = CDSCtlrManager->SaveCtlrSttsAll(FDb->GetConnection());
  111. break;
  112. case dbm_load_onoff_time:
  113. nRes = CDSManager->LoadVmsOnOffTime(FDb->GetConnection());
  114. break;
  115. case dbm_load_download_job:
  116. nRes = CDSManager->LoadVmsDownLoadJob(FDb->GetConnection());
  117. break;
  118. case dbm_save_form_download:
  119. nRes = CDSManager->SaveVmsDownloadForm(FDb->GetConnection());
  120. break;
  121. case dbm_dnld_form_init:
  122. case dbm_dnld_form_cycle:
  123. case dbm_dnld_form_mode:
  124. /*
  125. * VMS 정보제공구간 교통정보를 생성한 후 VMS 메세지를 생성하는 경우
  126. */
  127. if (CDSVmsIfscTrafManager)
  128. {
  129. nRes = CDSVmsIfscTrafManager->CreateVmsIfscTraffic(FDb->GetConnection());
  130. }
  131. nRes = CDSManager->DownloadVmsForm(FDb->GetConnection(), pDbm->Command);
  132. break; // static_cycle, mode_change
  133. case dbm_provide_result:
  134. nRes = CDSManager->SaveProvideResult(FDb->GetConnection(), pDbm->U.Data);
  135. break;
  136. case dbm_master_symbol_image:
  137. case dbm_master_symbol_traffic:
  138. nRes = CDSImageManager->LoadVmsSymbolInfo(FDb->GetConnection());
  139. break;
  140. case dbm_master_form:
  141. nRes = CDSFormManager->LoadVmsFormInfo(FDb->GetConnection());
  142. break;
  143. case dbm_master_vms_ifsc:
  144. /*
  145. * VMS 정보제공구간 교통정보를 생성한 후 VMS 메세지를 생성하는 경우
  146. */
  147. if (CDSVmsIfscTrafManager)
  148. {
  149. nRes = CDSVmsIfscTrafManager->InitVmsIfscTraffic(FDb->GetConnection());
  150. }
  151. nRes = CDSIfscManager->LoadVmsIfscInfo(FDb->GetConnection());
  152. break;
  153. case dbm_check_db_alive:
  154. nRes = FDb->IsDbAlive(sSysTime);
  155. break;
  156. case dbm_initialize:
  157. nRes = app_initialize();
  158. break;
  159. default: break;
  160. }
  161. if (nRes < 0)
  162. {
  163. FDb->SetDbState(false);
  164. FDb->Close();
  165. MERROR("Database job error: %d", pDbm->Command);
  166. }
  167. else
  168. {
  169. FDb->SetDbState(true);
  170. }
  171. }
  172. }
  173. __finally
  174. {
  175. if (g_AppCfg.IsDbConnectPerJob == true)
  176. {
  177. FDb->Close();
  178. }
  179. SendMessage((HWND)g_AppCfg.lMainWinHandle, WM_DB_THREAD, pDbm->Command, nRes);
  180. //SAFE_DELETE(pDbm);
  181. }
  182. }
  183. if (g_AppCfg.IsDbConnectPerJob == false)
  184. {
  185. FDb->Close();
  186. }
  187. SAFE_DELETE(FDb);
  188. Term();
  189. CoUninitialize();
  190. return 0;
  191. }
  192. //---------------------------------------------------------------------------
  193. int __fastcall TDbmThread::app_initialize()
  194. {
  195. #if 0
  196. MINFO("DCOM Process Information initialize...");
  197. if (DMManager->GetProcessInfo() != VERR_NONE)
  198. {
  199. MERROR("DCOM Process Information initialize failed. Program abnormal terminate.");
  200. return -1;
  201. }
  202. MINFO("DCOM VMS Information initialize...");
  203. if (DMManager->GeTCDSCtlrInfo() != VERR_NONE)
  204. {
  205. MERROR("DCOM VMS Information initialize failed. Program abnormal terminate.");
  206. return -2;
  207. }
  208. MINFO("DCOM VMS Scetion Information initialize...");
  209. if (DMManager->GetVmsIfscInfo() != VERR_NONE)
  210. {
  211. MERROR("DCOM VMS Scetion Information initialize failed. Program abnormal terminate.");
  212. return -3;
  213. }
  214. MINFO("DCOM VMS Form Information initialize...");
  215. if (DMManager->GetVmsImage() != VERR_NONE)
  216. {
  217. MERROR("DCOM VMS Form Information initialize failed. Program abnormal terminate.");
  218. return -4;
  219. }
  220. MINFO("DCOM VMS Form Information initialize...");
  221. if (DMManager->LoadVmsForm() != VERR_NONE)
  222. {
  223. MERROR("DCOM VMS Form Information initialize failed. Program abnormal terminate.");
  224. return -5;
  225. }
  226. #endif
  227. return 0;
  228. }
  229. //---------------------------------------------------------------------------