DbmThreadF.cpp 8.9 KB

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