more G-Labs products

Author Topic: Example of CSharp versus Wizard Script with Trigger  (Read 1606 times)

April 01, 2016, 04:11:18 AM
Read 1606 times

HGexperimenter

  • **
  • Information
  • Jr. Member
  • Posts: 42
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!
}

April 01, 2016, 04:57:08 PM
Reply #1

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
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.

April 01, 2016, 07:11:46 PM
Reply #2

HGexperimenter

  • **
  • Information
  • Jr. Member
  • Posts: 42
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.

April 02, 2016, 07:42:35 PM
Reply #3

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
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.

:)

April 02, 2016, 07:44:24 PM
Reply #4

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
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   :)

April 04, 2016, 02:48:32 PM
Reply #5

mchias13

  • **
  • Information
  • Jr. Member
  • Posts: 45
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.

April 04, 2016, 05:37:16 PM
Reply #6

HGexperimenter

  • **
  • Information
  • Jr. Member
  • Posts: 42
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.


April 04, 2016, 07:45:10 PM
Reply #7

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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.

April 04, 2016, 09:22:25 PM
Reply #8

HGexperimenter

  • **
  • Information
  • Jr. Member
  • Posts: 42
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?
« Last Edit: April 04, 2016, 09:41:30 PM by HGexperimenter »

April 04, 2016, 10:40:38 PM
Reply #9

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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.

April 04, 2016, 11:05:39 PM
Reply #10

HGexperimenter

  • **
  • Information
  • Jr. Member
  • Posts: 42