ThermostatMonitorLib.Temperatures.GetTempAverage C# (CSharp) Method

GetTempAverage() public method

public GetTempAverage ( System.DateTime startTime, System.DateTime endTime ) : double
startTime System.DateTime
endTime System.DateTime
return double
        public double GetTempAverage(DateTime startTime, DateTime endTime)
        {
            int i=0;
            double totalSeconds = 0;
            double totalDegrees = 0;
            foreach (Temperature temp in this)
            {
                DateTime tempStart = temp.LogDate;
                DateTime tempEnd = endTime;
                if (startTime.Ticks > tempStart.Ticks) tempStart = startTime;
                if (this.Count > i + 1) tempEnd = this[i + 1].LogDate;
                double seconds = (tempEnd.Ticks - tempStart.Ticks) / 1000.0;
                totalSeconds += seconds;
                totalDegrees += temp.Degrees * seconds;
                i++;
            }
            double result = System.Math.Round(totalDegrees / totalSeconds, 1);
            return result;
        }

Usage Example

Example #1
0
        private static void LogOffCycle(int thermostatId, Cycle cycle, DateTime lastCycleEndDate, Temperatures allTemperatures, OutsideConditions allConditions)
        {
            Temperatures      temperatures        = allTemperatures.GetRange(lastCycleEndDate, cycle.StartDate);
            OutsideConditions conditions          = allConditions.GetRange(lastCycleEndDate, cycle.StartDate);
            Temperature       previousTemperature = allTemperatures.GetByTime(lastCycleEndDate);
            OutsideCondition  previousCondition   = allConditions.GetByTime(lastCycleEndDate);

            temperatures.Insert(0, previousTemperature);
            conditions.Insert(0, previousCondition);

            if (cycle.StartDate <= lastCycleEndDate)
            {
                return;
            }

            if (conditions.Count > 0 && temperatures.Count > 0)
            {
                DateTime endDate = cycle.StartDate;
                Snapshot s       = new Snapshot();
                s.StartTime          = lastCycleEndDate;
                s.Seconds            = Convert.ToInt32(new TimeSpan(cycle.StartDate.Ticks - lastCycleEndDate.Ticks).TotalSeconds);
                s.ThermostatId       = thermostatId;
                s.Mode               = "Off";
                s.InsideTempAverage  = Convert.ToInt32(temperatures.GetTempAverage(lastCycleEndDate, cycle.StartDate));
                s.InsideTempHigh     = Convert.ToInt32(temperatures.GetTempHigh());
                s.InsideTempLow      = Convert.ToInt32(temperatures.GetTempLow());
                s.OutsideTempAverage = Convert.ToInt32(conditions.GetTempAverage(lastCycleEndDate, cycle.StartDate));
                s.OutsideTempHigh    = Convert.ToInt32(conditions.GetTempHigh());
                s.OutsideTempLow     = Convert.ToInt32(conditions.GetTempLow());
                if (s.Seconds > 10 && s.Seconds < 86400) //if significant and less than a day
                {
                    Snapshot.SaveSnapshot(s);
                }
            }
        }