Skip to content

Commit

Permalink
some fixes to the new scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
genemars committed Jun 19, 2016
1 parent 1246e72 commit 9a2ff90
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@
$$.showCalendar();
});
page.find('[data-ui-field="add-button"]').on('click', function(){
var _btn = $(this);
$$._CurrentEventName = "";
$$._CurrentEventIndex = -1;
setTimeout($$.EditCurrentItem, 500);
$$.EditCurrentItem();
setTimeout(function() {
_btn.removeClass('ui-btn-active');
}, 200);
});
};

Expand Down
4 changes: 2 additions & 2 deletions BaseFiles/Common/modules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2275,7 +2275,7 @@
<RoutingNode />
</Module>
<Module>
<Name>Porch Light</Name>
<Name>Light 1</Name>
<Description />
<DeviceType>Dimmer</DeviceType>
<Domain>HomeAutomation.PhilipsHue</Domain>
Expand Down Expand Up @@ -2423,7 +2423,7 @@
<RoutingNode />
</Module>
<Module>
<Name>Light 3</Name>
<Name>Porch Light</Name>
<Description />
<DeviceType>Dimmer</DeviceType>
<Domain>HomeAutomation.PhilipsHue</Domain>
Expand Down
19 changes: 4 additions & 15 deletions BaseFiles/Common/scheduler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,12 @@ if ($$.onPrevious() &amp;&amp; !$$.onNext())
<string>Light</string>
</BoundDevices>
<BoundModules>
<ModuleReference>
<Address>1</Address>
<Domain>HomeAutomation.PhilipsHue</Domain>
</ModuleReference>
<ModuleReference>
<Address>2</Address>
<Domain>HomeAutomation.PhilipsHue</Domain>
</ModuleReference>
<ModuleReference>
<Address>3</Address>
<Domain>HomeAutomation.PhilipsHue</Domain>
</ModuleReference>
</BoundModules>
<LastOccurrence>-</LastOccurrence>
<NextOccurrence>-</NextOccurrence>
<LastOccurrence></LastOccurrence>
<ProgramId />
</SchedulerItem>
<SchedulerItem>
Expand Down Expand Up @@ -61,8 +52,7 @@ if ($$.onPrevious() &amp;&amp; !$$.onNext())
<Domain>HomeAutomation.BasicThermostat</Domain>
</ModuleReference>
</BoundModules>
<LastOccurrence>-</LastOccurrence>
<NextOccurrence>-</NextOccurrence>
<LastOccurrence></LastOccurrence>
<ProgramId />
</SchedulerItem>
<SchedulerItem>
Expand All @@ -74,7 +64,7 @@ if ($$.onPrevious() &amp;&amp; !$$.onNext())
<Script>// Set Mode=Heat, if previous minute was not in schedule (start occurrence range)
if (!$$.onPrevious())
$$.boundModules
.command('Thermostat.ModeSet')
.command('TherNmostat.ModeSet')
.set('Heat');
// Set Mode=Off, if next minute won't be in schedule (end occurrence range)
if ($$.onPrevious() &amp;&amp; !$$.onNext())
Expand All @@ -89,8 +79,7 @@ if ($$.onPrevious() &amp;&amp; !$$.onNext())
<Domain>HomeAutomation.BasicThermostat</Domain>
</ModuleReference>
</BoundModules>
<LastOccurrence>-</LastOccurrence>
<NextOccurrence>-</NextOccurrence>
<LastOccurrence></LastOccurrence>
<ProgramId />
</SchedulerItem>
</ArrayOfSchedulerItem>
6 changes: 2 additions & 4 deletions HomeGenie/Automation/Scheduler/SchedulerItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ public class SchedulerItem
/// <value>The bound modules.</value>
public List<ModuleReference> BoundModules { get; set; }

// TODO: deprecate the following two
[XmlIgnore,JsonIgnore]
public string LastOccurrence { get; set; }
public string NextOccurrence { get; set; }

// TODO: deprecate this field - left for compatibility with hg <= r521
public string ProgramId { get; set; }
Expand All @@ -94,8 +93,7 @@ public SchedulerItem()
CronExpression = "";
ProgramId = "";
IsEnabled = false;
LastOccurrence = "-";
NextOccurrence = "-";
LastOccurrence = "";
BoundDevices = new List<string>();
BoundModules = new List<ModuleReference>();
}
Expand Down
58 changes: 23 additions & 35 deletions HomeGenie/Automation/Scheduler/SchedulerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class SchedulerService
private List<SchedulerItem> events = new List<SchedulerItem>();
private Timer serviceChecker;
private ProgramManager masterControlProgram;

