package com.orange.zstack.service;
import com.orange.zstack.service.element.GeneralFrameFormat;
import com.orange.zstack.service.element.zigbee.ByteArrayData;
import com.orange.zstack.service.element.zigbee.ClusterId;
import com.orange.zstack.service.element.zigbee.EpId;
import com.orange.zstack.service.exception.ZStackException;
/**
* this class include all fields which could be tested to check if answer
* is waited.
* @author totof alias Bob
*
*/
public class ZStackListenerRules {
public static final int INFINITE = -1;
private ZStackListener listener;
private Class[] frameTypes = null;
private ByteArrayData srcAddr = null;
private EpId epId = null;
private ClusterId clusterId = null;
private Integer transId = null;
private int nbResponse;
/**
* set information to have call back. At least one condition.
* @param listener the listener which want to be notified
* @param frameTypes list of Class of object, GeneralFrameFormat or sub classes
* @param srcAddr the ShortAddress or IEEEAddress we want into the answer
* @param epId the source EpId we want into the answer
* @param clusterId the ClusterId we want into the answer
* @param transId the transaction Id we want into the answer
* @param nbResponse nb response to be notified. INFINITE is always until user delete it.
*/
public ZStackListenerRules(ZStackListener listener, Class[] frameTypes, ByteArrayData srcAddr, EpId epId,
ClusterId clusterId, Integer transId, int nbResponse) {
super();
this.listener = listener;
this.frameTypes = frameTypes;
this.srcAddr = srcAddr;
this.epId = epId;
this.clusterId = clusterId;
this.transId = transId;
this.nbResponse = nbResponse;
}
public ZStackListener getListener() {
return listener;
}
/**
* check if the frame is included in the required frame types
* @return true if frame type is valid or not filtering
*/
public boolean isValidFrameType(GeneralFrameFormat frame) {
if (frameTypes == null) {
// not a filter
return true;
}
boolean valid = false;
for (int id = 0; id < frameTypes.length && !valid; id++) {
valid = frameTypes[id].isInstance(frame);
}
return valid;
}
/**
* check if the address is valid
* @param address
* @return true if valid address or not filtering
*/
public boolean isSrcAddr(ByteArrayData address) {
if (srcAddr == null) {
// not a filter
return true;
}
return srcAddr.equals(address);
}
public boolean isEpId(EpId id) {
if (epId == null) {
// not a filter
return true;
}
return epId.equals(id);
}
public boolean isClusterId(ClusterId id) {
if (clusterId == null) {
// not a filter
return true;
}
return clusterId.equals(id);
}
public boolean isTransId(int id) {
if (transId == null) {
// not a filter
return true;
}
return transId.intValue() == id;
}
public boolean isFinished() throws ZStackException {
if (nbResponse == INFINITE) {
throw new ZStackException("");
}
if (nbResponse > 0) {
nbResponse--;
}
if (nbResponse > 0) {
return false;
}
return true;
}
public void isValid() throws ZStackException {
if (listener == null || (frameTypes == null && srcAddr == null &&
epId == null && clusterId == null && transId == null)) {
throw new ZStackException("null value");
}
}
}