AppGlobalF.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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.MaxMemory = pIniFile->ReadString("RESOURCE", "MAXMEMORY", "512").ToIntDef(512);
  143. g_AppCfg.MaxHandle = pIniFile->ReadString("RESOURCE", "MAXHANDLE", "0").ToIntDef(0);
  144. g_AppCfg.MaxThread = pIniFile->ReadString("RESOURCE", "MAXTHREAD", "4096").ToIntDef(4096);
  145. g_AppCfg.MaxGdi = pIniFile->ReadString("RESOURCE", "MAXGDI", "0").ToIntDef(0);
  146. g_AppCfg.RcRestart = pIniFile->ReadString("RESOURCE", "RESTART", "0").ToIntDef(0) == 1 ? true : false;
  147. g_AppCfg.comm.nConnectWait = pIniFile->ReadString("COMMUNICATION", "CONNECTWAIT", "60").ToIntDef(60);
  148. g_AppCfg.comm.nCmdTimeOut = pIniFile->ReadString("COMMUNICATION", "CMDTIMEOUT", "5").ToIntDef(5);
  149. g_AppCfg.comm.nWatchDogTime = pIniFile->ReadString("COMMUNICATION", "WATCHDOGTIME", "30").ToIntDef(30);
  150. g_AppCfg.comm.nPollingTime = pIniFile->ReadString("COMMUNICATION", "POLLINGTIME", "20").ToIntDef(20);
  151. g_AppCfg.IsIpChecking = pIniFile->ReadString("COMMUNICATION", "IPCHECKING", "1").ToIntDef(1) == 1 ? true : false;
  152. if (g_AppCfg.comm.nPollingTime < 10) g_AppCfg.comm.nPollingTime = 10;
  153. if (g_AppCfg.comm.nPollingTime > 90) g_AppCfg.comm.nPollingTime = 90;
  154. if (g_AppCfg.comm.nConnectWait < 30) g_AppCfg.comm.nConnectWait = 30;
  155. if (g_AppCfg.comm.nConnectWait > 60) g_AppCfg.comm.nConnectWait = 60;
  156. if (g_AppCfg.comm.nCmdTimeOut < 5) g_AppCfg.comm.nCmdTimeOut = 5;
  157. if (g_AppCfg.comm.nWatchDogTime < 30) g_AppCfg.comm.nWatchDogTime = 30;
  158. g_AppCfg.comm.nWatchDogTime += 5; // 5초정도를 더 추가해 준다(딜레이발생할 경우)
  159. g_AppCfg.BottomTrafficCycle = pIniFile->ReadString("FIGURE", "TRAFFIC_CYCLE", "0").ToIntDef(0);
  160. g_AppCfg.BottomTrafficMaxCnt = pIniFile->ReadString("FIGURE", "TRAFFIC_MAX", "1").ToIntDef(1);
  161. g_AppCfg.BottomTrafficCenter = pIniFile->ReadString("FIGURE", "TRAFFIC_CENTER", "1").ToIntDef(1) == 1 ? true : false;
  162. if (g_AppCfg.BottomTrafficCycle != 0)
  163. {
  164. if (g_AppCfg.BottomTrafficCycle < 3)
  165. {
  166. g_AppCfg.BottomTrafficCycle = 3; //하단소통정보 표출하는 경우 3초보다 작으면 최소 3초로 설정한다.
  167. }
  168. }
  169. FIGURE.GRADE0 = pIniFile->ReadString("FIGURE", "0", "-").Trim();
  170. FIGURE.GRADE1 = pIniFile->ReadString("FIGURE", "1", "월활").Trim();
  171. FIGURE.GRADE2 = pIniFile->ReadString("FIGURE", "2", "지체").Trim();
  172. FIGURE.GRADE3 = pIniFile->ReadString("FIGURE", "3", "정체").Trim();
  173. g_AppCfg.TextTrafficCenter = pIniFile->ReadString("TEXT", "TRAFFIC_CENTER", "1").ToIntDef(1) == 1 ? true : false;
  174. TEXT.GRADE0 = pIniFile->ReadString("TEXT", "0", "-").Trim();
  175. TEXT.GRADE1 = pIniFile->ReadString("TEXT", "1", "소통원활").Trim();
  176. TEXT.GRADE2 = pIniFile->ReadString("TEXT", "2", "지 체").Trim();
  177. TEXT.GRADE3 = pIniFile->ReadString("TEXT", "3", "정 체").Trim();
  178. }
  179. __finally
  180. {
  181. if (pIniFile) delete pIniFile;
  182. pIniFile = NULL;
  183. }
  184. return true;
  185. }
  186. //---------------------------------------------------------------------------
  187. /*
  188. * 응용프로그램이 중복으로 실행되는 것을 막기위해 뮤텍스를 생성한다.
  189. */
  190. bool APP_ApplicationSingleInstance(AnsiString AProgName, AnsiString APidFile)
  191. {
  192. bool isRunning = false;
  193. HANDLE hMutex = 0;
  194. AnsiString sMutexName;
  195. sMutexName = "Global\\" + AProgName;
  196. hMutex = CreateMutex(NULL, TRUE, sMutexName.c_str());
  197. if (hMutex && GetLastError() == ERROR_ALREADY_EXISTS)
  198. {
  199. char buffer[1000];
  200. ReleaseMutex(hMutex);
  201. hMutex=0;
  202. isRunning = true;
  203. ////////////////////////////////////////////////////////////////////////////
  204. AnsiString fn;
  205. char tmp[500];
  206. if (FileExists(APidFile))
  207. {
  208. TFileStream *fp = new TFileStream(APidFile, fmOpenRead);
  209. HWND hwnd;
  210. AnsiString nfo;
  211. memset(buffer, 0, 1000);
  212. fp->Read(buffer, 100);
  213. delete fp;
  214. hwnd = (HWND)(String(buffer).ToIntDef(0));
  215. if (hwnd)
  216. {
  217. SendMessage(hwnd, WM_USER, WM_WINDOW_RESTORE, 0);
  218. }
  219. }
  220. }
  221. if (hMutex)
  222. {
  223. ReleaseMutex(hMutex);
  224. hMutex=0;
  225. }
  226. return isRunning;
  227. }
  228. //---------------------------------------------------------------------------
  229. /*
  230. * 환경설정 정보를 저장하는 함수.
  231. */
  232. bool APP_WriteConfigInfo(String ASection, String AItem, String AValue, String ACfgFile/*=""*/)
  233. {
  234. String sConfigFile;
  235. TIniFile *pIniFile = NULL;
  236. if (ACfgFile == "")
  237. sConfigFile = g_AppCfg.sConfigFile;
  238. else
  239. sConfigFile = ACfgFile;
  240. try
  241. {
  242. pIniFile = new TIniFile(sConfigFile);
  243. if (pIniFile == NULL)
  244. {
  245. return false;
  246. }
  247. pIniFile->WriteString(ASection, AItem, AValue);
  248. }
  249. catch(...)
  250. {
  251. }
  252. if (pIniFile)
  253. {
  254. pIniFile->Free();
  255. pIniFile = NULL;
  256. }
  257. return true;
  258. }
  259. //---------------------------------------------------------------------------
  260. /*
  261. * 환경설정 정보를 읽어오는 함수.
  262. */
  263. bool APP_ReadConfigInfo(String ASection, String AItem, String &AValue, String ACfgFile/*=""*/)
  264. {
  265. bool bRes;
  266. String sConfigFile;
  267. TIniFile *pIniFile = NULL;
  268. bRes = false;
  269. if (ACfgFile == "")
  270. sConfigFile = g_AppCfg.sConfigFile;
  271. else
  272. sConfigFile = ACfgFile;
  273. try
  274. {
  275. pIniFile = new TIniFile(sConfigFile);
  276. if (pIniFile == NULL)
  277. {
  278. return bRes;
  279. }
  280. AValue = pIniFile->ReadString(ASection, AItem, "");
  281. if (AValue != "")
  282. {
  283. bRes = true;
  284. }
  285. }
  286. catch(...)
  287. {
  288. }
  289. if (pIniFile)
  290. {
  291. pIniFile->Free();
  292. pIniFile = NULL;
  293. }
  294. return bRes;
  295. }
  296. //---------------------------------------------------------------------------
  297. /*
  298. * 시스템오류 메시지를 얻는다.
  299. */
  300. String APP_GetSysError()
  301. {
  302. AnsiString sErrMsg = "";
  303. LPVOID lpMsgBuf = NULL;
  304. try
  305. {
  306. try
  307. {
  308. FormatMessage(
  309. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  310. NULL,
  311. GetLastError(),
  312. //MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  313. MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
  314. (LPTSTR) &lpMsgBuf,
  315. 0,
  316. NULL
  317. );
  318. char *pData = (char*)lpMsgBuf;
  319. if (strlen(pData) > 2)
  320. {
  321. pData[strlen(pData)-2] = 0x00;
  322. pData[strlen(pData)-1] = 0x00;
  323. }
  324. sErrMsg = AnsiString(pData);
  325. }
  326. __finally
  327. {
  328. if (lpMsgBuf) LocalFree(lpMsgBuf);
  329. lpMsgBuf = NULL;
  330. }
  331. }
  332. catch(Exception &e)
  333. {
  334. }
  335. return sErrMsg;
  336. }
  337. //---------------------------------------------------------------------------