Using condition listening

Conditions can be used to get notifications when a certain sensor channel value meets 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 SensorConnection.getData() or DataListener.dataReceived(), conditionMet() returns only a single Data object. This Data object contains the following values:

In addition to Data, 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 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, 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.