Previous Topic

Next Topic

Book Contents

Book Index

Implementing a Device Item

To demonstrate implementing a device item, we will use the com.prosyst.mbs.impl.demo.da.adapter.items.StepperMotorImpl. This class extends an AbstractFunctionalItem and implements the functional interface com.prosyst.mbs.demo.da.items.StepperMotor. This functional interface represents a simple stepper motor which allows rotating with a given number of steps and directions. Also the StepperMotor is annotated with @Item annotation and extends the FunctionalItem interface. It has the following functional properties:

There is also one operation - StepperMotor.OPERATION_ROTATE

After you have implemented your Device Item, you need to add this item to the supported device class object from your protocol adapter. The ProtocolAdapterInfo holds the supported device classes.

public class StepperMotorImpl extends AbstractFunctionalItem implements StepperMotor {

  public static final String    DEVICE_CLASS    = StepperMotor.class.getName();

  private static final String[] PROPERTY_NAMES  = new String[] { PROPERTY_DIRECTION, PROPERTY_STEPS };

  private static final String[] OPERATION_NAMES = new String[] { OPERATION_ROTATE };

  private String direction;

  private int    steps;

  public StepperMotorImpl(HomeDeviceSpiImpl homeDevice, HomeProtocolAdapterImpl homeProtocolAdapter) {

    super(homeDevice, homeProtocolAdapter);

    this.direction = DIRECTION_CW;

    this.steps = 0;

  }

  public String getDirection() throws IOException {

    return direction;

  }

  public void setDirection(String newDirection) throws IOException {

    if (!DIRECTION_CW.equals(newDirection) || !DIRECTION_CCW.equals(newDirection)) {

      throw new IllegalArgumentException("Illegal direction: " + newDirection);

    }

    // provide the direction to the real device

    direction = newDirection;

    // notify Device Access for property changed

    propertyChanged(PROPERTY_DIRECTION, direction);

  }

  public int getSteps() throws IOException {

    return steps;

  }

  public void setSteps(int newSteps) throws IOException {

    // provide the steps to the real device

    steps = newSteps;

    // notify Device Access for property changed

    propertyChanged(PROPERTY_STEPS, new Integer(steps));

  }

  public void rotate() throws IOException {

    // execute rotate operation to the real device

  }

  public String getDeviceClass() {

    return DEVICE_CLASS;

  }

  public String[] getPropertyNames() {

    return PROPERTY_NAMES;

  }

  public String[] getOperationNames() {

    return OPERATION_NAMES;

  }

}