using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Collections; using System.Text.RegularExpressions; using System.Data.SqlClient; using System.Collections.Concurrent; namespace DataGateWayProject { class Expression_Process { private string[] temp; LogFileCreate LFC = new LogFileCreate(); // CSV파일을 읽는 메서드 public void ReadCSV_Passing_Expression(IDictionary AllDic, int NowMin, string Table_Type, IDictionary MatchingDic) { //ConcurrentDictionary Fift_Data_Insert = new ConcurrentDictionary(); StreamReader sr = new StreamReader("Expression.csv", Encoding.GetEncoding("euc-kr")); Parsing Ps = new Parsing(); My_DB_DML MDD = new My_DB_DML(); string[] tempId = new string[4]; string strcon = MDD.ConnDBInfo(); string query = ""; string TempExp = ""; string ResultValue = ""; string[] Store_Type = new string[3]; int Count = 0; while (!sr.EndOfStream) // Stream의 끝이 올때까지 반복 { //s의 형태는 '1/XXX/식/가상관제점' 의 포맷으로 되어있음 string s = sr.ReadLine(); // 한줄을 읽음. temp = s.Split(','); // Split() 메서드를 이용하여 ',' 구분하여 잘라냄 if (!(s.Length > 0)) { continue; } //temp[0] = 일련번호 //temp[1] = 데이터 삽입 형태 (15분 단위냐,, 1시간단위냐,, 하루단위냐..) //temp[2] = 성능분석식 및 단위 환산 부 //temp[3] = 1.100.4900.30 // 순서대로 SiteId.FacilityTypeId.FacilityCode.PropertyId 의 순서임. /////////////////////////////////////////// tempId = temp[3].Split('.'); string Virtual_SiteId = tempId[0].Trim(); string Virtual_FacilityTypeId = tempId[1].Trim(); string Virtual_FacilityCode = tempId[2].Trim(); string Virtual_PropertyId = tempId[3].Trim(); /////////////////////////////////////////// Store_Type = temp[1].Split('.'); if (!("".Equals(s))) // 읽은 값이 null이 아닌 경우. { object resultexp = 0; MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl(); // Script를 사용할 수 있게 만드는 스크립트 컨트롤을 객체 선언 sc.Language = "VBScript"; // 사용할 스크립트 언어는 Visaul Basic Script string Pattern = "Tag" + @"\d{1,5}"; Regex regex = new Regex(Pattern); // 불러들인 수식에서 포인트 명을 값으로... foreach (Match match in regex.Matches(temp[2])) { string Tag_Name = Convert.ToString(match); //Matching 된 값을 string형식으로 변환. Tag_name 은 Tag1, Tag2 등등.... if (Table_Type == "Raw" && Store_Type[0].Trim() == "T") { temp[2] = temp[2].Replace(Tag_Name, AllDic[MatchingDic[Tag_Name]][0].ToString()); // 파싱한 식의 값을 Tag를 '값'으로 치환 // [Key] = TagXXX [Value] = 1.1.4478.xxx Count++; } else if(Table_Type == "FiftMin" && Store_Type[1].Trim() == "T") { int Array_Value_Select = Ps.Select_MinTime_Data(NowMin); //시간에 따른 Array 변수뽑아냄 temp[2] = temp[2].Replace(Tag_Name, AllDic[MatchingDic[Tag_Name]][Array_Value_Select].ToString()); // 파싱한 식의 값을 Tag를 '값'으로 치환 Count++; } else if(Table_Type == "Hour" && Store_Type[2].Trim() == "T" && NowMin == 00) { temp[2] = temp[2].Replace(Tag_Name, AllDic[MatchingDic[Tag_Name]][12].ToString()); Count++; } } if (Count > 0) { TempExp = temp[2]; resultexp = sc.Eval(TempExp); // 문자열 값을 계산 ResultValue = Convert.ToString(Math.Round(Convert.ToDouble(resultexp), 4, MidpointRounding.AwayFromZero)); using (SqlConnection conn = new SqlConnection(strcon)) { conn.Open(); string InsertDate = DateTime.Now.ToString("yyyy-MM-dd HH:" + NowMin + ":00.000"); switch (Table_Type) { case "Raw": query = "INSERT INTO " + Table.RawT + "(SiteId,FacilityTypeId,FacilityCode,PropertyId,CreatedDateTime,CurrentValue) " + "VALUES(" + Virtual_SiteId + "," + Virtual_FacilityTypeId + "," + Virtual_FacilityCode + "," + Virtual_PropertyId + ",'" + InsertDate + "'," + ResultValue + ")"; break; case "FiftMin": query = "INSERT INTO " + Table.FiFt + "(SiteId,FacilityTypeId,FacilityCode,PropertyId,CreatedDateTime,CurrentValue) " + "VALUES(" + Virtual_SiteId + "," + Virtual_FacilityTypeId + "," + Virtual_FacilityCode + "," + Virtual_PropertyId + ",'" + InsertDate + "'," + ResultValue + ")"; break; case "Hour": query = "INSERT INTO " + Table.Hourly + "(SiteId,FacilityTypeId,FacilityCode,PropertyId,CreatedDateTime,CurrentValue) " + "VALUES(" + Virtual_SiteId + "," + Virtual_FacilityTypeId + "," + Virtual_FacilityCode + "," + Virtual_PropertyId + ",'" + InsertDate + "'," + ResultValue + ")"; break; } SqlCommand cmd = new SqlCommand(query, conn); LFC.Log(query); cmd.ExecuteNonQuery(); conn.Close(); } } Count = 0; } } } } }