Пример #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);
                }
            }
        }
 public OutsideConditions GetRange(DateTime startDate, DateTime endDate)
 {
     OutsideConditions result = new OutsideConditions();
     foreach (OutsideCondition cond in this)
     {
         if (cond.LogDate >= startDate && cond.LogDate <= endDate) result.Add(cond);
     }
     return result;
 }
Пример #3
0
        public static OutsideConditions ConvertFromDT(DataTable dt)
        {
            OutsideConditions result = new OutsideConditions();

            foreach (DataRow row in dt.Rows)
            {
                result.Add(OutsideCondition.GetOutsideCondition(row));
            }
            return(result);
        }
Пример #4
0
        public OutsideConditions Sort(string column, bool desc)
        {
            var sortedList           = desc ? this.OrderByDescending(x => x.GetPropertyValue(column)) : this.OrderBy(x => x.GetPropertyValue(column));
            OutsideConditions result = new OutsideConditions();

            foreach (var i in sortedList)
            {
                result.Add((OutsideCondition)i);
            }
            return(result);
        }
        public OutsideConditions GetRange(DateTime startDate, DateTime endDate)
        {
            OutsideConditions result = new OutsideConditions();

            foreach (OutsideCondition cond in this)
            {
                if (cond.LogDate >= startDate && cond.LogDate <= endDate)
                {
                    result.Add(cond);
                }
            }
            return(result);
        }
        public static OutsideCondition LoadCurrentCondition(int locationId)
        {
            OutsideConditions conditions = OutsideConditions.LoadOutsideConditions("SELECT * FROM outside_conditions WHERE location_id=@LocationId ORDER BY ID DESC limit 1;", CommandType.Text, new MySqlParameter[] { new MySqlParameter("@LocationId", locationId) });

            if (conditions.Count == 0)
            {
                return(null);
            }
            else
            {
                return(conditions[0]);
            }
        }
