using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO; using System.Collections.Concurrent; using System.Net.Sockets; using System.Net; using System.Threading; using System.Timers; using System.Diagnostics; namespace DataGateWayProject { public partial class Form1 : Form { LogFileCreate LFC = new LogFileCreate(); My_TimerThread MTT = new My_TimerThread(); //System.Threading.Timer timer_NetStat; //private Object iCosCommObj = new Object(); //Stopwatch sw = new Stopwatch(); public Form1() { InitializeComponent(); } //public void Timer_netStat() //iCOS 연결 상태 확인 쓰레드 동작 함수 //{ // timer_NetStat = new System.Threading.Timer(net_state_Thread,null,1,9000); //} //bool changed = false; //public void net_state_Thread(object status)//(object sender, ElapsedEventArgs e) //iCOS 연결 상태 확인 쓰레드 함수 //{ // GetData_From_iCos GFI = new GetData_From_iCos(); // string connectstate = ""; // //lock (iCosCommObj) // //{ // try // { // connectstate = GFI.GetConnectionStatus(); // if (connectstate == "iCOS 통신 중") // { // if (changed == true) // { // Wirte_logbox_State(Convert.ToString(DateTime.Now) + " iCOS 통신 연결" + Environment.NewLine); // LFC.Log("iCOS 통신 연결" + Environment.NewLine); // } // changed = false; // } // else // { // Wirte_logbox_State(Convert.ToString(DateTime.Now) + " iCOS 통신 끊김" + Environment.NewLine); // LFC.Log("iCOS 통신 끊김" + Environment.NewLine); // changed = true; // } // Wirte_Textbox_State(connectstate); // } // catch (Exception E) // { // LFC.Log("[Form] net_state_Thread 오류 : " + E + Environment.NewLine); // LogTxt.AppendText(Convert.ToString(DateTime.Now) + " [Form] net_state_Thread 오류 : " + E + Environment.NewLine); // } //} //private delegate void DelegateTextbox(string theValue); //private void Wirte_Textbox_State(string theValue) //통신상태 변화 Form text box에 적용 함수 //{ // try // { // if (this.Net_Stat.InvokeRequired) // { // DelegateTextbox d = new DelegateTextbox(Wirte_Textbox_State); // this.Invoke(d, new object[] { theValue }); // } // else // { // this.Net_Stat.Text = theValue; // } // } // catch (Exception E) // { // LFC.Log("[Form] DelegateTextbox 오류 : " + E + Environment.NewLine); // } //} //private delegate void DelegateLogbox(string theValue); //private void Wirte_logbox_State(string theValue) //통신상태 변화 Form text box에 적용 함수 //{ // try // { // if (this.LogTxt.InvokeRequired) // { // DelegateLogbox d = new DelegateLogbox(Wirte_logbox_State); // this.Invoke(d, new object[] { theValue }); // } // else // { // this.LogTxt.AppendText(theValue); // LogTxt.Select(LogTxt.Text.Length, 0); // LogTxt.ScrollToCaret(); // } // } // catch (Exception E) // { // LFC.Log("[Form] DelegateLogbox 오류 : " + E + Environment.NewLine); // } //} private void Form1_Load(object sender, EventArgs e) { ProStartBtn.Enabled = false; //시작 버튼 비활성화 ProStopBtn.Enabled = false; //중단 버튼 비활성화 PointSetBtn.Enabled = false; //관제점 등록 버튼 비활성화 PointUnRegBtn.Enabled = false; //관제점 해제 버튼 비활성화 AllValueCheck_btn.Enabled = false; // 일관 확인 버튼 비활성화 RequestValue_btn.Enabled = false; //특정 값 확인 버튼 비활성화 //Timer_netStat(); //iCOS 네트워크 연결 확인 함수 } private void ProStartBtn_Click(object sender, EventArgs e) //시작버튼 함수 { FolderBrowserDialog(); if (toolStripStatusLabel1.Text != "")//경로 설정 확인 { try { EndTxt.Clear(); //모니터링 종료시간 clear StartTxt.Text = Convert.ToString(DateTime.Now); LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 프로그램 시작" + Environment.NewLine); LFC.Log("프로그램 시작 " + Environment.NewLine); ProStartBtn.Enabled = false; // 시작버튼 비활성화. ProStopBtn.Enabled = true; //정지버튼 활성화 FileSettingBtn.Enabled = false; //프로젝트 설정 버튼 비활성화 FileMatchBtn.Enabled = false; //관제점 설정 버튼 비활성화 FileExpBtn.Enabled = false; //성능분석시 버튼 비활성화 FirstCheck FC = new FirstCheck(); FC.Check_DailyData(); OneMinute_Insert OMI = new OneMinute_Insert(); OMI.Today_PeakMinMax(); OMI.Year_PeakMinMax(); MTT.timer2start(); LogTxt.Select(LogTxt.Text.Length, 0); LogTxt.ScrollToCaret(); } catch (Exception eProStart) { LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 프로그램 오류 : " + eProStart + Environment.NewLine); LFC.Log("프로그램 오류 : " + eProStart + Environment.NewLine); LogTxt.Select(LogTxt.Text.Length, 0); LogTxt.ScrollToCaret(); } } else MessageBox.Show("경로를 설정해주세요"); } private void ProStopBtn_Click(object sender, EventArgs e) //중단 { GetData_From_iCos GFi = new GetData_From_iCos(); GFi.StopTimer(); MTT.timer2Stop(); FileSettingBtn.Enabled = true; //프로젝트 설정 버튼 활성화 FileMatchBtn.Enabled = true; //관제점설정 버튼 활성화 FileExpBtn.Enabled = true; //성는분석식 버튼 활성화 ProStartBtn.Enabled = true; EndTxt.Text = Convert.ToString(DateTime.Now); LFC.Log("프로그램 중단" + Environment.NewLine); LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 프로그램 중단 " + Environment.NewLine); ProStopBtn.Enabled = false; LogTxt.Select(LogTxt.Text.Length, 0); LogTxt.ScrollToCaret(); } private void ProSetBtn_Click(object sender, EventArgs e) //접속 설정 { SettingForm SF = new SettingForm(); SF.ShowDialog(); } private void ProPathBtn_Click_1(object sender, EventArgs e) //프로젝트 경로 { System.Diagnostics.Process.Start("explorer.exe", Environment.CurrentDirectory); } /// /// 셋팅파일 불러오기 버튼 /// /// /// private void FileSettingBtn_Click(object sender, EventArgs e)// 프로젝트 설정 { string FSBLocation = Environment.CurrentDirectory + "\\Login.ini"; try { System.Diagnostics.Process.Start(FSBLocation);// Login 파일에 접근 } catch { FileError(FSBLocation); } } /// /// 매칭파일 불러오기 버튼 /// /// /// private void FileMatchBtn_Click(object sender, EventArgs e) //관제점 설정 { string FSBLocation = Environment.CurrentDirectory + "\\SettingFile.csv"; try { System.Diagnostics.Process.Start(FSBLocation); } catch { FileError(FSBLocation); } } /// /// 수식파일 불러오기 버튼 /// /// /// private void FileExpBtn_Click(object sender, EventArgs e) //성능 분석식 { string FSBLocation = Environment.CurrentDirectory + "\\Expression.csv"; try { System.Diagnostics.Process.Start(FSBLocation); } catch { FileError(FSBLocation); } } public void FileError(string Error)// 오류 같음[Matching.csv => SettingFile.csv] { if ((!Error.Contains("Login.ini")) || (!Error.Contains("expression.csv")) || (!Error.Contains("Matching.csv"))) { MessageBox.Show("파일을 찾을 수 없음"); LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 파일을 찾을 수 없음 : " + Error + Environment.NewLine); LFC.Log("파일을 찾을 수 없음 : " + Error + Environment.NewLine); LogTxt.Select(LogTxt.Text.Length, 0); LogTxt.ScrollToCaret(); } } public void FolderBrowserDialog() //프로젝트 경로 버튼 클릭 -> 팝업창 { toolStripStatusLabel1.Text = Environment.CurrentDirectory; LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 프로젝트 경로 설정 완료" + Environment.NewLine); LogTxt.AppendText(toolStripStatusLabel1 + Environment.NewLine); LFC.Log("프로젝트 경로 설정 완료"); LFC.Log(toolStripStatusLabel1 + Environment.NewLine); LogTxt.Select(LogTxt.Text.Length, 0); LogTxt.ScrollToCaret(); } private void EraseLog_Click(object sender, EventArgs e) //로그지우기 { LogTxt.Clear(); LogTxt.Select(LogTxt.Text.Length, 0); LogTxt.ScrollToCaret(); } // 프로그램 종료시 이벤트 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show("프로그램을 종료하시겠습니까?", "종료", MessageBoxButtons.YesNo) == DialogResult.Yes) { MTT.timer2Stop(); // 스레드 종료 Application.ExitThread(); for (int i = 0; i < 1000000; i++) {; } // 프로세스 종료 Application.Exit(); // 프로세스가 남아있다면 최종 종료 (2개의 프로젝트가 공존하고 Application.ProductName 값이 2개다 System.Diagnostics.Process[] mProcess = System.Diagnostics.Process.GetProcessesByName(Application.ProductName); foreach (System.Diagnostics.Process p in mProcess) p.Kill(); // 최종 정리 (보완코드 : 크로스 스레드 문제 발생 여지가 있으므로 사용주의) Environment.Exit(0); LFC.Log("사용자에 의해 프로그램이 종료되었습니다."); } else { LFC.Log("사용자에 의해 프로그램 종료가 취소되었습니다."); e.Cancel = true; return; } } private void PointSetBtn_Click(object sender, EventArgs e) //관제점 등록 { bool ReadTag_Result; GetData_From_iCos GFi = new GetData_From_iCos(); ReadTag_Result = GFi.ReadTag(); if (ReadTag_Result == true) { LFC.Log("관제점 등록 완료 " + Environment.NewLine); LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 관제점 등록 완료" + Environment.NewLine); ProStartBtn.Enabled = true; PointUnRegBtn.Enabled = true; GFi.GetValueTimer(); Thread.Sleep(4500); RequestValue_btn.Enabled = true; AllValueCheck_btn.Enabled = true; } else { LFC.Log("관제점 등록 실패 " + Convert.ToString(DateTime.Now)); LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 관제점 등록 실패" + Environment.NewLine); } LogTxt.Select(LogTxt.Text.Length, 0); LogTxt.ScrollToCaret(); } private void Conn_iCos_Btn_Click(object sender, EventArgs e) //icos 접속 { GetData_From_iCos GetData = new GetData_From_iCos(); string ReturnValue = GetData.iCos3_Conn(); if (ReturnValue == "SUCCESS") { EndTxt.Clear(); LFC.Log("iCOS 접속 성공" + Environment.NewLine); LogTxt.AppendText(Convert.ToString(DateTime.Now) + " iCOS 접속 성공" + Environment.NewLine); PointSetBtn.Enabled = true; } else { LFC.Log("iCOS 접속 실패" + Environment.NewLine); LogTxt.AppendText(Convert.ToString(DateTime.Now) + " iCOS 접속 실패" + Environment.NewLine); } LogTxt.Select(LogTxt.Text.Length, 0); LogTxt.ScrollToCaret(); } private void Btn_SettingFile_Click(object sender, EventArgs e) //설정파일 생성 버튼 { Creating_Setting_File CSF = new Creating_Setting_File(); CSF.Create_Setting_Point_File(); } private void Btn_TagSetFile_Click(object sender, EventArgs e) //태그등록파일 생성 버튼 { Creating_Setting_File CSF = new Creating_Setting_File(); CSF.Create_Point_File(); } private void RequestValue_btn_Click(object sender, EventArgs e) //확인 버튼 { if (RequestValue.Text != "") { LogTxt.AppendText("*** 현재 시간 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ***" + Environment.NewLine); LFC.Log("*** 현재 시간 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ***"); try { string tag = RequestValue.Text; //태그값 string temp = GetData_From_iCos.Key_Value_Dic[RequestValue.Text]; LogTxt.AppendText('"' + tag + '"' + " 의 값 : " + temp + Environment.NewLine); LogTxt.AppendText("***********************************" + Environment.NewLine); LFC.Log('"' + tag + '"' + " 의 값 : " + temp); LFC.Log("***************************************" + Environment.NewLine); } catch (Exception er) { MessageBox.Show("오류 발생"); LogTxt.AppendText("오류 발생 : " + er); LFC.Log("오류 발생 : " + er); } } else { MessageBox.Show("확인 값이 없습니다."); return; } LogTxt.Select(LogTxt.Text.Length, 0); LogTxt.ScrollToCaret(); } /// /// 패킷분석 기능 추가 /// private void AllValueCheck_btn_Click(object sender, EventArgs e) //값 일괄 확인 버튼 { string[] sArray = File.ReadAllLines("TagSetFile.csv", Encoding.Default); GetData_From_iCos GFi = new GetData_From_iCos(); LogTxt.AppendText("*** 현재 시간 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ***" + Environment.NewLine); LFC.Log("****** 현재 시간 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " *****"); try { for (int i = 0; i < sArray.Length; i++) { string[] tmp = sArray[i].Split(','); // tmp[0] = index, tmp[1] = Tag명. string a = GFi.GetTagValue(tmp[1]); LogTxt.AppendText('"' + tmp[1] + '"' + " 의 값 : " + GetData_From_iCos.Key_Value_Dic[tmp[1]] + Environment.NewLine); LFC.Log('"' + tmp[1] + '"' + " 의 값 : " + GetData_From_iCos.Key_Value_Dic[tmp[1]]); } LogTxt.AppendText("***********************************" + Environment.NewLine); LFC.Log("********************************************" + Environment.NewLine); } catch (Exception er) { LogTxt.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 오류 발생 : " + er + Environment.NewLine); LFC.Log("오류 발생 : " + er + Environment.NewLine); MessageBox.Show("오류 발생 : " + er); } LogTxt.Select(LogTxt.Text.Length, 0); LogTxt.ScrollToCaret(); } } }