Helper Class Reference  1.3
HomeGenie - Automation Programs' Engine API
Public Member Functions | List of all members
EventsHelper Class Reference

Events Helper class.
Class instance accessor: When More...

Public Member Functions

EventsHelper SystemStarted (Func< bool > handler)
 Call the specified handler after HomeGenie service started. More...
 
EventsHelper SystemStopping (Func< bool > handler)
 Call the specified handler when HomeGenie service is stopping. More...
 
EventsHelper ProgramStopping (Func< bool > handler)
 Call the specified handler when the program is beign stopped. More...
 
EventsHelper ModuleParameterChanged (Func< ModuleHelper, ModuleParameter, bool > handler)
 Call the specified handler function when a parameter of a module changed. If either the handler returns false or changes the event value, the propagation will stop. More...
 
EventsHelper ModuleParameterIsChanging (Func< ModuleHelper, ModuleParameter, bool > handler)
 Call the specified handler function when a parameter of a module is changing. If either the handler returns false or changes the event value, the propagation will stop. More...
 
EventsHelper WebServiceCallReceived (string apiCall, Func< object, object > handler)
 Define a handler function that will be called when a web service call starting with apiCall is received. Use this to create user-defined web service API methods. More...
 

Detailed Description

Events Helper class.
Class instance accessor: When

Member Function Documentation

◆ SystemStarted()

EventsHelper SystemStarted ( Func< bool >  handler)

Call the specified handler after HomeGenie service started.

Returns
EventsHelper
Parameters
handlerThe handler function to call.

Example:

When.SystemStarted( () =>
{
Program.Say("HomeGenie is now ready!");
// returning false will prevent this event from being routed to other listeners
return false;
});

◆ SystemStopping()

EventsHelper SystemStopping ( Func< bool >  handler)

Call the specified handler when HomeGenie service is stopping.

Returns
EventsHelper
Parameters
handlerThe handler function to call.

Example:

When.SystemStopping( () =>
{
Program.Say("See ya soon!");
// returning true will route this event to other listeners
return true;
});

◆ ProgramStopping()

EventsHelper ProgramStopping ( Func< bool >  handler)

Call the specified handler when the program is beign stopped.

Returns
EventsHelper
Parameters
handlerThe handler function to call.

Example:

When.Stopping( () =>
{
Program.Say("Oh-oh! I'm quitting!");
// returning true will route this event to other listeners
return true;
});

◆ ModuleParameterChanged()

EventsHelper ModuleParameterChanged ( Func< ModuleHelper, ModuleParameter, bool >  handler)

Call the specified handler function when a parameter of a module changed. If either the handler returns false or changes the event value, the propagation will stop.

Returns
EventsHelper
Parameters
handlerThe handler function to call.

Example:

When.ModuleParameterChange( (module, parameter) =>
{
if (module.Is("Kitchen Motion Sensor") && parameter.Is("Status.Level"))
{
// ...
return false;
}
return true;
});
See also
ModuleParameterIsChanging

◆ ModuleParameterIsChanging()

EventsHelper ModuleParameterIsChanging ( Func< ModuleHelper, ModuleParameter, bool >  handler)

Call the specified handler function when a parameter of a module is changing. If either the handler returns false or changes the event value, the propagation will stop.

Returns
EventsHelper
Parameters
handlerThe handler function to call.

Example:

When.ModuleParameterIsChanging( (module, parameter) =>
{
if (module.Is("Kitchen Motion Sensor") && parameter.Is("Status.Level"))
{
// ...
// stop event propagation
return false;
}
// continue event propagation
return true;
});
See also
ModuleParameterChanged

◆ WebServiceCallReceived()

EventsHelper WebServiceCallReceived ( string  apiCall,
Func< object, object >  handler 
)

Define a handler function that will be called when a web service call starting with apiCall is received. Use this to create user-defined web service API methods.

Returns
EventsHelper
Parameters
apiCallAPI call.
handlerHandler.

API methods should respect the following format:

<domain>/<address>/<command>[/<option_0>[/.../<option_n>]]

For instance, a program that control Philips Hue lights will implement API methods like this:

When.WebServiceCallReceived( "HomeAutomation.PhilipsHue", (args) =>
{
// handle the received request
});

So an API call to set a Philips Hue light with address 3 to 50% can be done via HTTP GET

GET /api/HomeAutomation.PhilipsHue/3/Control.Level/50

or from a csharp program

var responseObject = Program.ApiCall("HomeAutomation.PhilipsHue/3/Control.Level/50");

When this call is received by the handler, the object args passed to it must be parsed using Program.ParseApiCall method, which will return an object containing the following fields

var request = Program.ParseApiCall(args);
// request -> {
// Domain, // (string)
// Address, // (string)
// Command, // (string)
// Data, // (object)
// OriginalRequest // (string)
// }

This object also provide a method request.GetOption(<index>) to get eventual options passed with this call.

Example

When.WebServiceCallReceived( "HomeAutomation.PhilipsHue", (args) =>
{
var request = Program.ParseApiCall(args);
// request.Domain -> "HomeAutomtion.PhilipsHue"
// request.Address -> 3
// request.Command -> Control.Level
// request.GetOption(0) -> 50
// request.Data -> null (not used in this case)
// request.OriginalRequest -> "HomeAutomation.PhilipsHue/3/Control.Level/50"
if (request.Domain == "HomeAutomtion.PhilipsHue" && request.Command == "Control.Level")
{
var deviceAddress = request.Address;
var deviceLevel = request.GetOption(0); // the first option has index 0
// TODO: set dimming level of light with address 'deviceAddress' to 'dimmerLevel' %
return new ResponseText("OK");
}
return new ResponseText("ERROR");
});

The documentation for this class was generated from the following file: