b6e5ad83676b976042df84e3a6171a7f5a42aa07.svn-base 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 BEMSDataGateway
  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. //temp[0] = 일련번호
  36. //temp[1] = 데이터 삽입 형태 (15분 단위냐,, 1시간단위냐,, 하루단위냐..)
  37. //temp[2] = 성능분석식 및 단위 환산 부
  38. //temp[3] = 1.100.4900.30 // 순서대로 SiteId.FacilityTypeId.FacilityCode.PropertyId 의 순서임.
  39. ///////////////////////////////////////////
  40. tempId = temp[3].Split('.');
  41. string Virtual_SiteId = tempId[0];
  42. string Virtual_FacilityTypeId = tempId[1];
  43. string Virtual_FacilityCode = tempId[2];
  44. string Virtual_PropertyId = tempId[3];
  45. ///////////////////////////////////////////
  46. Store_Type = temp[1].Split('.');
  47. if (!("".Equals(s))) // 읽은 값이 null이 아닌 경우.
  48. {
  49. object resultexp = 0;
  50. MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl(); // Script를 사용할 수 있게 만드는 스크립트 컨트롤을 객체 선언
  51. sc.Language = "VBScript"; // 사용할 스크립트 언어는 Visaul Basic Script
  52. string Pattern = "Tag" + @"\d{1,5}";
  53. Regex regex = new Regex(Pattern);
  54. // 불러들인 수식에서 포인트 명을 값으로...
  55. foreach (Match match in regex.Matches(temp[2]))
  56. {
  57. string Tag_Name = Convert.ToString(match); //Matching 된 값을 string형식으로 변환. Tag_name 은 Tag1, Tag2 등등....
  58. if (Table_Type == "Raw" && Store_Type[0] == "T")
  59. {
  60. temp[2] = temp[2].Replace(Tag_Name, AllDic[MatchingDic[Tag_Name]][0].ToString()); // 파싱한 식의 값을 Tag를 '값'으로 치환 // [Key] = TagXXX [Value] = 1.1.4478.xxx
  61. Count++;
  62. }
  63. else if(Table_Type == "FiftMin" && Store_Type[1] == "T")
  64. {
  65. int Array_Value_Select = Ps.Select_MinTime_Data(NowMin); //시간에 따른 Array 변수뽑아냄
  66. temp[2] = temp[2].Replace(Tag_Name, AllDic[MatchingDic[Tag_Name]][Array_Value_Select].ToString()); // 파싱한 식의 값을 Tag를 '값'으로 치환
  67. Count++;
  68. }
  69. else if(Table_Type == "Hour" && Store_Type[2] == "T" && NowMin == 00)
  70. {
  71. temp[2] = temp[2].Replace(Tag_Name, AllDic[MatchingDic[Tag_Name]][12].ToString());
  72. Count++;
  73. }
  74. }
  75. if (Count > 0)
  76. {
  77. TempExp = temp[2];
  78. resultexp = sc.Eval(TempExp); // 문자열 값을 계산
  79. ResultValue = Convert.ToString(Math.Round(Convert.ToDouble(resultexp), 4, MidpointRounding.AwayFromZero));
  80. using (SqlConnection conn = new SqlConnection(strcon))
  81. {
  82. conn.Open();
  83. string InsertDate = DateTime.Now.ToString("yyyy-MM-dd HH:" + NowMin + ":00.000");
  84. switch (Table_Type)
  85. {
  86. case "Raw":
  87. query = "INSERT INTO " + Table.RawT + "(SiteId,FacilityTypeId,FacilityCode,PropertyId,CreatedDateTime,CurrentValue) "
  88. + "VALUES(" + Virtual_SiteId + "," + Virtual_FacilityTypeId + "," + Virtual_FacilityCode + "," + Virtual_PropertyId + ",'" + InsertDate + "'," + ResultValue + ")";
  89. break;
  90. case "FiftMin":
  91. query = "INSERT INTO " + Table.FiFt + "(SiteId,FacilityTypeId,FacilityCode,PropertyId,CreatedDateTime,CurrentValue) "
  92. + "VALUES(" + Virtual_SiteId + "," + Virtual_FacilityTypeId + "," + Virtual_FacilityCode + "," + Virtual_PropertyId + ",'" + InsertDate + "'," + ResultValue + ")";
  93. break;
  94. case "Hour":
  95. query = "INSERT INTO " + Table.Hourly + "(SiteId,FacilityTypeId,FacilityCode,PropertyId,CreatedDateTime,CurrentValue) "
  96. + "VALUES(" + Virtual_SiteId + "," + Virtual_FacilityTypeId + "," + Virtual_FacilityCode + "," + Virtual_PropertyId + ",'" + InsertDate + "'," + ResultValue + ")";
  97. break;
  98. }
  99. SqlCommand cmd = new SqlCommand(query, conn);
  100. LFC.Log(query);
  101. cmd.ExecuteNonQuery();
  102. conn.Close();
  103. }
  104. }
  105. Count = 0;
  106. }
  107. }
  108. }
  109. }
  110. }