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 9a2ff90 commit f67915d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 57 deletions.
11 changes: 10 additions & 1 deletion HomeGenie/Automation/Scheduler/SchedulerScriptingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,27 @@ public class SchedulerScriptingEngine
prevMin = new Date(prevMin.getTime()-60000);
return $$.scheduler.isOccurrence(prevMin, event.CronExpression);
};
$$.data = function(k,v) {
if (typeof v == 'undefined') {
return hg.data(k).Value;
} else {
hg.data(k).value = v;
return $$;
}
};
";

public SchedulerScriptingEngine()
{
// we do not dispose the scripting host to keep volatile data persistent across instances
hgScriptingHost = new SchedulerScriptingHost();
}

public void SetHost(HomeGenieService hg, SchedulerItem item)
{
homegenie = hg;
eventItem = item;
scriptEngine = new Engine();
hgScriptingHost = new SchedulerScriptingHost();
hgScriptingHost.SetHost(homegenie, item);
scriptEngine.SetValue("hg", hgScriptingHost);
scriptEngine.SetValue("event", eventItem);
Expand Down
14 changes: 14 additions & 0 deletions HomeGenie/Automation/Scheduler/SchedulerScriptingHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class SchedulerScriptingHost

private HomeGenieService homegenie = null;
private SchedulerItem schedulerItem = null;
private Store localStore;
//
private NetHelper netHelper;
private SerialPortHelper serialPortHelper;
Expand All @@ -54,11 +55,19 @@ public class SchedulerScriptingHost
private KnxClientHelper knxClientHelper;
private SchedulerHelper schedulerHelper;
private ProgramHelperBase programHelper;
private StoreHelper storeHelper;

public SchedulerScriptingHost()
{
localStore = new Store();
storeHelper = new StoreHelper(new TsList<Store>(){localStore}, "local");
}

public void SetHost(HomeGenieService hg, SchedulerItem item)
{
homegenie = hg;
schedulerItem = item;
Reset();
netHelper = new NetHelper(homegenie);
serialPortHelper = new SerialPortHelper();
tcpClientHelper = new TcpClientHelper();
Expand Down Expand Up @@ -167,6 +176,11 @@ public SchedulerHelper Scheduler
}
}

public ModuleParameter Data(string name)
{
return storeHelper.Get(name);
}

public void Pause(double seconds)
{
System.Threading.Thread.Sleep((int)(seconds * 1000));
Expand Down
48 changes: 3 additions & 45 deletions HomeGenie/Automation/Scheduler/SchedulerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public bool SetData(string name, string jsonData)
return false;
}

[Obsolete("use 'SetScript' instead")]
[Obsolete()]
public bool SetProgram(string name, string pid)
{
var eventItem = Get(name);
Expand Down Expand Up @@ -243,6 +243,8 @@ public bool Remove(string name)

public bool IsScheduling(DateTime date, string cronExpression, int recursionCount = 0)
{
if (date.Kind != DateTimeKind.Local)
date = date.ToLocalTime();
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;
Expand Down Expand Up @@ -532,50 +534,6 @@ private bool EvaluateCronEntry(DateTime date, string cronExpression)
return false;
}

// TODO: deprecate this method
private bool IsEventActive(string cronExpression, List<string> checkedStack = null)
{
if (checkedStack == null) checkedStack = new List<string>();
//
string[] expressionList = cronExpression.Split(':'); // <-- ':' is OR operator
for (int e = 0; e < expressionList.Length; e++)
{
string currentExpression = expressionList[e].Trim();
// avoid loops
if (checkedStack.Contains(currentExpression))
{
continue;
}
checkedStack.Add(currentExpression);
if (currentExpression.StartsWith("@"))
{
// Check expresion from scheduled item with a given name
var eventItem = Get(currentExpression.Substring(1));
if (eventItem != null && eventItem.IsEnabled && IsEventActive(eventItem.CronExpression, checkedStack))
{
return true;
}
}
else
{
// Check current expression
var cronSchedule = NCrontab.CrontabSchedule.TryParse(currentExpression);
if (!cronSchedule.IsError)
{
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;
}
}
}
}
//
return false;
}

private List<DateTime> GetNextOccurrences(DateTime dateStart, DateTime dateEnd, string cronExpression)
{
if (dateStart.Kind != DateTimeKind.Local)
Expand Down
12 changes: 1 addition & 11 deletions HomeGenie/Automation/Scripting/SchedulerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,13 @@ public SchedulerHelper SetSchedule(string cronExpression)
/// Sets the program id to run when the selected schedule occurs.
/// </summary>
/// <param name="programId">Program ID.</param>
[Obsolete("use 'SetScript' instead")]
[Obsolete()]
public SchedulerHelper SetProgram(string programId)
{
homegenie.ProgramManager.SchedulerService.SetProgram(scheduleName, programId);
return this;
}

/// <summary>
/// Sets the script (hg javascript) to run when the selected schedule occurs.
/// </summary>
/// <param name="script">HomeGenie Javascript code</param>
public SchedulerHelper SetScript(string script)
{
homegenie.ProgramManager.SchedulerService.SetScript(scheduleName, script);
return this;
}

/// <summary>
/// Determines whether the selected schedule is matching in this very moment.
/// </summary>
Expand Down

0 comments on commit f67915d

Please sign in to comment.