public void SummarizeKwhToDaysOfMonthTest() { DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); DateTime endDate = startDate.AddMonths(1); MeasureKwH measure_day_1 = BigMama.GetKwhDay(1.0); MeasureKwH measure_day_2 = BigMama.GetKwhDay(3.0); measure_day_1.DateTime = startDate.AddDays(1); measure_day_2.DateTime = measure_day_1.DateTime.AddDays(1); SortedKwhTable measures = new SortedKwhTable(); measures.AddMeasure(measure_day_1); measures.AddMeasure(measure_day_2); var actual = KwhCalculator.SummarizeKwh(measures, startDate, endDate, E_TimeMode.day, true); var expectedCount = DateTime.DaysInMonth(startDate.Year, startDate.Month); Assert.AreEqual(expectedCount, actual.Count); var actualList = actual.ToList(); for (int i = 0; i < actualList.Count; i++) { if (i == 0) Assert.AreEqual(0, actualList[i].Value); else if (i == 1) Assert.AreEqual(1.0, actualList[i].Value); else if (i == 2) Assert.AreEqual(3.0, actualList[i].Value); else Assert.AreEqual(0.0, actual.ToList()[i].Value); } }
/// <summary> /// Builds a kwh table which contains hourly kwh values for each inverter id /// </summary> /// <param name="startDate">Defines the beginning of the time range for the result.</param> /// <param name="endDate">Defines the end of the time range for the result</param> /// <param name="inverterIDs">The inverter ID's which should be contained in the result kwh table</param> /// <returns>A hourly kwh table with the provided time range and inverter ID's</returns> public SortedKwhTable GetkwhHourlyByTimeFrame(DateTime startDate, DateTime endDate, List<int> inverterIDs) { SortedKwhTable result = new SortedKwhTable(); foreach (var inverterID in inverterIDs) { var measures = _measureRepository.GetMinuteWiseMeasures(startDate, endDate, inverterID).Cast<IMeasure>().ToList(); SortedList<DateTime, MeasureKwH> kwhByHours = KwhCalculator.GetKwhHourly(measures, MySettings.DiagDay_StartHour, MySettings.DiagDay_EndHour); foreach (var kwhByHour in kwhByHours.Values) { result.AddMeasure(kwhByHour); } } return result; }
public SortedKwhTable GetDayKwhByDateRange(DateTime startDate, DateTime endDate, int systemID) { startDate = Utils.CropHourMinuteSecond(startDate); //mysql handles "between date" inclusive so we have to shrink the timeframe endDate = Utils.CropHourMinuteSecond(endDate).AddDays(-1); SortedKwhTable result = new SortedKwhTable(); string text = @" SELECT ID, Date, i.InverterId, i.PublicInverterId, kwh FROM kwh_by_day k INNER JOIN inverter i ON k.InverterID = i.InverterId AND i.PlantId = @plantId WHERE (DATE BETWEEN @startDate AND @endDate) ORDER BY Date ASC, PublicInverterId ASC;"; var sqlCom = base.GetReadCommand(text); //Add Parameters sqlCom.Parameters.AddWithValue("@plantID", systemID); sqlCom.Parameters.AddWithValue("@startDate", startDate); sqlCom.Parameters.AddWithValue("@endDate", endDate); //Execute SQL and read data using (var rdr = sqlCom.ExecuteReader()) { while (rdr.Read()) { var measureKwh = new MeasureKwH(); measureKwh.Value = rdr.GetDouble("kwh"); measureKwh.ID = rdr.GetInt32("ID"); measureKwh.DateTime = rdr.GetDateTime("Date"); measureKwh.PublicInverterId = rdr.GetInt32("PublicInverterId"); measureKwh.TimeMode = Enums.E_TimeMode.day; measureKwh.PrivateInverterId = rdr.GetInt32("InverterId"); result.AddMeasure(measureKwh); } } return result; }
public void SummarizeKwhToDaysTest() { //Create some measures for 10 days starting at 8am to 18 pm SortedKwhTable hourSums = new SortedKwhTable(); int inverterID = 1; var startDate = new DateTime(2010, 1, 1); var endDate = new DateTime(2010, 2, 1).AddDays(-1); var countDate = new DateTime(2010, 1, 10, 8, 0, 0); //10 days from 10th to 20th for (int i = 0; i < 10; i++) { //8 - 17 o'clock while (countDate.Hour < 18) { MeasureKwH measure = new MeasureKwH(); measure.DateTime = countDate; measure.PrivateInverterId = inverterID; measure.TimeMode = PVLog.Enums.E_TimeMode.hour; measure.Value = 1; // 1kwh //add to list hourSums.AddMeasure(measure); countDate = countDate.AddHours(1); } countDate = countDate.AddDays(1).AddHours(-10); } //we should get 10 kw for each day SortedKwhTable actual = KwhCalculator.SummarizeKwh(hourSums, startDate, endDate, E_TimeMode.day, true); for (int i = 1; i < 10; i++) { var currentDaySum = actual.ToList()[i]; // day 1 - 9 = 0 kwh if (i < 9) { Assert.AreEqual(0, currentDaySum.Value); } // day 10 - 19 10 kwh per day else if (i < 19) { Assert.AreEqual(10, currentDaySum.Value); } // day 20 - 31 = 0 kwh else if (i <= 31) { Assert.AreEqual(0, currentDaySum.Value); } Assert.AreEqual(inverterID, currentDaySum.PrivateInverterId); } //null testing hourSums = new SortedKwhTable(); actual = KwhCalculator.SummarizeKwh(hourSums, startDate, endDate, E_TimeMode.month, true); foreach (var dayItem in actual.ToList()) { Assert.AreEqual(0, dayItem.Value); } }
/// <summary> /// Initializes the resultlist. It can be chosen wether we want to fill gaps or not. /// This is important for the "kwh by day" calculation because if we would fill gaps there, kwh_days would be /// deleted when there is no more measurement data for the timeframe /// </summary> /// <param name="measures"></param> /// <param name="startDate"></param> /// <param name="endDate"></param> /// <param name="timeMode"></param> /// <param name="systemID"></param> /// <param name="fillGaps"></param> /// <returns></returns> private static SortedKwhTable InitializeResultList(SortedKwhTable measures , DateTime startDate, DateTime endDate , E_TimeMode timeMode , bool fillGaps) { //init day sum list for the hole time frame SortedKwhTable result = new SortedKwhTable(); // Initialize the result with empty values first var countDate = startDate; while (countDate < endDate) { foreach (var inverterInfo in measures.KnownInverters) { if (fillGaps || ContainsMeasuresForThisTimePoint(measures, countDate, timeMode, inverterInfo.Value)) { MeasureKwH dummy = new MeasureKwH(); dummy.DateTime = countDate; dummy.PrivateInverterId = inverterInfo.Key; dummy.PublicInverterId = inverterInfo.Value; dummy.Value = 0; result.AddMeasure(dummy); } } //increase countdate countDate = IncreaseCountDate(timeMode, countDate); } return result; }