more G-Labs products

Author Topic: Scheduler with "OR"  (Read 5326 times)

June 20, 2014, 03:27:41 PM
Reply #15

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
Have you tried compiling this and testing the results?  I've tried to compile the HG exe and it didn't work in the past.  I was able to compile XTenLib.dll, so I might give this another go just to see if the code repair works if you can't verify.

I've been testing different configurations of Scheduler ON/OFF events and the only thing that seems to work is using cron expressions with no logic (and, or, parenthesis).  The following error (or similar) keeps popping up for me when I try anything more complex.

Code: [Select]
06:18:50.113 Scheduler.Error Missing binary operator:
(0) [?] 0
0 [?] (0) ( @SolarAltitude.Morning.Sunrise.End ) : ( 30 22 * * * ) HomeAutomation.HomeGenie.Scheduler

In this case, I added a space before and after each element to eliminate the p++ issue.  It's possible HG doesn't like this space and that's causing the issue.

June 20, 2014, 11:43:34 PM
Reply #16

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi guys,

the problems reported should be fixed in next update.
This is the new code, in case some of you want help testing it:

Code: [Select]
        public bool IsScheduling(string cronExpression)
        {
            string buildExpression = "";
            int p = 0;
            while (p < cronExpression.Length)
            {
                char token = cronExpression[p];
                if (token == '(' || token == ')' || token == ';' || token == ':')
                {
                    buildExpression += token;
                    p++;
                    continue;
                }

                string currentExpression = token.ToString();
                p++;
                while (p < cronExpression.Length)
                {
                    token = cronExpression[p];
                    if (token != '(' && token != ')' && token != ';' && token != ':')
                    {
                        currentExpression += token;
                        p++;
                    }
                    else
                    {
                        break;
                    }
                }

                currentExpression = currentExpression.Trim(new char[]{ ' ', ' ' });
                if (String.IsNullOrEmpty(currentExpression)) continue;

                bool isEntryActive = false;
                if (currentExpression.StartsWith("@"))
                {
                    // Check expresion from scheduled item with a given name
                    var eventItem = Get(currentExpression.Substring(1));
                    isEntryActive = (eventItem != null && eventItem.IsEnabled && EvaluateCronEntry(eventItem.CronExpression));
                }
                else
                {
                    isEntryActive = EvaluateCronEntry(currentExpression);
                }

                buildExpression += (isEntryActive ? "1" : "0");

            }

            buildExpression = buildExpression.Replace(":", "+");
            buildExpression = buildExpression.Replace(";", "*");
           
            bool success = false;
            try
            {
                ExpressionEval eval = new ExpressionEval();
                eval.Expression = buildExpression;
                success = eval.EvaluateBool();
            }
            catch (Exception ex)
            {
                masterControlProgram.HomeGenie.LogBroadcastEvent(
                    Domains.HomeAutomation_HomeGenie_Scheduler,
                    cronExpression,
                    "Scheduler Expression",
                    Properties.SCHEDULER_ERROR,
                    JsonConvert.SerializeObject(ex.Message));
            }
            return success;
        }

Just added the code to skip empty expressions (white spaces).

I tested it against the expressions you guys posted here and it worked.
When using "@" to reference an item in the scheduler, ensure that the item is enabled from the UI, otherwise it will be evaluated as false.

Thanks!
g.
« Last Edit: June 20, 2014, 11:48:21 PM by Gene »

June 20, 2014, 11:59:30 PM
Reply #17

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I will test it out as soon as the testing release is populated on SourceForge.  Thanks!

June 21, 2014, 05:46:14 PM
Reply #18

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
Gene

I didn't understand what you were saying before about enabling the scheduled event.  I thought you meant in the "Scheduled ON/OFF" setup within the module.  I found that there is a toggle for the scheduled event in the Scheduler list of items as well.  Once I enabled my test item, it worked great every time.  I am now able to create complex expressions and they work!

Code: [Select]
(@test_time;@weekend):(40 8 * * *;@weekend)
This works great now.  So, I have to ask why the default for these scheduler items is disabled?  In my mind, the most logical approach would be to enable the item when it's initially created (manually in the Scheduler view or by a script).  If it is manually or programically disabled, it should not be overridden.  All of the timer events created by Jen's code are not turned on, so this is why they don't work.  I think Jen's code should either enable them the first time it is run or HG should automatically enable all new scheduler items on instantiation.