FrmMainF.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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. ClientSessionManager->Init(CDSCtlrManager->FLists.Size()*2);
  356. FrmControllerInfo->UpdateCtlrList();
  357. MINFO("MAIN Center communication initialize...");
  358. TCDSProcess *pPrcs = CDSProcessManager->FLists.Find(g_AppCfg.sSystemId);
  359. if (!pPrcs)
  360. {
  361. sMsg.printf(L"MAIN Center process information not found: %s, error %d", g_AppCfg.sSystemId.c_str(), nResult);
  362. MERROR(AnsiString(sMsg).c_str());
  363. return false;
  364. }
  365. g_AppCfg.comm.nCenterPort = pPrcs->PRGM_PORT.ToIntDef(g_AppCfg.comm.nCenterPort); // 디비에 설정된 포트를 사용
  366. //통신초기화를 최초 수행한 후 디비읽어서 처리
  367. nResult = CComm_Initialize((long)VmsCommMain->Handle, g_AppCfg.sSystemId, "VMS", g_AppCfg.comm.nCenterPort, g_AppCfg.sLogDay);
  368. if (VERR_NONE != nResult)
  369. {
  370. sMsg.printf(L"MAIN Center communication initialize failed: error %d", nResult);
  371. MERROR(AnsiString(sMsg).c_str());
  372. return false;
  373. }
  374. FOR_STL(TCDSProcess*, pObj, CDSProcessManager->FLists)
  375. {
  376. if (pObj->DEL_YN == "Y") continue;
  377. if (g_AppCfg.sSystemId == pObj->SYST_ID)
  378. {
  379. //자기자신은 목록에 추가하지 않는다...
  380. continue;
  381. }
  382. if (CComm_AddProcess(pObj->SYST_ID, pObj->SYST_TYPE, pObj->SYST_IP_1, pObj->PRGM_PORT) >= 0)
  383. {
  384. if ("UOT" == pObj->SYST_TYPE)
  385. {
  386. int nPort = pObj->PRGM_PORT.ToIntDef(0);
  387. CComm_AddProcess(pObj->SYST_ID, pObj->SYST_TYPE, pObj->SYST_IP_1, AnsiString(nPort+1)); /* VMS 운영단말 */
  388. #if 0
  389. CComm_AddProcess(pObj->SYST_ID, pObj->SYST_TYPE, pObj->SYST_IP_1, AnsiString(nPort+2)); /* CCTV 운영단말 */
  390. CComm_AddProcess(pObj->SYST_ID, pObj->SYST_TYPE, pObj->SYST_IP_1, AnsiString(nPort+3)); /* VDS 운영단말 */
  391. #endif
  392. }
  393. }
  394. }
  395. if (VERR_NONE != CComm_Open())
  396. {
  397. sMsg.printf(L"MAIN Center communication open failed: error %d", nResult);
  398. MERROR(AnsiString(sMsg).c_str());
  399. return false;
  400. }
  401. g_SysInfo->RunSts = state_normal;
  402. g_SysInfo->ComSts = state_error;
  403. g_SysInfo->DbmSts = state_normal;
  404. FTickState = 0;
  405. FServerRun = true;
  406. m_dtChkDbAive = Now(); // 데이터베이스 연결 체크 타이머
  407. m_dtChkVmsJob = Now(); // VMS 작업 및 주기작업
  408. m_dtProvider = Now()-1; // 정보제공 타이머
  409. COMM_MakeBasisTime(m_tTimer, g_AppCfg.nTrafficCycleTime, g_AppCfg.nTrafficBasisTime);
  410. //APP_PostDbThreadMessage(dbm_save_unit_stts);
  411. APP_PostDbThreadMessage(dbm_dnld_form_init);
  412. //통신서버 시작
  413. Sleep(500);
  414. tbConnectClick(this);
  415. MINFO("MAIN Program initialize success...[Completed]");
  416. return true;
  417. }
  418. //---------------------------------------------------------------------------
  419. void __fastcall TVmsCommMain::tmrAppStateTimer(TObject *Sender)
  420. {
  421. tmrAppState->Enabled = false;
  422. plTime->Caption = Now().FormatString("yyyy-MM-dd hh:nn:ss");
  423. if (!g_AppCfg.bAppLoad)
  424. {
  425. if (!InitApplication())
  426. {
  427. g_AppCfg.bAppClose = true;
  428. Close();
  429. return;
  430. }
  431. // 초기화작업완료
  432. CheckResource((g_AppCfg.MaxMemory * 1024), g_AppCfg.MaxHandle, g_AppCfg.MaxThread, g_AppCfg.MaxGdi);
  433. g_AppCfg.bAppLoad = true; // 프로그램이 로딩되었음을 확인
  434. tmrAppState->Interval = 990; // 프로그램 상태점검주기=1초
  435. tmrAppState->Enabled = true;
  436. return;
  437. }
  438. if (g_AppCfg.bAppClose)
  439. {
  440. // 응용프로그램 종료
  441. TerminateApplication();
  442. JobThreadStop();
  443. Close();
  444. return;
  445. }
  446. g_AppCfg.lMainWinHandle = (long)Handle;
  447. if (!(++FTickState % 5))
  448. {
  449. CheckCommState(); //통신상태정보 체크
  450. }
  451. UpdateProcessStatePanel();
  452. FCurrDay = StrToInt(Now().FormatString("dd"));
  453. FCurrHour = StrToInt(Now().FormatString("hh"));
  454. FCurrMin = StrToInt(Now().FormatString("nn"));
  455. FCurrSec = StrToInt(Now().FormatString("ss"));
  456. // 0시 3분에 시각 동기화 명령을 전송하자
  457. if (FCurrDay != FTimeSyncDay
  458. && FCurrMin != FTimeSyncMin
  459. && FCurrMin == 3
  460. && FCurrHour == 0)
  461. {
  462. FTimeSyncDay = FCurrDay;
  463. FTimeSyncMin = FCurrMin;
  464. #if 0
  465. SendTimeSync();
  466. #endif
  467. }
  468. // 프로세스 상태정보 이력 DB 입력
  469. if (FCurrSec > 1)
  470. {
  471. //매 1분마다 상태정보 업데이트 하고 상태정보 업데이트 하는 곳에서 5분주기로 이력정보를 저장한다.
  472. if (FUnitSttsHsMin != FCurrMin)
  473. {
  474. MINFO("MAIN >>> Save UNIT STTS");
  475. FUnitSttsHsMin = FCurrMin;
  476. APP_PostDbThreadMessage(dbm_save_unit_stts);
  477. }
  478. }
  479. // 제어기 상태정보 요청
  480. if (FCurrSec > 20)
  481. {
  482. //매 1분마다 상태정보 요청한다.
  483. if (FCtlrSttsReqMin != FCurrMin)
  484. {
  485. FCtlrSttsReqMin = FCurrMin;
  486. MINFO("MAIN >>> Req CTLR STTS");
  487. g_jobQ.PushBlocking(eVmsStatusReqAll);
  488. }
  489. }
  490. // 제어기 상태정보 이력 DB 입력
  491. if (FCurrSec > 30)
  492. {
  493. //매 1분마다 상태정보 업데이트 하고 상태정보 업데이트 하는 곳에서 5분주기로 이력정보를 저장한다.
  494. if (FCtlrSttsHsMin != FCurrMin)
  495. {
  496. FCtlrSttsHsMin = FCurrMin;
  497. MINFO("MAIN >>> Save CTLR STTS");
  498. APP_PostDbThreadMessage(dbm_save_ctlr_stts_all);
  499. }
  500. }
  501. if (FCurrSec > 40)
  502. {
  503. if (g_SysInfo->RunSts == state_normal && COMM_TimeDiff(m_dtChkVmsJob) > 60)
  504. {
  505. //VMS 주기적인 작업을 데이터베이스에서 조회
  506. MINFO("MAIN >>> Check VMS Job Schedule");
  507. APP_PostDbThreadMessage(dbm_load_onoff_time);
  508. //APP_PostDbThreadMessage(dbm_load_download_job);
  509. m_dtChkVmsJob = Now();
  510. }
  511. }
  512. if (FCurrSec > 10)
  513. {
  514. if (g_SysInfo->RunSts == state_normal && COMM_TimeDiff(m_dtChkDbAive) > 120)
  515. {
  516. //서버가 연결상태이고 DB 작업이 1분이상 경과된 경우 디비 연결상태를 체크한다.
  517. MINFO("MAIN >>> Check Database Alive");
  518. APP_PostDbThreadMessage(dbm_check_db_alive);
  519. m_dtChkDbAive = Now();
  520. }
  521. }
  522. plRemain->Caption = String(FProvideRemain--);
  523. if (FFirstRunning)
  524. {
  525. if ( (FCurrHour*3600 + FCurrMin*60) % g_AppCfg.nTrafficCycleTime == 0 &&
  526. FCurrSec >= g_AppCfg.nTrafficBasisTime )
  527. //if (CheckBasisTime(m_tTimer, g_AppCfg.nTrafficCycleTime, g_AppCfg.nTrafficBasisTime))
  528. {
  529. if (COMM_TimeDiff(m_dtProvider) > g_AppCfg.nTrafficCycleTime)
  530. {
  531. SendVmsTrafficProvide(1);
  532. }
  533. }
  534. }
  535. CheckProvideTimeout();
  536. if (!(FTickState % 60))
  537. {
  538. #if 0
  539. if (CheckResource((g_AppCfg.MaxMemory * 1024), g_AppCfg.MaxHandle, g_AppCfg.MaxThread, g_AppCfg.MaxGdi))
  540. {
  541. MERROR("MAIN Resource error");
  542. //AutoClose();
  543. }
  544. #endif
  545. }
  546. JobThreadCheck();
  547. #if 0
  548. g_logQ.PushBlocking(Q_TICK);
  549. g_dbmQ.PushBlocking(Q_TICK);
  550. #endif
  551. g_jobQ.PushBlocking(Q_TICK); //접속한 클라이언트 작업을 위해 스레드를 깨운다.
  552. tmrAppState->Enabled = true;
  553. }
  554. //---------------------------------------------------------------------------
  555. void __fastcall TVmsCommMain::AutoClose()
  556. {
  557. g_AppCfg.bAppClose = true;
  558. //Close();
  559. }
  560. //---------------------------------------------------------------------------
  561. void __fastcall TVmsCommMain::tbConnectClick(TObject *Sender)
  562. {
  563. g_AppCfg.bThrExit = false;
  564. //FrmNetwork->Show();
  565. if (FrmNetwork->Start())
  566. {
  567. FServerRun = true;
  568. tbConnect->Enabled = false;
  569. tbDisconnect->Enabled = true;
  570. }
  571. }
  572. //---------------------------------------------------------------------------
  573. void __fastcall TVmsCommMain::tbDisconnectClick(TObject *Sender)
  574. {
  575. FServerRun = false;
  576. g_jobQ.PushBlocking(eTcpCloseAll);
  577. FrmNetwork->Stop();
  578. tbDisconnect->Enabled = false;
  579. tbConnect->Enabled = true;
  580. }
  581. //---------------------------------------------------------------------------
  582. void __fastcall TVmsCommMain::tbOptionsClick(TObject *Sender)
  583. {
  584. FrmOption = new TFrmOption(this);
  585. FrmOption->Left = VmsCommMain->Left + 80;
  586. FrmOption->Top = VmsCommMain->Top + 80;
  587. FrmOption->ShowModal();
  588. if (FrmOption->FUpdate)
  589. {
  590. try
  591. {
  592. ITSLog->FLogCfg = g_LogCfg;
  593. CtlrItr it;
  594. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  595. {
  596. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  597. if (!pObj->FDispLog)
  598. {
  599. pObj->FCLog->FLogCfg = g_LogCfg;
  600. pObj->FSLog->FLogCfg = g_LogCfg;
  601. }
  602. }
  603. }
  604. catch(Exception &e)
  605. {
  606. }
  607. }
  608. delete FrmOption;
  609. FrmOption = NULL;
  610. }
  611. //---------------------------------------------------------------------------
  612. void __fastcall TVmsCommMain::CheckCommState()
  613. {
  614. //g_SysInfo->RunSts = (FServerRun && DMManager->TcpServer->Active) ? state_normal : state_error;
  615. g_SysInfo->RunSts = (FServerRun) ? state_normal : state_error;
  616. if (g_SysInfo->RunSts == state_normal)
  617. {
  618. if (CDSCtlrManager->Total == CDSCtlrManager->Normal) g_SysInfo->ComSts = state_normal;
  619. else g_SysInfo->ComSts = state_error;
  620. }
  621. else
  622. {
  623. g_SysInfo->ComSts = state_error;
  624. }
  625. }
  626. //---------------------------------------------------------------------------
  627. void __fastcall TVmsCommMain::UpdateProcessStatePanel()
  628. {
  629. plStateAction->Color = (g_SysInfo->RunSts == state_normal) ? clGreen : clRed;
  630. plStateComm->Color = (g_SysInfo->ComSts == state_normal) ? clGreen : clRed;
  631. plStateDb->Color = (g_SysInfo->DbmSts == state_normal) ? clGreen : clRed;
  632. }
  633. //---------------------------------------------------------------------------
  634. void __fastcall TVmsCommMain::OnWMDbThreadMessage(TMessage &Msg)
  635. {
  636. int nCommand = (int)Msg.WParam;
  637. int nResult = (int)Msg.LParam;
  638. ReplyMessage(0);
  639. if (nCommand == dbm_job_start)
  640. {
  641. tmrDbCheck->Enabled = true;
  642. return;
  643. }
  644. tmrDbCheck->Enabled = false;
  645. switch(nCommand)
  646. {
  647. case dbm_status:
  648. g_SysInfo->DbmSts = nResult >= 0 ? state_normal : state_error;
  649. break;
  650. case dbm_dnld_form_init:
  651. MINFO("MAIN VMS Message init start event");
  652. MakeVmsProvideForm();
  653. break;
  654. case dbm_dnld_form_cycle:
  655. case dbm_dnld_form_mode:
  656. MINFO("MAIN VMS Message create start event");
  657. MakeVmsProvideForm();
  658. APP_PostDbThreadMessage(dbm_save_form_download);
  659. break;
  660. case dbm_save_form_download:
  661. MINFO("MAIN VMS Message download start event");
  662. //클라이언트로 VMS폼 스케쥴이 저장되었다는 이벤트를 전송
  663. CDSManager->SendVmsDownloadForm();
  664. //VMS 제어기에 VMS폼 스케쥴을 다운로드 하도록 이벤트 전송
  665. g_jobQ.PushBlocking(eVmsFormDownload);
  666. CheckResource((g_AppCfg.MaxMemory * 1024), g_AppCfg.MaxHandle, g_AppCfg.MaxThread, g_AppCfg.MaxGdi);
  667. break;
  668. case dbm_initialize:
  669. break;
  670. }
  671. m_dtChkDbAive = Now(); // 데이터베이스 작업 성공/실패 여부와 상관없이 체크타이머 초기화
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TVmsCommMain::OnWMUserMessage(TMessage &Msg)
  675. {
  676. ReplyMessage(0);
  677. if ((int)Msg.WParam == WM_WINDOW_RESTORE)
  678. {
  679. Application->Restore();
  680. Application->BringToFront();
  681. return;
  682. }
  683. }
  684. //---------------------------------------------------------------------------
  685. void __fastcall TVmsCommMain::OnTcpThreadMessage(TMessage &Msg)
  686. {
  687. TCDSCtlr *pObj = (TCDSCtlr*)Msg.LParam;
  688. CTLR_STTS stts;
  689. switch(Msg.WParam)
  690. {
  691. case WM_TCP_SERVER_STTS: // 클라이언트의 연결 또는 연결종료시 수신 메시지
  692. try
  693. {
  694. if (pObj->Server.commState == comm_open)
  695. {
  696. pObj->dwConnectCnt++;
  697. MINFO("MAIN VMS Connect Event: %s", pObj->CTLR_NMBR.c_str());
  698. }
  699. else
  700. {
  701. MINFO("MAIN VMS Closed Event: %s", pObj->CTLR_NMBR.c_str());
  702. }
  703. } catch(Exception &e) {}
  704. FrmControllerInfo->UpdateServerState((int)pObj);
  705. stts.Type = 1;
  706. stts.ObjPtr = (void*)pObj;
  707. APP_PostDbThreadMessage(dbm_save_ctlr_stts, sizeof(stts), &stts);
  708. break;
  709. case WM_CTLR_STATE_STTS: // 연결된 제어기로 부터 상태정보를 수신할시
  710. try
  711. {
  712. MINFO("MAIN VMS State Event: %s", pObj->CTLR_NMBR.c_str());
  713. } catch(Exception &e) {}
  714. FrmControllerInfo->UpdateClientState((int)pObj);
  715. stts.Type = 0;
  716. stts.ObjPtr = (void*)pObj;
  717. APP_PostDbThreadMessage(dbm_save_ctlr_stts, sizeof(stts), &stts);
  718. break;
  719. }
  720. }
  721. //---------------------------------------------------------------------------
  722. void __fastcall TVmsCommMain::OnUdpDataRecvMessage(TMessage &Msg)
  723. {
  724. int nResult;
  725. IPC_MSG_DATA *RcvData = (IPC_MSG_DATA*)Msg.LParam;
  726. INT_SENDER *Sender = &RcvData->Sender;
  727. INT_PACKET *Pkt = &RcvData->Pkt;
  728. INT_HEAD *head = &Pkt->head;
  729. INT_DATA *data = &Pkt->data;
  730. char *pData = &Pkt->data.Data[0];
  731. BYTE OpCode = head->OpCode;
  732. MINFO("MAIN Center Msg: %s.%d, OpCode: %02X, From: %02X, To: %02X", CComm_ulong2ip(Sender->IP), Sender->Port, OpCode, head->SendId, head->RecvId);
  733. switch(OpCode)
  734. {
  735. case INT_OP_TRAFFIC_CHANGE:
  736. MINFO("MAIN INT_OP_TRAFFIC_CHANGE");
  737. CDSManager->FIsStaticCycle = true; //정주기 정보제공주기 플래그 설정
  738. SendVmsTrafficProvide(0);
  739. break;
  740. case INT_OP_VMS_POWER_CTL: //전광판 On/Off
  741. {
  742. INT_VMS_POWER_CTL_REQ *pReq = (INT_VMS_POWER_CTL_REQ*)pData;
  743. 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);
  744. TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR));
  745. if (!pCDSCtlr) break;
  746. if (!pCDSCtlr->FSession) break;
  747. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  748. pMsg->Type = eVmsStatusControl;
  749. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  750. pMsg->Buff[0] = 0x01;
  751. pMsg->Buff[1] = (BYTE)pReq->Command;
  752. pMsg->Len = 2;
  753. g_jobQ.PushBlocking((DWORD)pMsg);
  754. }
  755. break;
  756. case INT_OP_VMS_RESET:
  757. {
  758. INT_VMS_RESET_REQ *pReq = (INT_VMS_RESET_REQ*)pData;
  759. MINFO("MAIN INT_OP_VMS_RESET: %.*s %d, %s", INT_VMS_MAX_DATETIME, pReq->CmdTime, pReq->VMS_CTLR_NMBR, pReq->OperId);
  760. TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR));
  761. if (!pCDSCtlr) break;
  762. if (!pCDSCtlr->FSession) break;
  763. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  764. pMsg->Type = eVmsStatusControl;
  765. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  766. pMsg->Buff[0] = 0x02;
  767. pMsg->Len = 1;
  768. g_jobQ.PushBlocking((DWORD)pMsg);
  769. }
  770. break;
  771. case INT_OP_VMS_LUMINANCE_CTL: //휘도설정
  772. {
  773. INT_VMS_LUMINANCE_REQ *pReq = (INT_VMS_LUMINANCE_REQ*)pData;
  774. 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);
  775. TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR));
  776. if (!pCDSCtlr) break;
  777. if (!pCDSCtlr->FSession) break;
  778. BYTE cmdVal = 0x00;
  779. switch(pReq->BrghMode)
  780. {
  781. case 0x00: //주간
  782. cmdVal = pReq->BrghWeekStep;
  783. break;
  784. case 0x01: //야간
  785. cmdVal = pReq->BrghNghtStep;
  786. break;
  787. case 0x02: //자동
  788. cmdVal = 0x00;
  789. break;
  790. default: return;
  791. }
  792. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  793. pMsg->Type = eVmsStatusControl;
  794. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  795. pMsg->Buff[0] = 0x06;
  796. pMsg->Buff[1] = (BYTE)pReq->BrghMode;
  797. pMsg->Buff[2] = cmdVal;
  798. pMsg->Len = 3;
  799. g_jobQ.PushBlocking((DWORD)pMsg);
  800. }
  801. break;
  802. case INT_OP_VMS_PARA_SET: //환경설정 정보 설정
  803. {
  804. INT_VMS_PARA_SET_REQ *pReq = (INT_VMS_PARA_SET_REQ*)pData;
  805. MINFO("MAIN INT_OP_VMS_PARA_SET: %.*s %d, %s", INT_VMS_MAX_DATETIME, pReq->CmdTime, pReq->VMS_CTLR_NMBR, pReq->OperId);
  806. TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR));
  807. if (!pCDSCtlr) break;
  808. if (!pCDSCtlr->FSession) break;
  809. //도형식에는 설정정보가 없고 문자식에만 설정하는 프로토콜이 있음
  810. //현재 적용하지 않았음.........
  811. //적용할 경우 VMS유형(AObj->PROTOCOL_VER == 1) 확인해서 처리해야함
  812. //함체 PAN/HEATER 온도 설정
  813. //Download Parameter
  814. #if 0
  815. SIGNT_CONFIG_SET set;
  816. set.VMS_CTLR_NMBR = pCDSCtlr->CTLR_NMBR.ToIntDef(0);
  817. set.FanRunTmpr = pReq->FanRunTmpr;
  818. set.HetrRunTmpr = pReq->HetrRunTmpr;
  819. memcpy(set.PanlOnTime, pReq->DispStrTm, 4);
  820. memcpy(set.PanlOffTime, pReq->DispEndTm, 4);
  821. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  822. pMsg->Type = eVmsConfigSet;
  823. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  824. memcpy(pMsg->Buff, (char*)&set, sizeof(SIGNT_CONFIG_SET));
  825. g_jobQ.PushBlocking((DWORD)pMsg);
  826. #endif
  827. }
  828. break;
  829. case INT_OP_VMS_PARAM_REQ:
  830. {
  831. INT_VMS_PARAM_REQ *pReq = (INT_VMS_PARAM_REQ*)pData;
  832. MINFO("MAIN INT_OP_VMS_PARAM_REQ: %d", pReq->Count);
  833. for (int ii = 0; ii < pReq->Count && ii < INT_MAX_IMDT_SCN_DNLD_MAX; ii++)
  834. {
  835. TCDSCtlr *pCDSCtlr = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR[ii]));
  836. if (!pCDSCtlr) continue;
  837. if (!pCDSCtlr->FSession) continue;
  838. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  839. pMsg->Type = eVmsParamReq;
  840. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  841. g_jobQ.PushBlocking((DWORD)pMsg);
  842. }
  843. }
  844. break;
  845. case INT_OP_VMS_IMMEDIATE_SCN_DNLD:
  846. {
  847. INT_VMS_IMMEDIATE_SCENARIO_DNLD_REQ *pReq = (INT_VMS_IMMEDIATE_SCENARIO_DNLD_REQ*)pData;
  848. MINFO("MAIN INT_OP_VMS_IMMEDIATE_SCN_DNLD: %.*s, %s", INT_VMS_MAX_DATETIME, pReq->CmdTime, pReq->OperId);
  849. for (int ii = 0; ii < pReq->Count && ii < INT_MAX_IMDT_SCN_DNLD_MAX; ii++)
  850. {
  851. TCDSCtlr *pObj = CDSCtlrManager->FLists.Find(AnsiString(pReq->VMS_CTLR_NMBR[ii]));
  852. if (!pObj) continue;
  853. if (!pObj->FSession) continue;
  854. CDSCtlrManager->InitProvide(pObj);
  855. #if 0
  856. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  857. pMsg->Type = eVmsScenarioDownload;
  858. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  859. g_jobQ.PushBlocking((DWORD)pMsg);
  860. #endif
  861. }
  862. APP_PostDbThreadMessage(dbm_dnld_form_cycle);
  863. //APP_PostDbThreadMessage(dbm_dnld_form_mode);
  864. }
  865. break;
  866. case INT_OP_VMS_MODE:
  867. {
  868. INT_VMS_MODE_REQ *pReq = (INT_VMS_MODE_REQ*)pData;
  869. MINFO("MAIN INT_OP_VMS_MODE: %.*s %s, %s, %d EA", INT_VMS_MAX_DATETIME, pReq->CmdTime, pReq->OperId, pReq->Count);
  870. //모든 VMS에 메시지다운로드 플래그 비활성화 시킴
  871. //CDSCtlrManager->InitProvide(false);
  872. //수신한 VMS에 대하여 다운로드 플래그 활성화 시킴
  873. for(int ii = 0; ii < pReq->Count && ii < INT_VMS_MAX_MODE; ii++)
  874. {
  875. TCDSCtlr *pObj = CDSCtlrManager->FLists.Find(AnsiString(pReq->Unit[ii].VMS_CTLR_NMBR));
  876. if (!pObj) continue;
  877. if (pObj->Server.useFlag != use_enable) continue;
  878. if (pObj->CTRLMODE->Control == pReq->Unit[ii].Mode)
  879. pObj->FModeChange = false;
  880. else
  881. pObj->FModeChange = true;
  882. pObj->CTRLMODE->Control = pReq->Unit[ii].Mode;
  883. pObj->DSTATE.OprMode = pObj->CTRLMODE->Control;
  884. pObj->RSTATE.OprMode = pObj->CTRLMODE->Control;
  885. if (pObj->CTRLMODE->Control == 'A') pObj->OPER_MODE = "A";
  886. else if (pObj->CTRLMODE->Control == 'B') pObj->OPER_MODE = "B";
  887. else pObj->OPER_MODE = "F";
  888. //pObj->FProvide = true;
  889. CDSCtlrManager->InitProvide(pObj);
  890. }
  891. FrmControllerInfo->UpdateCtlrList();
  892. APP_PostDbThreadMessage(dbm_dnld_form_mode);
  893. }
  894. break;
  895. case INT_OP_VMS_STATE_REQ:
  896. MINFO("MAIN INT_OP_VMS_STATE_REQ");
  897. CDSCtlrManager->SendCtlrSttsAll();
  898. break;
  899. case INT_OP_VMS_IMAGE_SYMBOL:
  900. MINFO("MAIN INT_OP_VMS_IMAGE_SYMBOL");
  901. APP_PostDbThreadMessage(dbm_master_symbol_image);
  902. break;
  903. case INT_OP_VMS_IMAGE_TRAFFIC:
  904. MINFO("MAIN INT_OP_VMS_IMAGE_TRAFFIC");
  905. APP_PostDbThreadMessage(dbm_master_symbol_traffic);
  906. break;
  907. case INT_OP_VMS_FORM_UPDATE:
  908. MINFO("MAIN INT_OP_VMS_FORM_UPDATE");
  909. APP_PostDbThreadMessage(dbm_master_form);
  910. break;
  911. case INT_OP_VMS_IFSC_UPDATE:
  912. MINFO("MAIN INT_OP_VMS_IFSC_UPDATE");
  913. APP_PostDbThreadMessage(dbm_master_vms_ifsc);
  914. break;
  915. case INT_ID_TRAFFIC_SERVER:
  916. MINFO("MAIN INT_ID_TRAFFIC_SERVER");
  917. //APP_PostDbThreadMessage(dbm_master_vms_ifsc);
  918. break;
  919. default:
  920. MINFO("MAIN INT_UNKNOWN: %02X", OpCode);
  921. break;
  922. }
  923. }
  924. //---------------------------------------------------------------------------
  925. #if 0
  926. void __fastcall TVmsCommMain::ClientResponse(int Flag, REQ_INFO_STRUCT *pReq, int error, void *pData)
  927. {
  928. #ifdef CENTERCOMM
  929. BYTE ErrCode;
  930. int sResult;
  931. switch(error)
  932. {
  933. case SYS_ERR_NONE: break;
  934. case SYS_ERR_OTHER: ErrCode = INT_ERROR_OTHER; break;
  935. case SYS_ERR_INTERNAL: ErrCode = INT_ERROR_SYSTEM; break;
  936. case SYS_ERR_MEMORY: ErrCode = INT_ERROR_SYSTEM; break;
  937. case SYS_ERR_DATABASE: ErrCode = INT_ERROR_SYSTEM; break;
  938. case SYS_ERR_RESET: ErrCode = INT_ERROR_SYSTEM; break;
  939. case SYS_ERR_START: ErrCode = INT_ERROR_SYSTEM; break;
  940. case SYS_ERR_OFFLINE: ErrCode = INT_ERROR_OFFLINE; break;
  941. case SYS_ERR_NO_RES: ErrCode = INT_ERROR_NO_RESPONSE; break;
  942. case SYS_ERR_ACCESS: ErrCode = INT_ERROR_PROCESS; break;
  943. case SYS_ERR_INVALID: ErrCode = INT_ERROR_OTHER; break;
  944. case SYS_ERR_UNKNOWN: ErrCode = INT_ERROR_UNKNOWN_CODE; break;
  945. case SYS_ERR_WRITE_LENGTH: ErrCode = INT_ERROR_OTHER; break;
  946. case SYS_ERR_PACKET_SIZE_LARGE: ErrCode = INT_ERROR_OTHER; break;
  947. case SYS_ERR_INVALID_TAG: ErrCode = INT_ERROR_OTHER; break;
  948. case SYS_ERR_CRC: ErrCode = INT_ERROR_OTHER; break;
  949. case SYS_ERR_LENGTH: ErrCode = INT_ERROR_LENGTH; break;
  950. case SYS_ERR_ADDRESS: ErrCode = INT_ERROR_ADDRESS; break;
  951. case SYS_ERR_HEADER_OPTIONS: ErrCode = INT_ERROR_HEADER; break;
  952. case SYS_ERR_UNKNOWN_COMMAND: ErrCode = INT_ERROR_UNKNOWN_OPCODE; break;
  953. case SYS_ERR_RECEIVE_TIMEOUT: ErrCode = INT_ERROR_OTHER; break;
  954. case SYS_ERR_DATA_SIZE: ErrCode = INT_ERROR_DISCORD; break;
  955. case SYS_ERR_MISMATCH: ErrCode = INT_ERROR_OTHER; break;
  956. case SYS_ERR_INVALID_PARA: ErrCode = INT_ERROR_INVALID_DATA; break;
  957. case SYS_ERR_MOVEMENT: ErrCode = INT_ERROR_MOVEMENT; break;
  958. case SYS_ERR_UNAUTHORIZED: ErrCode = INT_ERROR_UNAUTHORIZED; break;
  959. default: ErrCode = INT_ERROR_OTHER; break;
  960. }
  961. if (Flag == SEND_ACK)
  962. {
  963. INT_ACK_RES Ack;
  964. memset(&Ack, 0x00, sizeof(Ack));
  965. Ack.OPCode.Type = INT_TYPE(pReq->OpCode);
  966. Ack.OPCode.Kind = INT_KIND(pReq->OpCode);
  967. Ack.MsgSeq = pReq->MsgSeq;
  968. if ((sResult = AckRes(&Ack, pReq->ProcessID)) != UDP_ERR_NONE)
  969. {
  970. MERROR("MAIN AckRes fail %d", sResult);
  971. }
  972. }
  973. else
  974. if (Flag == SEND_NACK)
  975. {
  976. INT_NACK_RES Nack;
  977. memset(&Nack, 0x00, sizeof(Nack));
  978. Nack.OPCode.Type = INT_TYPE(pReq->OpCode);
  979. Nack.OPCode.Kind = INT_KIND(pReq->OpCode);
  980. Nack.MsgSeq = pReq->MsgSeq;
  981. Nack.ErrCode = ErrCode;
  982. if ((sResult = NackRes(&Nack, pReq->ProcessID)) != UDP_ERR_NONE)
  983. {
  984. MERROR("MAIN NackRes fail %d", sResult);
  985. }
  986. }
  987. #endif
  988. }
  989. //---------------------------------------------------------------------------
  990. #endif
  991. bool __fastcall TVmsCommMain::CheckBasisTime(char *p, int Cycle, int Basis)
  992. {
  993. char cTime[15];
  994. COMM_MakeBasisTime(cTime, Cycle, Basis);
  995. if (memcmp(p, cTime, 14) != 0)
  996. {
  997. memcpy(p, cTime, 14);
  998. return true;
  999. }
  1000. return false;
  1001. }
  1002. //---------------------------------------------------------------------------
  1003. void __fastcall TVmsCommMain::CheckProvideTimeout()
  1004. {
  1005. VMS_PROVIDE_RESULE ProvideSave;
  1006. VMS_PROVIDE_RESULE DownLoadSave;
  1007. memset(&ProvideSave, 0x00, sizeof(ProvideSave));
  1008. memset(&DownLoadSave, 0x00, sizeof(DownLoadSave));
  1009. ProvideSave.Type = provide_form;
  1010. ProvideSave.Count = 0;
  1011. DownLoadSave.Type = provide_download;
  1012. DownLoadSave.Count = 0;
  1013. CtlrItr it;
  1014. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  1015. {
  1016. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  1017. if (pObj->Server.useFlag != use_enable) continue;
  1018. if ((pObj->DOWNLOAD->SaveFlag == false) &&
  1019. (COMM_TimeDiff(pObj->DOWNLOAD->dTimer) > VMS_DOWNLOAD_TIMEOUT))
  1020. {
  1021. DownLoadSave.pObj[DownLoadSave.Count++] = (void *)pObj;
  1022. pObj->DOWNLOAD->SaveFlag = true;
  1023. pObj->DOWNLOAD->dTimer = Now();
  1024. }
  1025. if ((pObj->CTRLMODE->SaveFlag == false) &&
  1026. (COMM_TimeDiff(pObj->CTRLMODE->pTimer) > VMS_PROVIDE_TIMEOUT))
  1027. {
  1028. ProvideSave.pObj[ProvideSave.Count++] = (void *)pObj;
  1029. pObj->CTRLMODE->SaveFlag = true;
  1030. pObj->CTRLMODE->pTimer = Now();
  1031. }
  1032. // 패킷크기가 채워졌으면 DB 작업스레드로 전송
  1033. if (ProvideSave.Count >= MAX_VMS_PROVIDE_RESULT)
  1034. {
  1035. APP_PostDbThreadMessage(dbm_provide_result, sizeof(ProvideSave), &ProvideSave);
  1036. }
  1037. if (DownLoadSave.Count >= MAX_VMS_PROVIDE_RESULT)
  1038. {
  1039. APP_PostDbThreadMessage(dbm_provide_result, sizeof(DownLoadSave), &DownLoadSave);
  1040. }
  1041. }
  1042. // 실패작업이 존재하면 DB 작업스레드로 전송
  1043. if (ProvideSave.Count > 0)
  1044. {
  1045. APP_PostDbThreadMessage(dbm_provide_result, sizeof(ProvideSave), &ProvideSave);
  1046. }
  1047. if (DownLoadSave.Count > 0)
  1048. {
  1049. APP_PostDbThreadMessage(dbm_provide_result, sizeof(DownLoadSave), &DownLoadSave);
  1050. }
  1051. }
  1052. //---------------------------------------------------------------------------
  1053. void __fastcall TVmsCommMain::SendTimeSync()
  1054. {
  1055. CtlrItr it;
  1056. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  1057. {
  1058. TCDSCtlr *pCDSCtlr = (TCDSCtlr*)it->second;
  1059. if (pCDSCtlr->Server.useFlag != use_enable) continue;
  1060. if (pCDSCtlr->FSession)
  1061. {
  1062. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  1063. pMsg->Type = eVmsStatusControl;
  1064. pMsg->ObjPtr = (DWORD)pCDSCtlr->FSession;
  1065. pMsg->Buff[0] = 0x04;
  1066. memcpy(&pMsg->Buff[1], AnsiString(Now().FormatString("yyyymmddhhnnss")).c_str(), 14);
  1067. pMsg->Len = 1+14;
  1068. g_jobQ.PushBlocking((DWORD)pMsg);
  1069. // 제어기가 연결되어 있으면 전송
  1070. MINFO("MAIN SendTimeSync command send: %s", pCDSCtlr->CTLR_NMBR.c_str());
  1071. }
  1072. }
  1073. }
  1074. //---------------------------------------------------------------------------
  1075. bool __fastcall TVmsCommMain::CheckResource(size_t MaxMemory, DWORD MaxHandle, DWORD MaxThread, DWORD MaxGdi)
  1076. {
  1077. bool result;
  1078. HANDLE hProcess;
  1079. PROCESS_MEMORY_COUNTERS pmc;
  1080. size_t uSize;
  1081. DWORD HandleCount, ThreadCount, GdiCount;
  1082. HANDLE hProcessSnap;
  1083. PROCESSENTRY32 pe32;
  1084. DWORD ProcessId;
  1085. result = false;
  1086. uSize = 0;
  1087. HandleCount = 0;
  1088. ThreadCount = 0;
  1089. GdiCount = 0;
  1090. hProcess = GetCurrentProcess();
  1091. if (hProcess != NULL)
  1092. {
  1093. if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
  1094. {
  1095. uSize = pmc.WorkingSetSize/1024;
  1096. if ((MaxMemory != 0) && (uSize > MaxMemory))
  1097. {
  1098. MERROR("MAIN Memory usage exceeds standards!!!...[%d][%d]", MaxMemory, uSize);
  1099. result = true;
  1100. }
  1101. }
  1102. if ( GetProcessHandleCount( hProcess, &HandleCount) )
  1103. {
  1104. if ((MaxHandle != 0) && (HandleCount > MaxHandle))
  1105. {
  1106. MERROR("MAIN Window handle exceed standards!!!...[%d][%d]", MaxHandle, HandleCount);
  1107. result = true;
  1108. }
  1109. }
  1110. ProcessId = GetCurrentProcessId();
  1111. if ((hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 )) != INVALID_HANDLE_VALUE)
  1112. {
  1113. pe32.dwSize = sizeof( PROCESSENTRY32 );
  1114. if ( Process32First( hProcessSnap, &pe32 ) )
  1115. {
  1116. do
  1117. {
  1118. if (pe32.th32ProcessID == ProcessId)
  1119. {
  1120. ThreadCount = pe32.cntThreads;
  1121. if ((MaxThread != 0) && (ThreadCount > MaxThread))
  1122. {
  1123. MERROR("MAIN Criteria exceeded thread!!!...[%d][%d]", MaxThread, ThreadCount);
  1124. result = true;
  1125. }
  1126. break;
  1127. }
  1128. } while( Process32Next( hProcessSnap, &pe32 ) );
  1129. }
  1130. CloseHandle( hProcessSnap );
  1131. }
  1132. GdiCount = GetGuiResources( hProcess, GR_GDIOBJECTS);
  1133. if ((MaxGdi != 0) && (GdiCount > MaxGdi))
  1134. {
  1135. MERROR("MAIN GDI Objects exceeding standards!!!...[%d][%d]", MaxGdi, GdiCount);
  1136. result = true;
  1137. }
  1138. CloseHandle( hProcess );
  1139. }
  1140. try
  1141. {
  1142. FMemSize = uSize;
  1143. plMemory->Caption = FormatFloat("##,##0", uSize) ;// + " KB";
  1144. plHandle->Caption = FormatFloat("##,##0", HandleCount);// + " EA";
  1145. plThread->Caption = FormatFloat("##,##0", ThreadCount);// + " EA";
  1146. plGdi->Caption = FormatFloat("##,##0", GdiCount) ;// + " EA";
  1147. MINFO("MAIN Memory: %s, Haldle: %s, Thread: %s, GDI: %s",
  1148. AnsiString(plMemory->Caption).c_str(),
  1149. AnsiString(plHandle->Caption).c_str(),
  1150. AnsiString(plThread->Caption).c_str(),
  1151. AnsiString(plGdi->Caption).c_str()
  1152. );
  1153. } catch (Exception &e) {}
  1154. return result;
  1155. }
  1156. //---------------------------------------------------------------------------
  1157. void __fastcall TVmsCommMain::Panel16DblClick(TObject *Sender)
  1158. {
  1159. CheckResource((g_AppCfg.MaxMemory * 1024), g_AppCfg.MaxHandle, g_AppCfg.MaxThread, g_AppCfg.MaxGdi);
  1160. }
  1161. //---------------------------------------------------------------------------
  1162. void __fastcall TVmsCommMain::plStateDbDblClick(TObject *Sender)
  1163. {
  1164. APP_PostDbThreadMessage(dbm_master_symbol_image);
  1165. APP_PostDbThreadMessage(dbm_master_form);
  1166. APP_PostDbThreadMessage(dbm_master_vms_ifsc);
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. void __fastcall TVmsCommMain::plStateCommDblClick(TObject *Sender)
  1170. {
  1171. AnsiString VMS_IFSC_ID;
  1172. int SECT_LNGT;
  1173. TItsMap<int, TObjectId*> FLists;
  1174. TItsMap<AnsiString, TVmsIfscGrad*> FGrads;
  1175. VMS_TRAF traf;
  1176. int SPED; // N NUMBER(3) Y 0 속도
  1177. int TRVL_HH; // N NUMBER(6) Y 0 통행 시간
  1178. AnsiString CMTR_GRAD_CD; // N VARCHAR2(7) Y 소통 등급 코드
  1179. if (!CDSVmsIfscTrafManager) return;
  1180. FOR_STL(TCDSVmsIfscTraf*, pObj, CDSVmsIfscTrafManager->FVmsIfscLists)
  1181. {
  1182. int objValid = pObj->IsValid == false ? 0 : 1;
  1183. MINFO("VMS_IFSC_ID: %s, SECT_LNGT: %5d, VALID: %d, Objects: %d, SPED: %3d, TRVL_HH: %4d, GRAD_CD: %s",
  1184. pObj->VMS_IFSC_ID, pObj->SECT_LNGT, objValid, pObj->FLists.Size(), pObj->traf.SPED, pObj->traf.TRVL_HH, pObj->traf.CMTR_GRAD_CD);
  1185. FOR_STL(TVmsIfscGrad*, pGrad, pObj->FGrads)
  1186. {
  1187. int gradValid = pGrad->IsValid == false ? 0 : 1;
  1188. MINFO(" GRAD_CD: %s, VALID: %d, LWSTSPED: %3d, HGHSSPED: %3d", pGrad->CMTR_GRAD_CD, gradValid, pGrad->LWSTSPED, pGrad->HGHSSPED);
  1189. }
  1190. int ii = 1;
  1191. FOR_STL(TObjectId*, pID, pObj->FLists)
  1192. {
  1193. int idValid = pID->IsValid == false ? 0 : 1;
  1194. TCDSIfscTraf* pIfsc = CDSVmsIfscTrafManager->FIfscTrafLists.Find(pID->ID);
  1195. if (pIfsc)
  1196. {
  1197. 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);
  1198. }
  1199. else
  1200. {
  1201. MINFO(" %2d: IFSC_ID: %s, Not Found", ii, pID->ID);
  1202. }
  1203. ii++;
  1204. }
  1205. MINFO("");
  1206. }
  1207. }
  1208. //---------------------------------------------------------------------------
  1209. void __fastcall TVmsCommMain::plStateActionDblClick(TObject *Sender)
  1210. {
  1211. plStateAction->Enabled = false;
  1212. tmrRunProvide->Enabled = false;
  1213. tmrRunProvide->Enabled = true;
  1214. CDSManager->FIsStaticCycle = true; //정주기 정보제공주기 플래그 설정
  1215. CtlrItr it;
  1216. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  1217. {
  1218. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  1219. if (!pObj->FSession) continue;
  1220. CDSCtlrManager->InitProvide(pObj);
  1221. }
  1222. APP_PostDbThreadMessage(dbm_dnld_form_cycle);
  1223. }
  1224. //---------------------------------------------------------------------------
  1225. void __fastcall TVmsCommMain::tmrRunProvideTimer(TObject *Sender)
  1226. {
  1227. tmrRunProvide->Enabled = false;
  1228. plStateAction->Enabled = true;
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. bool __fastcall TVmsCommMain::JobThreadStart()
  1232. {
  1233. int nThreadCnt = 0;
  1234. if (true)
  1235. {
  1236. LogThread = new TLogThread();
  1237. if (!LogThread) return false;
  1238. LogThread->FPnlState = plStateLog;
  1239. LogThread->FTmrDb = tmrDbCheck;
  1240. LogThread->Name = "LogThread";
  1241. LogThread->Execute();
  1242. G_Threads.Push(nThreadCnt++, LogThread);
  1243. }
  1244. if (true)
  1245. {
  1246. DbmThread = new TDbmThread();
  1247. if (!DbmThread) return false;
  1248. DbmThread->FPnlState = plStateDbm;
  1249. DbmThread->FTmrDb = tmrDbCheck;
  1250. DbmThread->Name = "DbmThread";
  1251. DbmThread->Execute();
  1252. G_Threads.Push(nThreadCnt++, DbmThread);
  1253. }
  1254. if (true)
  1255. {
  1256. JobThread = new TJobThread();
  1257. if (!JobThread) return false;
  1258. JobThread->FPnlState = plStateJob;
  1259. JobThread->FTmrDb = tmrDbCheck;
  1260. JobThread->Name = "JobThread";
  1261. JobThread->Execute();
  1262. G_Threads.Push(nThreadCnt++, JobThread);
  1263. }
  1264. return true;
  1265. }
  1266. //---------------------------------------------------------------------------
  1267. void __fastcall TVmsCommMain::JobThreadCheck()
  1268. {
  1269. int nTermCnt = 0;
  1270. for (thrIterator it=G_Threads.FObjects.begin(); it != G_Threads.FObjects.end(); ++it)
  1271. {
  1272. TItsThread *pObj = (TItsThread*)it->second;
  1273. if (pObj->IsTerm())
  1274. {
  1275. pObj->Execute();
  1276. nTermCnt++;
  1277. }
  1278. }
  1279. }
  1280. //---------------------------------------------------------------------------
  1281. void __fastcall TVmsCommMain::JobThreadStop()
  1282. {
  1283. g_AppCfg.bThrExit = true;
  1284. g_jobQ.PushBlocking(Q_CLOSE);
  1285. g_dbmQ.PushBlocking(Q_CLOSE);
  1286. g_logQ.PushBlocking(Q_CLOSE);
  1287. HANDLE handles[50];
  1288. int nWaits = 0;
  1289. for (thrIterator it=G_Threads.FObjects.begin(); it != G_Threads.FObjects.end(); ++it)
  1290. {
  1291. TItsThread *pObj = (TItsThread*)it->second;
  1292. if (!pObj->IsTerm())
  1293. {
  1294. //pObj->Terminate(2000);
  1295. handles[nWaits++] = pObj->Handle;
  1296. }
  1297. }
  1298. WaitForMultipleObjects(nWaits, handles, true, 1000);
  1299. G_Threads.RemoveAll();
  1300. }
  1301. //---------------------------------------------------------------------------
  1302. void __fastcall TVmsCommMain::TerminateApplication()
  1303. {
  1304. MERROR("MAIN Program terminated start");
  1305. SendTerminal();
  1306. CComm_Close();
  1307. MERROR("MAIN Program terminated end");
  1308. g_logQ.PushBlocking(Q_CLOSE);
  1309. for (int ii = 0; ii < 50; ii++)
  1310. {
  1311. Sleep(30);
  1312. Application->ProcessMessages();
  1313. if (g_AppCfg.bThrJobExit) break;
  1314. }
  1315. }
  1316. //---------------------------------------------------------------------------
  1317. void __fastcall TVmsCommMain::SendTerminal()
  1318. {
  1319. int result;
  1320. MINFO("MAIN SendTerminal");
  1321. // 동작, 통신, DB 모두 이상 처리
  1322. g_SysInfo->RunSts = state_error;
  1323. g_SysInfo->ComSts = state_error;
  1324. g_SysInfo->DbmSts = state_error;
  1325. // 모든 시설물 통신 오프라인 처리
  1326. CtlrItr it;
  1327. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  1328. {
  1329. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  1330. if (!pObj->Used) continue;
  1331. pObj->Server.thrState = thr_stop;
  1332. pObj->Client.thrState = thr_stop;
  1333. pObj->Server.commState = comm_close;
  1334. pObj->Client.commState = comm_close;
  1335. }
  1336. INT_PG_STATE_RES PgState;
  1337. memset(&PgState, 0x00, sizeof(PgState));
  1338. PgState.Count = 1;
  1339. PgState.Unit[0].Type = INT_ID_VMS_SERVER;
  1340. PgState.Unit[0].DB = g_SysInfo->DbmSts;
  1341. PgState.Unit[0].Comm = g_SysInfo->ComSts;
  1342. PgState.Unit[0].Action = g_SysInfo->RunSts;
  1343. result = CComm_CmmProcessStateNotify(&PgState);
  1344. TDMAdoDb *FDb = new TDMAdoDb(NULL);
  1345. try
  1346. {
  1347. if (FDb->Connect(g_AppCfg.db.sConnectStr))
  1348. {
  1349. CDSProcessManager->SaveUnitStts(FDb->GetConnection());
  1350. CDSCtlrManager->SaveCtlrSttsAll(FDb->GetConnection());
  1351. }
  1352. }
  1353. __finally
  1354. {
  1355. if (FDb)
  1356. {
  1357. FDb->Close();
  1358. }
  1359. SAFE_DELETE(FDb);
  1360. }
  1361. }
  1362. //---------------------------------------------------------------------------
  1363. int __fastcall TVmsCommMain::SendVmsThreadMessage(BYTE *AVmsId, IPC_MSG_DATA *ARcvData, BYTE AOpCode, char *AData)
  1364. {
  1365. #if 0
  1366. AnsiString sVmsId;
  1367. IPCUDPPACKET *pPI = NULL;
  1368. sVmsId.sprintf("%.*s", INT_VMS_MAX_ID, (char*)AVmsId);
  1369. TCDSCtlr *pObj = CDSCtlrManager->FLists.Find(sVmsId);
  1370. if (!pObj)
  1371. {
  1372. return VERR_NOTFOUND_VMS;
  1373. }
  1374. if (pObj->Server.commState != comm_close)
  1375. {
  1376. return VERR_OFFLINE;
  1377. }
  1378. if (!pObj->pPackQueue)
  1379. {
  1380. return VERR_INTERNAL;
  1381. }
  1382. pPI = new IPCUDPPACKET;
  1383. if (pPI == NULL) return VERR_MEMORY_ALLOC;
  1384. pPI->Sender.IP = ARcvData->Sender.IP;
  1385. pPI->Sender.Port = ARcvData->Sender.Port;
  1386. pPI->RecvId = ARcvData->Pkt.head.SendId;
  1387. pPI->OPCode = AOpCode;
  1388. pPI->Size = (int)ARcvData->Pkt.head.Length;
  1389. if (pPI->Size > 0)
  1390. {
  1391. pPI->Data = new char[pPI->Size];
  1392. if (!pPI->Data)
  1393. {
  1394. delete pPI;
  1395. return VERR_INTERNAL;
  1396. }
  1397. CopyMemory(pPI->Data, AData, pPI->Size);
  1398. }
  1399. pObj->pPackQueue->Add((void*)pPI);
  1400. #endif
  1401. return VERR_NONE;
  1402. }
  1403. //---------------------------------------------------------------------------
  1404. void __fastcall TVmsCommMain::plTimeDblClick(TObject *Sender)
  1405. {
  1406. if (!FrmNetwork->Showing)
  1407. {
  1408. FrmNetwork->Show();
  1409. }
  1410. }
  1411. //---------------------------------------------------------------------------
  1412. void __fastcall TVmsCommMain::tmrProvideTimer(TObject *Sender)
  1413. {
  1414. SendVmsTrafficProvide(2);
  1415. }
  1416. //---------------------------------------------------------------------------
  1417. void __fastcall TVmsCommMain::SendVmsTrafficProvide(int AType)
  1418. {
  1419. tmrProvide->Enabled = false;
  1420. MINFO("MAIN *** VMS Traffic Event Acitve, %d", AType);
  1421. APP_PostDbThreadMessage(dbm_dnld_form_cycle);
  1422. m_dtProvider = Now(); // 정보제공 타이머
  1423. int nProvideRemain = 0;
  1424. nProvideRemain = SecondOfTheDay(Now());
  1425. nProvideRemain %= g_AppCfg.nTrafficCycleTime;
  1426. nProvideRemain = g_AppCfg.nTrafficCycleTime - nProvideRemain;
  1427. nProvideRemain += g_AppCfg.nTrafficBasisTime;
  1428. if (AType == 0)
  1429. {
  1430. tmrProvide->Interval = nProvideRemain*1000;
  1431. //tmrProvide->Interval = (g_AppCfg.nTrafficCycleTime)*1000 + (g_AppCfg.nTrafficBasisTime*1000);
  1432. plRemain->Color = clBlack;
  1433. }
  1434. else
  1435. {
  1436. //tmrProvide->Interval = (g_AppCfg.nTrafficCycleTime)*1000; //분단위 주기만큼 타이머가동해야 함(소통정보 이벤트 처리가 없는 경우)
  1437. tmrProvide->Interval = nProvideRemain*1000; //분단위 주기만큼 타이머가동해야 함(소통정보 이벤트 처리가 없는 경우)
  1438. plRemain->Color = clGreen;
  1439. }
  1440. FFirstRunning = false;
  1441. //FProvideRemain = tmrProvide->Interval / 1000;
  1442. FProvideRemain = nProvideRemain;
  1443. tmrProvide->Enabled = true;
  1444. }
  1445. //---------------------------------------------------------------------------
  1446. void __fastcall TVmsCommMain::plRemainDblClick(TObject *Sender)
  1447. {
  1448. APP_ReLoadConfigInfo();
  1449. }
  1450. //---------------------------------------------------------------------------
  1451. void __fastcall TVmsCommMain::tmrDbCheckTimer(TObject *Sender)
  1452. {
  1453. tmrDbCheck->Enabled = false;
  1454. MERROR("DBMS Job Hang Error: %s", AnsiString(GetDbJobName(tmrDbCheck->Tag)).c_str());
  1455. bool bRestart = false;
  1456. String sRestart;
  1457. if (APP_ReadConfigInfo("APPLICATION", "RESTART", sRestart))
  1458. {
  1459. if (sRestart.UpperCase() == "TRUE")
  1460. {
  1461. bRestart = true;
  1462. }
  1463. }
  1464. if (bRestart)
  1465. {
  1466. MERROR("DBMS Job Hang Error: Server Self Die....");
  1467. String sRestartApp = g_sAppDir + "RestarterApp.exe";
  1468. if (FileExists(sRestartApp))
  1469. {
  1470. try {
  1471. for (thrIterator it=G_Threads.FObjects.begin(); it != G_Threads.FObjects.end(); ++it)
  1472. {
  1473. TItsThread *pObj = (TItsThread*)it->second;
  1474. if (pObj->Name == "DbmThread")
  1475. {
  1476. TDbmThread *dbmThread = (TDbmThread*)pObj;
  1477. dbmThread->CloseDb();
  1478. }
  1479. }
  1480. TerminateApplication();
  1481. JobThreadStop();
  1482. } catch(Exception &e) {
  1483. }
  1484. ShellExecute(NULL, NULL, AnsiString(sRestartApp).c_str(), NULL, NULL, SW_SHOWNORMAL);
  1485. g_AppCfg.bAppClose = true;
  1486. Close();
  1487. }
  1488. }
  1489. }
  1490. //---------------------------------------------------------------------------
  1491. String __fastcall TVmsCommMain::GetDbJobName(int nCommand)
  1492. {
  1493. switch(nCommand)
  1494. {
  1495. case dbm_save_unit_stts: return "dbm_save_unit_stts";
  1496. case dbm_save_ctlr_stts: return "dbm_save_ctlr_stts";
  1497. case dbm_parma_res: return "dbm_parma_res";
  1498. case dbm_save_ctlr_stts_all: return "dbm_save_ctlr_stts_all";
  1499. case dbm_load_onoff_time: return "dbm_load_onoff_time";
  1500. case dbm_load_download_job: return "dbm_load_download_job";
  1501. case dbm_save_form_download: return "dbm_save_form_download";
  1502. case dbm_dnld_form_init: return "dbm_dnld_form_init";
  1503. case dbm_dnld_form_cycle: return "dbm_dnld_form_cycle";
  1504. case dbm_dnld_form_mode: return "dbm_dnld_form_mode";
  1505. case dbm_provide_result: return "dbm_provide_result";
  1506. case dbm_master_symbol_image: return "dbm_master_symbol_image";
  1507. case dbm_master_symbol_traffic: return "dbm_master_symbol_traffic";
  1508. case dbm_master_form: return "dbm_master_form";
  1509. case dbm_master_vms_ifsc: return "dbm_master_vms_ifsc";
  1510. case dbm_check_db_alive: return "dbm_check_db_alive";
  1511. case dbm_initialize: return "dbm_initialize";
  1512. default:
  1513. break;
  1514. }
  1515. return String(nCommand);
  1516. }
  1517. //---------------------------------------------------------------------------