Previous Topic

Next Topic

Book Contents

Book Index

Managing Rules

How to define a new rule and retrieve an existing one.

A rule allows you to start a non-scene command and start, stop, pause, resume, or quickrun a scene command.

Defining a Rule for a Non-Scene Command

To create a new rule for a non-scene command:

  1. Get the Home Automation Admin service (com.prosyst.mbs.services.ham.HomeAutomationAdmin) from the OSGi framework. If you are accessing the Home Automation Manager via the  JSON Remote API  use the functions from the HAM group.
  2. Call the createRule method (or HAM/createRule).
  3. Configure the rule object by specifying the command and the condition that you want to pair and call the save method to apply your changes. In the  JSON Remote API  the properties are passed as arguments to the HAM/createRule function, so this step is not applicable.

The following code snippets (Java and JavaScript) demonstrate how to create and save a rule that links a LogCommand with ID "LogCommand1" and a condition with ID "EventCondition1":

  HomeAutomationAdmin hamAdmin = (HomeAutomationAdmin) service;
   //create a rule
  Rule myrule = hamAdmin.createRule();
   //configure its properties(in this case command and condition)
  myrule.setCommand(LogCommand.TYPE, "LogCommand1");
  myrule.setCondition(EventCondition.TYPE, "EventCondition1");
  myrule.save();
  String ruleID = myrule.getId();

   //configure the properties of the rule
   //(in this case the command and condition)
  ruleprops = new Object()
  ruleprops.command_type="LogCommand"
  ruleprops.command_id="LogCommand1"
  ruleprops.condition_type="EventCondition"
  ruleprops.condition_id="EventCondition1"
    //create the rule
  var xhr = new XMLHttpRequest();
  xhr.open("POST", "/remote/json-rpc", false);
  xhr.setRequestHeader("Content-Type","application/json")
  xhr.send('{"jsonrpc": "2.0", "method": "HAM/createRule", "params":'+
        JSON.stringify(ruleprops)+', "id": 1}')
  myruleID=JSON.parse(xhr.responseText)

Defining a Rule for a Scene Command

To create a new rule for a scene command:

  1. Get the Home Automation Admin service (com.prosyst.mbs.services.ham.HomeAutomationAdmin) from the OSGi framework. If you are accessing the Home Automation Manager via the  JSON Remote API use the functions from the HAM group.
  2. Call the createRule method (or HAM/createRule).
  3. Configure the rule object by specifying the scene and the condition that you want to pair, and the action to be performed with the scene. Call the save method to apply your changes. In the  JSON Remote API  the properties are passed as arguments to the HAM/createRule function, so this step is not applicable.

The following code snippets (Java and JavaScript) demonstrate how to create and save a rule that starts a SceneCommand with ID "SceneCommand1" upon triggering a condition with ID "EventCondition1":

  HomeAutomationAdmin hamAdmin = (HomeAutomationAdmin) service;
   //create a rule
  Rule myrule = hamAdmin.createRule();
   //configure the properties of the rule(in this case a scene command,
   //a condition and a property specifying start scene action)
  myrule.setCommand(SceneCommand.TYPE, "SceneCommand1");
  myrule.setCondition(EventCondition.TYPE, "EventCondition1");
  Hashtable ruleProps = new Hashtable ();
  ruleProps.put(Scene.SCENE_ACTION, Scene.SCENE_ACTION_START);
  myrule.setProperties(ruleProps);
  myrule.save();
  String ruleID = myrule.getId();

   //configure the properties of the rule
   //(in this case the command and condition)
  ruleprops = new Object()
  ruleprops.command_type="SceneCommand"
  ruleprops.command_id="SceneCommand1"
  ruleprops.condition_type="EventCondition"
  ruleprops.condition_id="EventCondition1"
  ruleprops.scene_action="start"
    //create the rule
  var xhr = new XMLHttpRequest();
  xhr.open("POST", "/remote/json-rpc", false);
  xhr.setRequestHeader("Content-Type","application/json")
  xhr.send('{"jsonrpc": "2.0", "method": "HAM/createRule", "params":'+
        JSON.stringify(ruleprops)+', "id": 1}')
  myruleID=JSON.parse(xhr.responseText)

Editing a Rule

To edit and existing rule:

  1. Retrieve it by using the getRules and getRule methods (or HAM/getRules / HAM/getRule if you are using the JSON Remote API ).
  2. Modify its properties.
  3. Apply your changes by calling the save method to the updated object (or by passing it to the HAM/updateRule function if you are using the  JSON Remote API ).

The following code examples (Java and JavaScript) demonstrate how to retrieve the rule, created in the previous example and change its command with another command from the same type.

    //Retrieve the rule
  Rule myrule1 = hamAdmin.getRule(ruleID);
    //change a property
  myrule.setCommandId("LogCommand2");
    //apply the changes    
  myrule.save();

    //retrieve the rule
  var xhr = new XMLHttpRequest();
  xhr.open("POST", "/remote/json-rpc", false);
  xhr.setRequestHeader("Content-Type","application/json")
  xhr.send('{"jsonrpc": "2.0", "method": "HAM/getRule", "params": ["'+
      myruleID+'" ], "id": 1}')
  myrule=JSON.parse(xhr.responseText).result
   //change the ID of the command
  myrule.command_id = "LogCommand2"
   //apply the changes
  xhr.open("POST", "/remote/json-rpc", false);
  xhr.setRequestHeader("Content-Type","application/json")
  xhr.send('{"jsonrpc": "2.0", "method": "HAM/updateRule", "params": ["'+
     myruleID+'", '+JSON.stringify(myrule)+' ], "id": 1}')