Program.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Text;
  7. using System.Diagnostics;
  8. using System.Collections;
  9. using System.Data.SqlClient;
  10. using System.Timers;
  11. using System.Threading;
  12. using System.Runtime.InteropServices;
  13. namespace DataGateWayProject
  14. {
  15. static class Program
  16. {
  17. /// <summary>
  18. /// 해당 응용 프로그램의 주 진입점입니다.
  19. /// </summary>
  20. ///
  21. [STAThread]
  22. static void Main()
  23. {
  24. //string mtxName = "DGW";
  25. //Mutex mtx = new Mutex(true, mtxName);
  26. ////1초 동안 뮤텍스를 획득하려 대기
  27. //TimeSpan tsWait = new TimeSpan(0, 0, 1);
  28. //bool success = mtx.WaitOne(tsWait);
  29. //if (!success)
  30. //{
  31. // MessageBox.Show("프로그램이 이미 실행중입니다.");
  32. // return;
  33. //}
  34. Application.EnableVisualStyles();
  35. Application.SetCompatibleTextRenderingDefault(false);
  36. Application.Run(new Form1()); //UI 폼 열기
  37. }
  38. }
  39. /// <summary>
  40. /// 타이머 스레드 클래스
  41. /// </summary>
  42. class My_TimerThread
  43. {
  44. static string TimeCheck = "";
  45. Parsing PS = new Parsing();
  46. Control_Point_Group CPG = new Control_Point_Group();
  47. LogFileCreate LFC = new LogFileCreate();
  48. System.Timers.Timer timer2 = new System.Timers.Timer();
  49. public static bool check = false;
  50. public void timer2start()
  51. {
  52. timer2.Elapsed -= new ElapsedEventHandler(timer2_Elapsed);
  53. timer2.Elapsed += new ElapsedEventHandler(timer2_Elapsed);
  54. timer2.Interval = 2000; // 20sec 간격
  55. timer2.Start();
  56. LFC.Log("timer 시작");
  57. }
  58. public void timer2Stop()
  59. {
  60. timer2.Elapsed -= new ElapsedEventHandler(timer2_Elapsed);
  61. timer2.Stop();
  62. timer2.Close();
  63. LFC.Log("timer 중단");
  64. }
  65. public void timer2_Elapsed(object sender, ElapsedEventArgs e) // 1분마다 실행되는 데이터 수집 로직
  66. {
  67. string tmpMin = DateTime.Now.Minute.ToString(); // 현재의 '분' , if 51이라고했을때.
  68. if ((TimeCheck != tmpMin) && check == false)
  69. {
  70. check = true;
  71. CPG.Control_Group();
  72. CPG.Control_Point();
  73. TimeCheck = PS.SettingFileParsing();
  74. check = false;
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// 결과 값을 DB에 Insert시키기 위한 클래스
  80. /// </summary>
  81. class My_SqlDBInsert
  82. {
  83. SqlConnection conn; //sql 접속
  84. LogFileCreate LFC = new LogFileCreate();
  85. Readini Ri = new Readini();
  86. Form1 SF = new Form1();
  87. static private bool InsertOK = false;
  88. /// <summary>
  89. /// DB에 INSERT하기 위한 함수.
  90. /// </summary>
  91. /// <param name="FacilityTypeId"></param>
  92. /// <param name="FacilityCode"></param>
  93. /// <param name="PropertyId"></param>
  94. /// <param name="Value"></param>
  95. /// <returns></returns>
  96. public bool INsert(string FacilityTypeId, string FacilityCode, string PropertyId, object Value)
  97. {
  98. try
  99. {
  100. Ri.ReadData(); // ini의 설정값을 불러옴.
  101. string strcon = @"Data Source = " + Ri.UserIP + "," + Ri.UserPort + "; Initial Catalog =" + Ri.UserDB + ";Persist Security Info=True; User ID =" + Ri.UserID + "; Password =" + Ri.UserPW + "";
  102. conn = new SqlConnection(strcon);
  103. conn.Open();
  104. string query = "INSERT INTO BemsMonitoringPointHistory15min(SiteId,FacilityTypeId,FacilityCode,PropertyId,CreatedDateTime,CurrentValue) " +
  105. "VALUES('1','" + FacilityTypeId + "','" + FacilityCode + "','" + PropertyId + "','2016-09-08 08:43:00.000','" + Value + "')"; // INSERT 쿼리문.
  106. SqlCommand cmd = new SqlCommand(query, conn);
  107. cmd.ExecuteNonQuery();
  108. cmd.Dispose();
  109. LFC.Log(query);
  110. SF.DBAddressTxt.Text = "연결";
  111. InsertOK = true;
  112. }
  113. catch (Exception e)
  114. {
  115. SF.DBAddressTxt.Text = "연결오류";
  116. InsertOK = false;
  117. LFC.Log("프로그램 연결 오류 : " + e);
  118. }
  119. finally
  120. {
  121. conn.Close();
  122. conn.Dispose();
  123. }
  124. return InsertOK;
  125. }
  126. }
  127. }