From f67915d9528da79a28c2cbb9c8090f284f2fbb67 Mon Sep 17 00:00:00 2001 From: Generoso Martello Date: Mon, 20 Jun 2016 00:59:57 +0200 Subject: [PATCH] some fixes to the new scheduler --- .../Scheduler/SchedulerScriptingEngine.cs | 11 ++++- .../Scheduler/SchedulerScriptingHost.cs | 14 ++++++ .../Automation/Scheduler/SchedulerService.cs | 48 ++----------------- .../Automation/Scripting/SchedulerHelper.cs | 12 +---- 4 files changed, 28 insertions(+), 57 deletions(-) diff --git a/HomeGenie/Automation/Scheduler/SchedulerScriptingEngine.cs b/HomeGenie/Automation/Scheduler/SchedulerScriptingEngine.cs index 38c63dc8f..29b5aa62b 100644 --- a/HomeGenie/Automation/Scheduler/SchedulerScriptingEngine.cs +++ b/HomeGenie/Automation/Scheduler/SchedulerScriptingEngine.cs @@ -56,10 +56,20 @@ 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) @@ -67,7 +77,6 @@ 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); diff --git a/HomeGenie/Automation/Scheduler/SchedulerScriptingHost.cs b/HomeGenie/Automation/Scheduler/SchedulerScriptingHost.cs index 995b094f6..3ec2eeb95 100644 --- a/HomeGenie/Automation/Scheduler/SchedulerScriptingHost.cs +++ b/HomeGenie/Automation/Scheduler/SchedulerScriptingHost.cs @@ -45,6 +45,7 @@ public class SchedulerScriptingHost private HomeGenieService homegenie = null; private SchedulerItem schedulerItem = null; + private Store localStore; // private NetHelper netHelper; private SerialPortHelper serialPortHelper; @@ -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(){localStore}, "local"); + } public void SetHost(HomeGenieService hg, SchedulerItem item) { homegenie = hg; schedulerItem = item; + Reset(); netHelper = new NetHelper(homegenie); serialPortHelper = new SerialPortHelper(); tcpClientHelper = new TcpClientHelper(); @@ -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)); diff --git a/HomeGenie/Automation/Scheduler/SchedulerService.cs b/HomeGenie/Automation/Scheduler/SchedulerService.cs index 6f2c8b111..577be9fa2 100644 --- a/HomeGenie/Automation/Scheduler/SchedulerService.cs +++ b/HomeGenie/Automation/Scheduler/SchedulerService.cs @@ -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); @@ -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; @@ -532,50 +534,6 @@ private bool EvaluateCronEntry(DateTime date, string cronExpression) return false; } - // TODO: deprecate this method - private bool IsEventActive(string cronExpression, List checkedStack = null) - { - if (checkedStack == null) checkedStack = new List(); - // - 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 GetNextOccurrences(DateTime dateStart, DateTime dateEnd, string cronExpression) { if (dateStart.Kind != DateTimeKind.Local) diff --git a/HomeGenie/Automation/Scripting/SchedulerHelper.cs b/HomeGenie/Automation/Scripting/SchedulerHelper.cs index 1319d6612..67314b86f 100644 --- a/HomeGenie/Automation/Scripting/SchedulerHelper.cs +++ b/HomeGenie/Automation/Scripting/SchedulerHelper.cs @@ -79,23 +79,13 @@ public SchedulerHelper SetSchedule(string cronExpression) /// Sets the program id to run when the selected schedule occurs. /// /// Program ID. - [Obsolete("use 'SetScript' instead")] + [Obsolete()] public SchedulerHelper SetProgram(string programId) { homegenie.ProgramManager.SchedulerService.SetProgram(scheduleName, programId); return this; } - /// - /// Sets the script (hg javascript) to run when the selected schedule occurs. - /// - /// HomeGenie Javascript code - public SchedulerHelper SetScript(string script) - { - homegenie.ProgramManager.SchedulerService.SetScript(scheduleName, script); - return this; - } - /// /// Determines whether the selected schedule is matching in this very moment. ///