FrmMainF.cpp 56 KB

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