Previous Topic

Next Topic

Book Contents

Book Index

Implementing a Command Provider

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

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

  1. Extend the com.prosyst.mbs.services.ham.util.BaseCommandProvider class.
  2. Implement the createCommand() method to create an object by calling the first command constructor.
  3. Implement the getCommand(String) method. You may use the getHAObjectConfig(id) method for retrieving the command's configuration node by its ID (refer to the next code example).
  4. Implement the runCommand(BaseCommand command, Map params) method for command execution. In each of its scenarios, this method should indicate the execution status of the command by returning either one of the status constants defined in the command class: STATUS_OK, STATUS_ERROR, or a custom status string. You can set a custom string as status value by using the setStatus method.

You don't need to implement the getCommands() method - it is already implemented in the base command provider.

The following example shows a command provider for the DemoCommand command type, which prints in the system output the value of its only configuration property:

package demo.command.impl;

import java.util.Map;
import com.prosyst.mbs.services.configtree.CftException;
import com.prosyst.mbs.services.configtree.ConfigNode;
import com.prosyst.mbs.services.ham.Command;
import com.prosyst.mbs.services.ham.spi.CommandProvider;
import com.prosyst.mbs.services.ham.util.BaseCommand;
import com.prosyst.mbs.services.ham.util.BaseHAObject;
import com.prosyst.mbs.services.ham.util.BaseCommandProvider;
import com.prosyst.util.ref.Log;
import demo.command.serv.DemoCommandInt;

public class DemoCommandProvider extends BaseCommandProvider implements
  CommandProvider {
  static Log log = new Log(Activator.getContext());
  
  // The command provider constructor
  public DemoCommandProvider() {
    super(DemoCommandInt.TYPE, log);
  }

  // Creates a new command instance, by calling the first constructor of the Command class.
  public Command createCommand() {
    DemoCommand comm = new DemoCommand(this);
    return comm;
  }

  // Provides a command by specified ID by retrieving the node that contains the command's configuration...
  public synchronized final Command getCommand(String id) {
    ConfigNode cmdNode = getHAObjectConfig(id);
    DemoCommand cmd = null;
    if (cmdNode != null) {
       try {
        // ...and creates a new command object from that node..
        cmd = new DemoCommand(this, cmdNode);
      } catch (CftException e) {
    

  }
    }
    return cmd;
  }

  //Executes the command (prints the value of its property) and returns an OK status.
  protected String runCommand(BaseCommand command, Map params)
      throws Exception {
    DemoCommand commandCast = ((DemoCommand) command);
    System.out.println("The value of " + commandCast.PROP_1 + " is "+ commandCast.getProp());
      return command.STATUS_OK;
  }
}