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 Temperatures GetRange(DateTime startDate, DateTime endDate) { Temperatures result = new Temperatures(); foreach (Temperature temp in this) { if (temp.LogDate >= startDate && temp.LogDate <= endDate) result.Add(temp); } return result; }
public static Temperatures ConvertFromDT(DataTable dt) { Temperatures result = new Temperatures(); foreach (DataRow row in dt.Rows) { result.Add(Temperature.GetTemperature(row)); } return(result); }
public Temperatures Sort(string column, bool desc) { var sortedList = desc ? this.OrderByDescending(x => x.GetPropertyValue(column)) : this.OrderBy(x => x.GetPropertyValue(column)); Temperatures result = new Temperatures(); foreach (var i in sortedList) { result.Add((Temperature)i); } return(result); }
public Temperatures GetRange(DateTime startDate, DateTime endDate) { Temperatures result = new Temperatures(); foreach (Temperature temp in this) { if (temp.LogDate >= startDate && temp.LogDate <= endDate) { result.Add(temp); } } return(result); }
public static Temperature LoadCurrentTemperature(int thermostatId) { Temperatures temperatures = Temperatures.LoadTemperatures("SELECT TOP 1 * FROM Temperatures WHERE ThermostatId=@ThermostatId ORDER BY ID DESC", CommandType.Text, new SqlParameter[] { new SqlParameter("@ThermostatId", thermostatId) }); if (temperatures.Count == 0) { return(null); } else { return(temperatures[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 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; }
public static Temperatures LoadTemperaturesByThermostatId(System.Int32 thermostatId) { return(Temperatures.LoadTemperatures("LoadTemperaturesByThermostatId", CommandType.StoredProcedure, new SqlParameter[] { new SqlParameter("@ThermostatId", thermostatId) })); }
public static Temperatures LoadAllTemperatures() { return(Temperatures.LoadTemperatures("LoadTemperaturesAll", CommandType.StoredProcedure, null)); }
public static Temperatures LoadTemperatures(string sql, System.Data.CommandType commandType, System.Data.SqlClient.SqlParameter[] parameters) { return(Temperatures.ConvertFromDT(Utils.ExecuteQuery(sql, commandType, parameters))); }
public static Temperatures LoadRange(System.Int32 thermostatId, DateTime startDate, DateTime endDate) { return(Temperatures.LoadTemperatures("SELECT * FROM Temperatures WHERE ThermostatID=@ThermostatId AND LogDate BETWEEN @StartDate and @EndDate", CommandType.Text, new SqlParameter[] { new SqlParameter("@ThermostatId", thermostatId), new SqlParameter("@StartDate", startDate), new SqlParameter("@EndDate", endDate) })); }
public static Temperatures LoadTemperaturesByThermostatId(int thermostatId) { return(Temperatures.LoadTemperatures("temperatures_load_by_thermostat_id", CommandType.StoredProcedure, new MySqlParameter[] { new MySqlParameter("@thermostat_id", thermostatId) })); }
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); } } }