Using condition listening

You can use conditions to receive notifications when a certain sensor channel value meets the specified criteria. When these criteria are met, the ConditionListener receives a conditionMet notification:

class MyConditionListener implements ConditionListener {
    public void conditionMet(SensorConnection aConnection, Data aData, Condition aCondition) {
        // Signal worker thread or do something else
    }
}

Unlike the SensorConnection.getData or DataListener.dataReceived methods, conditionMet returns only a single Data object. This Data object contains the following values:

In addition to the Data object, the associated SensorConnection and Condition objects are received.

Condition listening is also different from data listening in how the condition listeners are assigned to a specific sensor channel. To start condition-listening with a ConditionListener, create a Condition object and add it with the Channel.addCondition method.

// Create ConditionListener and LimitCondition
MyConditionListener conditionListener = new MyConditionListener();
LimitCondition condition = new LimitCondition(10, Condition.OP_GREATER_THAN);

// Get first channel of the sensor
ChannelInfo[] channelInfos = sensorConnection.getSensorInfo().getChannelInfos();
Channel channel = sensorConnection.getChannel(channelInfos[0]);

// Add condition listener and condition to channel
channel.addCondition(conditionListener, condition);

The ConditionListener is notified only once per a met condition. This prevents condition listeners from being flooded with notifications, because successive data values are very likely to meet the same condition. To receive more notifications, the client has to add the ConditionListener and the Condition again with the Channel.addCondition method.

For a comparison of data retrieval methods, see section Choosing the listening method.