HomeGenie Forum

General Category => General Discussion => Topic started by: nolio on April 26, 2014, 09:07:10 AM

Title: Scheduler with "OR"
Post by: nolio on April 26, 2014, 09:07:10 AM
Hi,
I try to use the following scheduler expression (on "turn on" of a devices) :
Code: [Select]
@SunCalc.Morning.Sunrise.End;@Weekdays:30 8 * * 0,6,7
This one is trigger well (turn on the deivces) :
Code: [Select]
@SunCalc.Morning.Sunrise.End;@WeekdaysBut this one don't trigger (don't turn on devices on weekend) :
Code: [Select]
30 8 * * 0,6,7
Any idea why ?

Bye
Title: Re: Scheduler with "OR"
Post by: mvdarend on April 26, 2014, 09:21:26 AM
Code: [Select]
30 8 * * 0,6,7
I don't think 7 is a valid value here, as the days of the week are a zero based array. With Sunday as the first day of the week, its value is 0, Saturday will be 6.

Hope that helps.
Title: Re: Scheduler with "OR"
Post by: nolio on April 26, 2014, 09:49:51 AM
I already try :
Code: [Select]
30 8 * * 0,6or :
Code: [Select]
30 8 * * *;@Weekend
Title: Re: Scheduler with "OR"
Post by: Gene on April 26, 2014, 10:41:18 AM
Did you check timezone on your server?

Code: [Select]
pi@raspberrypi ~ $ date
Sat Apr 26 10:40:58 CEST 2014

g.
Title: Re: Scheduler with "OR"
Post by: Gene on April 26, 2014, 10:48:53 AM
Also, in current implementation there is no grouping in building expressions (eg. by using parenthesys), so the precedence is given by the AND (;) operator first and the OR operator later.

So your expression will be evaluated as:

Code: [Select]
@SunCalc.Morning.Sunrise.End

AND

@Weekdays OR 30 8 * * 0,6,7

The 7 is allowed but it's the same as 0, so you can have both Monday to Sunday as 1-7 or Sunday to Saturday as 0-6.

g.
Title: Re: Scheduler with "OR"
Post by: nolio on April 26, 2014, 01:25:01 PM
Oki that's clear (It's like multiplication has priority on addition :)).

So i simplify in scheduler expression :
Code: [Select]
@SunCalc.Morning.Sunrise.End;30 7 * * *
Code: [Select]
date
Sat Apr 26 13:14:52 CEST 2014

Thanks.
Bye


Title: Re: Scheduler with "OR"
Post by: nolio on June 11, 2014, 09:05:35 PM
Hi,

I try again with HG r374 and parentheses function, but it doesn't work.
My original  scheduler :
Code: [Select]
@SunCalc.Morning.Sunrise.End;@Weekdays:30 8 * * 0,6,7And i try this one :
Code: [Select]
(@SunCalc.Morning.Sunrise.End;@Weekdays):(30 8 * * 0,6,7)
Any idea why it doesn't work ?

Bye
Title: Re: Scheduler with "OR"
Post by: bkenobi on June 18, 2014, 07:47:51 AM
I don't know if this is resolved or not, but I think the problem is that you are using the wrong value.  I believe that it should be @SolarAltitude... rather than @SunCalc...  At least that's what it appears to be in r374.
Title: Re: Scheduler with "OR"
Post by: nolio on June 18, 2014, 09:05:48 AM
Hi,
You are right :).
I am not sure if i use this exact expression or another one in my HG configuration (and do the mistake only in the forum).
So i will try again in HG configuration to be sure.
Thanks
Bye
Title: Re: Scheduler with "OR"
Post by: bkenobi on June 18, 2014, 03:19:41 PM
I may be right, but it doesn't work for me either.   ;D

I used the scheduler code:

Code: [Select]
@SolarAltitude.Morning.Sunrise.End
I do not see any activity that indicates that the code is working in my logs.  So, there must be something I'm doing wrong as well.
Title: Re: Scheduler with "OR"
Post by: nolio on June 19, 2014, 04:11:11 PM
Hi,

KO for me too.
I am trying to take a look at the code in
Code: [Select]
HomeGenie/Automation/Scheduler/SchedulerService.cs-->> https://github.com/genielabs/HomeGenie/commit/e9f639bfceb85cc9650c6bdca3052f453be81951#diff-1
But in my first look, i didn't understand all the management of parenthese.

Bye
Title: Re: Scheduler with "OR"
Post by: bkenobi on June 19, 2014, 04:34:56 PM
The on/off at a specific time seems to work, but the shortcut types (e.g. @Morning.Sunrise.Start)  do not.  I'll look at the code you linked and see if I can figure anything out.
Title: Re: Scheduler with "OR"
Post by: bkenobi on June 19, 2014, 06:00:14 PM
I think I found the problem with the code.  I believe there is an issue with tracking the current position in the cronExpression.  Take the following expression:
Code: [Select]
(@SunCalc.Morning.Sunrise.End;@Weekdays):(30 8 * * 0,6,7)
Now, using the code from:
https://github.com/genielabs/HomeGenie/blob/master/HomeGenie/Automation/Scheduler/SchedulerService.cs
and looking at line 156 which is the start of the IsSchduling code.  If I work through the code from the start:

Code: [Select]
line Variable Value
156 cronExpression (@SunCalc.Morning.Sunrise.End;@Weekdays):(30 8 * * 0,6,7)
158 buildExpression ""
159 p 0
162 token (
165 buildExpression (
166 p 1
170 currentExpression (
171 p 2
174 token S
177 currentExpression (S

This is the error.  currentExpression should contain the @ symbol but because the p was incremented twice in a row, it skipped that symbol.  It may do the same for other situations, but it looks like the token check is incrementing the pointer p one too many times.
Title: Re: Scheduler with "OR"
Post by: bkenobi on June 20, 2014, 07:38:04 AM
I tried using the scheduler this evening and it works correctly with a single cron expression however it does not work at all when combining multiple statements nor does it work with scheduler items (shortcuts with @).  I'm not sure what to do to get it working.
Title: Re: Scheduler with "OR"
Post by: dani on June 20, 2014, 10:12:27 AM
I think you are right, you have to move the line 171 : p++ at the end of the while so at the line 200.
And modify the second while at line 172 to include ":" or ";" in the buildExpression

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

Cheers
Dani
Title: Re: Scheduler with "OR"
Post by: bkenobi on June 20, 2014, 03:27:41 PM
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.
Title: Re: Scheduler with "OR"
Post by: Gene on June 20, 2014, 11:43:34 PM
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.
Title: Re: Scheduler with "OR"
Post by: bkenobi on June 20, 2014, 11:59:30 PM
I will test it out as soon as the testing release is populated on SourceForge.  Thanks!
Title: Re: Scheduler with "OR"
Post by: bkenobi on June 21, 2014, 05:46:14 PM
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.