Previous Topic

Next Topic

Book Contents

Book Index

Implementing a Condition Provider

Implement the  com.prosyst.mbs.services.ham.ConditionProvider  interface.

In the condition provider, implement the following methods for handling conditions, which are used by the Home Automation Manager:

Apart from these methods a condition provider can contain a mechanism for triggering its conditions.

If you are using the basic implementation of condition provider, proceed in the following way:

  1. Extend the  com.prosyst.mbs.services.ham.util.BaseConditionProvider  class.
  2. Implement the createCondition() method to create an object by calling the first condition constructor.
  3. Implement the  getCondition(String)  method. You may use the  getHAObjectConfig(id)  method for retrieving the condition's configuration node by its ID (refer to the next code example).

You don't need to implement the getConditions() method - it is already implemented in the BaseConditionProvider.

The following example shows a condition provider for the DemoCondition condition type defined here. The demo condition provider implements OSGi bundle listener in order to use it as a mechanism for triggering conditions.

package demo.condition.impl;

import org.osgi.framework.BundleEvent;

import org.osgi.framework.BundleListener;

import org.osgi.service.event.EventAdmin;

import com.prosyst.mbs.services.configtree.CftException;

import com.prosyst.mbs.services.configtree.ConfigNode;

import com.prosyst.mbs.services.ham.Condition;

import com.prosyst.mbs.services.ham.spi.ConditionProvider;

import com.prosyst.mbs.services.ham.util.BaseConditionProvider;

import com.prosyst.util.ref.Log;

import demo.condition.impl.Activator;

//The condition provider is also registered as a bundlel istener in order for it to be notified when a bundle starts.

public class DemoConditionProvider extends BaseConditionProvider implements ConditionProvider, BundleListener {

  static Log log = new Log(Activator.getContext());

  public DemoConditionProvider(BundleContext bc){

    super(DemoCondition.type, bc);

  }

  // Creates a new condition object.

  public Condition createCondition() {

  //Calls the first constructor of the condition class:        

  DemoCondition cond = new DemoCondition(this);

  System.out.println(cond.getId());        

  return cond;

  }

  // Retrieves a condition by id.

  public Condition getCondition(String id) {

  //Retrieves the node that contains the condition's configuration...

    ConfigNode conditionNode = getHAObjectConfig(id, false, true);

    DemoCondition cond = null;

    if (conditionNode != null) {

      try {

        // ...and creates a new condition object from that

        node.

        cond = new DemoCondition(this, conditionNode,true);

      } catch (CftException e) {

      }

    }

    return cond;

  }

  //The method for event handling from the BundleListener interface

     public void bundleChanged(BundleEvent event) {

    //If a bundle is started

    if (event.getType() == BundleEvent.STARTED) {

      //We retrieve all our condition objects

      String[] allConditions = getConditions();

      //Check whether there is a condition whose configuratin property matches the bundle that has just started...

      for (int i = 0; i < allConditions.length; i++) {

        DemoCondition mycondition = (DemoCondition)getCondition(allConditions[i]);

          if (mycondition.getBundleName().equals(

            event.getBundle().getSymbolicName())) {

        //...and if there is one, we trigger it:

          trigger(mycondition, null);

        }

      }

    }

  }

}