AppGlobalF.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. //---------------------------------------------------------------------------
  2. #pragma hdrstop
  3. //---------------------------------------------------------------------------
  4. #include <vcl.h>
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <memory.h>
  10. #include <Systobj.h>
  11. #include <time.h>
  12. #include <Buttons.hpp>
  13. #include <Classes.hpp>
  14. #include <ComCtrls.hpp>
  15. #include <Controls.hpp>
  16. #include <Chart.hpp>
  17. #include <DB.hpp>
  18. #include <DBClient.hpp>
  19. #include <DBGrids.hpp>
  20. #include <DBXpress.hpp>
  21. #include <Dialogs.hpp>
  22. #include <ExtCtrls.hpp>
  23. #include <FMTBcd.hpp>
  24. #include <Forms.hpp>
  25. #include <Graphics.hpp>
  26. #include <Grids.hpp>
  27. #include <jpeg.hpp>
  28. #include <Mask.hpp>
  29. #include <Menus.hpp>
  30. #include <Messages.hpp>
  31. #include <OleCtrls.hpp>
  32. #include <Outline.hpp>
  33. #include <Provider.hpp>
  34. #include <SqlExpr.hpp>
  35. #include <StdCtrls.hpp>
  36. #include <System.hpp>
  37. #include <SysUtils.hpp>
  38. #include <Types.hpp>
  39. //#include <Windows.hpp>
  40. #include <inifiles.hpp>
  41. //---------------------------------------------------------------------------
  42. #include "AppGlobalF.h"
  43. //---------------------------------------------------------------------------
  44. #pragma package(smart_init)
  45. //---------------------------------------------------------------------------
  46. //---------------------------------------------------------------------------
  47. /*
  48. * 프로그램 환경설정 파일에서 설정 정보를 읽어온다.
  49. */
  50. bool APP_LoadConfigInfo()
  51. {
  52. String sCfgFile;
  53. TIniFile *pIniFile = NULL;
  54. sCfgFile = g_sCfgDir + g_sAppName + ".ini";
  55. g_AppCfg.sConfigFile = sCfgFile;
  56. DATABASE_INFO *pDb;
  57. try
  58. {
  59. String sTmp;
  60. pIniFile = new TIniFile(sCfgFile);
  61. g_AppCfg.sTitle = pIniFile->ReadString("APPLICATION", "TITLE", "VMS Communication Server");
  62. g_AppCfg.sSystemId = pIniFile->ReadString("APPLICATION", "SYSTEMID", "VMS01");
  63. g_AppCfg.sProcessId = pIniFile->ReadString("APPLICATION", "PROCESSID", "30101");
  64. g_AppCfg.nProcessId = g_AppCfg.sProcessId.ToIntDef(0);
  65. g_AppCfg.nProcessPort = pIniFile->ReadInteger("APPLICATION", "PROCESSPORT", 5301);;
  66. g_AppCfg.DownloadBitmap = pIniFile->ReadInteger("APPLICATION", "DOWNLOADBITMAP", 0) == 1 ? true : false;
  67. LOAD_DB = pIniFile->ReadInteger("APPLICATION", "LOADDB", 1) == 1 ? true: false;
  68. g_sFtpHomeDir = pIniFile->ReadString("APPLICATION", "FTPHOMEDIR", g_sFtpHomeDir);
  69. if (g_sFtpHomeDir.SubString(g_sFtpHomeDir.Length(), 1) != "\\")
  70. g_sFtpHomeDir += "\\";
  71. g_AppCfg.nTrafficCycleTime = pIniFile->ReadInteger("APPLICATION", "TrafficCycleTime", 300);
  72. if (g_AppCfg.nTrafficCycleTime < 60) g_AppCfg.nTrafficCycleTime = 60;
  73. g_AppCfg.nTrafficBasisTime = pIniFile->ReadInteger("APPLICATION", "TrafficBasisTime", 60);
  74. g_AppCfg.nSaveFormType = pIniFile->ReadInteger("APPLICATION", "SaveFormType", 0);
  75. if (g_AppCfg.nSaveFormType < 0 || g_AppCfg.nSaveFormType > 2) g_AppCfg.nSaveFormType = 0;
  76. g_AppCfg.nDefaultFillColor = pIniFile->ReadInteger("APPLICATION", "DefaultFillColor", 0);
  77. //정체폼 판정 주기(즉, 주기값이상인 경우 정체폼생성 >= )
  78. g_AppCfg.CngsContCnt = pIniFile->ReadString("APPLICATION", "CngsContCnt", "2").ToIntDef(2);
  79. if (g_AppCfg.CngsContCnt < 1) g_AppCfg.CngsContCnt = 1;
  80. //1단 교통정보폼 중앙정렬 여부
  81. g_AppCfg.Traf1RowCenter = pIniFile->ReadString ("APPLICATION", "Traffic1RowCenter", "0").ToIntDef(0) == 1 ? true : false;
  82. g_AppCfg.IsImageSeqSave = pIniFile->ReadString ("APPLICATION", "IMAGESEQSAVE", "0").ToIntDef(0) == 1 ? true : false;
  83. g_AppCfg.MaxMemory = pIniFile->ReadInteger("RESOURCE", "MAXMEMORY", 512);
  84. g_AppCfg.MaxHandle = pIniFile->ReadInteger("RESOURCE", "MAXHANDLE", 0);
  85. g_AppCfg.MaxThread = pIniFile->ReadInteger("RESOURCE", "MAXTHREAD", 4096);
  86. g_AppCfg.MaxGdi = pIniFile->ReadInteger("RESOURCE", "MAXGDI", 0);
  87. g_AppCfg.RcRestart = (bool)pIniFile->ReadInteger("RESOURCE", "RESTART", 0);
  88. g_AppCfg.comm.nCenterPort = pIniFile->ReadInteger("COMMUNICATION", "CENTERPORT", 6020);
  89. g_AppCfg.comm.nListenPort = (short)pIniFile->ReadInteger("COMMUNICATION", "LISTENPORT", 6020);
  90. g_AppCfg.comm.sLocalDomain = pIniFile->ReadString ("COMMUNICATION", "LOCALDOMAIN", "VMS_SERVER");
  91. g_AppCfg.comm.nClientPort = pIniFile->ReadInteger("COMMUNICATION", "CLIENTPORT", 355);
  92. g_AppCfg.comm.sLoginUser = pIniFile->ReadString ("COMMUNICATION", "LOGINUSER", "ysvms");
  93. g_AppCfg.comm.nConnectWait = pIniFile->ReadInteger("COMMUNICATION", "CONNECTWAIT", 60);
  94. g_AppCfg.comm.nCmdTimeOut = pIniFile->ReadInteger("COMMUNICATION", "CMDTIMEOUT", 5);
  95. g_AppCfg.comm.nWatchDogTime = pIniFile->ReadInteger("COMMUNICATION", "WATCHDOGTIME", 30);
  96. g_AppCfg.comm.nPollingTime = pIniFile->ReadInteger("COMMUNICATION", "POLLINGTIME", 20);
  97. g_AppCfg.IsIpChecking = pIniFile->ReadString ("COMMUNICATION", "IPCHECKING", "1").ToIntDef(0) == 1 ? true : false;
  98. if (g_AppCfg.comm.nPollingTime < 10) g_AppCfg.comm.nPollingTime = 10;
  99. if (g_AppCfg.comm.nPollingTime > 90) g_AppCfg.comm.nPollingTime = 90;
  100. if (g_AppCfg.comm.nConnectWait < 30) g_AppCfg.comm.nConnectWait = 30;
  101. if (g_AppCfg.comm.nConnectWait > 60) g_AppCfg.comm.nConnectWait = 60;
  102. if (g_AppCfg.comm.nCmdTimeOut < 5) g_AppCfg.comm.nCmdTimeOut = 5;
  103. if (g_AppCfg.comm.nWatchDogTime < 30) g_AppCfg.comm.nWatchDogTime = 30;
  104. g_AppCfg.comm.nWatchDogTime += 5; // 5초정도를 더 추가해 준다(딜레이발생할 경우)
  105. g_AppCfg.BottomTrafficCycle = pIniFile->ReadString("FIGURE", "TRAFFIC_CYCLE", "0").ToIntDef(0);
  106. g_AppCfg.BottomTrafficMaxCnt = pIniFile->ReadString("FIGURE", "TRAFFIC_MAX", "1").ToIntDef(0);
  107. g_AppCfg.BottomTrafficCenter = pIniFile->ReadString("FIGURE", "TRAFFIC_CENTER", "1").ToIntDef(0) == 1 ? true : false;
  108. if (g_AppCfg.BottomTrafficCycle != 0)
  109. {
  110. if (g_AppCfg.BottomTrafficCycle < 3)
  111. {
  112. g_AppCfg.BottomTrafficCycle = 3; //하단소통정보 표출하는 경우 3초보다 작으면 최소 3초로 설정한다.
  113. }
  114. }
  115. FIGURE.GRADE0 = pIniFile->ReadString("FIGURE", "0", "-").Trim();
  116. FIGURE.GRADE1 = pIniFile->ReadString("FIGURE", "1", "월활").Trim();
  117. FIGURE.GRADE2 = pIniFile->ReadString("FIGURE", "2", "지체").Trim();
  118. FIGURE.GRADE3 = pIniFile->ReadString("FIGURE", "3", "정체").Trim();
  119. TEXT.GRADE0 = pIniFile->ReadString("TEXT", "0", "-").Trim();
  120. TEXT.GRADE1 = pIniFile->ReadString("TEXT", "1", "소통원활").Trim();
  121. TEXT.GRADE2 = pIniFile->ReadString("TEXT", "2", "지 체").Trim();
  122. TEXT.GRADE3 = pIniFile->ReadString("TEXT", "3", "정 체").Trim();
  123. pDb = &g_AppCfg.db;
  124. pDb->sConnectStr = pIniFile->ReadString("DATABASE", "CONNECTSTR", "");
  125. if (pDb->sConnectStr == "")
  126. {
  127. pDb->sProvider = pIniFile->ReadString("DATABASE", "PROVIDER", "");
  128. pDb->sServerName = pIniFile->ReadString("DATABASE", "SERVERNAME", "");
  129. pDb->sCatalog = pIniFile->ReadString("DATABASE", "CATALOG", "");
  130. pDb->sUserName = pIniFile->ReadString("DATABASE", "USERNAME", "");
  131. pDb->sPassword = pIniFile->ReadString("DATABASE", "PASSWORD", "");
  132. pDb->MakeConnString();
  133. }
  134. try
  135. {
  136. g_AppCfg.nMaxLogLines = pIniFile->ReadInteger("LOG", "LOGLINE", 200);
  137. if (g_AppCfg.nMaxLogLines > 1000) g_AppCfg.nMaxLogLines = 1000;
  138. if (g_AppCfg.nMaxLogLines < 0) g_AppCfg.nMaxLogLines = 100;
  139. g_AppCfg.sLogDay = pIniFile->ReadString ("LOG", "LOGDAY", "");
  140. g_LogCfg.Info = (bool)pIniFile->ReadInteger("LOG", "INFO", 1);
  141. g_LogCfg.Data = (bool)pIniFile->ReadInteger("LOG", "DATA", 0);
  142. g_LogCfg.Error = (bool)pIniFile->ReadInteger("LOG", "ERROR", 1);
  143. g_LogCfg.Warning = (bool)pIniFile->ReadInteger("LOG", "WARNING", 1);
  144. g_LogCfg.Debug = (bool)pIniFile->ReadInteger("LOG", "DEBUG", 0);
  145. g_LogCfg.Detail = (bool)pIniFile->ReadInteger("LOG", "DETAIL", 0);
  146. g_LogCfg.Detail = 0;
  147. } catch(Exception &e) {}
  148. g_AppCfg.bThrExit = false;
  149. g_AppCfg.bThrJobExit = false;
  150. g_AppCfg.hFinishEvent = NULL;
  151. }
  152. __finally
  153. {
  154. if (pIniFile) delete pIniFile;
  155. pIniFile = NULL;
  156. }
  157. return true;
  158. }
  159. //---------------------------------------------------------------------------
  160. bool APP_ReLoadConfigInfo()
  161. {
  162. String sCfgFile;
  163. TIniFile *pIniFile = NULL;
  164. sCfgFile = g_AppCfg.sConfigFile;
  165. try
  166. {
  167. String sTmp;
  168. pIniFile = new TIniFile(sCfgFile);
  169. //정체폼 판정 주기(즉, 주기값이상인 경우 정체폼생성 >= )
  170. g_AppCfg.CngsContCnt = pIniFile->ReadString("APPLICATION", "CngsContCnt", "2").ToIntDef(2);
  171. if (g_AppCfg.CngsContCnt < 1) g_AppCfg.CngsContCnt = 1;
  172. //1단 교통정보폼 중앙정렬 여부
  173. g_AppCfg.Traf1RowCenter = pIniFile->ReadString ("APPLICATION", "Traffic1RowCenter", "0").ToIntDef(0) == 1 ? true : false;
  174. g_AppCfg.IsImageSeqSave = pIniFile->ReadString ("APPLICATION", "IMAGESEQSAVE", "0").ToIntDef(0) == 1 ? true : false;
  175. g_AppCfg.comm.nConnectWait = pIniFile->ReadInteger("COMMUNICATION", "CONNECTWAIT", 60);
  176. g_AppCfg.comm.nCmdTimeOut = pIniFile->ReadInteger("COMMUNICATION", "CMDTIMEOUT", 5);
  177. g_AppCfg.comm.nWatchDogTime = pIniFile->ReadInteger("COMMUNICATION", "WATCHDOGTIME", 30);
  178. g_AppCfg.comm.nPollingTime = pIniFile->ReadInteger("COMMUNICATION", "POLLINGTIME", 20);
  179. g_AppCfg.IsIpChecking = pIniFile->ReadString ("COMMUNICATION", "IPCHECKING", "1").ToIntDef(0) == 1 ? true : false;
  180. if (g_AppCfg.comm.nPollingTime < 10) g_AppCfg.comm.nPollingTime = 10;
  181. if (g_AppCfg.comm.nPollingTime > 90) g_AppCfg.comm.nPollingTime = 90;
  182. if (g_AppCfg.comm.nConnectWait < 30) g_AppCfg.comm.nConnectWait = 30;
  183. if (g_AppCfg.comm.nConnectWait > 60) g_AppCfg.comm.nConnectWait = 60;
  184. if (g_AppCfg.comm.nCmdTimeOut < 5) g_AppCfg.comm.nCmdTimeOut = 5;
  185. if (g_AppCfg.comm.nWatchDogTime < 30) g_AppCfg.comm.nWatchDogTime = 30;
  186. g_AppCfg.comm.nWatchDogTime += 5; // 5초정도를 더 추가해 준다(딜레이발생할 경우)
  187. g_AppCfg.BottomTrafficCycle = pIniFile->ReadString("FIGURE", "TRAFFIC_CYCLE", "0").ToIntDef(0);
  188. g_AppCfg.BottomTrafficMaxCnt = pIniFile->ReadString("FIGURE", "TRAFFIC_MAX", "1").ToIntDef(0);
  189. g_AppCfg.BottomTrafficCenter = pIniFile->ReadString("FIGURE", "TRAFFIC_CENTER", "1").ToIntDef(0) == 1 ? true : false;
  190. if (g_AppCfg.BottomTrafficCycle != 0)
  191. {
  192. if (g_AppCfg.BottomTrafficCycle < 3)
  193. {
  194. g_AppCfg.BottomTrafficCycle = 3; //하단소통정보 표출하는 경우 3초보다 작으면 최소 3초로 설정한다.
  195. }
  196. }
  197. FIGURE.GRADE0 = pIniFile->ReadString("FIGURE", "0", "-").Trim();
  198. FIGURE.GRADE1 = pIniFile->ReadString("FIGURE", "1", "월활").Trim();
  199. FIGURE.GRADE2 = pIniFile->ReadString("FIGURE", "2", "지체").Trim();
  200. FIGURE.GRADE3 = pIniFile->ReadString("FIGURE", "3", "정체").Trim();
  201. TEXT.GRADE0 = pIniFile->ReadString("TEXT", "0", "-").Trim();
  202. TEXT.GRADE1 = pIniFile->ReadString("TEXT", "1", "소통원활").Trim();
  203. TEXT.GRADE2 = pIniFile->ReadString("TEXT", "2", "지 체").Trim();
  204. TEXT.GRADE3 = pIniFile->ReadString("TEXT", "3", "정 체").Trim();
  205. }
  206. __finally
  207. {
  208. if (pIniFile) delete pIniFile;
  209. pIniFile = NULL;
  210. }
  211. return true;
  212. }
  213. //---------------------------------------------------------------------------
  214. /*
  215. * 응용프로그램이 중복으로 실행되는 것을 막기위해 뮤텍스를 생성한다.
  216. */
  217. bool APP_ApplicationSingleInstance(AnsiString AProgName, AnsiString APidFile)
  218. {
  219. bool isRunning = false;
  220. HANDLE hMutex = 0;
  221. AnsiString sMutexName;
  222. sMutexName = "Global\\" + AProgName;
  223. hMutex = CreateMutex(NULL, TRUE, sMutexName.c_str());
  224. if (hMutex && GetLastError() == ERROR_ALREADY_EXISTS)
  225. {
  226. char buffer[1000];
  227. ReleaseMutex(hMutex);
  228. hMutex=0;
  229. isRunning = true;
  230. ////////////////////////////////////////////////////////////////////////////
  231. AnsiString fn;
  232. char tmp[500];
  233. if (FileExists(APidFile))
  234. {
  235. TFileStream *fp = new TFileStream(APidFile, fmOpenRead);
  236. HWND hwnd;
  237. AnsiString nfo;
  238. memset(buffer, 0, 1000);
  239. fp->Read(buffer, 100);
  240. delete fp;
  241. hwnd = (HWND)(String(buffer).ToIntDef(0));
  242. if (hwnd)
  243. {
  244. SendMessage(hwnd, WM_USER, WM_WINDOW_RESTORE, 0);
  245. }
  246. }
  247. }
  248. if (hMutex)
  249. {
  250. ReleaseMutex(hMutex);
  251. hMutex=0;
  252. }
  253. return isRunning;
  254. }
  255. //---------------------------------------------------------------------------
  256. /*
  257. * 환경설정 정보를 저장하는 함수.
  258. */
  259. bool APP_WriteConfigInfo(String ASection, String AItem, String AValue, String ACfgFile/*=""*/)
  260. {
  261. String sConfigFile;
  262. TIniFile *pIniFile = NULL;
  263. sConfigFile = ACfgFile;
  264. try
  265. {
  266. pIniFile = new TIniFile(sConfigFile);
  267. if (pIniFile == NULL)
  268. {
  269. return false;
  270. }
  271. pIniFile->WriteString(ASection, AItem, AValue);
  272. }
  273. catch(...)
  274. {
  275. }
  276. if (pIniFile)
  277. {
  278. pIniFile->Free();
  279. pIniFile = NULL;
  280. }
  281. return true;
  282. }
  283. //---------------------------------------------------------------------------
  284. /*
  285. * 환경설정 정보를 읽어오는 함수.
  286. */
  287. bool APP_ReadConfigInfo(String ASection, String AItem, String &AValue, String ACfgFile/*=""*/)
  288. {
  289. bool bRes;
  290. String sConfigFile;
  291. TIniFile *pIniFile = NULL;
  292. bRes = false;
  293. sConfigFile = ACfgFile;
  294. try
  295. {
  296. pIniFile = new TIniFile(sConfigFile);
  297. if (pIniFile == NULL)
  298. {
  299. return bRes;
  300. }
  301. AValue = pIniFile->ReadString(ASection, AItem, "");
  302. if (AValue != "")
  303. {
  304. bRes = true;
  305. }
  306. }
  307. catch(...)
  308. {
  309. }
  310. if (pIniFile)
  311. {
  312. pIniFile->Free();
  313. pIniFile = NULL;
  314. }
  315. return bRes;
  316. }
  317. //---------------------------------------------------------------------------
  318. /*
  319. * 시스템오류 메시지를 얻는다.
  320. */
  321. String APP_GetSysError()
  322. {
  323. AnsiString sErrMsg = "";
  324. LPVOID lpMsgBuf = NULL;
  325. try
  326. {
  327. try
  328. {
  329. FormatMessage(
  330. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  331. NULL,
  332. GetLastError(),
  333. //MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  334. MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
  335. (LPTSTR) &lpMsgBuf,
  336. 0,
  337. NULL
  338. );
  339. char *pData = (char*)lpMsgBuf;
  340. if (strlen(pData) > 2)
  341. {
  342. pData[strlen(pData)-2] = 0x00;
  343. pData[strlen(pData)-1] = 0x00;
  344. }
  345. sErrMsg = AnsiString(pData);
  346. }
  347. __finally
  348. {
  349. if (lpMsgBuf) LocalFree(lpMsgBuf);
  350. lpMsgBuf = NULL;
  351. }
  352. }
  353. catch(Exception &e)
  354. {
  355. }
  356. return sErrMsg;
  357. }
  358. //---------------------------------------------------------------------------