AppGlobalF.cpp 14 KB

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