private const string FORMAT_DATETIME = "yyyy-MM-dd HH:mm";
public class EvalNode
{
public List<DateTime> Occurrences;
Expand Down Expand Up @@ -91,10 +91,8 @@ private void CheckScheduledEvents(object state)
var eventItem = events[i];
if (eventItem.IsEnabled)
{
// update next occurrence value
eventItem.NextOccurrence = GetNextEventOccurrence(date, eventItem.CronExpression);
// execute items only once instead of repeating for the whole minute
string currentOccurrence = date.ToString("yyyy-MM-dd HH:mm");
string currentOccurrence = date.ToUniversalTime().ToString(FORMAT_DATETIME);
if (eventItem.LastOccurrence != currentOccurrence && IsScheduling(date, eventItem.CronExpression))
{
masterControlProgram.HomeGenie.MigService.RaiseEvent(
Expand Down Expand Up @@ -163,8 +161,7 @@ public SchedulerItem AddOrUpdate(string name, string cronExpression, string data
eventItem.Script = script;
if (eventItem.ScriptEngine != null)
eventItem.ScriptEngine.StopScript();
eventItem.LastOccurrence = "-";
eventItem.NextOccurrence = GetNextEventOccurrence(DateTime.Now, eventItem.CronExpression);
eventItem.LastOccurrence = "";
// by default newly added events are enabled
if (justAdded)
{
Expand Down Expand Up @@ -212,7 +209,6 @@ public bool Enable(string name)
if (eventItem != null)
{
eventItem.IsEnabled = true;
eventItem.NextOccurrence = GetNextEventOccurrence(DateTime.Now, eventItem.CronExpression);
return true;
}
return false;
Expand All @@ -224,8 +220,7 @@ public bool Disable(string name)
if (eventItem != null)
{
eventItem.IsEnabled = false;
eventItem.LastOccurrence = "-";
eventItem.NextOccurrence = "-";
eventItem.LastOccurrence = "";
if (eventItem.ScriptEngine != null)
eventItem.ScriptEngine.StopScript();
return true;
Expand All @@ -248,7 +243,9 @@ public bool Remove(string name)

public bool IsScheduling(DateTime date, string cronExpression, int recursionCount = 0)
{
return GetScheduling(date, date, cronExpression).Count == 1;
var hits = GetScheduling(date.Date, date.Date.AddHours(24).AddMinutes(-1), cronExpression);
var match = (DateTime?)hits.Find(d => d.ToUniversalTime().ToString(FORMAT_DATETIME) == date.ToUniversalTime().ToString(FORMAT_DATETIME));
return match != null && match != DateTime.MinValue;
}

public List<DateTime> GetScheduling(DateTime dateStart, DateTime dateEnd, string cronExpression, int recursionCount = 0)
Expand Down Expand Up @@ -346,7 +343,7 @@ public List<DateTime> GetScheduling(DateTime dateStart, DateTime dateEnd, string
var solarTimes = new SolarTimes(start.ToLocalTime(), Location["latitude"].Value, Location["longitude"].Value);
var sunrise = solarTimes.Sunrise;
sunrise = sunrise.AddMinutes(addMinutes);
if (sunrise.Ticks >= start.Ticks && sunrise.Ticks <= dateEnd.Ticks)
if (IsBetween(sunrise, start, dateEnd))
{
sunrise = sunrise.AddSeconds(-sunrise.Second);
evalNode.Occurrences.Add(sunrise);
Expand All @@ -365,7 +362,7 @@ public List<DateTime> GetScheduling(DateTime dateStart, DateTime dateEnd, string
var solarTimes = new SolarTimes(start.ToLocalTime(), Location["latitude"].Value, Location["longitude"].Value);
var sunset = solarTimes.Sunset;
sunset = sunset.AddMinutes(addMinutes);
if (sunset.Ticks >= start.Ticks && sunset.Ticks <= dateEnd.Ticks)
if (IsBetween(sunset, start, dateEnd))
{
sunset = sunset.AddSeconds(-sunset.Second);
evalNode.Occurrences.Add(sunset);
Expand All @@ -384,7 +381,7 @@ public List<DateTime> GetScheduling(DateTime dateStart, DateTime dateEnd, string
var solarTimes = new SolarTimes(start.ToLocalTime(), Location["latitude"].Value, Location["longitude"].Value);
var solarNoon = solarTimes.SolarNoon;
solarNoon = solarNoon.AddMinutes(addMinutes);
if (solarNoon.Ticks >= start.Ticks && solarNoon.Ticks <= dateEnd.Ticks)
if (IsBetween(solarNoon, start, dateEnd))
{
solarNoon = solarNoon.AddSeconds(-solarNoon.Second);
evalNode.Occurrences.Add(solarNoon);
Expand Down Expand Up @@ -471,7 +468,7 @@ public List<DateTime> EvalNodes(EvalNode currentNode)
{
var start = occurs.Last();
var end = matchList.First();
var inc = start.AddMinutes(1);
var inc = start.AddMinutes(1).AddSeconds(-start.Second);
while (Math.Floor((end - inc).TotalMinutes) != 0)
{
occurs.Add(inc);
Expand Down Expand Up @@ -511,13 +508,12 @@ private bool EvaluateCronEntry(DateTime date, string cronExpression)
{
if (date.Kind != DateTimeKind.Local)
date = date.ToLocalTime();
date = date.AddSeconds(-1);
var cronSchedule = NCrontab.CrontabSchedule.TryParse(cronExpression);
if (!cronSchedule.IsError)
{
var occurrence = cronSchedule.Value.GetNextOccurrence(date.AddMinutes(-1));
string d1 = date.ToString("yyyy-MM-dd HH:mm");
string d2 = occurrence.ToString("yyyy-MM-dd HH:mm");
string d1 = date.ToUniversalTime().ToString(FORMAT_DATETIME);
string d2 = occurrence.ToUniversalTime().ToString(FORMAT_DATETIME);
if (d1 == d2)
{
return true;
Expand Down Expand Up @@ -566,9 +562,9 @@ private bool IsEventActive(string cronExpression, List<string> checkedStack = nu
var cronSchedule = NCrontab.CrontabSchedule.TryParse(currentExpression);
if (!cronSchedule.IsError)
{
var occurrence = cronSchedule.Value.GetNextOccurrence(DateTime.Now.AddSeconds(-1));
string d1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
string d2 = occurrence.ToString("yyyy-MM-dd HH:mm");
var occurrence = cronSchedule.Value.GetNextOccurrence(DateTime.Now.AddMinutes(-1));
string d1 = DateTime.Now.ToUniversalTime().ToString(FORMAT_DATETIME);
string d2 = occurrence.ToUniversalTime().ToString(FORMAT_DATETIME);
if (d1 == d2)
{
return true;
Expand All @@ -580,25 +576,10 @@ private bool IsEventActive(string cronExpression, List<string> checkedStack = nu
return false;
}

private string GetNextEventOccurrence(DateTime date, string cronExpression)
{
if (date.Kind != DateTimeKind.Local)
date = date.ToLocalTime();
date = date.AddSeconds(-1);
var cronSchedule = NCrontab.CrontabSchedule.TryParse(cronExpression);
if (!cronSchedule.IsError)
{
var occurrence = cronSchedule.Value.GetNextOccurrence(date);
return occurrence.ToString("yyyy-MM-dd HH:mm");
}
return "-";
}

private List<DateTime> GetNextOccurrences(DateTime dateStart, DateTime dateEnd, string cronExpression)
{
if (dateStart.Kind != DateTimeKind.Local)
dateStart = dateStart.ToLocalTime();
dateStart = dateStart.AddSeconds(-1);
if (dateEnd.Kind != DateTimeKind.Local)
dateEnd = dateEnd.ToLocalTime();
var cronSchedule = NCrontab.CrontabSchedule.TryParse(cronExpression);
Expand All @@ -609,5 +590,12 @@ private List<DateTime> GetNextOccurrences(DateTime dateStart, DateTime dateEnd,
return null;
}

private bool IsBetween(DateTime date, DateTime dateStart, DateTime dateEnd)
{
bool dsr = String.Compare(date.ToUniversalTime().ToString(FORMAT_DATETIME), dateStart.ToUniversalTime().ToString(FORMAT_DATETIME)) >= 0;
bool der = String.Compare(date.ToUniversalTime().ToString(FORMAT_DATETIME), dateEnd.ToUniversalTime().ToString(FORMAT_DATETIME)) <= 0;
return (dsr && der);
}

}
}

0 comments on commit 9a2ff90

Please sign in to comment.