3c1a0bdca1faf05afd327d65314c5b440437d9c1.svn-base 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using iBemsDataService.Model;
  2. using Quartz;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Transactions;
  8. using System.Web;
  9. namespace iBemsDataService.Controllers.Fms.WorkManagement.ScheduledTask
  10. {
  11. public class RegressionPlanJob : IJob
  12. {
  13. private iBemsEntities db = new iBemsEntities();
  14. public void Execute(IJobExecutionContext context)
  15. {
  16. using (var transaction = new TransactionScope())
  17. {
  18. // 현재시간
  19. //DateTime today = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
  20. int TimeSpanValue = 0;
  21. DateTime BaseTime = new DateTime(Int32.Parse(DateTime.Now.ToString("yyyy")), Int32.Parse(DateTime.Now.ToString("MM")), Int32.Parse(DateTime.Now.ToString("dd")), 23, 0, 0);
  22. DateTime ForecastTime = BaseTime + TimeSpan.FromHours(28 + TimeSpanValue); // ex) 07-18 23시 -> 07-20 03시 예측 이용
  23. DateTime BemsEnergyDailyTime = ForecastTime + TimeSpan.FromHours(-3 - TimeSpanValue);
  24. int DayOfWeek = (int)ForecastTime.DayOfWeek;
  25. double TMN = 0;
  26. double TMX = 0;
  27. double MeanT = 0;
  28. double HDD = 0;
  29. double CDD = 0;
  30. // 기상 정보 확인
  31. var query1 =
  32. from s in db.WeatherDongNaeForecast
  33. where s.BaseTime == BaseTime && s.ForecastTime == ForecastTime
  34. select s;
  35. foreach (var s in query1)
  36. {
  37. TMN = (double)s.TMN;
  38. TMX = (double)s.TMX;
  39. MeanT = (TMN + TMX) / 2;
  40. HDD = 15.7 - MeanT;
  41. if (HDD < 0) HDD = 0;
  42. CDD = MeanT - 15.7;
  43. if (CDD < 0) CDD = 0;
  44. }
  45. int SiteId = 0;
  46. string Weekday_Power = String.Empty;
  47. string Weekday_Gas = String.Empty;
  48. string Weekday_Water = String.Empty;
  49. string Holiday_Power = String.Empty;
  50. string Holiday_Gas = String.Empty;
  51. string Holiday_Water = String.Empty;
  52. bool IsRegression = false;
  53. string PowerValue = String.Empty;
  54. string GasValue = String.Empty;
  55. string WaterValue = String.Empty;
  56. double PowerPercent = 0;
  57. double GasPercent = 0;
  58. double WaterPercent = 0;
  59. // 회귀식 확인
  60. var query2 =
  61. from s in db.BemsEnergyDailyRegression
  62. select s;
  63. foreach (var s in query2)
  64. {
  65. SiteId = s.SiteId;
  66. Weekday_Power = s.Weekday_Power;
  67. Weekday_Gas = s.Weekday_Gas;
  68. Weekday_Water = s.Weekday_Water;
  69. Holiday_Power = s.Holiday_Power;
  70. Holiday_Gas = s.Holiday_Gas;
  71. Holiday_Water = s.Holiday_Water;
  72. IsRegression = (bool)s.IsRegression;
  73. }
  74. // 회귀식 계산(예측 확인)
  75. if (DayOfWeek == 0 || DayOfWeek == 6) // 주말
  76. {
  77. Holiday_Power = Holiday_Power.Replace("H", HDD.ToString());
  78. Holiday_Power = Holiday_Power.Replace("C", CDD.ToString());
  79. PowerValue = new DataTable().Compute(Holiday_Power, null).ToString();
  80. Holiday_Gas = Holiday_Gas.Replace("H", HDD.ToString());
  81. Holiday_Gas = Holiday_Gas.Replace("C", CDD.ToString());
  82. GasValue = new DataTable().Compute(Holiday_Gas, null).ToString();
  83. Holiday_Water = Holiday_Water.Replace("H", HDD.ToString());
  84. Holiday_Water = Holiday_Water.Replace("C", CDD.ToString());
  85. WaterValue = new DataTable().Compute(Holiday_Water, null).ToString();
  86. }
  87. else // 평일
  88. {
  89. Weekday_Power = Weekday_Power.Replace("H", HDD.ToString());
  90. Weekday_Power = Weekday_Power.Replace("C", CDD.ToString());
  91. PowerValue = new DataTable().Compute(Weekday_Power, null).ToString();
  92. Weekday_Gas = Weekday_Gas.Replace("H", HDD.ToString());
  93. Weekday_Gas = Weekday_Gas.Replace("C", CDD.ToString());
  94. GasValue = new DataTable().Compute(Weekday_Gas, null).ToString();
  95. Weekday_Water = Weekday_Water.Replace("H", HDD.ToString());
  96. Weekday_Water = Weekday_Water.Replace("C", CDD.ToString());
  97. WaterValue = new DataTable().Compute(Weekday_Water, null).ToString();
  98. }
  99. // 에너지별로 예측 대비 목표 비율 계산
  100. var query3 =
  101. from s in db.BemsEnergyDaily
  102. where s.SiteId == SiteId && s.CreatedDate == BemsEnergyDailyTime
  103. select s;
  104. foreach (var s in query3)
  105. {
  106. if (s.FuelTypeId == 1) // 전기
  107. {
  108. PowerPercent = (double)s.Goal / (double)s.Prediction;
  109. }
  110. else if (s.FuelTypeId == 2) // 가스
  111. {
  112. GasPercent = (double)s.Goal / (double)s.Prediction;
  113. }
  114. else if (s.FuelTypeId == 3) // 수도
  115. {
  116. WaterPercent = (double)s.Goal / (double)s.Prediction;
  117. }
  118. }
  119. // 에너지별 Toe Factor
  120. double Power_ToeFactor = 0;
  121. double Gas_ToeFactor = 0;
  122. var query4 =
  123. from s in db.BemsFactorToe
  124. select s;
  125. // 단위 환산
  126. foreach (var s in query4)
  127. {
  128. if (s.FuelTypeId == 1) Power_ToeFactor = s.TotalFactor;
  129. else if (s.FuelTypeId == 2) Gas_ToeFactor = s.TotalFactor;
  130. }
  131. // 에너지별로 업데이트
  132. for (int i = 1; i <= 3; i++)
  133. {
  134. var update_BemsEnergyDaily = (from d in db.BemsEnergyDaily
  135. where d.SiteId == SiteId &&
  136. d.CreatedDate == BemsEnergyDailyTime &&
  137. d.FuelTypeId == i
  138. select d).Single();
  139. // 예측값을 계산해서 저장한다(그냥 저장하는 거임)
  140. double Temp_Prediction_Regression = 0;
  141. if (i == 1) Temp_Prediction_Regression = Convert.ToDouble(PowerValue);
  142. else if (i == 2)
  143. {
  144. // 단위 환산을 하여 Kwh 저장
  145. double Temp_GasValue = Convert.ToDouble(GasValue) * Gas_ToeFactor / Power_ToeFactor;
  146. Temp_Prediction_Regression = Temp_GasValue;
  147. }
  148. else if (i == 3) Temp_Prediction_Regression = Convert.ToDouble(WaterValue);
  149. update_BemsEnergyDaily.Prediction_Regression = Temp_Prediction_Regression;
  150. update_BemsEnergyDaily.MeanT = MeanT;
  151. update_BemsEnergyDaily.HDD = HDD;
  152. update_BemsEnergyDaily.CDD = CDD;
  153. // 실제로 예측과 목표가 비율에 따라 수정된다
  154. if (IsRegression == true)
  155. {
  156. // 예측 다시 저장
  157. update_BemsEnergyDaily.Prediction = Temp_Prediction_Regression;
  158. // 목표 다시 저장
  159. double Temp_Goal = 0;
  160. if (i == 1) Temp_Goal = Temp_Prediction_Regression * PowerPercent;
  161. else if (i == 2) Temp_Goal = Temp_Prediction_Regression * GasPercent;
  162. else if (i == 3) Temp_Goal = Temp_Prediction_Regression * WaterPercent;
  163. update_BemsEnergyDaily.Goal = Temp_Goal;
  164. }
  165. db.SaveChanges();
  166. }
  167. transaction.Complete();
  168. }
  169. }
  170. }
  171. }