more G-Labs products

Author Topic: How to manually trigger a program?  (Read 1567 times)

March 28, 2015, 09:13:54 PM
Read 1567 times

BruceS

  • *
  • Information
  • Newbie
  • Posts: 13
I have a few scenarios where I would like to trigger a program by clicking a virtual module (such as pushing a garage door button).  I created a test program below but when I click on the module I assigned to the program it doesn't trigger program or else the module isn't clickable.  I would appreciate any guidance.

Trigger
Code: [Select]
Program.Setup(()=>
{                 
  Program.AddControlWidget("homegenie/generic/status");
});
return true;

Program
Code: [Select]
//do something intelligent
Program.Notify("Manual Trigger Test", "Success");
Program.Say("Test Successful", "en-US");

March 28, 2015, 10:33:17 PM
Reply #1

CptJack

  • **
  • Information
  • Jr. Member
  • Posts: 30
Assuming you're using C# Program as your program type, the Program Code section needs to be in the form of a loop or function. For example:

Code: [Select]
When.ModuleParameterChanged((module, parameter) => {
  if (module.Instance.Name == "MyModule"){
    Program.Notify("MyModule", parameter.Value);
  }
  return true;
});
Program.GoBackground();

Depending on what you're doing, you could look for your module by name, or a type of module by domain or parameter if you plan on creating several of these modules.

March 29, 2015, 06:03:19 AM
Reply #2

BruceS

  • *
  • Information
  • Newbie
  • Posts: 13
OK I tried using When.ModuleParameterChanged with C# but I am still puzzled.  How do I get my module to change when I click on it?  And what parameter am I looking for? Maybe I'm stating it wrong. Should I be calling it a control widget?


March 29, 2015, 07:29:05 PM
Reply #3

CptJack

  • **
  • Information
  • Jr. Member
  • Posts: 30
You're using a status widget (homegenie/generic/status), so it doesn't have any input. Try using a switch widget (homegenie/generic/switch) instead. Then, you need to use the web service commands to handle any commands received from the switch widget.

Because I'm better at doing than explaining, I've attached a basic virtual switch example. Let me know if you have any questions about using it.

March 29, 2015, 07:57:41 PM
Reply #4

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
When I was testing, I changed the module type from a sensor to a switch and allowed my code to continue to trigger based off of the same logic.  When I wanted to trigger an event that was a bit more complicated, I'd typically just write a simple script (Wizard or C#) that set the state of the module I was trying to simulate.  I'm not sure if you are looking to add notification or something more complicated, but this approach should work.

March 30, 2015, 12:51:50 AM
Reply #5

BruceS

  • *
  • Information
  • Newbie
  • Posts: 13
Thank you for the follow ups.  I used the web service script that CptJack submitted and it worked for me in a browser.  But I got an "Array out of range" error when using my Android phone app.  I'm not able to determine why the array error occurs.