123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using iBemsDataService.Model;
- using Quartz;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Transactions;
- using System.Web;
- namespace iBemsDataService.Controllers.Fms.WorkManagement.ScheduledTask
- {
- public class RegressionPlanJob : IJob
- {
- private iBemsEntities db = new iBemsEntities();
- public void Execute(IJobExecutionContext context)
- {
- using (var transaction = new TransactionScope())
- {
- // 현재시간
- //DateTime today = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
- int TimeSpanValue = 0;
- 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);
- DateTime ForecastTime = BaseTime + TimeSpan.FromHours(28 + TimeSpanValue); // ex) 07-18 23시 -> 07-20 03시 예측 이용
- DateTime BemsEnergyDailyTime = ForecastTime + TimeSpan.FromHours(-3 - TimeSpanValue);
- int DayOfWeek = (int)ForecastTime.DayOfWeek;
- double TMN = 0;
- double TMX = 0;
- double MeanT = 0;
- double HDD = 0;
- double CDD = 0;
- // 기상 정보 확인
- var query1 =
- from s in db.WeatherDongNaeForecast
- where s.BaseTime == BaseTime && s.ForecastTime == ForecastTime
- select s;
- foreach (var s in query1)
- {
- TMN = (double)s.TMN;
- TMX = (double)s.TMX;
- MeanT = (TMN + TMX) / 2;
- HDD = 15.7 - MeanT;
- if (HDD < 0) HDD = 0;
- CDD = MeanT - 15.7;
- if (CDD < 0) CDD = 0;
- }
- int SiteId = 0;
- string Weekday_Power = String.Empty;
- string Weekday_Gas = String.Empty;
- string Weekday_Water = String.Empty;
- string Holiday_Power = String.Empty;
- string Holiday_Gas = String.Empty;
- string Holiday_Water = String.Empty;
- bool IsRegression = false;
- string PowerValue = String.Empty;
- string GasValue = String.Empty;
- string WaterValue = String.Empty;
- double PowerPercent = 0;
- double GasPercent = 0;
- double WaterPercent = 0;
- // 회귀식 확인
- var query2 =
- from s in db.BemsEnergyDailyRegression
- select s;
- foreach (var s in query2)
- {
- SiteId = s.SiteId;
- Weekday_Power = s.Weekday_Power;
- Weekday_Gas = s.Weekday_Gas;
- Weekday_Water = s.Weekday_Water;
- Holiday_Power = s.Holiday_Power;
- Holiday_Gas = s.Holiday_Gas;
- Holiday_Water = s.Holiday_Water;
- IsRegression = (bool)s.IsRegression;
- }
- // 회귀식 계산(예측 확인)
- if (DayOfWeek == 0 || DayOfWeek == 6) // 주말
- {
- Holiday_Power = Holiday_Power.Replace("H", HDD.ToString());
- Holiday_Power = Holiday_Power.Replace("C", CDD.ToString());
- PowerValue = new DataTable().Compute(Holiday_Power, null).ToString();
- Holiday_Gas = Holiday_Gas.Replace("H", HDD.ToString());
- Holiday_Gas = Holiday_Gas.Replace("C", CDD.ToString());
- GasValue = new DataTable().Compute(Holiday_Gas, null).ToString();
- Holiday_Water = Holiday_Water.Replace("H", HDD.ToString());
- Holiday_Water = Holiday_Water.Replace("C", CDD.ToString());
- WaterValue = new DataTable().Compute(Holiday_Water, null).ToString();
- }
- else // 평일
- {
- Weekday_Power = Weekday_Power.Replace("H", HDD.ToString());
- Weekday_Power = Weekday_Power.Replace("C", CDD.ToString());
- PowerValue = new DataTable().Compute(Weekday_Power, null).ToString();
- Weekday_Gas = Weekday_Gas.Replace("H", HDD.ToString());
- Weekday_Gas = Weekday_Gas.Replace("C", CDD.ToString());
- GasValue = new DataTable().Compute(Weekday_Gas, null).ToString();
- Weekday_Water = Weekday_Water.Replace("H", HDD.ToString());
- Weekday_Water = Weekday_Water.Replace("C", CDD.ToString());
- WaterValue = new DataTable().Compute(Weekday_Water, null).ToString();
- }
- // 에너지별로 예측 대비 목표 비율 계산
- var query3 =
- from s in db.BemsEnergyDaily
- where s.SiteId == SiteId && s.CreatedDate == BemsEnergyDailyTime
- select s;
- foreach (var s in query3)
- {
- if (s.FuelTypeId == 1) // 전기
- {
- PowerPercent = (double)s.Goal / (double)s.Prediction;
- }
- else if (s.FuelTypeId == 2) // 가스
- {
- GasPercent = (double)s.Goal / (double)s.Prediction;
- }
- else if (s.FuelTypeId == 3) // 수도
- {
- WaterPercent = (double)s.Goal / (double)s.Prediction;
- }
- }
- // 에너지별 Toe Factor
- double Power_ToeFactor = 0;
- double Gas_ToeFactor = 0;
- var query4 =
- from s in db.BemsFactorToe
- select s;
- // 단위 환산
- foreach (var s in query4)
- {
- if (s.FuelTypeId == 1) Power_ToeFactor = s.TotalFactor;
- else if (s.FuelTypeId == 2) Gas_ToeFactor = s.TotalFactor;
- }
- // 에너지별로 업데이트
- for (int i = 1; i <= 3; i++)
- {
- var update_BemsEnergyDaily = (from d in db.BemsEnergyDaily
- where d.SiteId == SiteId &&
- d.CreatedDate == BemsEnergyDailyTime &&
- d.FuelTypeId == i
- select d).Single();
- // 예측값을 계산해서 저장한다(그냥 저장하는 거임)
- double Temp_Prediction_Regression = 0;
- if (i == 1) Temp_Prediction_Regression = Convert.ToDouble(PowerValue);
- else if (i == 2)
- {
- // 단위 환산을 하여 Kwh 저장
- double Temp_GasValue = Convert.ToDouble(GasValue) * Gas_ToeFactor / Power_ToeFactor;
- Temp_Prediction_Regression = Temp_GasValue;
- }
- else if (i == 3) Temp_Prediction_Regression = Convert.ToDouble(WaterValue);
- update_BemsEnergyDaily.Prediction_Regression = Temp_Prediction_Regression;
- update_BemsEnergyDaily.MeanT = MeanT;
- update_BemsEnergyDaily.HDD = HDD;
- update_BemsEnergyDaily.CDD = CDD;
- // 실제로 예측과 목표가 비율에 따라 수정된다
- if (IsRegression == true)
- {
- // 예측 다시 저장
- update_BemsEnergyDaily.Prediction = Temp_Prediction_Regression;
- // 목표 다시 저장
- double Temp_Goal = 0;
- if (i == 1) Temp_Goal = Temp_Prediction_Regression * PowerPercent;
- else if (i == 2) Temp_Goal = Temp_Prediction_Regression * GasPercent;
- else if (i == 3) Temp_Goal = Temp_Prediction_Regression * WaterPercent;
- update_BemsEnergyDaily.Goal = Temp_Goal;
- }
- db.SaveChanges();
- }
- transaction.Complete();
- }
- }
- }
- }
|