123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850 |
- 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.Threading;
- using System.Timers;
- namespace BEMSDataGateway
- {
-
- public partial class Form1 : Form
- {
-
-
-
-
- bool startOk = false;
- public bool bConnect;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public delegate int ErrorFunc(int nErrorCode);
- public ErrorFunc _errfunc;
- public static int ResponseFunc(int nErrorCode)
- {
- LogFileCreate LFC = new LogFileCreate();
- Form1 F1 = new Form1();
-
-
-
-
-
-
-
- switch (nErrorCode)
- {
- case 1:
- LFC.Log("서버에 연결 성공 = " + nErrorCode);
-
- break;
- case 2:
- LFC.Log("서버에 의해서 소켓 연결이 끊어졌습니다 = " + nErrorCode);
-
-
-
- break;
- case 3:
- LFC.Log("프로젝트 다운로드를 완료했습니다. = " + nErrorCode);
-
- UserLogin("bems_gw", "1", true);
-
- break;
- case 4:
-
- LFC.Log("서버 로그인에 성공했습니다. = " + nErrorCode);
- My_TimerThread.check = false;
- F1.ReadTag();
-
-
- break;
- case 5:
- LFC.Log("알 수 없는 에러로 연결이 끊겼습니다. = " + nErrorCode);
-
-
- break;
- default:
- LFC.Log("서버 로그인에 실패했습니다. 연결을 종료합니다. = " + nErrorCode);
-
-
-
- break;
- }
-
- return nErrorCode;
- }
-
-
-
- public struct runMode
- {
- public UInt32 uNetType;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
- public String szRunIp;
- public UInt32 uRunPort;
- public UInt32 uRunScan;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
- public String szServer2;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
- public String szHaIp;
- public UInt32 uHaPort;
- public UInt32 uHaScan;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
- public String szUser;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
- public String szPassword;
- }
- public struct DBInfo
- {
- public UInt32 uDbType;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
- public String szDBPath;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
- public String szSqlIP;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
- public String szSqlDB;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
- public String szSqlID;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
- public String szSqlPW;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
- public String szDbAddr;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
- public String szDbTable;
- }
-
- [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
- public static extern bool InitSocket(string szUser, string szPassword, bool bLogout);
-
- [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
- public static extern void SetNetwork(ref runMode tRunMode);
-
- [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
- public static extern void SetDBInfo(ref DBInfo tDBInfo);
-
- [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
- public static extern void RegTag(int nTagCnt, string szTag);
-
- [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
- public static extern void UnregTag(int nTagCnt, string szTag);
-
- [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
- public static extern void SetDeviceDB(int nDBType, string szDBPath);
-
- [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
- public static extern float GetTagValue(string szTag);
-
- [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
- public static extern float SetTagValue(string szTag, float fValue);
-
- [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
- public static extern bool UserLogin(string szUser, string szPassword, bool bLogout);
-
- [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
- public static extern bool UserLogout(string szUser);
-
- [DllImport("ICosSocket.dll")]
- public static extern void CloseConnection();
-
- [DllImport("ICosSocket.dll", EntryPoint = "SetErrorFunc")]
- public static extern void SetErrorFunc(ErrorFunc x);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
- public static extern int GetNetworkStatus();
- public string iCosFourConn()
- {
- string strRet = "";
- string Result_Conn_iCos = "";
- Readini Ri = new Readini();
- Ri.ReadData();
- LogTxt.Text += "iCos 에 접속중입니다. 기다려주세요...." + Environment.NewLine;
- runMode tRunMode = new runMode();
- tRunMode.szRunIp = Ri.IP1;
- tRunMode.szHaIp = Ri.DGW_IP;
- tRunMode.szServer2 = Ri.Ip2;
- tRunMode.uNetType = 7;
- tRunMode.uRunPort = 6000;
- tRunMode.uRunScan = 3000;
- tRunMode.uHaPort = 2;
- tRunMode.uHaScan = 1;
- tRunMode.szUser = "bems_gw";
- tRunMode.szPassword = "1";
- try
- {
- SetNetwork(ref tRunMode);
-
- _errfunc = new ErrorFunc(ResponseFunc);
-
- SetErrorFunc(_errfunc);
-
-
-
- bool bRet = InitSocket("bems_gw", "1", true);
-
- strRet = bRet.ToString();
- LogTxt.Text += "iCos 접속 함수 동작 시작 : " + Convert.ToString(strRet) + " = " + Convert.ToString(DateTime.Now) + Environment.NewLine;
- Result_Conn_iCos = "SUCCESS";
- }
- catch(Exception e)
- {
- LogTxt.Text += "iCos 접속완료 여부 : 실패하였습니다. = " + Convert.ToString(DateTime.Now) + e + Environment.NewLine ;
- Result_Conn_iCos = "FAIL";
- }
- return Result_Conn_iCos;
-
- }
-
- public void iCosFourReadDB()
- {
- SetDeviceDB(0, "C:\\DGW2.0\\Release\\Project\\DB\\iCos4.mdb");
- }
- string[] ReturnTag = new string[2];
- public void ReadTag()
- {
- timerstart_readTag();
- }
- System.Timers.Timer timer2 = new System.Timers.Timer();
- public void timerstart_readTag()
- {
- timer2.Interval = 3000;
- timer2.Elapsed += new ElapsedEventHandler(timer2_Elapsed);
- timer2.Start();
- }
- bool Check_reconn = false;
- public void timer2_Elapsed(object sender, ElapsedEventArgs e)
- {
- if (Check_reconn == false)
- {
- Check_reconn = true;
- System.Threading.Thread.Sleep(5000);
- Parsing Ps = new Parsing();
- ReturnTag = Ps.TagParsing();
- RegTag(Convert.ToInt32(ReturnTag[1]), ReturnTag[0]);
- LogFileCreate LFC = new LogFileCreate();
- LFC.Log("포인트 등록완료");
- Check_reconn = false;
- timer2.Stop();
- timer2.Enabled = false;
-
- }
- }
- public void UnReg()
- {
- Parsing Ps = new Parsing();
- ReturnTag = Ps.TagParsing();
- UnregTag(Convert.ToInt32(ReturnTag[1]), ReturnTag[0]);
- }
-
-
-
-
-
-
-
-
-
- My_TimerThread TTs = new My_TimerThread();
- static string SelecDir;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- ProStopBtn.Enabled = false;
- PointSetBtn.Enabled = false;
- PointUnRegBtn.Enabled = false;
- ProStartBtn.Enabled = false;
- ProStopBtn.Enabled = false;
-
- Shown += Form1_Shown;
- }
- System.Timers.Timer timer_NetStat = new System.Timers.Timer();
- LogFileCreate LFC = new LogFileCreate();
- public void Timer_netStat()
- {
- timer_NetStat.Interval = 5000;
- timer_NetStat.Elapsed += new ElapsedEventHandler(net_state_Thread);
- timer_NetStat.Start();
- }
- private delegate void myDelegate(string theValue);
- private void Wirte_Communicaion_State(string theValue)
- {
- try
- {
- LFC.Log(theValue + "= 들어온 통신상태");
- if (theValue == "1")
- {
- Net_Stat.BackColor = Color.LightSkyBlue;
- Net_Stat.Text = "통신중";
- }
- else if (theValue == "0")
- {
- Net_Stat.BackColor = Color.Red;
- Net_Stat.Text = "통신끊김";
- }
- }
- catch(Exception E)
- {
- LFC.Log(Convert.ToString(E));
- LogTxt.Text += Convert.ToString(E)+ Environment.NewLine;
- }
- }
- public void net_state_Thread(object sender, ElapsedEventArgs e)
- {
- try
- {
-
- int nNet;
- nNet = GetNetworkStatus();
- string strRet;
- strRet = nNet.ToString();
-
- this.Invoke(new myDelegate(Wirte_Communicaion_State), new object[] { strRet });
- }
- catch (Exception E)
- {
- LFC.Log(Convert.ToString(E));
- LogTxt.Text += Convert.ToString(E) + Environment.NewLine;
- }
- }
-
- private void Form1_Shown(object sender, EventArgs e)
- {
-
- LogFileCreate LFC = new LogFileCreate();
- string ReturnValue = iCosFourConn();
- iCosFourReadDB();
- LFC.Log("접속 함수 동작 시작 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- LogTxt.Text += "접속 함수 동작 시작 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine;
- if (ReturnValue == "SUCCESS")
- {
- LogTxt.Text += "접속 성공 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine;
- PointSetBtn.Enabled = true;
- PointUnRegBtn.Enabled = true;
- ProStartBtn.Enabled = true;
- }
- SetDeviceDB(0, "");
-
-
- try
- {
-
-
- EndTxt.Clear();
-
- ProStartBtn.Enabled = false;
- StartTxt.Text = Convert.ToString(DateTime.Now);
- LogTxt.Text += "프로그램 시작 " + Convert.ToString(DateTime.Now) + Environment.NewLine;
- 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();
- My_TimerThread MTT = new My_TimerThread();
- MTT.timer2start();
-
- }
- catch (Exception eProStart)
- {
- LogTxt.Text += "프로그램 오류 " + Convert.ToString("DB에 접속되지 않았습니다.") + " " + eProStart + " = " + Convert.ToString(DateTime.Now) + Environment.NewLine;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- private void ProStartBtn_Click(object sender, EventArgs e)
- {
- if (toolStripStatusLabel1.Text != "")
- {
- try
- {
-
-
-
-
- EndTxt.Clear();
-
- ProStartBtn.Enabled = false;
- StartTxt.Text = Convert.ToString(DateTime.Now);
- LogTxt.Text += "프로그램 시작 " + Convert.ToString(DateTime.Now) + Environment.NewLine;
- 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();
- My_TimerThread MTT = new My_TimerThread();
- MTT.timer2start();
- }
- catch (Exception eProStart)
- {
- LogTxt.Text += "프로그램 오류 " + Convert.ToString("DB에 접속되지 않았습니다.") + " " + eProStart + " = " + Convert.ToString(DateTime.Now) + Environment.NewLine;
- }
- }
- else
- MessageBox.Show("경로를 설정해주세요");
- }
- private void ProStopBtn_Click(object sender, EventArgs e)
- {
- CloseConnection();
- TTs.timer2Stop();
- FileSettingBtn.Enabled = true;
- FileMatchBtn.Enabled = true;
- FileExpBtn.Enabled = true;
-
- ProStartBtn.Enabled = true;
- EndTxt.Text = Convert.ToString(DateTime.Now);
- LogTxt.Text += "프로그램 중단 " + " = " + Convert.ToString(DateTime.Now) + Environment.NewLine;
- ProStopBtn.Enabled = false;
- }
- private void ProSetBtn_Click(object sender, EventArgs e)
- {
- SettingForm SF = new SettingForm();
- SF.ShowDialog();
- }
- private void ProPathBtn_Click_1(object sender, EventArgs e)
- {
- FolderBrowserDialog();
- }
-
-
-
-
-
- private void FileSettingBtn_Click(object sender, EventArgs e)
- {
- string FSBLocation = Environment.CurrentDirectory + "\\Login.ini";
- try
- {
- System.Diagnostics.Process.Start(FSBLocation);
- }
- 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)
- {
- if ((!Error.Contains("Login.ini")) || (!Error.Contains("expression.csv")) || (!Error.Contains("Matching.csv")))
- {
- MessageBox.Show("파일을 찾을 수 없습니다.");
- }
- }
- public string FolderBrowserDialog()
- {
-
- LogFileCreate LFC = new LogFileCreate();
- FolderBrowserDialog folderDig = new FolderBrowserDialog();
- folderDig.ShowNewFolderButton = true;
-
-
- DialogResult result = folderDig.ShowDialog();
- if (result == DialogResult.OK)
- {
- SelecDir = folderDig.SelectedPath;
- Environment.SpecialFolder root = folderDig.RootFolder;
- toolStripStatusLabel1.Text = "프로젝트 경로 : " + Environment.CurrentDirectory;
- LogTxt.Text += "경로설정완료 " + SelecDir + " " + Convert.ToString(DateTime.Now) + Environment.NewLine;
- LFC.Log("경로 설정 완료" + SelecDir);
- }
- return SelecDir;
- }
-
- private void EraseLog_Click(object sender, EventArgs e)
- {
- LogTxt.Clear();
- }
-
-
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- LogFileCreate LFC = new LogFileCreate();
- if (MessageBox.Show("프로그램을 종료하시겠습니까?", "종료", MessageBoxButtons.YesNo) != DialogResult.Yes)
- {
- LFC.Log("사용자에 의해 프로그램 종료가 취소되었습니다. = " + Convert.ToString(DateTime.Now));
- e.Cancel = true;
- }
- else
- {
- LFC.Log("사용자에 의해 프로그램이 종료되었습니다. = " + Convert.ToString(DateTime.Now));
- }
-
- }
- private void PointSetBtn_Click(object sender, EventArgs e)
- {
-
- ReadTag();
-
- }
- private void Conn_iCos_Btn_Click(object sender, EventArgs e)
- {
-
-
-
-
-
-
-
-
-
-
- LogFileCreate LFC = new LogFileCreate();
- string ReturnValue = iCosFourConn();
- iCosFourReadDB();
- LFC.Log("접속 함수 동작 시작 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- LogTxt.Text += "접속 함수 동작 시작 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine;
- if (ReturnValue == "SUCCESS")
- {
- LogTxt.Text += "접속 성공 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine;
- PointSetBtn.Enabled = true;
- PointUnRegBtn.Enabled = true;
- ProStartBtn.Enabled = true;
-
- }
- SetDeviceDB(0, "");
- }
-
-
- delegate void CrossThreadSafetySetErrorCodeTxt(string text);
- private void SetText(string text)
- {
- if (this.LogTxt.InvokeRequired)
- {
- CrossThreadSafetySetErrorCodeTxt d = new CrossThreadSafetySetErrorCodeTxt(SetText);
- this.Invoke(d, text);
-
- }
- else
- {
- this.LogTxt.Text = text;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private void PointUnRegBtn_Click(object sender, EventArgs e)
- {
- UnReg();
- }
- private void Btn_TagSetFile_Click(object sender, EventArgs e)
- {
- Creating_Setting_File CSF = new Creating_Setting_File();
- CSF.Create_Point_File();
- }
- private void Btn_SettingFile_Click(object sender, EventArgs e)
- {
- Creating_Setting_File CSF = new Creating_Setting_File();
- CSF.Create_Setting_Point_File();
- }
-
- private void AllValueCheck_btn_Click(object sender, EventArgs e)
- {
- string[] sArray = File.ReadAllLines("TagSetFile.csv", Encoding.Default);
- LogTxt.Text += "*********** 현재 시간 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "**********" + Environment.NewLine;
- try
- {
- for (int i = 0; i < sArray.Length; i++)
- {
- string[] tmp = sArray[i].Split(',');
- LogTxt.Text += tmp[1] + " 의 값 : " + GetTagValue(tmp[1]) + Environment.NewLine;
- }
- LogTxt.Text += "**************************************************************" + Environment.NewLine;
-
- }
- catch (Exception er)
- {
- MessageBox.Show("오류 발생 = " + er);
- }
- }
- }
-
-
- }
|