123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include "VMSCommLibF.h"
- #pragma hdrstop
- #include "DbmReadThreadF.h"
- #include "CDSProcessF.h"
- #include "CDSManagerF.h"
- #include "CDSImageF.h"
- #include "CDSFormF.h"
- #include "CDSIfscF.h"
- #pragma package(smart_init)
- TDbmReadThread *DbmReadThread = NULL;
- //---------------------------------------------------------------------------
- TDbmReadThread::TDbmReadThread()
- {
- FPnlState = NULL;
- FTmrDb = NULL;
- }
- //---------------------------------------------------------------------------
- TDbmReadThread::~TDbmReadThread()
- {
- }
- //---------------------------------------------------------------------------
- int __fastcall TDbmReadThread::CloseDb()
- {
- try
- {
- if (FDb)
- {
- FDb->Close();
- }
- }
- catch(Exception &e)
- {
- }
- return 0;
- }
- //---------------------------------------------------------------------------
- void __fastcall TDbmReadThread::updateLedColor(TColor AColor)
- {
- try {
- if (FPnlState) {
- PostMessage((HWND)g_AppCfg.lMainWinHandle, WM_PANEL_REFRESH, 1, (LPARAM)AColor);
- }
- } catch(Exception &e) {}
- }
- //---------------------------------------------------------------------------
- DWORD __fastcall TDbmReadThread::Process(LPVOID AParam)
- {
- IPC_DBM_MESSAGE *pDbm;
- DWORD dwJob;
- AnsiString sSysTime;
- bool isDbOk;
- CoInitialize(NULL);
- FDb = new TDMAdoDb(NULL);
- if (g_AppCfg.IsDbConnectPerJob == false)
- {
- FDb->Connect(g_AppCfg.db.sConnectStr);
- }
- UINT nMsg = WM_DB_THREAD;
- while(!IsTerm() && !g_AppCfg.bThrExit)
- {
- updateLedColor(clGreen);
- if (!g_dbmReadQ.PopBlocking(dwJob))
- {
- continue;
- }
- if (dwJob == Q_CLOSE)
- {
- break;
- }
- else
- if (dwJob == Q_TICK)
- {
- updateLedColor(clRed);
- continue;
- }
- updateLedColor(clLime);
- pDbm = (IPC_DBM_MESSAGE*)dwJob;
- int nRes = 0;
- try
- {
- if (g_AppCfg.IsDbConnectPerJob == true)
- {
- FDb->Connect(g_AppCfg.db.sConnectStr);
- }
- if (FDb->IsDbConnected() == false)
- {
- if (!FDb->Connect(g_AppCfg.db.sConnectStr))
- {
- nRes = -1;
- FDb->SetDbState(false, nMsg);
- MERROR("DBMR=============> Database connect failed...");
- }
- }
- AnsiString dbJobName = APP_GetDbJobName(pDbm->Command);
- DWORD starDbJobTick = GetTickCount();
- if (pDbm->Command != dbm_save_ctlr_stts) {
- MINFO("DBMR=============> DBMS JOB START: %s", dbJobName.c_str());
- }
- if (nRes == 0)
- {
- if (FTmrDb) {
- FTmrDb->Tag = (int)pDbm->Command;
- }
- SendMessage((HWND)g_AppCfg.lMainWinHandle, nMsg, dbm_job_start, pDbm->Command);
- switch(pDbm->Command)
- {
- case dbm_save_unit_stts:
- nRes = CDSProcessManager->SaveUnitStts(FDb->GetConnection());
- break;
- //case dbm_save_unit_stts_hs:
- // nRes = CDSProcessManager->SaveUnitSttsHs(FDb->GetConnection());
- // break;
- case dbm_save_ctlr_stts:
- nRes = CDSCtlrManager->SaveCtlrStts(FDb->GetConnection(), pDbm->U.Data);
- break;
- case dbm_parma_res:
- nRes = CDSCtlrManager->SaveCtlrParam(FDb->GetConnection(), pDbm->U.Data);
- break;
- case dbm_save_ctlr_stts_all:
- nRes = CDSCtlrManager->SaveCtlrSttsAll(FDb->GetConnection());
- break;
- case dbm_load_onoff_time:
- nRes = CDSManager->LoadVmsOnOffTime(FDb->GetConnection());
- break;
- case dbm_load_download_job:
- nRes = CDSManager->LoadVmsDownLoadJob(FDb->GetConnection());
- break;
- case dbm_save_form_download:
- nRes = CDSManager->SaveVmsDownloadForm(FDb->GetConnection());
- break;
- case dbm_dnld_form_init:
- case dbm_dnld_form_cycle:
- case dbm_dnld_form_mode:
- /*
- * VMS 정보제공구간 교통정보를 생성한 후 VMS 메세지를 생성하는 경우
- */
- if (CDSVmsIfscTrafManager)
- {
- nRes = CDSVmsIfscTrafManager->CreateVmsIfscTraffic(FDb->GetConnection());
- }
- nRes = CDSManager->DownloadVmsForm(FDb->GetConnection(), pDbm->Command);
- break; // static_cycle, mode_change
- case dbm_provide_result:
- nRes = CDSManager->SaveProvideResult(FDb->GetConnection(), pDbm->U.Data);
- break;
- case dbm_master_symbol_image:
- case dbm_master_symbol_traffic:
- nRes = CDSImageManager->LoadVmsSymbolInfo(FDb->GetConnection());
- break;
- case dbm_master_form:
- nRes = CDSFormManager->LoadVmsFormInfo(FDb->GetConnection());
- break;
- case dbm_master_vms_ifsc:
- /*
- * VMS 정보제공구간 교통정보를 생성한 후 VMS 메세지를 생성하는 경우
- */
- if (CDSVmsIfscTrafManager)
- {
- nRes = CDSVmsIfscTrafManager->InitVmsIfscTraffic(FDb->GetConnection());
- }
- nRes = CDSIfscManager->LoadVmsIfscInfo(FDb->GetConnection());
- break;
- case dbm_check_db_alive:
- nRes = FDb->IsDbAlive(sSysTime);
- break;
- case dbm_initialize:
- nRes = app_initialize();
- break;
- default:
- MERROR("DBMR=============> Unknown DBMS Job Type: %d", pDbm->Command);
- break;
- }
- if (nRes < 0)
- {
- FDb->SetDbState(false, nMsg);
- FDb->Close();
- MERROR("DBMR=============> Database job error: %d", pDbm->Command);
- }
- else
- {
- FDb->SetDbState(true, nMsg);
- }
- }
- if (pDbm->Command != dbm_save_ctlr_stts) {
- MINFO("DBMR=============> DBMS JOB ..END: %s, %d, Elapsed: %u ms.", dbJobName.c_str(), nRes, GetTickCount() - starDbJobTick);
- }
- }
- __finally
- {
- if (g_AppCfg.IsDbConnectPerJob == true)
- {
- FDb->Close();
- }
- SendMessage((HWND)g_AppCfg.lMainWinHandle, nMsg, pDbm->Command, nRes);
- //SAFE_DELETE(pDbm);
- }
- }
- if (g_AppCfg.IsDbConnectPerJob == false)
- {
- FDb->Close();
- }
- SAFE_DELETE(FDb);
- Term();
- CoUninitialize();
- return 0;
- }
- //---------------------------------------------------------------------------
- int __fastcall TDbmReadThread::app_initialize()
- {
- #if 0
- MINFO("DCOM Process Information initialize...");
- if (DMManager->GetProcessInfo() != VERR_NONE)
- {
- MERROR("DCOM Process Information initialize failed. Program abnormal terminate.");
- return -1;
- }
- MINFO("DCOM VMS Information initialize...");
- if (DMManager->GeTCDSCtlrInfo() != VERR_NONE)
- {
- MERROR("DCOM VMS Information initialize failed. Program abnormal terminate.");
- return -2;
- }
- MINFO("DCOM VMS Scetion Information initialize...");
- if (DMManager->GetVmsIfscInfo() != VERR_NONE)
- {
- MERROR("DCOM VMS Scetion Information initialize failed. Program abnormal terminate.");
- return -3;
- }
- MINFO("DCOM VMS Form Information initialize...");
- if (DMManager->GetVmsImage() != VERR_NONE)
- {
- MERROR("DCOM VMS Form Information initialize failed. Program abnormal terminate.");
- return -4;
- }
- MINFO("DCOM VMS Form Information initialize...");
- if (DMManager->LoadVmsForm() != VERR_NONE)
- {
- MERROR("DCOM VMS Form Information initialize failed. Program abnormal terminate.");
- return -5;
- }
- #endif
- return 0;
- }
- //---------------------------------------------------------------------------
|