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;
}
}