HomeGenie Forum

General Category => Troubleshooting and Support => Topic started by: HGexperimenter on April 01, 2016, 04:11:18 AM

Title: Example of CSharp versus Wizard Script with Trigger
Post by: HGexperimenter on April 01, 2016, 04:11:18 AM
Is there a working example of a C# program that replaces a Wizard Script using the Scheduler as a Trigger?

I created one but found that I need to have a Pause(50) at the end to get it to recognize the Trigger when it happens (GoBackground does not do this):

Code: [Select]
while (Program.IsEnabled) {
  var mySunset = Scheduler.WithName("Sunset");
  if(mySunset.IsScheduling()) {
    Program.Notify("X10 on:","Started");
    Pause(900);
    Modules.InDomain("HomeAutomation.X10").WithAddress("A1").Command("Control.On").Execute();
  }
  Pause(50); // 50 secs (< 1 min) so as not to miss next scheduler event  GoBackground does not work!
}
Title: Re: Example of CSharp versus Wizard Script with Trigger
Post by: bkenobi on April 01, 2016, 04:57:08 PM
You shouldn't need a pause(50) to make it catch the next event.  What that will do is make sure you don't see the same event more than once.  Scheduler only checks by the minute so every time it cycles through (potentially many times per second) it will see that it is currently scheduling and thus run the code.  By adding a pause(30), it only checks twice per minute.  By adding a pause(50) it will check once per minute.  You can see this implemented in the builtin Scheduled ON/OFF APP.
Title: Re: Example of CSharp versus Wizard Script with Trigger
Post by: HGexperimenter on April 01, 2016, 07:11:46 PM
Thanks. I really want to have the C# program called once every Sunset event and not have it running in a loop. (Which shows a green button). A wizard script with a scheduler trigger does not show a green running state but a yellow state.

I couldn't find a "OnEvent()" type of helper and searched a lot for a working C# example.

Seems a waste of CPU to continuously loop and GoBackground does not work for this purpose.
Title: Re: Example of CSharp versus Wizard Script with Trigger
Post by: kevinvinv on April 02, 2016, 07:42:35 PM
If I understand what you are requesting-  take a look at the HomeGenie for Dummies document.   There is an example in there of a C# program that runs at scheduled intervals.

:)
Title: Re: Example of CSharp versus Wizard Script with Trigger
Post by: kevinvinv on April 02, 2016, 07:44:24 PM
It is possible that the example I referenced above is not what you want afterall...   I think it consumes CPU cycles all the time... anyway... maybe it is of some use to someone...  I personally am not too concerned about burning CPU at the moment... I just am still trying to get reasonably basic functionality from HG in general   :)
Title: Re: Example of CSharp versus Wizard Script with Trigger
Post by: mchias13 on April 04, 2016, 02:48:32 PM
I had to set up mine the same way with the Pause() command.  I could never get my program to respond to the scheduler if I sent it to background.  Was hoping I would have heard from Gene about it in my thread.
Title: Re: Example of CSharp versus Wizard Script with Trigger
Post by: HGexperimenter on April 04, 2016, 05:37:16 PM
I agree - it should be very common to have a program start every Scheduler Event, do it's thing and exit (with no need for a loop).

There are no examples for this that I could find except for one using the Script.

For now I will be going with the Pause() too.

Title: Re: Example of CSharp versus Wizard Script with Trigger
Post by: Gene on April 04, 2016, 07:45:10 PM
Startup Code

Code: [Select]
  var mySunset = Scheduler.WithName("Sunset");
  if(mySunset.IsScheduling())
    Program.Run();

Program Code

Code: [Select]
    Program.Notify("X10 on:","Started");
    Modules.InDomain("HomeAutomation.X10").WithAddress("A1").Command("Control.On").Execute();

the startup code determines when the program code has to be run. once the program code is running, the startup code waits for the program code to complete so if you put pause or infinite loop, the start condition will never be evaluated again.

https://genielabs.github.io/HomeGenie/programs.html

g.
Title: Re: Example of CSharp versus Wizard Script with Trigger
Post by: HGexperimenter on April 04, 2016, 09:22:25 PM
Great - got it!

So in other words the while (Program.IsEnabled) { keeps it running and does not it just execute and exit. The Startup code is called each time the event occurs.

To be safe should I put in If (Program.IsEnabled) or is that redundant since the Startup code won't run if it is not enabled?

I ran a test:  If my program executes and goes idle within a minute, the program gets run again by "IsScheduling()", so I inserted a Pause(60) to avoid multiple times running. Is that OK?
Title: Re: Example of CSharp versus Wizard Script with Trigger
Post by: Gene on April 04, 2016, 10:40:38 PM
it won't run if the program is disabled and yes, you need the pause at the end of the program code:

Code: [Select]
Pause(60 - DateTime.Now.Second);
g.
Title: Re: Example of CSharp versus Wizard Script with Trigger
Post by: HGexperimenter on April 04, 2016, 11:05:39 PM
Thank you