Beispiel #1
0
        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);

            }
        }
Beispiel #2
0
        //, Type dateType)
        /// <summary>
        /// Creates a Table by a given "Sorted Measure List" with one date time column and one column for each inverter.
        /// </summary>
        /// <param name="kwhTable">Creates google data table compatible conent</param>
        /// <param name="yMode">Use Euro or kwh as y-axis</param>
        /// <param name="xMode">type of x-axis</param>
        /// <returns>Google DataTable content</returns>
        public string BuildGoogleDataTable(SortedKwhTable kwhTable, E_EurKwh yMode, E_TimeMode xMode)
        {
            //create datatable and add time column
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("Zeit", typeof(string)).Caption = "Zeit";

            //add one column for each inverter ID
            foreach (var inverterInfo in kwhTable.KnownInverters)
            {
                string caption = GetRowName(inverterInfo.Value);
                dt.Columns.Add(caption, typeof(System.Double)).Caption = caption;
            }

            //Add the data
            foreach (var row in kwhTable.Rows.Values)
            {
                //create a new row and add the time first (key)
                var rowToAdd = dt.NewRow();
                rowToAdd[0] = GetTimeCaption(row.Index, xMode);

                //add the values foreach inverter
                foreach (var measure in row.kwhValues)
                {
                    rowToAdd[GetRowName(measure.PublicInverterId)] = measure.Value * GetPerEuroFactor(yMode, measure.PublicInverterId);
                }

                //add the new row to the datatable
                dt.Rows.Add(rowToAdd);
            }

            GoogleDataTable gdt = new Bortosky.Google.Visualization.GoogleDataTable(dt);
            return gdt.GetJson();
        }
Beispiel #3
0
        private string GenerateGoogleDataTableContent(int plantId, E_EurKwh yMode,
                                                SortedKwhTable kwhTable, E_TimeMode xMode)
        {
            DatatableConverter dtConverter = new DatatableConverter();

              //include money per kwh mapping if neccessary
              if (yMode == E_EurKwh.money)
              {
            IncludeEuroPerKwhMapping(plantId, dtConverter);
              }

              //create google data table content string
              return dtConverter.BuildGoogleDataTable(kwhTable, yMode, xMode);
        }
Beispiel #4
0
        /// <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;
        }
Beispiel #5
0
        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;
        }
Beispiel #6
0
        private double? GetCumulatedEuro(SortedKwhTable kwhTable, IEnumerable<Inverter> inverters)
        {
            double result = 0;
              foreach (var row in kwhTable.Rows.Values)
              {
            foreach (var kwh in row.kwhValues)
            {
              result += kwh.Value * inverters.Single(x => x.InverterId == kwh.PrivateInverterId).EuroPerKwh;
            }
              }

              return result;
        }
Beispiel #7
0
        private double GetCumulatedKwh(SortedKwhTable kwhTable)
        {
            double result = 0;
              foreach (var row in kwhTable.Rows.Values)
              {
            foreach (var kwh in row.kwhValues)
            {
              result += kwh.Value;
            }
              }

              return result;
        }
Beispiel #8
0
        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);
            }
        }
Beispiel #9
0
        /// <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;
        }
Beispiel #10
0
        /// <summary>
        /// Returns true if we have a measure which falls into the countdate timeframe
        /// </summary>
        /// <param name="measures"></param>
        /// <param name="countDate"></param>
        /// <param name="timeMode"></param>
        /// <param name="publicInverterID"></param>
        /// <returns></returns>
        private static bool ContainsMeasuresForThisTimePoint(SortedKwhTable measures, DateTime countDate, E_TimeMode timeMode, int publicInverterID)
        {
            foreach (var row in measures.Rows)
              {
            //datetime contains measure for this inverter ID
            if (measures.ContainsMeasureForPublicInverterId(row.Key, publicInverterID))
            {
              DateTime comparabledate = GetCompareableDate(row.Key, timeMode);
              if (countDate == comparabledate)
            return true;
            }

              }
              return false;
        }
Beispiel #11
0
        /// <summary>
        /// Summarizes kwh values from hourly up to yearly. At first it initializes a result list, which is then filled with cumulated data from the input.
        /// </summary>
        /// <param name="measures">Kwh table for an undefined time range</param>
        /// <param name="startDate">The startDate Parameter can be used to crop
        /// unwanted kwh values at the beginning</param>
        /// <param name="endDate">The endDate Parameter crops all unwanted tailing kwh
        /// values</param>
        /// <param name="timeMode">The timeMode defines the target kwh stepping.
        /// Stepping includes from hourly up to yearly.</param>
        /// <param name="fillGaps"></param>
        /// <returns>The summarized kwh table</returns>
        public static SortedKwhTable SummarizeKwh(SortedKwhTable measures, DateTime startDate, DateTime endDate,
                                                                E_TimeMode timeMode, bool fillGaps)
        {
            //setup start and enddate
              startDate = GetTimePointByMode(timeMode, startDate);
              endDate = GetTimePointByMode(timeMode, endDate);

              //Initialize result list
              SortedKwhTable result;
              result = InitializeResultList(measures, startDate, endDate, timeMode, fillGaps);

              //Build the sum for the measures by each timepoint, depending on the timeMode
              foreach (var item in measures.ToList())
              {

            // get target time point where the current kwh should be added to
            DateTime currentTimePoint = GetTimePointByMode(timeMode, item.DateTime);

            //add the kwh of this hour for the inverter
            if (currentTimePoint >= startDate && currentTimePoint < endDate)
            {
              result.GetKwh(currentTimePoint, item.PublicInverterId).Value += item.Value;
            }
            else
            {
              Logger.Log(new ApplicationException(), SeverityLevel.Warning, "kwh value is out of the specified startDate and endate");
            }
              }

              return result;
        }