Пример #7
0
        public static void Generate(int thermostatId)
        {
            DateTime startDate    = new DateTime(2000, 1, 1);
            Snapshot lastSnapshot = Snapshot.LoadLastSnapshot(thermostatId);

            if (lastSnapshot != null)
            {
                startDate = lastSnapshot.StartTime.AddSeconds(lastSnapshot.Seconds);
            }

            Thermostat        thermostat   = Thermostat.LoadThermostat(thermostatId);
            Cycles            cycles       = Cycles.LoadRange(thermostatId, startDate, DateTime.Now);
            Temperatures      temperatures = Temperatures.LoadRange(thermostatId, startDate.AddDays(-1), DateTime.Now); //we need the previous temperature and conditions
            OutsideConditions conditions   = OutsideConditions.LoadRange(thermostat.LocationId, startDate.AddDays(-1), DateTime.Now);

            cycles.RemoveIncomplete();

            if (cycles.Count == 0)
            {
                return;
            }

            DateTime lastCycleEndDate = cycles[0].StartDate;

            if (lastSnapshot != null)
            {
                lastCycleEndDate = startDate;
            }

            foreach (Cycle cycle in cycles)
            {
                try
                {
                    LogOffCycle(thermostat.Id, cycle, lastCycleEndDate, temperatures, conditions);
                }
                catch { }
                try
                {
                    LogOnCycle(thermostat.Id, cycle, temperatures, conditions);
                }
                catch { }
                lastCycleEndDate = cycle.EndDate;
            }
        }
 public static OutsideConditions LoadRange(int locationId, DateTime startDate, DateTime endDate)
 {
     return(OutsideConditions.LoadOutsideConditions("SELECT * FROM outside_conditions WHERE location_id=@LocationId AND log_date BETWEEN @StartDate and @EndDate", CommandType.Text, new MySqlParameter[] { new MySqlParameter("@LocationId", locationId), new MySqlParameter("@StartDate", startDate), new MySqlParameter("@EndDate", endDate) }));
 }
    public void Populate(int thermostatId, DateTime startTime, DateTime endTime)
    {
        ThermostatMonitorLib.Thermostat thermostat = ThermostatMonitorLib.Thermostat.LoadThermostat(thermostatId);

        ThermostatMonitorLib.Temperatures      temps      = ThermostatMonitorLib.Temperatures.LoadRange(thermostatId, startTime, endTime);
        ThermostatMonitorLib.OutsideConditions conditions = ThermostatMonitorLib.OutsideConditions.LoadRange(thermostat.LocationId, startTime, endTime);



        DataTable dt = new DataTable();

        dt.Columns.Add("LogDate", typeof(DateTime));
        dt.Columns.Add("InsideTemperature", typeof(double));
        //dt.Columns.Add("AC_On", typeof(double));
        dt.Columns.Add("OutsideTemperature", typeof(double));


        for (int i = 0; i < temps.Count; i++)
        {
            ThermostatMonitorLib.Temperature temp = temps[i];
            if (temp.Degrees == Convert.ToInt32(temp.Degrees))
            {
                if (i > 0)
                {
                    DataRow pRow = dt.NewRow();
                    pRow[0] = temp.LogDate.AddSeconds(-1);
                    pRow[1] = temps[i - 1].Degrees;
                    dt.Rows.Add(pRow);
                }
                DataRow row = dt.NewRow();
                row[0] = temp.LogDate;
                row[1] = temp.Degrees;
                dt.Rows.Add(row);
            }
        }



        foreach (ThermostatMonitorLib.OutsideCondition condition in conditions)
        {
            DataRow row = dt.NewRow();
            row[0] = condition.LogDate;
            row[2] = condition.Degrees;
            dt.Rows.Add(row);
        }
        if (conditions.Count > 0 && temps.Count > 0)
        {
            DataRow row = dt.NewRow();
            row[0] = startTime;
            row[1] = temps[0].Degrees;
            row[2] = conditions[0].Degrees;
            dt.Rows.Add(row);

            row    = dt.NewRow();
            row[0] = endTime;
            row[1] = temps[temps.Count - 1].Degrees;
            row[2] = conditions[conditions.Count - 1].Degrees;
            dt.Rows.Add(row);
        }



        OutputData(dt);
        return;
    }
Пример #10
0
        public static DataTable LoadFullSummary(int locationId, int thermostatId, DateTime startDate, DateTime endDate, int timezoneDifference)
        {
            DataTable cycles  = LoadSummary(thermostatId, startDate, endDate, timezoneDifference);
            DataTable weather = OutsideConditions.LoadSummary(locationId, startDate, endDate, timezoneDifference);

            DataTable result = new DataTable();

            result.Columns.Add("LogDate", typeof(DateTime));
            result.Columns.Add("OutsideMin", typeof(int));
            result.Columns.Add("OutsideMax", typeof(int));
            System.Collections.Hashtable cycleTypes = new System.Collections.Hashtable();
            foreach (DataRow row in cycles.Rows)
            {
                string cycleType = Convert.ToString(row["CycleType"]);
                if (!cycleTypes.Contains(cycleType))
                {
                    cycleTypes.Add(cycleType, cycleType);
                }
            }
            foreach (string cycleType in cycleTypes.Keys)
            {
                result.Columns.Add(cycleType + "_CycleCount", typeof(int));
                result.Columns.Add(cycleType + "_TotalSeconds", typeof(double));
                result.Columns.Add(cycleType + "_AverageSeconds", typeof(double));
            }


            System.Collections.Hashtable dateHash = new System.Collections.Hashtable();


            foreach (DataRow row in cycles.Rows)
            {
                string   cycleType      = Convert.ToString(row["CycleType"]);
                int      cycleCount     = Convert.ToInt32(row["CycleCount"]);
                int      totalSeconds   = Convert.ToInt32(row["TotalSeconds"]);
                double   averageSeconds = Convert.ToDouble(totalSeconds) / Convert.ToDouble(cycleCount);
                DateTime logDate        = Convert.ToDateTime(row["LogDate"]);

                bool    newDate   = !dateHash.Contains(logDate);
                DataRow resultRow = null;
                if (newDate)
                {
                    resultRow = result.NewRow();
                }
                else
                {
                    resultRow = result.Rows[Convert.ToInt32(dateHash[logDate])];
                }
                resultRow[cycleType + "_CycleCount"]     = cycleCount;
                resultRow[cycleType + "_TotalSeconds"]   = totalSeconds;
                resultRow[cycleType + "_AverageSeconds"] = averageSeconds;
                if (newDate)
                {
                    resultRow["LogDate"] = logDate;
                    result.Rows.Add(resultRow);
                    dateHash.Add(logDate, result.Rows.Count - 1);
                }
            }

            foreach (DataRow row in weather.Rows)
            {
                DateTime logDate = Convert.ToDateTime(row["LogDate"]);
                bool     newDate = !dateHash.Contains(logDate);
                if (!newDate)
                {
                    DataRow resultRow = result.Rows[Convert.ToInt32(dateHash[logDate])];
                    resultRow["OutsideMin"] = Convert.ToInt32(row["MinDegrees"]);
                    resultRow["OutsideMax"] = Convert.ToInt32(row["MaxDegrees"]);
                }
            }


            return(result);
        }
