more G-Labs products

Author Topic: C# Script vs Wizard - Intro?  (Read 2380 times)

January 11, 2016, 03:30:12 AM
Read 2380 times

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196

I guess one thing I don't understand yet after looking through a bunch of the "program" code etc.

The Wizard scripts seem to have a "Trigger Condition" which makes sense to me... but the C# programs have "Startup Code" which seems to maybe allow the code to be attached to other modules or something.

I just want to detect a particular X10 signal,  and do a little macro based on that involving many other modules.  I think is should use C# b/c the logic will be somewhat complicated but I don't understand how to trigger off a particular event...

Am I making sense?  Any pieces of advice?

Thanks!!

January 11, 2016, 01:27:09 PM
Reply #1

kevin1

  • *****
  • Information
  • Hero Member
  • Posts: 330
Look at the Security System program code or documentation here:
http://genielabs.github.io/HomeGenie/programs.html

This is untested code, just pasting in examples...
Code: [Select]
// this function will be called each time a module parameter is updated
When.ModuleParameterChanged((module, parameter) => {

//then you can check if this module which triggered here has certain features or name
    if (Program.Parameter("HomeGenie.SecurityArmed").Value == "1")
        if (module.Is("Motion1") && parameter.Is("Status.Level") )
       if (module.HasFeature("HomeGenie.TurnOffDelay")

//turn this module off, use this inside the if when correct module matches the if statements
 module.Off();





January 11, 2016, 04:42:01 PM
Reply #2

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I'd also recommend looking at the Smart Lights code.  It watches for a module to send a specific signal (motion detection) and turns on lights.  If you can get through that one, I have a more advanced version that adds some capabilities onto it that might be good for learning.  It's located in the "App Contributions" sub forum under "Advanced Smart Lights".  I have contributed several scripts in that thread so you might find them interesting/useful as well.

January 11, 2016, 05:32:37 PM
Reply #3

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
Thanks!

Hi beknobi,   I have studied your advanced smart lights a bit and I guess, as I stated above,  what I am confused about is that the smart lights code seems to get "attached" to a particular module... am I right about that?

What I want is pretty simple but it isn't anything I would attach to a module per se'.

I want to detect motion and then turn on a BUNCH of modules with some time delays between them etc...   What we would have considered a "macro" back in the AHP days.

Again,  I dont understand about attaching code to modules... instead of just having a free-form spaghetti code macro that is triggered by some event....

January 11, 2016, 06:37:44 PM
Reply #4

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
When you say "attach" to modules, I assume you mean that there are certain modules that are set up to use the ASL code.  What it sounds like you want is to have a code that uses a specific motion sensor and control a specific subset of lights.

You can definitely do that.  In fact, that's a bit easier than either the Smart Lights or ASL code.  In your approach, you would loop through the modules that have changed (typically only 1, but the loop is safer) and see if it's the motion sensor you care about.  If so and the value is on, you would turn on the desired lights.  I don't have any code in front of me to simplify, but using the ASL or SL code, you should be able to strip it down to a couple dozen lines to do what you want.

January 12, 2016, 05:15:10 AM
Reply #5

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
Hi Bekenobi,

When I say "Attach to Module" I mean ...  for instance,   just what do you do with the "smart lights" script as is...   it seems like the only place it shows up is INSIDE a module...   I can select   "Smart Lights" on Module A1 for example.   

This is not at all what I think I want to do...   I don't want to make Module A1 into a Smart Light... I want to execute a bunch of code when C1 (a motion sensor) fires....   

If (C1==Off to On) {
   wait(2 sec)
   A1-On
   A2-On
   wait(1 sec)
   A2-Off

}


Something like this.

Anyhow ... I think I'll figure it out...

January 12, 2016, 04:58:56 PM
Reply #6

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
That's what I thought you meant.  And yes, you certainly can do that.  The basic form would be something like:

Code: [Select]
var time_sunrise = DateTime.Now;
var time_sunset = DateTime.Now;
bool Night = false;


// Process all module changes
When.ModuleParameterIsChanging((module, parameter) =>
{
  // Set sunrise/sunset times
  try
  {
    //jkUtils
    time_sunrise = DateTime.ParseExact(Program.WithName("jkUtils - Solar Altitude").Parameter("jkUtils.SolarAltitude.Morning.Sunrise.Start").Value, "H:mm", System.Globalization.CultureInfo.InvariantCulture);
    time_sunset = DateTime.ParseExact(Program.WithName("jkUtils - Solar Altitude").Parameter("jkUtils.SolarAltitude.Evening.Sunset.End").Value, "H:mm", System.Globalization.CultureInfo.InvariantCulture);

    //Log("jkUtils evaluated:\ntime_sunrise = " + time_sunrise + "\ntime_sunset = " + time_sunset + "\n\n");
 
  }
  catch (Exception e)
  {
    Log("1-ERROR: could not generate sunrise/sunset times.\n" + e.Message + "\ntime_sunrise = " + time_sunrise + "\ntime_sunset = " + time_sunset + "\n\n");
    time_sunrise = DateTime.Now;
    time_sunset = DateTime.Now;
  }

  if (DateTime.Compare(DateTime.Now, time_sunrise)<0 || DateTime.Compare(DateTime.Now, time_sunset)>0)
  {
    Night = true;
    //Log("Night");
  }
  else
  {
    Night = false;
    //Log("Day");
  }
 
  if (module.Is("C1") && parameter.Is("C1") && parameter.Value>0 && Night)
  {
    Modules.WithAddress("A1,A2").On();
    Pause(1);
    Modules.WithAddress("A2").Off();
  }
 
  //break; don't check any more items
  return true;
});

Program.GoBackground();

I didn't compile the code, so it could have a typo but this is the basic structure.  I kept the night check for reference.  If you want this to happen at all times of the day, simply delete everything related to determining day/night.

January 12, 2016, 07:08:52 PM
Reply #7

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
OK this will help.   For startup code last night I just put in  program.Run() but that obviously is wrong b/c it nearly brought the HG server to a halt.   I'll look in your ASL to try and understand what goes in the Startup block.

I haven't yet got my arms around the paradigm for programming this yet...   I guess that the "module"  call's functions that I am defining in these "program's"  ?  Is that the idea...?

That whenever a module state changes IT places a call to  ModuleStateChanged() or what have you?

January 12, 2016, 11:14:52 PM
Reply #8

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
The ASL code is a bit old and probably needs updating in the near future.  The trigger section is outdated and could be simplified to Program.Run() I believe.  The code in the trigger section can apparently be moved to the main code section (I haven't done the research to verify that though).  I'd reference the builtin Smart Lights code for what should go in the trigger section as mine is based on the original.

As for how to access a module,  you have two options.  Either access it directly with:
Code: [Select]
Modules.<some method to pick desired modules>.<some action method>
Modules.WithAddress("A1").On()

Or, you can create a variable that points to the modules you are interested in and do things with them.
Code: [Select]
var module = Modules.WithAddress("A1");
module.On();

In this case, it is silly to use the second approach as it doesn't save any time and only adds complexity.  However, this reference is how the When.ModuleParameterIsChanging is looping through using "module" as the pointer and "parameter" to hold the action and the value of said action.

January 13, 2016, 03:50:57 AM
Reply #9

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
Is there any kind of debugger or printf() type statements available for debug?  I noticed the Log() function but it won't compile saying something about invalid context.  Tried Say() too.   :)

Thanks!

January 13, 2016, 04:19:11 AM
Reply #10

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Program.Log.Trace("Sample trace message");
Program.Log.Debug("Sample debug message");
Program.Log.Info("Sample informational message");
Program.Log.Warn("Sample warning message");
Program.Log.Error("Sample error message");
Program.Log.Fatal("Sample fatal error message");

January 13, 2016, 04:20:00 AM
Reply #11

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
Thanks.  I also found Program.Notify() which seems handy.   :)

January 13, 2016, 04:25:19 AM
Reply #12

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
One other question.

I see in one of the doc pages a snip of code like this:

// Check if the module has just been turned on
    if (parameter.Is("Status.Level") &&
        parameter.Statistics.Last.Value == 0 &&
        parameter.DecimalValue > 0)

How does a person know WHAT parameters are available to be queried for a given module?

January 14, 2016, 09:30:05 PM
Reply #13

Michel

  • **
  • Information
  • Jr. Member
  • Posts: 42
Hi kevinvinv,

I have a simple macro that does, I think, what you are looking for. My macro "Bonne Nuit" is launched when HG receive X10 code E7. It then turns off fifferent lights with a delay of 1 second between each one and wait 1 minute to turn off the last one.

January 14, 2016, 10:06:35 PM
Reply #14

kevinvinv

  • ****
  • Information
  • Sr. Member
  • Posts: 196
Thanks Michel,

I'll check it out.

I think I also got something working but I tell you,  it has not really been too fun given the lack of documentation.   

Thanks again!   I'll see what you have and learn something from it!!

KV.