FrmMainF.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  1. //---------------------------------------------------------------------------
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <DateUtils.hpp>
  4. #include "ITSNetF.h"
  5. #include <vcl.h>
  6. #pragma hdrstop
  7. #include <psapi.h>
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include <time.h>
  11. //---------------------------------------------------------------------------
  12. #include "AppGlobalF.h"
  13. #include "VmsUtilsF.h"
  14. #include "FrmMainF.h"
  15. #include "MainCommF.h"
  16. #include "FrmOptionF.h"
  17. #include "FrmSysLogF.h"
  18. #include "FrmCommLogF.h"
  19. #include "FrmControllerInfoF.h"
  20. #include "FrmNetworkF.h"
  21. #include "LogThreadF.h"
  22. #include "DbmThreadF.h"
  23. #include "JobThreadF.h"
  24. #include "DMAdoDbF.h"
  25. #include "PacketHandllingF.h"
  26. #include "ClientSessionManagerF.h"
  27. //---------------------------------------------------------------------------
  28. #include "CDSProcessF.h"
  29. #include "CDSCtlrF.h"
  30. #include "CDSImageF.h"
  31. #include "CDSFormF.h"
  32. #include "CDSIfscF.h"
  33. #include "CDSFontF.h"
  34. #include "CDSColorF.h"
  35. #include "CDSAtmpF.h"
  36. #include "CDSSafeF.h"
  37. #include "CDSParkF.h"
  38. #include "CDSManagerF.h"
  39. //---------------------------------------------------------------------------
  40. #pragma package(smart_init)
  41. #pragma link "IdAntiFreeze"
  42. #pragma link "IdAntiFreezeBase"
  43. #pragma link "IdBaseComponent"
  44. #pragma link "IdComponent"
  45. #pragma link "IdUDPBase"
  46. #pragma link "IdUDPServer"
  47. #pragma resource "*.dfm"
  48. TItsMap<int, TItsThread*> G_Threads;
  49. typedef std::map<int, TItsThread*>::iterator thrIterator;
  50. TVmsCommMain *VmsCommMain;
  51. //---------------------------------------------------------------------------
  52. __fastcall TVmsCommMain::TVmsCommMain(TComponent* Owner)
  53. : TForm(Owner)
  54. {
  55. Application->OnException = OnAppException;
  56. ::CoInitialize(NULL);
  57. FServerRun = false;
  58. FTickState = 0;
  59. g_AppCfg.bAppLoad = false;
  60. g_AppCfg.bAppClose = false;
  61. Caption = " " + g_AppCfg.sTitle + " - " + Now().FormatString("yyyy-MM-dd hh:nn:ss");
  62. ClientSessionManager = new TClientSessionManager();
  63. CDSProcessManager = new TCDSProcessManager();
  64. CDSCtlrManager = new TCDSCtlrManager();
  65. CDSFormManager = new TCDSFormManager();
  66. CDSImageManager = new TCDSImageManager();
  67. CDSIfscManager = new TCDSIfscManager();
  68. CDSFontManager = new TCDSFontManager();
  69. CDSAtmpManager = new TCDSAtmpManager();
  70. #if 0
  71. CDSSafeManager = new TCDSSafeManager();
  72. #else
  73. CDSSafeManager = NULL;
  74. #endif
  75. #if 1
  76. CDSParkManager = new TCDSParkManager();
  77. #else
  78. CDSParkManager = NULL;
  79. #endif
  80. CDSManager = new TCDSManager();
  81. #if 0
  82. //VMS 소통정보를 가공해햐 하는 경우
  83. CDSVmsIfscTrafManager = new TCDSVmsIfscTrafManager();
  84. #else
  85. CDSVmsIfscTrafManager = NULL;
  86. #endif
  87. FCurrMin = StrToInt(Now().FormatString("nn"));;
  88. FCur1MinComm = FCurrMin;
  89. FCur1MinRun = FCurrMin;
  90. FCur5Min = FCurrMin;
  91. FCurrHour = -1;
  92. FCurrSec = -1;
  93. FTimeSyncDay = -1;
  94. FTimeSyncMin = -1;
  95. FUnitSttsHsMin = -1;
  96. FCtlrSttsHsMin = -1;
  97. FProvideRemain = -1;
  98. FCtlrSttsReqMin= -1;
  99. FOneMinCycle = false;
  100. FFirstRunning = true;
  101. m_dtChkDbAive = Now();
  102. FrmCommLog = new TFrmCommLog(Application);
  103. FrmNetwork = new TFrmNetwork(Application);
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TVmsCommMain::OnAppException(TObject *Sender, Exception *exception)
  107. {
  108. AnsiString sClassName = (NULL != Sender) ? AnsiString(Sender->ClassName()) : AnsiString("Unknown_Class");
  109. AnsiString sErrCls = (NULL != exception) ? AnsiString(exception->ClassName()) : AnsiString("Unknown_Error");
  110. AnsiString sErrMsg = (NULL != exception) ? AnsiString(exception->Message) : AnsiString("Unknown_Error");
  111. if (g_AppCfg.bAppClose)
  112. {
  113. LOGERROR("Program Close Exception Terminated. %s.%s.%s", sClassName.c_str(), sErrCls.c_str(), sErrMsg.c_str());
  114. }
  115. else
  116. {
  117. LOGERROR("Program Exception Terminated. %s.%s.%s", sClassName.c_str(), sErrCls.c_str(), sErrMsg.c_str());
  118. }
  119. Close();
  120. }
  121. //---------------------------------------------------------------------------
  122. void __fastcall TVmsCommMain::FormCreate(TObject *Sender)
  123. {
  124. Application->ShowMainForm = false;
  125. try {
  126. Application->Icon->LoadFromResourceName(((unsigned int)HInstance), "MAINICON");
  127. } catch(...) { ShowMessage("LoadFromResourceName failed"); }
  128. g_AppCfg.lMainWinHandle = (long)Handle;
  129. FrmSysLog = new TFrmSysLog(pnlLog);
  130. FrmSysLog->Parent = pnlLog;
  131. FrmSysLog->Show();
  132. FrmControllerInfo = new TFrmControllerInfo(pnlController);
  133. FrmControllerInfo->Parent = pnlController;
  134. FrmControllerInfo->Show();
  135. char szLogMsg[MAX_LOG_BUFFER];
  136. if (!g_logQ.Create("gmvmsLog", MAX_LOGQ_BUFF))
  137. {
  138. sprintf(szLogMsg, "Log Q Create failed. Program abnormal terminate.");
  139. AbnormalTerminate(szLogMsg);
  140. Close();
  141. Close();
  142. }
  143. if (!g_dbmQ.Create("gmvmsDbm", MAX_DBMQ_BUFF))
  144. {
  145. sprintf(szLogMsg, "Dbm Q Create failed. Program abnormal terminate.");
  146. AbnormalTerminate(szLogMsg);
  147. Close();
  148. Close();
  149. }
  150. if (!g_jobQ.Create("gmvmsJob", MAX_JOBQ_BUFF))
  151. {
  152. sprintf(szLogMsg, "Job Q Create failed. Program abnormal terminate.");
  153. AbnormalTerminate(szLogMsg);
  154. Close();
  155. Close();
  156. }
  157. Application->ShowMainForm = true;
  158. }
  159. //---------------------------------------------------------------------------
  160. void __fastcall TVmsCommMain::FormShow(TObject *Sender)
  161. {
  162. FProvideRemain = SecondOfTheDay(Now());
  163. FProvideRemain %= g_AppCfg.nTrafficCycleTime;
  164. FProvideRemain = g_AppCfg.nTrafficCycleTime - FProvideRemain + g_AppCfg.nTrafficBasisTime;
  165. CheckResource((g_AppCfg.MaxMemory * 1024), g_AppCfg.MaxHandle, g_AppCfg.MaxThread, g_AppCfg.MaxGdi);
  166. Refresh();
  167. tmrAppState->Enabled = true;
  168. }
  169. //---------------------------------------------------------------------------
  170. void __fastcall TVmsCommMain::FormCloseQuery(TObject *Sender, bool &CanClose)
  171. {
  172. if (!g_AppCfg.bAppClose)
  173. {
  174. String strMsg;
  175. strMsg = Caption + "\r\n\r\nAre you sure you want to exit the program?\r\nIf the Process Manager is running the program will be restarted automatically!!!";
  176. //if (Application->MessageBox(strMsg.c_str(), L"Confirm program exit", MB_YESNO|MB_ICONQUESTION) == IDYES)
  177. {
  178. FServerRun = false;
  179. g_AppCfg.bAppClose = true;
  180. }
  181. CanClose = false;
  182. }
  183. }
  184. //---------------------------------------------------------------------------
  185. void __fastcall TVmsCommMain::FormClose(TObject *Sender, TCloseAction &Action)
  186. {
  187. CommClose();
  188. ::CoUninitialize();
  189. Action = caFree;
  190. VmsCommMain = NULL;
  191. }
  192. //---------------------------------------------------------------------------
  193. void __fastcall TVmsCommMain::CommClose()
  194. {
  195. try
  196. {
  197. g_AppCfg.bAppClose = true;
  198. APP_WriteConfigInfo("LOG", "LOGDAY", Now().FormatString("dd"), g_AppCfg.sConfigFile);
  199. if (IdUDPCenterServer->Active == true)
  200. {
  201. IdUDPCenterServer->Active = false;
  202. for (int ii = 0; ii < IdUDPCenterServer->Bindings->Count; ii++)
  203. {
  204. IdUDPCenterServer->Bindings->Items[ii]->Free();
  205. }
  206. }
  207. #if 0
  208. if (FrmCommLog) FrmCommLog->Close();
  209. if (FrmNetwork)
  210. {
  211. FrmNetwork->Stop();
  212. FrmNetwork->Close();
  213. }
  214. #endif
  215. }
  216. catch(Exception &e)
  217. {
  218. }
  219. }
  220. //---------------------------------------------------------------------------
  221. void __fastcall TVmsCommMain::AbnormalTerminate(char* AMsg)
  222. {
  223. for (int ii = 0; ii < 5; ii++)
  224. {
  225. ITSLog->LogWrite(NULL, eLOG_ERROR, AMsg);
  226. if (FrmSysLog != NULL) FrmSysLog->reMsg->Lines->Add(Now().FormatString("hh:nn:ss") + " " + String(AMsg));
  227. Refresh();
  228. Application->ProcessMessages();
  229. Sleep(1000);
  230. }
  231. }
  232. //---------------------------------------------------------------------------
  233. bool __fastcall TVmsCommMain::InitApplication()
  234. {
  235. int nResult;
  236. String sTitle, sMsg;
  237. char szLogMsg[MAX_LOG_BUFFER];
  238. Application->ProcessMessages();
  239. if (JobThreadStart() == false)
  240. {
  241. memset(szLogMsg, 0x00, sizeof(szLogMsg));
  242. sprintf(szLogMsg, "Job Thread Create failed. Program abnormal terminate.");
  243. AbnormalTerminate(szLogMsg);
  244. return false;
  245. }
  246. MINFO("MAIN Program initialize start..."); // Q 생성후 메시지 출력함
  247. TDMAdoDb *FDb = new TDMAdoDb(NULL);
  248. try
  249. {
  250. MINFO("MAIN Database connect...");
  251. if (!FDb->Connect(g_AppCfg.db.sConnectStr))
  252. {
  253. memset(szLogMsg, 0x00, sizeof(szLogMsg));
  254. sprintf(szLogMsg, "MAIN Database connect failed. Program abnormal terminate.");
  255. AbnormalTerminate(szLogMsg);
  256. return false;
  257. }
  258. if (CDSProcessManager)
  259. {
  260. MINFO("MAIN Process Information initialize...");
  261. if (CDSProcessManager->LoadUnitInfo(FDb->GetConnection()) < 0)
  262. {
  263. memset(szLogMsg, 0x00, sizeof(szLogMsg));
  264. sprintf(szLogMsg, "MAIN Process Information initialize failed. Program abnormal terminate.");
  265. AbnormalTerminate(szLogMsg);
  266. return false;
  267. }
  268. }
  269. if (CDSCtlrManager)
  270. {
  271. MINFO("MAIN Controller Information initialize...");
  272. if (CDSCtlrManager->LoadCtlrInfo(FDb->GetConnection()) < 0)
  273. {
  274. memset(szLogMsg, 0x00, sizeof(szLogMsg));
  275. sprintf(szLogMsg, "MAIN Controller Information initialize failed. Program abnormal terminate.");
  276. AbnormalTerminate(szLogMsg);
  277. return false;
  278. }
  279. }
  280. if (CDSFontManager)
  281. {
  282. MINFO("MAIN VMS Font Information initialize...");
  283. if (CDSFontManager->LoadFontInfo(FDb->GetConnection()) < 0)
  284. {
  285. memset(szLogMsg, 0x00, sizeof(szLogMsg));
  286. sprintf(szLogMsg, "MAIN VMS Font Information initialize failed. Program abnormal terminate.");
  287. AbnormalTerminate(szLogMsg);
  288. return false;
  289. }
  290. }
  291. if (CDSIfscManager)
  292. {
  293. MINFO("MAIN VMS Scetion Information initialize...");
  294. if (CDSIfscManager->LoadVmsIfscInfo(FDb->GetConnection()) < 0)
  295. {
  296. memset(szLogMsg, 0x00, sizeof(szLogMsg));
  297. sprintf(szLogMsg, "MAIN VMS Scetion Information initialize failed. Program abnormal terminate.");
  298. AbnormalTerminate(szLogMsg);
  299. return false;
  300. }
  301. }
  302. if (CDSImageManager)
  303. {
  304. MINFO("MAIN VMS Form Information initialize...");
  305. if (CDSImageManager->LoadVmsSymbolInfo(FDb->GetConnection()) < 0)
  306. {
  307. memset(szLogMsg, 0x00, sizeof(szLogMsg));
  308. sprintf(szLogMsg, "MAIN VMS Form Information initialize failed. Program abnormal terminate.");
  309. AbnormalTerminate(szLogMsg);
  310. return false;
  311. }
  312. }
  313. if (CDSAtmpManager)
  314. {
  315. MINFO("MAIN VMS ATMP Information initialize...");
  316. if (CDSAtmpManager->LoadAtmpInfo(FDb->GetConnection()) < 0)
  317. {
  318. memset(szLogMsg, 0x00, sizeof(szLogMsg));
  319. sprintf(szLogMsg, "MAIN VMS ATMP Information initialize failed. Program abnormal terminate.");
  320. AbnormalTerminate(szLogMsg);
  321. return false;
  322. }
  323. }
  324. if (CDSFormManager)
  325. {
  326. MINFO("MAIN VMS Form Information initialize...");
  327. if (CDSFormManager->LoadVmsFormInfo(FDb->GetConnection()) < 0)
  328. {
  329. memset(szLogMsg, 0x00, sizeof(szLogMsg));
  330. sprintf(szLogMsg, "MAIN VMS Form Information initialize failed. Program abnormal terminate.");
  331. AbnormalTerminate(szLogMsg);
  332. return false;
  333. }
  334. }
  335. if (CDSVmsIfscTrafManager)
  336. {
  337. MINFO("MAIN VMS IFSC TRAF Information initialize...");
  338. if (CDSVmsIfscTrafManager->InitVmsIfscTraffic(FDb->GetConnection()) < 0)
  339. {
  340. memset(szLogMsg, 0x00, sizeof(szLogMsg));
  341. sprintf(szLogMsg, "MAIN VMS IFSC TRAF Information initialize failed. Program abnormal terminate.");
  342. AbnormalTerminate(szLogMsg);
  343. return false;
  344. }
  345. }
  346. }
  347. __finally
  348. {
  349. if (FDb)
  350. {
  351. FDb->Close();
  352. }
  353. SAFE_DELETE(FDb);
  354. }
  355. LoadStuffingVms();
  356. ClientSessionManager->Init(CDSCtlrManager->FLists.Size()*2);
  357. FrmControllerInfo->UpdateCtlrList();
  358. MINFO("MAIN Center communication initialize...");
  359. TCDSProcess *pPrcs = CDSProcessManager->FLists.Find(g_AppCfg.sSystemId);
  360. if (!pPrcs)
  361. {
  362. sMsg.printf(L"MAIN Center process information not found: %s, error %d", g_AppCfg.sSystemId.c_str(), nResult);
  363. MERROR(AnsiString(sMsg).c_str());
  364. return false;
  365. }
  366. g_AppCfg.comm.nCenterPort = pPrcs->PRGM_PORT.ToIntDef(g_AppCfg.comm.nCenterPort); // 디비에 설정된 포트를 사용
  367. //통신초기화를 최초 수행한 후 디비읽어서 처리
  368. nResult = CComm_Initialize((long)VmsCommMain->Handle, g_AppCfg.sSystemId, "VMS", g_AppCfg.comm.nCenterPort, g_AppCfg.sLogDay);
  369. if (VERR_NONE != nResult)
  370. {
  371. sMsg.printf(L"MAIN Center communication initialize failed: error %d", nResult);
  372. MERROR(AnsiString(sMsg).c_str());
  373. return false;
  374. }
  375. FOR_STL(TCDSProcess*, pObj, CDSProcessManager->FLists)
  376. {
  377. if (pObj->DEL_YN == "Y") continue;
  378. if (g_AppCfg.sSystemId == pObj->SYST_ID)
  379. {
  380. //자기자신은 목록에 추가하지 않는다...
  381. continue;
  382. }
  383. if (CComm_AddProcess(pObj->SYST_ID, pObj->SYST_TYPE, pObj->SYST_IP_1, pObj->PRGM_PORT) >= 0)
  384. {
  385. if ("UOT" == pObj->SYST_TYPE)
  386. {
  387. int nPort = pObj->PRGM_PORT.ToIntDef(0);
  388. CComm_AddProcess(pObj->SYST_ID, pObj->SYST_TYPE, pObj->SYST_IP_1, AnsiString(nPort+1)); /* VMS 운영단말 */
  389. #if 0
  390. CComm_AddProcess(pObj->SYST_ID, pObj->SYST_TYPE, pObj->SYST_IP_1, AnsiString(nPort+2)); /* CCTV 운영단말 */
  391. CComm_AddProcess(pObj->SYST_ID, pObj->SYST_TYPE, pObj->SYST_IP_1, AnsiString(nPort+3)); /* VDS 운영단말 */
  392. #endif
  393. }
  394. }
  395. }
  396. if (VERR_NONE != CComm_Open())
  397. {
  398. sMsg.printf(L"MAIN Center communication open failed: error %d", nResult);
  399. MERROR(AnsiString(sMsg).c_str());
  400. return false;
  401. }
  402. g_SysInfo->RunSts = state_normal;
  403. g_SysInfo->ComSts = state_error;
  404. g_SysInfo->DbmSts = state_normal;
  405. FTickState = 0;
  406. FServerRun = true;
  407. m_dtChkDbAive = Now(); // 데이터베이스 연결 체크 타이머
  408. m_dtChkVmsJob = Now(); // VMS 작업 및 주기작업
  409. m_dtProvider = Now()-1; // 정보제공 타이머
  410. COMM_MakeBasisTime(m_tTimer, g_AppCfg.nTrafficCycleTime, g_AppCfg.nTrafficBasisTime);
  411. //APP_PostDbThreadMessage(dbm_save_unit_stts);
  412. APP_PostDbThreadMessage(dbm_dnld_form_init);
  413. //통신서버 시작
  414. Sleep(500);
  415. tbConnectClick(this);
  416. MINFO("MAIN Program initialize success...[Completed]");
  417. return true;
  418. }
  419. //---------------------------------------------------------------------------
  420. void __fastcall TVmsCommMain::LoadStuffingVms()
  421. {
  422. TStringList *slFile = NULL;
  423. String sCfgFile = g_sCfgDir + "StuffingVMS.inf";
  424. try
  425. {
  426. slFile = new TStringList;
  427. slFile->Clear();
  428. slFile->LoadFromFile(sCfgFile);
  429. for (int ii = 0; ii < slFile->Count; ii++)
  430. {
  431. AnsiString CTLR_NMBR = slFile->Strings[ii].Trim();
  432. if (CTLR_NMBR == "") continue;
  433. if (CTLR_NMBR == "\r\n") continue;
  434. TCDSCtlr* pObj = CDSCtlrManager->FLists.Find(CTLR_NMBR);
  435. if (pObj)
  436. {
  437. pObj->IsDle = false;
  438. }
  439. }
  440. }
  441. catch(Exception &e)
  442. {
  443. }
  444. delete slFile;
  445. }
  446. //---------------------------------------------------------------------------
  447. void __fastcall TVmsCommMain::tmrAppStateTimer(TObject *Sender)
  448. {
  449. tmrAppState->Enabled = false;
  450. plTime->Caption = Now().FormatString("yyyy-MM-dd hh:nn:ss");
  451. if (!g_AppCfg.bAppLoad)
  452. {
  453. if (!InitApplication())
  454. {
  455. g_AppCfg.bAppClose = true;
  456. Close();
  457. return;
  458. }
  459. // 초기화작업완료
  460. CheckResource((g_AppCfg.MaxMemory * 1024), g_AppCfg.MaxHandle, g_AppCfg.MaxThread, g_AppCfg.MaxGdi);
  461. g_AppCfg.bAppLoad = true; // 프로그램이 로딩되었음을 확인
  462. tmrAppState->Interval = 990; // 프로그램 상태점검주기=1초
  463. tmrAppState->Enabled = true;
  464. return;
  465. }
  466. if (g_AppCfg.bAppClose)
  467. {
  468. // 응용프로그램 종료
  469. TerminateApplication();
  470. JobThreadStop();
  471. Close();
  472. return;
  473. }
  474. g_AppCfg.lMainWinHandle = (long)Handle;
  475. if (!(++FTickState % 5))
  476. {
  477. CheckCommState(); //통신상태정보 체크
  478. }
  479. UpdateProcessStatePanel();
  480. FCurrDay = StrToInt(Now().FormatString("dd"));
  481. FCurrHour = StrToInt(Now().FormatString("hh"));
  482. FCurrMin = StrToInt(Now().FormatString("nn"));
  483. FCurrSec = StrToInt(Now().FormatString("ss"));
  484. // 0시 3분에 시각 동기화 명령을 전송하자
  485. if (FCurrDay != FTimeSyncDay
  486. && FCurrMin != FTimeSyncMin
  487. && FCurrMin == 3
  488. && FCurrHour == 0)
  489. {
  490. FTimeSyncDay = FCurrDay;
  491. FTimeSyncMin = FCurrMin;
  492. #if 0
  493. SendTimeSync();
  494. #endif
  495. }
  496. // 프로세스 상태정보 이력 DB 입력
  497. if (FCurrSec > 1)
  498. {
  499. //매 1분마다 상태정보 업데이트 하고 상태정보 업데이트 하는 곳에서 5분주기로 이력정보를 저장한다.
  500. if (FUnitSttsHsMin != FCurrMin)
  501. {
  502. MINFO("MAIN >>> Save UNIT STTS");
  503. FUnitSttsHsMin = FCurrMin;
  504. APP_PostDbThreadMessage(dbm_save_unit_stts);
  505. }
  506. }
  507. // 제어기 상태정보 요청
  508. if (FCurrSec > 20)
  509. {
  510. //매 1분마다 상태정보 요청한다.
  511. if (FCtlrSttsReqMin != FCurrMin)
  512. {
  513. FCtlrSttsReqMin = FCurrMin;
  514. MINFO("MAIN >>> Req CTLR STTS");
  515. g_jobQ.PushBlocking(eVmsStatusReqAll);
  516. }
  517. }
  518. // 제어기 상태정보 이력 DB 입력
  519. if (FCurrSec > 30)
  520. {
  521. //매 1분마다 상태정보 업데이트 하고 상태정보 업데이트 하는 곳에서 5분주기로 이력정보를 저장한다.
  522. if (FCtlrSttsHsMin != FCurrMin)
  523. {
  524. FCtlrSttsHsMin = FCurrMin;
  525. MINFO("MAIN >>> Save CTLR STTS");
  526. APP_PostDbThreadMessage(dbm_save_ctlr_stts_all);
  527. }
  528. }
  529. if (FCurrSec > 40)
  530. {
  531. if (g_SysInfo->RunSts == state_normal && COMM_TimeDiff(m_dtChkVmsJob) > 60)
  532. {
  533. //VMS 주기적인 작업을 데이터베이스에서 조회
  534. MINFO("MAIN >>> Check VMS Job Schedule");
  535. APP_PostDbThreadMessage(dbm_load_onoff_time);
  536. //APP_PostDbThreadMessage(dbm_load_download_job);
  537. m_dtChkVmsJob = Now();
  538. }
  539. }
  540. if (FCurrSec > 10)
  541. {
  542. if (g_SysInfo->RunSts == state_normal && COMM_TimeDiff(m_dtChkDbAive) > 120)
  543. {
  544. //서버가 연결상태이고 DB 작업이 1분이상 경과된 경우 디비 연결상태를 체크한다.
  545. MINFO("MAIN >>> Check Database Alive");
  546. APP_PostDbThreadMessage(dbm_check_db_alive);
  547. m_dtChkDbAive = Now();
  548. }
  549. }
  550. plRemain->Caption = String(FProvideRemain--);
  551. if (FFirstRunning)
  552. {
  553. if ( (FCurrHour*3600 + FCurrMin*60) % g_AppCfg.nTrafficCycleTime == 0 &&
  554. FCurrSec >= g_AppCfg.nTrafficBasisTime )
  555. //if (CheckBasisTime(m_tTimer, g_AppCfg.nTrafficCycleTime, g_AppCfg.nTrafficBasisTime))
  556. {
  557. if (COMM_TimeDiff(m_dtProvider) > g_AppCfg.nTrafficCycleTime)
  558. {
  559. SendVmsTrafficProvide(1);
  560. }
  561. }
  562. }
  563. CheckProvideTimeout();
  564. if (!(FTickState % 60))
  565. {
  566. #if 0
  567. if (CheckResource((g_AppCfg.MaxMemory * 1024), g_AppCfg.MaxHandle, g_AppCfg.MaxThread, g_AppCfg.MaxGdi))
  568. {
  569. MERROR("MAIN Resource error");
  570. //AutoClose();
  571. }
  572. #endif
  573. }
  574. JobThreadCheck();
  575. #if 0
  576. g_logQ.PushBlocking(Q_TICK);
  577. g_dbmQ.PushBlocking(Q_TICK);
  578. #endif
  579. g_jobQ.PushBlocking(Q_TICK); //접속한 클라이언트 작업을 위해 스레드를 깨운다.
  580. tmrAppState->Enabled = true;
  581. }
  582. //---------------------------------------------------------------------------
  583. void __fastcall TVmsCommMain::AutoClose()
  584. {
  585. g_AppCfg.bAppClose = true;
  586. //Close();
  587. }
  588. //---------------------------------------------------------------------------
  589. void __fastcall TVmsCommMain::tbConnectClick(TObject *Sender)
  590. {
  591. g_AppCfg.bThrExit = false;
  592. //FrmNetwork->Show();
  593. if (FrmNetwork->Start())
  594. {
  595. FServerRun = true;
  596. tbConnect->Enabled = false;
  597. tbDisconnect->Enabled = true;
  598. }
  599. }
  600. //---------------------------------------------------------------------------
  601. void __fastcall TVmsCommMain::tbDisconnectClick(TObject *Sender)
  602. {
  603. FServerRun = false;
  604. g_jobQ.PushBlocking(eTcpCloseAll);
  605. FrmNetwork->Stop();
  606. tbDisconnect->Enabled = false;
  607. tbConnect->Enabled = true;
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TVmsCommMain::tbOptionsClick(TObject *Sender)
  611. {
  612. FrmOption = new TFrmOption(this);
  613. FrmOption->Left = VmsCommMain->Left + 80;
  614. FrmOption->Top = VmsCommMain->Top + 80;
  615. FrmOption->ShowModal();
  616. if (FrmOption->FUpdate)
  617. {
  618. try
  619. {
  620. ITSLog->FLogCfg = g_LogCfg;
  621. CtlrItr it;
  622. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  623. {
  624. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  625. if (!pObj->FDispLog)
  626. {
  627. pObj->FCLog->FLogCfg = g_LogCfg;
  628. pObj->FSLog->FLogCfg = g_LogCfg;
  629. }
  630. }
  631. }
  632. catch(Exception &e)
  633. {
  634. }
  635. }
  636. delete FrmOption;
  637. FrmOption = NULL;
  638. }
  639. //---------------------------------------------------------------------------
  640. void __fastcall TVmsCommMain::CheckCommState()
  641. {
  642. //g_SysInfo->RunSts = (FServerRun && DMManager->TcpServer->Active) ? state_normal : state_error;
  643. g_SysInfo->RunSts = (FServerRun) ? state_normal : state_error;
  644. if (g_SysInfo->RunSts == state_normal)
  645. {
  646. if (CDSCtlrManager->Total == CDSCtlrManager->Normal) g_SysInfo->ComSts = state_normal;
  647. else g_SysInfo->ComSts = state_error;
  648. }
  649. else
  650. {
  651. g_SysInfo->ComSts = state_error;
  652. }
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TVmsCommMain::UpdateProcessStatePanel()
  656. {
  657. plStateAction->Color = (g_SysInfo->RunSts == state_normal) ? clGreen : clRed;
  658. plStateComm->Color = (g_SysInfo->ComSts == state_normal) ? clGreen : clRed;
  659. plStateDb->Color = (g_SysInfo->DbmSts == state_normal) ? clGreen : clRed;
  660. }
  661. //---------------------------------------------------------------------------
  662. void __fastcall TVmsCommMain::OnWMDbThreadMessage(TMessage &Msg)
  663. {
  664. int nCommand = (int)Msg.WParam;
  665. int nResult = (int)Msg.LParam;
  666. ReplyMessage(0);
  667. if (nCommand == dbm_job_start)
  668. {
  669. tmrDbCheck->Enabled = true;
  670. return;
  671. }
  672. tmrDbCheck->Enabled = false;
  673. switch(nCommand)
  674. {
  675. case dbm_status:
  676. g_SysInfo->DbmSts = nResult >= 0 ? state_normal : state_error;
  677. break;
  678. case dbm_dnld_form_init:
  679. MINFO("MAIN VMS Message init start event");
  680. MakeVmsProvideForm();
  681. break;
  682. case dbm_dnld_form_cycle:
  683. case dbm_dnld_form_mode:
  684. MINFO("MAIN VMS Message create start event");
  685. MakeVmsProvideForm();
  686. APP_PostDbThreadMessage(dbm_save_form_download);
  687. break;
  688. case dbm_save_form_download:
  689. MINFO("MAIN VMS Message download start event");
  690. //클라이언트로 VMS폼 스케쥴이 저장되었다는 이벤트를 전송
  691. CDSManager->SendVmsDownloadForm();
  692. //VMS 제어기에 VMS폼 스케쥴을 다운로드 하도록 이벤트 전송
  693. g_jobQ.PushBlocking(eVmsFormDownload);
  694. CheckResource((g_AppCfg.MaxMemory * 1024), g_AppCfg.MaxHandle, g_AppCfg.MaxThread, g_AppCfg.MaxGdi);
  695. break;
  696. case dbm_initialize:
  697. break;
  698. }
  699. m_dtChkDbAive = Now(); // 데이터베이스 작업 성공/실패 여부와 상관없이 체크타이머 초기화
  700. }
  701. //---------------------------------------------------------------------------
  702. void __fastcall TVmsCommMain::OnWMUserMessage(TMessage &Msg)
  703. {
  704. ReplyMessage(0);
  705. if ((int)Msg.WParam == WM_WINDOW_RESTORE)
  706. {
  707. Application->Restore();
  708. Application->BringToFront();
  709. return;
  710. }
  711. }
  712. //---------------------------------------------------------------------------
  713. void __fastcall TVmsCommMain::OnTcpThreadMessage(TMessage &Msg)
  714. {
  715. TCDSCtlr *pObj = (TCDSCtlr*)Msg.LParam;
  716. CTLR_STTS stts;
  717. switch(Msg.WParam)
  718. {
  719. case WM_TCP_SERVER_STTS: // 클라이언트의 연결 또는 연결종료시 수신 메시지
  720. try
  721. {
  722. if (pObj->Server.commState == comm_open)
  723. {
  724. pObj->dwConnectCnt++;
  725. MINFO("MAIN VMS Connect Event: %s", pObj->CTLR_NMBR.c_str());
  726. }
  727. else
  728. {
  729. MINFO("MAIN VMS Closed Event: %s", pObj->CTLR_NMBR.c_str());
  730. }
  731. } catch(Exception &e) {}
  732. FrmControllerInfo->UpdateServerState((int)pObj);
  733. stts.Type = 1;
  734. stts.ObjPtr = (void*)pObj;
  735. APP_PostDbThreadMessage(dbm_save_ctlr_stts, sizeof(stts), &stts);
  736. break;
  737. case WM_CTLR_STATE_STTS: // 연결된 제어기로 부터 상태정보를 수신할시
  738. try
  739. {
  740. MINFO("MAIN VMS State Event: %s", pObj->CTLR_NMBR.c_str());
  741. } catch(Exception &e) {}
  742. FrmControllerInfo->UpdateClientState((int)pObj);
  743. stts.Type = 0;
  744. stts.ObjPtr = (void*)pObj;
  745. APP_PostDbThreadMessage(dbm_save_ctlr_stts, sizeof(stts), &stts);
  746. break;
  747. }
  748. }
  749. //---------------------------------------------------------------------------
  750. void __fastcall TVmsCommMain::OnUdpDataRecvMessage(TMessage &Msg)
  751. {
  752. int nResult;
  753. IPC_MSG_DATA *RcvData = (IPC_MSG_DATA*)Msg.LParam;
  754. INT_SENDER *Sender = &RcvData->Sender;
  755. INT_PACKET *Pkt = &RcvData->Pkt;
  756. INT_HEAD *head = &Pkt->head;
  757. INT_DATA *data = &Pkt->data;
  758. char *pData = &Pkt->data.Data[0];
  759. BYTE OpCode = head->OpCode;
  760. MINFO("MAIN Center Msg: %s.%d, OpCode: %02X, From: %02X, To: %02X", CComm_ulong2ip(Sender->IP), Sender->Port, OpCode, head->SendId, head->RecvId);
  761. switch(OpCode)
  762. {
  763. case INT_OP_TRAFFIC_CHANGE:
  764. MINFO("MAIN INT_OP_TRAFFIC_CHANGE");
  765. CDSManager->FIsStaticCycle = true; //정주기 정보제공주기 플래그 설정
  766. SendVmsTrafficProvide(0);
  767. break;
  768. case INT_OP_VMS_POWER_CTL: //전광판 On/Off
  769. {
  770. INT_VMS_POWER_CTL_REQ *pReq = (INT_VMS_POWER_CTL_REQ*)pData;
  771. MINFO("MAIN INT_OP_VMS_POWER_CTL: %.*s %d, %s, %d", INT_VMS_MAX_DATETIME, pReq->CmdTime, pReq->VMS_CTLR_NMBR, pReq->OperId, pReq->Command);
  772. TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR));
  773. if (!pCDSCtlr) break;
  774. if (!pCDSCtlr->FSession) break;
  775. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  776. pMsg->Type = eVmsStatusControl;
  777. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  778. pMsg->Buff[0] = 0x01;
  779. pMsg->Buff[1] = (BYTE)pReq->Command;
  780. pMsg->Len = 2;
  781. g_jobQ.PushBlocking((DWORD)pMsg);
  782. }
  783. break;
  784. case INT_OP_VMS_RESET:
  785. {
  786. INT_VMS_RESET_REQ *pReq = (INT_VMS_RESET_REQ*)pData;
  787. MINFO("MAIN INT_OP_VMS_RESET: %.*s %d, %s", INT_VMS_MAX_DATETIME, pReq->CmdTime, pReq->VMS_CTLR_NMBR, pReq->OperId);
  788. TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR));
  789. if (!pCDSCtlr) break;
  790. if (!pCDSCtlr->FSession) break;
  791. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  792. pMsg->Type = eVmsStatusControl;
  793. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  794. pMsg->Buff[0] = 0x02;
  795. pMsg->Len = 1;
  796. g_jobQ.PushBlocking((DWORD)pMsg);
  797. }
  798. break;
  799. case INT_OP_VMS_LUMINANCE_CTL: //휘도설정
  800. {
  801. INT_VMS_LUMINANCE_REQ *pReq = (INT_VMS_LUMINANCE_REQ*)pData;
  802. MINFO("MAIN INT_OP_VMS_LUMINANCE_CTL: %.*s %d, %s, %02X", INT_VMS_MAX_DATETIME, pReq->CmdTime, pReq->VMS_CTLR_NMBR, pReq->OperId, pReq->BrghMode);
  803. TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR));
  804. if (!pCDSCtlr) break;
  805. if (!pCDSCtlr->FSession) break;
  806. BYTE cmdVal = 0x00;
  807. switch(pReq->BrghMode)
  808. {
  809. case 0x00: //주간
  810. cmdVal = pReq->BrghWeekStep;
  811. break;
  812. case 0x01: //야간
  813. cmdVal = pReq->BrghNghtStep;
  814. break;
  815. case 0x02: //자동
  816. cmdVal = 0x00;
  817. break;
  818. default: return;
  819. }
  820. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  821. pMsg->Type = eVmsStatusControl;
  822. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  823. pMsg->Buff[0] = 0x06;
  824. pMsg->Buff[1] = (BYTE)pReq->BrghMode;
  825. pMsg->Buff[2] = cmdVal;
  826. pMsg->Len = 3;
  827. g_jobQ.PushBlocking((DWORD)pMsg);
  828. }
  829. break;
  830. case INT_OP_VMS_PARA_SET: //환경설정 정보 설정
  831. {
  832. INT_VMS_PARA_SET_REQ *pReq = (INT_VMS_PARA_SET_REQ*)pData;
  833. MINFO("MAIN INT_OP_VMS_PARA_SET: %.*s %d, %s", INT_VMS_MAX_DATETIME, pReq->CmdTime, pReq->VMS_CTLR_NMBR, pReq->OperId);
  834. TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR));
  835. if (!pCDSCtlr) break;
  836. if (!pCDSCtlr->FSession) break;
  837. //도형식에는 설정정보가 없고 문자식에만 설정하는 프로토콜이 있음
  838. //현재 적용하지 않았음.........
  839. //적용할 경우 VMS유형(AObj->PROTOCOL_VER == 1) 확인해서 처리해야함
  840. //함체 PAN/HEATER 온도 설정
  841. //Download Parameter
  842. #if 0
  843. SIGNT_CONFIG_SET set;
  844. set.VMS_CTLR_NMBR = pCDSCtlr->CTLR_NMBR.ToIntDef(0);
  845. set.FanRunTmpr = pReq->FanRunTmpr;
  846. set.HetrRunTmpr = pReq->HetrRunTmpr;
  847. memcpy(set.PanlOnTime, pReq->DispStrTm, 4);
  848. memcpy(set.PanlOffTime, pReq->DispEndTm, 4);
  849. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  850. pMsg->Type = eVmsConfigSet;
  851. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  852. memcpy(pMsg->Buff, (char*)&set, sizeof(SIGNT_CONFIG_SET));
  853. g_jobQ.PushBlocking((DWORD)pMsg);
  854. #endif
  855. }
  856. break;
  857. case INT_OP_VMS_PARAM_REQ:
  858. {
  859. INT_VMS_PARAM_REQ *pReq = (INT_VMS_PARAM_REQ*)pData;
  860. MINFO("MAIN INT_OP_VMS_PARAM_REQ: %d", pReq->Count);
  861. for (int ii = 0; ii < pReq->Count && ii < INT_MAX_IMDT_SCN_DNLD_MAX; ii++)
  862. {
  863. TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR[ii]));
  864. if (!pCDSCtlr) continue;
  865. if (!pCDSCtlr->FSession) continue;
  866. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  867. pMsg->Type = eVmsParamReq;
  868. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  869. g_jobQ.PushBlocking((DWORD)pMsg);
  870. }
  871. }
  872. break;
  873. case INT_OP_VMS_IMMEDIATE_SCN_DNLD:
  874. {
  875. INT_VMS_IMMEDIATE_SCENARIO_DNLD_REQ *pReq = (INT_VMS_IMMEDIATE_SCENARIO_DNLD_REQ*)pData;
  876. MINFO("MAIN INT_OP_VMS_IMMEDIATE_SCN_DNLD: %.*s, %s", INT_VMS_MAX_DATETIME, pReq->CmdTime, pReq->OperId);
  877. for (int ii = 0; ii < pReq->Count && ii < INT_MAX_IMDT_SCN_DNLD_MAX; ii++)
  878. {
  879. TCDSCtlr *pObj = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR[ii]));
  880. if (!pObj) continue;
  881. if (!pObj->FSession) continue;
  882. CDSCtlrManager->InitProvide(pObj);
  883. #if 0
  884. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  885. pMsg->Type = eVmsScenarioDownload;
  886. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  887. g_jobQ.PushBlocking((DWORD)pMsg);
  888. #endif
  889. }
  890. APP_PostDbThreadMessage(dbm_dnld_form_cycle);
  891. //APP_PostDbThreadMessage(dbm_dnld_form_mode);
  892. }
  893. break;
  894. case INT_OP_VMS_MODE:
  895. {
  896. INT_VMS_MODE_REQ *pReq = (INT_VMS_MODE_REQ*)pData;
  897. MINFO("MAIN INT_OP_VMS_MODE: %.*s %s, %s, %d EA", INT_VMS_MAX_DATETIME, pReq->CmdTime, pReq->OperId, pReq->Count);
  898. //모든 VMS에 메시지다운로드 플래그 비활성화 시킴
  899. //CDSCtlrManager->InitProvide(false);
  900. //수신한 VMS에 대하여 다운로드 플래그 활성화 시킴
  901. for(int ii = 0; ii < pReq->Count && ii < INT_VMS_MAX_MODE; ii++)
  902. {
  903. TCDSCtlr *pObj = CDSCtlrManager->FLists.Find(AnsiString(pReq->Unit[ii].VMS_CTLR_NMBR));
  904. if (!pObj) continue;
  905. if (pObj->Server.useFlag != use_enable) continue;
  906. if (pObj->CTRLMODE->Control == pReq->Unit[ii].Mode)
  907. pObj->FModeChange = false;
  908. else
  909. pObj->FModeChange = true;
  910. pObj->CTRLMODE->Control = pReq->Unit[ii].Mode;
  911. pObj->DSTATE.OprMode = pObj->CTRLMODE->Control;
  912. pObj->RSTATE.OprMode = pObj->CTRLMODE->Control;
  913. if (pObj->CTRLMODE->Control == 'A') pObj->OPER_MODE = "A";
  914. else if (pObj->CTRLMODE->Control == 'B') pObj->OPER_MODE = "B";
  915. else pObj->OPER_MODE = "F";
  916. //pObj->FProvide = true;
  917. CDSCtlrManager->InitProvide(pObj);
  918. }
  919. FrmControllerInfo->UpdateCtlrList();
  920. APP_PostDbThreadMessage(dbm_dnld_form_mode);
  921. }
  922. break;
  923. case INT_OP_VMS_STATE_REQ:
  924. MINFO("MAIN INT_OP_VMS_STATE_REQ");
  925. CDSCtlrManager->SendCtlrSttsAll();
  926. break;
  927. case INT_OP_VMS_IMAGE_SYMBOL:
  928. MINFO("MAIN INT_OP_VMS_IMAGE_SYMBOL");
  929. APP_PostDbThreadMessage(dbm_master_symbol_image);
  930. break;
  931. case INT_OP_VMS_IMAGE_TRAFFIC:
  932. MINFO("MAIN INT_OP_VMS_IMAGE_TRAFFIC");
  933. APP_PostDbThreadMessage(dbm_master_symbol_traffic);
  934. break;
  935. case INT_OP_VMS_FORM_UPDATE:
  936. MINFO("MAIN INT_OP_VMS_FORM_UPDATE");
  937. APP_PostDbThreadMessage(dbm_master_form);
  938. break;
  939. case INT_OP_VMS_IFSC_UPDATE:
  940. MINFO("MAIN INT_OP_VMS_IFSC_UPDATE");
  941. APP_PostDbThreadMessage(dbm_master_vms_ifsc);
  942. break;
  943. case INT_ID_TRAFFIC_SERVER:
  944. MINFO("MAIN INT_ID_TRAFFIC_SERVER");
  945. //APP_PostDbThreadMessage(dbm_master_vms_ifsc);
  946. break;
  947. default:
  948. MINFO("MAIN INT_UNKNOWN: %02X", OpCode);
  949. break;
  950. }
  951. }
  952. //---------------------------------------------------------------------------
  953. #if 0
  954. void __fastcall TVmsCommMain::ClientResponse(int Flag, REQ_INFO_STRUCT *pReq, int error, void *pData)
  955. {
  956. #ifdef CENTERCOMM
  957. BYTE ErrCode;
  958. int sResult;
  959. switch(error)
  960. {
  961. case SYS_ERR_NONE: break;
  962. case SYS_ERR_OTHER: ErrCode = INT_ERROR_OTHER; break;
  963. case SYS_ERR_INTERNAL: ErrCode = INT_ERROR_SYSTEM; break;
  964. case SYS_ERR_MEMORY: ErrCode = INT_ERROR_SYSTEM; break;
  965. case SYS_ERR_DATABASE: ErrCode = INT_ERROR_SYSTEM; break;
  966. case SYS_ERR_RESET: ErrCode = INT_ERROR_SYSTEM; break;
  967. case SYS_ERR_START: ErrCode = INT_ERROR_SYSTEM; break;
  968. case SYS_ERR_OFFLINE: ErrCode = INT_ERROR_OFFLINE; break;
  969. case SYS_ERR_NO_RES: ErrCode = INT_ERROR_NO_RESPONSE; break;
  970. case SYS_ERR_ACCESS: ErrCode = INT_ERROR_PROCESS; break;
  971. case SYS_ERR_INVALID: ErrCode = INT_ERROR_OTHER; break;
  972. case SYS_ERR_UNKNOWN: ErrCode = INT_ERROR_UNKNOWN_CODE; break;
  973. case SYS_ERR_WRITE_LENGTH: ErrCode = INT_ERROR_OTHER; break;
  974. case SYS_ERR_PACKET_SIZE_LARGE: ErrCode = INT_ERROR_OTHER; break;
  975. case SYS_ERR_INVALID_TAG: ErrCode = INT_ERROR_OTHER; break;
  976. case SYS_ERR_CRC: ErrCode = INT_ERROR_OTHER; break;
  977. case SYS_ERR_LENGTH: ErrCode = INT_ERROR_LENGTH; break;
  978. case SYS_ERR_ADDRESS: ErrCode = INT_ERROR_ADDRESS; break;
  979. case SYS_ERR_HEADER_OPTIONS: ErrCode = INT_ERROR_HEADER; break;
  980. case SYS_ERR_UNKNOWN_COMMAND: ErrCode = INT_ERROR_UNKNOWN_OPCODE; break;
  981. case SYS_ERR_RECEIVE_TIMEOUT: ErrCode = INT_ERROR_OTHER; break;
  982. case SYS_ERR_DATA_SIZE: ErrCode = INT_ERROR_DISCORD; break;
  983. case SYS_ERR_MISMATCH: ErrCode = INT_ERROR_OTHER; break;
  984. case SYS_ERR_INVALID_PARA: ErrCode = INT_ERROR_INVALID_DATA; break;
  985. case SYS_ERR_MOVEMENT: ErrCode = INT_ERROR_MOVEMENT; break;
  986. case SYS_ERR_UNAUTHORIZED: ErrCode = INT_ERROR_UNAUTHORIZED; break;
  987. default: ErrCode = INT_ERROR_OTHER; break;
  988. }
  989. if (Flag == SEND_ACK)
  990. {
  991. INT_ACK_RES Ack;
  992. memset(&Ack, 0x00, sizeof(Ack));
  993. Ack.OPCode.Type = INT_TYPE(pReq->OpCode);
  994. Ack.OPCode.Kind = INT_KIND(pReq->OpCode);
  995. Ack.MsgSeq = pReq->MsgSeq;
  996. if ((sResult = AckRes(&Ack, pReq->ProcessID)) != UDP_ERR_NONE)
  997. {
  998. MERROR("MAIN AckRes fail %d", sResult);
  999. }
  1000. }
  1001. else
  1002. if (Flag == SEND_NACK)
  1003. {
  1004. INT_NACK_RES Nack;
  1005. memset(&Nack, 0x00, sizeof(Nack));
  1006. Nack.OPCode.Type = INT_TYPE(pReq->OpCode);
  1007. Nack.OPCode.Kind = INT_KIND(pReq->OpCode);
  1008. Nack.MsgSeq = pReq->MsgSeq;
  1009. Nack.ErrCode = ErrCode;
  1010. if ((sResult = NackRes(&Nack, pReq->ProcessID)) != UDP_ERR_NONE)
  1011. {
  1012. MERROR("MAIN NackRes fail %d", sResult);
  1013. }
  1014. }
  1015. #endif
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. #endif
  1019. bool __fastcall TVmsCommMain::CheckBasisTime(char *p, int Cycle, int Basis)
  1020. {
  1021. char cTime[15];
  1022. COMM_MakeBasisTime(cTime, Cycle, Basis);
  1023. if (memcmp(p, cTime, 14) != 0)
  1024. {
  1025. memcpy(p, cTime, 14);
  1026. return true;
  1027. }
  1028. return false;
  1029. }
  1030. //---------------------------------------------------------------------------
  1031. void __fastcall TVmsCommMain::CheckProvideTimeout()
  1032. {
  1033. VMS_PROVIDE_RESULE ProvideSave;
  1034. VMS_PROVIDE_RESULE DownLoadSave;
  1035. memset(&ProvideSave, 0x00, sizeof(ProvideSave));
  1036. memset(&DownLoadSave, 0x00, sizeof(DownLoadSave));
  1037. ProvideSave.Type = provide_form;
  1038. ProvideSave.Count = 0;
  1039. DownLoadSave.Type = provide_download;
  1040. DownLoadSave.Count = 0;
  1041. CtlrItr it;
  1042. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  1043. {
  1044. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  1045. if (pObj->Server.useFlag != use_enable) continue;
  1046. if ((pObj->DOWNLOAD->SaveFlag == false) &&
  1047. (COMM_TimeDiff(pObj->DOWNLOAD->dTimer) > VMS_DOWNLOAD_TIMEOUT))
  1048. {
  1049. DownLoadSave.pObj[DownLoadSave.Count++] = (void *)pObj;
  1050. pObj->DOWNLOAD->SaveFlag = true;
  1051. pObj->DOWNLOAD->dTimer = Now();
  1052. }
  1053. if ((pObj->CTRLMODE->SaveFlag == false) &&
  1054. (COMM_TimeDiff(pObj->CTRLMODE->pTimer) > VMS_PROVIDE_TIMEOUT))
  1055. {
  1056. ProvideSave.pObj[ProvideSave.Count++] = (void *)pObj;
  1057. pObj->CTRLMODE->SaveFlag = true;
  1058. pObj->CTRLMODE->pTimer = Now();
  1059. }
  1060. // 패킷크기가 채워졌으면 DB 작업스레드로 전송
  1061. if (ProvideSave.Count >= MAX_VMS_PROVIDE_RESULT)
  1062. {
  1063. APP_PostDbThreadMessage(dbm_provide_result, sizeof(ProvideSave), &ProvideSave);
  1064. }
  1065. if (DownLoadSave.Count >= MAX_VMS_PROVIDE_RESULT)
  1066. {
  1067. APP_PostDbThreadMessage(dbm_provide_result, sizeof(DownLoadSave), &DownLoadSave);
  1068. }
  1069. }
  1070. // 실패작업이 존재하면 DB 작업스레드로 전송
  1071. if (ProvideSave.Count > 0)
  1072. {
  1073. APP_PostDbThreadMessage(dbm_provide_result, sizeof(ProvideSave), &ProvideSave);
  1074. }
  1075. if (DownLoadSave.Count > 0)
  1076. {
  1077. APP_PostDbThreadMessage(dbm_provide_result, sizeof(DownLoadSave), &DownLoadSave);
  1078. }
  1079. }
  1080. //---------------------------------------------------------------------------
  1081. void __fastcall TVmsCommMain::SendTimeSync()
  1082. {
  1083. CtlrItr it;
  1084. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  1085. {
  1086. TCDSCtlr *pCDSCtlr = (TCDSCtlr*)it->second;
  1087. if (pCDSCtlr->Server.useFlag != use_enable) continue;
  1088. if (pCDSCtlr->FSession)
  1089. {
  1090. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  1091. pMsg->Type = eVmsStatusControl;
  1092. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  1093. pMsg->Buff[0] = 0x04;
  1094. memcpy(&pMsg->Buff[1], AnsiString(Now().FormatString("yyyymmddhhnnss")).c_str(), 14);
  1095. pMsg->Len = 1+14;
  1096. g_jobQ.PushBlocking((DWORD)pMsg);
  1097. // 제어기가 연결되어 있으면 전송
  1098. MINFO("MAIN SendTimeSync command send: %s", pCDSCtlr->CTLR_NMBR.c_str());
  1099. }
  1100. }
  1101. }
  1102. //---------------------------------------------------------------------------
  1103. bool __fastcall TVmsCommMain::CheckResource(size_t MaxMemory, DWORD MaxHandle, DWORD MaxThread, DWORD MaxGdi)
  1104. {
  1105. bool result;
  1106. HANDLE hProcess;
  1107. PROCESS_MEMORY_COUNTERS pmc;
  1108. size_t uSize;
  1109. DWORD HandleCount, ThreadCount, GdiCount;
  1110. HANDLE hProcessSnap;
  1111. PROCESSENTRY32 pe32;
  1112. DWORD ProcessId;
  1113. result = false;
  1114. uSize = 0;
  1115. HandleCount = 0;
  1116. ThreadCount = 0;
  1117. GdiCount = 0;
  1118. hProcess = GetCurrentProcess();
  1119. if (hProcess != NULL)
  1120. {
  1121. if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
  1122. {
  1123. uSize = pmc.WorkingSetSize/1024;
  1124. if ((MaxMemory != 0) && (uSize > MaxMemory))
  1125. {
  1126. MERROR("MAIN Memory usage exceeds standards!!!...[%d][%d]", MaxMemory, uSize);
  1127. result = true;
  1128. }
  1129. }
  1130. if ( GetProcessHandleCount( hProcess, &HandleCount) )
  1131. {
  1132. if ((MaxHandle != 0) && (HandleCount > MaxHandle))
  1133. {
  1134. MERROR("MAIN Window handle exceed standards!!!...[%d][%d]", MaxHandle, HandleCount);
  1135. result = true;
  1136. }
  1137. }
  1138. ProcessId = GetCurrentProcessId();
  1139. if ((hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 )) != INVALID_HANDLE_VALUE)
  1140. {
  1141. pe32.dwSize = sizeof( PROCESSENTRY32 );
  1142. if ( Process32First( hProcessSnap, &pe32 ) )
  1143. {
  1144. do
  1145. {
  1146. if (pe32.th32ProcessID == ProcessId)
  1147. {
  1148. ThreadCount = pe32.cntThreads;
  1149. if ((MaxThread != 0) && (ThreadCount > MaxThread))
  1150. {
  1151. MERROR("MAIN Criteria exceeded thread!!!...[%d][%d]", MaxThread, ThreadCount);
  1152. result = true;
  1153. }
  1154. break;
  1155. }
  1156. } while( Process32Next( hProcessSnap, &pe32 ) );
  1157. }
  1158. CloseHandle( hProcessSnap );
  1159. }
  1160. GdiCount = GetGuiResources( hProcess, GR_GDIOBJECTS);
  1161. if ((MaxGdi != 0) && (GdiCount > MaxGdi))
  1162. {
  1163. MERROR("MAIN GDI Objects exceeding standards!!!...[%d][%d]", MaxGdi, GdiCount);
  1164. result = true;
  1165. }
  1166. CloseHandle( hProcess );
  1167. }
  1168. try
  1169. {
  1170. FMemSize = uSize;
  1171. plMemory->Caption = FormatFloat("##,##0", uSize) ;// + " KB";
  1172. plHandle->Caption = FormatFloat("##,##0", HandleCount);// + " EA";
  1173. plThread->Caption = FormatFloat("##,##0", ThreadCount);// + " EA";
  1174. plGdi->Caption = FormatFloat("##,##0", GdiCount) ;// + " EA";
  1175. MINFO("MAIN Memory: %s, Haldle: %s, Thread: %s, GDI: %s",
  1176. AnsiString(plMemory->Caption).c_str(),
  1177. AnsiString(plHandle->Caption).c_str(),
  1178. AnsiString(plThread->Caption).c_str(),
  1179. AnsiString(plGdi->Caption).c_str()
  1180. );
  1181. } catch (Exception &e) {}
  1182. return result;
  1183. }
  1184. //---------------------------------------------------------------------------
  1185. void __fastcall TVmsCommMain::Panel16DblClick(TObject *Sender)
  1186. {
  1187. CheckResource((g_AppCfg.MaxMemory * 1024), g_AppCfg.MaxHandle, g_AppCfg.MaxThread, g_AppCfg.MaxGdi);
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. void __fastcall TVmsCommMain::plStateDbDblClick(TObject *Sender)
  1191. {
  1192. APP_PostDbThreadMessage(dbm_master_symbol_image);
  1193. APP_PostDbThreadMessage(dbm_master_form);
  1194. APP_PostDbThreadMessage(dbm_master_vms_ifsc);
  1195. }
  1196. //---------------------------------------------------------------------------
  1197. void __fastcall TVmsCommMain::plStateCommDblClick(TObject *Sender)
  1198. {
  1199. AnsiString VMS_IFSC_ID;
  1200. int SECT_LNGT;
  1201. TItsMap<int, TObjectId*> FLists;
  1202. TItsMap<AnsiString, TVmsIfscGrad*> FGrads;
  1203. VMS_TRAF traf;
  1204. int SPED; // N NUMBER(3) Y 0 속도
  1205. int TRVL_HH; // N NUMBER(6) Y 0 통행 시간
  1206. AnsiString CMTR_GRAD_CD; // N VARCHAR2(7) Y 소통 등급 코드
  1207. if (!CDSVmsIfscTrafManager) return;
  1208. FOR_STL(TCDSVmsIfscTraf*, pObj, CDSVmsIfscTrafManager->FVmsIfscLists)
  1209. {
  1210. int objValid = pObj->IsValid == false ? 0 : 1;
  1211. MINFO("VMS_IFSC_ID: %s, SECT_LNGT: %5d, VALID: %d, Objects: %d, SPED: %3d, TRVL_HH: %4d, GRAD_CD: %s",
  1212. pObj->VMS_IFSC_ID, pObj->SECT_LNGT, objValid, pObj->FLists.Size(), pObj->traf.SPED, pObj->traf.TRVL_HH, pObj->traf.CMTR_GRAD_CD);
  1213. FOR_STL(TVmsIfscGrad*, pGrad, pObj->FGrads)
  1214. {
  1215. int gradValid = pGrad->IsValid == false ? 0 : 1;
  1216. MINFO(" GRAD_CD: %s, VALID: %d, LWSTSPED: %3d, HGHSSPED: %3d", pGrad->CMTR_GRAD_CD, gradValid, pGrad->LWSTSPED, pGrad->HGHSSPED);
  1217. }
  1218. int ii = 1;
  1219. FOR_STL(TObjectId*, pID, pObj->FLists)
  1220. {
  1221. int idValid = pID->IsValid == false ? 0 : 1;
  1222. TCDSIfscTraf* pIfsc = CDSVmsIfscTrafManager->FIfscTrafLists.Find(pID->ID);
  1223. if (pIfsc)
  1224. {
  1225. MINFO(" %2d: IFSC_ID: %s, SECT_LNGT: %5d, VALID: %d, SPED: %3d, TRVL_HH: %4d", ii, pIfsc->IFSC_ID, pIfsc->SECT_LNGT, idValid, pIfsc->SPED, pIfsc->TRVL_HH);
  1226. }
  1227. else
  1228. {
  1229. MINFO(" %2d: IFSC_ID: %s, Not Found", ii, pID->ID);
  1230. }
  1231. ii++;
  1232. }
  1233. MINFO("");
  1234. }
  1235. }
  1236. //---------------------------------------------------------------------------
  1237. void __fastcall TVmsCommMain::plStateActionDblClick(TObject *Sender)
  1238. {
  1239. plStateAction->Enabled = false;
  1240. tmrRunProvide->Enabled = false;
  1241. tmrRunProvide->Enabled = true;
  1242. CDSManager->FIsStaticCycle = true; //정주기 정보제공주기 플래그 설정
  1243. CtlrItr it;
  1244. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  1245. {
  1246. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  1247. if (!pObj->FSession) continue;
  1248. CDSCtlrManager->InitProvide(pObj);
  1249. }
  1250. APP_PostDbThreadMessage(dbm_dnld_form_cycle);
  1251. }
  1252. //---------------------------------------------------------------------------
  1253. void __fastcall TVmsCommMain::tmrRunProvideTimer(TObject *Sender)
  1254. {
  1255. tmrRunProvide->Enabled = false;
  1256. plStateAction->Enabled = true;
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. bool __fastcall TVmsCommMain::JobThreadStart()
  1260. {
  1261. int nThreadCnt = 0;
  1262. if (true)
  1263. {
  1264. LogThread = new TLogThread();
  1265. if (!LogThread) return false;
  1266. LogThread->FPnlState = plStateLog;
  1267. LogThread->FTmrDb = tmrDbCheck;
  1268. LogThread->Name = "LogThread";
  1269. LogThread->Execute();
  1270. G_Threads.Push(nThreadCnt++, LogThread);
  1271. }
  1272. if (true)
  1273. {
  1274. DbmThread = new TDbmThread();
  1275. if (!DbmThread) return false;
  1276. DbmThread->FPnlState = plStateDbm;
  1277. DbmThread->FTmrDb = tmrDbCheck;
  1278. DbmThread->Name = "DbmThread";
  1279. DbmThread->Execute();
  1280. G_Threads.Push(nThreadCnt++, DbmThread);
  1281. }
  1282. if (true)
  1283. {
  1284. JobThread = new TJobThread();
  1285. if (!JobThread) return false;
  1286. JobThread->FPnlState = plStateJob;
  1287. JobThread->FTmrDb = tmrDbCheck;
  1288. JobThread->Name = "JobThread";
  1289. JobThread->Execute();
  1290. G_Threads.Push(nThreadCnt++, JobThread);
  1291. }
  1292. return true;
  1293. }
  1294. //---------------------------------------------------------------------------
  1295. void __fastcall TVmsCommMain::JobThreadCheck()
  1296. {
  1297. int nTermCnt = 0;
  1298. for (thrIterator it=G_Threads.FObjects.begin(); it != G_Threads.FObjects.end(); ++it)
  1299. {
  1300. TItsThread *pObj = (TItsThread*)it->second;
  1301. if (pObj->IsTerm())
  1302. {
  1303. pObj->Execute();
  1304. nTermCnt++;
  1305. }
  1306. }
  1307. }
  1308. //---------------------------------------------------------------------------
  1309. void __fastcall TVmsCommMain::JobThreadStop()
  1310. {
  1311. g_AppCfg.bThrExit = true;
  1312. g_jobQ.PushBlocking(Q_CLOSE);
  1313. g_dbmQ.PushBlocking(Q_CLOSE);
  1314. g_logQ.PushBlocking(Q_CLOSE);
  1315. HANDLE handles[50];
  1316. int nWaits = 0;
  1317. for (thrIterator it=G_Threads.FObjects.begin(); it != G_Threads.FObjects.end(); ++it)
  1318. {
  1319. TItsThread *pObj = (TItsThread*)it->second;
  1320. if (!pObj->IsTerm())
  1321. {
  1322. //pObj->Terminate(2000);
  1323. handles[nWaits++] = pObj->Handle;
  1324. }
  1325. }
  1326. WaitForMultipleObjects(nWaits, handles, true, 1000);
  1327. G_Threads.RemoveAll();
  1328. }
  1329. //---------------------------------------------------------------------------
  1330. void __fastcall TVmsCommMain::TerminateApplication()
  1331. {
  1332. MERROR("MAIN Program terminated start");
  1333. SendTerminal();
  1334. CComm_Close();
  1335. MERROR("MAIN Program terminated end");
  1336. g_logQ.PushBlocking(Q_CLOSE);
  1337. for (int ii = 0; ii < 50; ii++)
  1338. {
  1339. Sleep(30);
  1340. Application->ProcessMessages();
  1341. if (g_AppCfg.bThrJobExit) break;
  1342. }
  1343. }
  1344. //---------------------------------------------------------------------------
  1345. void __fastcall TVmsCommMain::SendTerminal()
  1346. {
  1347. int result;
  1348. MINFO("MAIN SendTerminal");
  1349. // 동작, 통신, DB 모두 이상 처리
  1350. g_SysInfo->RunSts = state_error;
  1351. g_SysInfo->ComSts = state_error;
  1352. g_SysInfo->DbmSts = state_error;
  1353. // 모든 시설물 통신 오프라인 처리
  1354. CtlrItr it;
  1355. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  1356. {
  1357. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  1358. if (!pObj->Used) continue;
  1359. pObj->Server.thrState = thr_stop;
  1360. pObj->Client.thrState = thr_stop;
  1361. pObj->Server.commState = comm_close;
  1362. pObj->Client.commState = comm_close;
  1363. }
  1364. INT_PG_STATE_RES PgState;
  1365. memset(&PgState, 0x00, sizeof(PgState));
  1366. PgState.Count = 1;
  1367. PgState.Unit[0].Type = INT_ID_VMS_SERVER;
  1368. PgState.Unit[0].DB = g_SysInfo->DbmSts;
  1369. PgState.Unit[0].Comm = g_SysInfo->ComSts;
  1370. PgState.Unit[0].Action = g_SysInfo->RunSts;
  1371. result = CComm_CmmProcessStateNotify(&PgState);
  1372. TDMAdoDb *FDb = new TDMAdoDb(NULL);
  1373. try
  1374. {
  1375. if (FDb->Connect(g_AppCfg.db.sConnectStr))
  1376. {
  1377. CDSProcessManager->SaveUnitStts(FDb->GetConnection());
  1378. CDSCtlrManager->SaveCtlrSttsAll(FDb->GetConnection());
  1379. }
  1380. }
  1381. __finally
  1382. {
  1383. if (FDb)
  1384. {
  1385. FDb->Close();
  1386. }
  1387. SAFE_DELETE(FDb);
  1388. }
  1389. }
  1390. //---------------------------------------------------------------------------
  1391. int __fastcall TVmsCommMain::SendVmsThreadMessage(BYTE *AVmsId, IPC_MSG_DATA *ARcvData, BYTE AOpCode, char *AData)
  1392. {
  1393. #if 0
  1394. AnsiString sVmsId;
  1395. IPCUDPPACKET *pPI = NULL;
  1396. sVmsId.sprintf("%.*s", INT_VMS_MAX_ID, (char*)AVmsId);
  1397. TCDSCtlr *pObj = CDSCtlrManager->FLists.Find(sVmsId);
  1398. if (!pObj)
  1399. {
  1400. return VERR_NOTFOUND_VMS;
  1401. }
  1402. if (pObj->Server.commState != comm_close)
  1403. {
  1404. return VERR_OFFLINE;
  1405. }
  1406. if (!pObj->pPackQueue)
  1407. {
  1408. return VERR_INTERNAL;
  1409. }
  1410. pPI = new IPCUDPPACKET;
  1411. if (pPI == NULL) return VERR_MEMORY_ALLOC;
  1412. pPI->Sender.IP = ARcvData->Sender.IP;
  1413. pPI->Sender.Port = ARcvData->Sender.Port;
  1414. pPI->RecvId = ARcvData->Pkt.head.SendId;
  1415. pPI->OPCode = AOpCode;
  1416. pPI->Size = (int)ARcvData->Pkt.head.Length;
  1417. if (pPI->Size > 0)
  1418. {
  1419. pPI->Data = new char[pPI->Size];
  1420. if (!pPI->Data)
  1421. {
  1422. delete pPI;
  1423. return VERR_INTERNAL;
  1424. }
  1425. CopyMemory(pPI->Data, AData, pPI->Size);
  1426. }
  1427. pObj->pPackQueue->Add((void*)pPI);
  1428. #endif
  1429. return VERR_NONE;
  1430. }
  1431. //---------------------------------------------------------------------------
  1432. void __fastcall TVmsCommMain::plTimeDblClick(TObject *Sender)
  1433. {
  1434. if (!FrmNetwork->Showing)
  1435. {
  1436. FrmNetwork->Show();
  1437. }
  1438. }
  1439. //---------------------------------------------------------------------------
  1440. void __fastcall TVmsCommMain::tmrProvideTimer(TObject *Sender)
  1441. {
  1442. SendVmsTrafficProvide(2);
  1443. }
  1444. //---------------------------------------------------------------------------
  1445. void __fastcall TVmsCommMain::SendVmsTrafficProvide(int AType)
  1446. {
  1447. tmrProvide->Enabled = false;
  1448. MINFO("MAIN *** VMS Traffic Event Acitve, %d", AType);
  1449. APP_PostDbThreadMessage(dbm_dnld_form_cycle);
  1450. m_dtProvider = Now(); // 정보제공 타이머
  1451. int nProvideRemain = 0;
  1452. nProvideRemain = SecondOfTheDay(Now());
  1453. nProvideRemain %= g_AppCfg.nTrafficCycleTime;
  1454. nProvideRemain = g_AppCfg.nTrafficCycleTime - nProvideRemain;
  1455. nProvideRemain += g_AppCfg.nTrafficBasisTime;
  1456. if (AType == 0)
  1457. {
  1458. tmrProvide->Interval = nProvideRemain*1000;
  1459. //tmrProvide->Interval = (g_AppCfg.nTrafficCycleTime)*1000 + (g_AppCfg.nTrafficBasisTime*1000);
  1460. plRemain->Color = clBlack;
  1461. }
  1462. else
  1463. {
  1464. //tmrProvide->Interval = (g_AppCfg.nTrafficCycleTime)*1000; //분단위 주기만큼 타이머가동해야 함(소통정보 이벤트 처리가 없는 경우)
  1465. tmrProvide->Interval = nProvideRemain*1000; //분단위 주기만큼 타이머가동해야 함(소통정보 이벤트 처리가 없는 경우)
  1466. plRemain->Color = clGreen;
  1467. }
  1468. FFirstRunning = false;
  1469. //FProvideRemain = tmrProvide->Interval / 1000;
  1470. FProvideRemain = nProvideRemain;
  1471. tmrProvide->Enabled = true;
  1472. }
  1473. //---------------------------------------------------------------------------
  1474. void __fastcall TVmsCommMain::plRemainDblClick(TObject *Sender)
  1475. {
  1476. APP_ReLoadConfigInfo();
  1477. }
  1478. //---------------------------------------------------------------------------
  1479. void __fastcall TVmsCommMain::tmrDbCheckTimer(TObject *Sender)
  1480. {
  1481. tmrDbCheck->Enabled = false;
  1482. MERROR("DBMS Job Hang Error: %s", AnsiString(GetDbJobName(tmrDbCheck->Tag)).c_str());
  1483. bool bRestart = false;
  1484. String sRestart;
  1485. if (APP_ReadConfigInfo("APPLICATION", "RESTART", sRestart))
  1486. {
  1487. if (sRestart.UpperCase() == "TRUE")
  1488. {
  1489. bRestart = true;
  1490. }
  1491. }
  1492. if (bRestart)
  1493. {
  1494. MERROR("DBMS Job Hang Error: Server Self Die....");
  1495. String sRestartApp = g_sAppDir + "RestarterApp.exe";
  1496. if (FileExists(sRestartApp))
  1497. {
  1498. try {
  1499. for (thrIterator it=G_Threads.FObjects.begin(); it != G_Threads.FObjects.end(); ++it)
  1500. {
  1501. TItsThread *pObj = (TItsThread*)it->second;
  1502. if (pObj->Name == "DbmThread")
  1503. {
  1504. TDbmThread *dbmThread = (TDbmThread*)pObj;
  1505. dbmThread->CloseDb();
  1506. }
  1507. }
  1508. TerminateApplication();
  1509. JobThreadStop();
  1510. } catch(Exception &e) {
  1511. }
  1512. ShellExecute(NULL, NULL, AnsiString(sRestartApp).c_str(), NULL, NULL, SW_SHOWNORMAL);
  1513. g_AppCfg.bAppClose = true;
  1514. Close();
  1515. }
  1516. }
  1517. }
  1518. //---------------------------------------------------------------------------
  1519. String __fastcall TVmsCommMain::GetDbJobName(int nCommand)
  1520. {
  1521. switch(nCommand)
  1522. {
  1523. case dbm_save_unit_stts: return "dbm_save_unit_stts";
  1524. case dbm_save_ctlr_stts: return "dbm_save_ctlr_stts";
  1525. case dbm_parma_res: return "dbm_parma_res";
  1526. case dbm_save_ctlr_stts_all: return "dbm_save_ctlr_stts_all";
  1527. case dbm_load_onoff_time: return "dbm_load_onoff_time";
  1528. case dbm_load_download_job: return "dbm_load_download_job";
  1529. case dbm_save_form_download: return "dbm_save_form_download";
  1530. case dbm_dnld_form_init: return "dbm_dnld_form_init";
  1531. case dbm_dnld_form_cycle: return "dbm_dnld_form_cycle";
  1532. case dbm_dnld_form_mode: return "dbm_dnld_form_mode";
  1533. case dbm_provide_result: return "dbm_provide_result";
  1534. case dbm_master_symbol_image: return "dbm_master_symbol_image";
  1535. case dbm_master_symbol_traffic: return "dbm_master_symbol_traffic";
  1536. case dbm_master_form: return "dbm_master_form";
  1537. case dbm_master_vms_ifsc: return "dbm_master_vms_ifsc";
  1538. case dbm_check_db_alive: return "dbm_check_db_alive";
  1539. case dbm_initialize: return "dbm_initialize";
  1540. default:
  1541. break;
  1542. }
  1543. return String(nCommand);
  1544. }
  1545. //---------------------------------------------------------------------------