Пример #11
0
 public static OutsideConditions LoadOutsideConditionsByUserId(System.Int32 userId)
 {
     return(OutsideConditions.LoadOutsideConditions("LoadOutsideConditionsByUserId", CommandType.StoredProcedure, new SqlParameter[] { new SqlParameter("@UserId", userId) }));
 }
Пример #12
0
 public static OutsideConditions LoadAllOutsideConditions()
 {
     return(OutsideConditions.LoadOutsideConditions("LoadOutsideConditionsAll", CommandType.StoredProcedure, null));
 }
Пример #13
0
 public static OutsideConditions LoadOutsideConditions(string sql, System.Data.CommandType commandType, System.Data.SqlClient.SqlParameter[] parameters)
 {
     return(OutsideConditions.ConvertFromDT(Utils.ExecuteQuery(sql, commandType, parameters)));
 }
Пример #14
0
 public static OutsideConditions LoadOutsideConditionsByLocationId(int locationId)
 {
     return(OutsideConditions.LoadOutsideConditions("outside_conditions_load_by_location_id", CommandType.StoredProcedure, new MySqlParameter[] { new MySqlParameter("@location_id", locationId) }));
 }
        private static void LogOnCycle(int thermostatId, Cycle cycle, Temperatures allTemperatures, OutsideConditions allConditions)
        {
            Temperatures temperatures = allTemperatures.GetRange(cycle.StartDate, cycle.EndDate);
            OutsideConditions conditions = allConditions.GetRange(cycle.StartDate, cycle.EndDate);
            Temperature previousTemperature = allTemperatures.GetByTime(cycle.StartDate);
            OutsideCondition previousCondition = allConditions.GetByTime(cycle.StartDate);
            if (previousTemperature!=null) temperatures.Insert(0, previousTemperature);
            if (previousCondition != null) conditions.Insert(0, previousCondition);

            if (conditions.Count > 0 && temperatures.Count > 0)
            {
                Snapshot s = new Snapshot();
                s.StartTime = cycle.StartDate;
                s.Seconds = Convert.ToInt32(new TimeSpan(cycle.EndDate.Ticks - cycle.StartDate.Ticks).TotalSeconds);
                s.ThermostatId = thermostatId;
                s.Mode = cycle.CycleType;
                s.InsideTempAverage = Convert.ToInt32(temperatures.GetTempAverage(cycle.StartDate, cycle.EndDate));
                s.InsideTempHigh = Convert.ToInt32(temperatures.GetTempHigh());
                s.InsideTempLow = Convert.ToInt32(temperatures.GetTempLow());
                s.OutsideTempAverage = Convert.ToInt32(conditions.GetTempAverage(cycle.StartDate, cycle.EndDate));
                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);
                }
            }
        }