FrmMainF.cpp 58 KB

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