c6af1d65e07ecd268a3bf7cc8fd97955e5fc5796.svn-base 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Collections;
  7. using System.Text.RegularExpressions;
  8. using System.Data.SqlClient;
  9. using System.Collections.Concurrent;
  10. namespace DataGateWayProject
  11. {
  12. class Expression_Process
  13. {
  14. private string[] temp;
  15. LogFileCreate LFC = new LogFileCreate();
  16. // CSV파일을 읽는 메서드
  17. public void ReadCSV_Passing_Expression(IDictionary<string, float[]> AllDic, int NowMin, string Table_Type, IDictionary<string, string> MatchingDic)
  18. {
  19. //ConcurrentDictionary<string, float[]> Fift_Data_Insert = new ConcurrentDictionary<string, float[]>();
  20. StreamReader sr = new StreamReader("Expression.csv", Encoding.GetEncoding("euc-kr"));
  21. Parsing Ps = new Parsing();
  22. My_DB_DML MDD = new My_DB_DML();
  23. string[] tempId = new string[4];
  24. string strcon = MDD.ConnDBInfo();
  25. string query = "";
  26. string TempExp = "";
  27. string ResultValue = "";
  28. string[] Store_Type = new string[3];
  29. int Count = 0;
  30. while (!sr.EndOfStream) // Stream의 끝이 올때까지 반복
  31. {
  32. //s의 형태는 '1/XXX/식/가상관제점' 의 포맷으로 되어있음
  33. string s = sr.ReadLine(); // 한줄을 읽음.
  34. temp = s.Split(','); // Split() 메서드를 이용하여 ',' 구분하여 잘라냄
  35. if (!(s.Length > 0))
  36. {
  37. continue;
  38. }
  39. //temp[0] = 일련번호
  40. //temp[1] = 데이터 삽입 형태 (15분 단위냐,, 1시간단위냐,, 하루단위냐..)
  41. //temp[2] = 성능분석식 및 단위 환산 부
  42. //temp[3] = 1.100.4900.30 // 순서대로 SiteId.FacilityTypeId.FacilityCode.PropertyId 의 순서임.
  43. ///////////////////////////////////////////
  44. tempId = temp[3].Split('.');
  45. string Virtual_SiteId = tempId[0].Trim();
  46. string Virtual_FacilityTypeId = tempId[1].Trim();
  47. string Virtual_FacilityCode = tempId[2].Trim();
  48. string Virtual_PropertyId = tempId[3].Trim();
  49. ///////////////////////////////////////////
  50. Store_Type = temp[1].Split('.');
  51. if (!("".Equals(s))) // 읽은 값이 null이 아닌 경우.
  52. {
  53. object resultexp = 0;
  54. MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl(); // Script를 사용할 수 있게 만드는 스크립트 컨트롤을 객체 선언
  55. sc.Language = "VBScript"; // 사용할 스크립트 언어는 Visaul Basic Script
  56. string Pattern = "Tag" + @"\d{1,5}";
  57. Regex regex = new Regex(Pattern);
  58. // 불러들인 수식에서 포인트 명을 값으로...
  59. foreach (Match match in regex.Matches(temp[2]))
  60. {
  61. string Tag_Name = Convert.ToString(match); //Matching 된 값을 string형식으로 변환. Tag_name 은 Tag1, Tag2 등등....
  62. if (Table_Type == "Raw" && Store_Type[0].Trim() == "T")
  63. {
  64. temp[2] = temp[2].Replace(Tag_Name, AllDic[MatchingDic[Tag_Name]][0].ToString()); // 파싱한 식의 값을 Tag를 '값'으로 치환 // [Key] = TagXXX [Value] = 1.1.4478.xxx
  65. Count++;
  66. }
  67. else if(Table_Type == "FiftMin" && Store_Type[1].Trim() == "T")
  68. {
  69. int Array_Value_Select = Ps.Select_MinTime_Data(NowMin); //시간에 따른 Array 변수뽑아냄
  70. temp[2] = temp[2].Replace(Tag_Name, AllDic[MatchingDic[Tag_Name]][Array_Value_Select].ToString()); // 파싱한 식의 값을 Tag를 '값'으로 치환
  71. Count++;
  72. }
  73. else if(Table_Type == "Hour" && Store_Type[2].Trim() == "T" && NowMin == 00)
  74. {
  75. temp[2] = temp[2].Replace(Tag_Name, AllDic[MatchingDic[Tag_Name]][12].ToString());
  76. Count++;
  77. }
  78. }
  79. if (Count > 0)
  80. {
  81. TempExp = temp[2];
  82. resultexp = sc.Eval(TempExp); // 문자열 값을 계산
  83. ResultValue = Convert.ToString(Math.Round(Convert.ToDouble(resultexp), 4, MidpointRounding.AwayFromZero));
  84. using (SqlConnection conn = new SqlConnection(strcon))
  85. {
  86. conn.Open();
  87. string InsertDate = DateTime.Now.ToString("yyyy-MM-dd HH:" + NowMin + ":00.000");
  88. switch (Table_Type)
  89. {
  90. case "Raw":
  91. query = "INSERT INTO " + Table.RawT + "(SiteId,FacilityTypeId,FacilityCode,PropertyId,CreatedDateTime,CurrentValue) "
  92. + "VALUES(" + Virtual_SiteId + "," + Virtual_FacilityTypeId + "," + Virtual_FacilityCode + "," + Virtual_PropertyId + ",'" + InsertDate + "'," + ResultValue + ")";
  93. break;
  94. case "FiftMin":
  95. query = "INSERT INTO " + Table.FiFt + "(SiteId,FacilityTypeId,FacilityCode,PropertyId,CreatedDateTime,CurrentValue) "
  96. + "VALUES(" + Virtual_SiteId + "," + Virtual_FacilityTypeId + "," + Virtual_FacilityCode + "," + Virtual_PropertyId + ",'" + InsertDate + "'," + ResultValue + ")";
  97. break;
  98. case "Hour":
  99. query = "INSERT INTO " + Table.Hourly + "(SiteId,FacilityTypeId,FacilityCode,PropertyId,CreatedDateTime,CurrentValue) "
  100. + "VALUES(" + Virtual_SiteId + "," + Virtual_FacilityTypeId + "," + Virtual_FacilityCode + "," + Virtual_PropertyId + ",'" + InsertDate + "'," + ResultValue + ")";
  101. break;
  102. }
  103. SqlCommand cmd = new SqlCommand(query, conn);
  104. LFC.Log(query);
  105. cmd.ExecuteNonQuery();
  106. conn.Close();
  107. }
  108. }
  109. Count = 0;
  110. }
  111. }
  112. }
  113. }
  114. }